1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- =============================================
-- 考勤规则:支持同一部门配置多条规则(按生效日期区间区分)
--
-- 变更:hrm_attendance_rule 增加 2 列
--   effective_start_date 生效开始日期(NULL = 不限)
--   effective_end_date   生效结束日期(NULL = 不限)
--
-- 匹配规则(后端 HrmAttendanceRuleServiceImpl.getAttendanceRuleByDeptId):
--   1) 优先取部门专属规则,没有则取全局规则(dept_id IS NULL)
--   2) 只取 status=0 且 生效区间覆盖考勤日期 的规则
--   3) 多条命中时,取 effective_start_date 最晚的那条(NULL 视为长期有效、优先级最低)
--
-- 脚本可重复执行(幂等)
-- =============================================
SET NAMES utf8mb4;
 
SET @sch := DATABASE();
 
-- 1. 增加 effective_start_date
SET @exists := (SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @sch AND TABLE_NAME = 'hrm_attendance_rule' AND COLUMN_NAME = 'effective_start_date');
SET @sql := IF(@exists = 0,
    'ALTER TABLE `hrm_attendance_rule` ADD COLUMN `effective_start_date` date DEFAULT NULL COMMENT ''生效开始日期(空表示不限)'' AFTER `overtime_threshold`',
    'SELECT ''effective_start_date 已存在,跳过'' AS msg');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
 
-- 2. 增加 effective_end_date
SET @exists := (SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = @sch AND TABLE_NAME = 'hrm_attendance_rule' AND COLUMN_NAME = 'effective_end_date');
SET @sql := IF(@exists = 0,
    'ALTER TABLE `hrm_attendance_rule` ADD COLUMN `effective_end_date` date DEFAULT NULL COMMENT ''生效结束日期(空表示不限)'' AFTER `effective_start_date`',
    'SELECT ''effective_end_date 已存在,跳过'' AS msg');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
 
-- 3. 核对
SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @sch AND TABLE_NAME = 'hrm_attendance_rule'
  AND COLUMN_NAME IN ('effective_start_date', 'effective_end_date');
 
SELECT '考勤规则生效日期字段初始化完成' AS result;