migrate_20260820_duty_shift_dict_time.sql 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. -- backend/scripts/migrate_20260820_duty_shift_dict_time.sql
  2. -- 值班排班:字典类型/工作安排 + slot 时段与 work_plans
  3. -- 可重复执行(字典 NOT EXISTS;ALTER 检查 information_schema)
  4. SET NAMES utf8mb4;
  5. -- 1) 字典类型(tenant shanxia2.0)
  6. INSERT INTO `sys_dict_type` (
  7. `id`, `dict_name`, `dict_type`, `tenant_no`, `order_num`, `remark`,
  8. `create_by`, `create_dept_id`, `create_time`, `update_by`, `update_time`, `del_flag`
  9. )
  10. SELECT
  11. 'DUTYDICTTYPE00000000000000000001',
  12. '值班类型',
  13. 'duty_shift_type',
  14. 'shanxia2.0',
  15. 90,
  16. '值班排班班次类型',
  17. '',
  18. '',
  19. NOW(),
  20. '',
  21. NOW(),
  22. 0
  23. FROM DUAL
  24. WHERE NOT EXISTS (
  25. SELECT 1 FROM `sys_dict_type`
  26. WHERE `dict_type` = 'duty_shift_type'
  27. AND `tenant_no` = 'shanxia2.0'
  28. AND `del_flag` = 0
  29. );
  30. INSERT INTO `sys_dict_type` (
  31. `id`, `dict_name`, `dict_type`, `tenant_no`, `order_num`, `remark`,
  32. `create_by`, `create_dept_id`, `create_time`, `update_by`, `update_time`, `del_flag`
  33. )
  34. SELECT
  35. 'DUTYDICTTYPE00000000000000000002',
  36. '值班工作安排',
  37. 'duty_work_plan',
  38. 'shanxia2.0',
  39. 91,
  40. '值班排班工作安排',
  41. '',
  42. '',
  43. NOW(),
  44. '',
  45. NOW(),
  46. 0
  47. FROM DUAL
  48. WHERE NOT EXISTS (
  49. SELECT 1 FROM `sys_dict_type`
  50. WHERE `dict_type` = 'duty_work_plan'
  51. AND `tenant_no` = 'shanxia2.0'
  52. AND `del_flag` = 0
  53. );
  54. -- 2) 字典数据预置
  55. INSERT INTO `sys_dict_data` (
  56. `id`, `dict_label`, `dict_value`, `tag_type`, `dict_type`, `tenant_no`, `order_num`, `remark`,
  57. `create_by`, `create_dept_id`, `create_time`, `update_by`, `update_time`, `del_flag`
  58. )
  59. SELECT * FROM (
  60. SELECT 'DUTYDICTDATA0000000000000000001' AS id, '早班' AS dict_label, 'morning' AS dict_value, '' AS tag_type, 'duty_shift_type' AS dict_type, 'shanxia2.0' AS tenant_no, 1 AS order_num, NULL AS remark, '' AS create_by, '' AS create_dept_id, NOW() AS create_time, '' AS update_by, NOW() AS update_time, 0 AS del_flag
  61. UNION ALL SELECT 'DUTYDICTDATA0000000000000000002', '晚班', 'evening', '', 'duty_shift_type', 'shanxia2.0', 2, NULL, '', '', NOW(), '', NOW(), 0
  62. UNION ALL SELECT 'DUTYDICTDATA0000000000000000003', '值班', 'duty', '', 'duty_shift_type', 'shanxia2.0', 3, NULL, '', '', NOW(), '', NOW(), 0
  63. UNION ALL SELECT 'DUTYDICTDATA0000000000000000004', '配酸', 'peisuan', '', 'duty_work_plan', 'shanxia2.0', 1, NULL, '', '', NOW(), '', NOW(), 0
  64. UNION ALL SELECT 'DUTYDICTDATA0000000000000000005', '装卸', 'zhuangxie', '', 'duty_work_plan', 'shanxia2.0', 2, NULL, '', '', NOW(), '', NOW(), 0
  65. ) AS t
  66. WHERE NOT EXISTS (
  67. SELECT 1 FROM `sys_dict_data` d
  68. WHERE d.`tenant_no` = t.`tenant_no`
  69. AND d.`dict_type` = t.`dict_type`
  70. AND d.`dict_value` = t.`dict_value`
  71. AND d.`del_flag` = 0
  72. );
  73. -- 3) duty_slot 扩字段(列已存在则跳过)
  74. SET @db = DATABASE();
  75. SET @col_exists = (
  76. SELECT COUNT(*) FROM information_schema.COLUMNS
  77. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'duty_slot' AND COLUMN_NAME = 'work_plans'
  78. );
  79. SET @sql = IF(
  80. @col_exists = 0,
  81. 'ALTER TABLE `duty_slot` ADD COLUMN `work_plans` varchar(500) DEFAULT NULL COMMENT ''工作安排dict_value逗号分隔'' AFTER `capacity`',
  82. 'SELECT 1'
  83. );
  84. PREPARE stmt FROM @sql;
  85. EXECUTE stmt;
  86. DEALLOCATE PREPARE stmt;
  87. SET @col_exists = (
  88. SELECT COUNT(*) FROM information_schema.COLUMNS
  89. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'duty_slot' AND COLUMN_NAME = 'start_time'
  90. );
  91. SET @sql = IF(
  92. @col_exists = 0,
  93. 'ALTER TABLE `duty_slot` ADD COLUMN `start_time` time DEFAULT NULL COMMENT ''值班开始时间'' AFTER `work_plans`',
  94. 'SELECT 1'
  95. );
  96. PREPARE stmt FROM @sql;
  97. EXECUTE stmt;
  98. DEALLOCATE PREPARE stmt;
  99. SET @col_exists = (
  100. SELECT COUNT(*) FROM information_schema.COLUMNS
  101. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'duty_slot' AND COLUMN_NAME = 'end_time'
  102. );
  103. SET @sql = IF(
  104. @col_exists = 0,
  105. 'ALTER TABLE `duty_slot` ADD COLUMN `end_time` time DEFAULT NULL COMMENT ''值班结束时间'' AFTER `start_time`',
  106. 'SELECT 1'
  107. );
  108. PREPARE stmt FROM @sql;
  109. EXECUTE stmt;
  110. DEALLOCATE PREPARE stmt;
  111. SET @col_exists = (
  112. SELECT COUNT(*) FROM information_schema.COLUMNS
  113. WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'duty_slot' AND COLUMN_NAME = 'cross_day'
  114. );
  115. SET @sql = IF(
  116. @col_exists = 0,
  117. 'ALTER TABLE `duty_slot` ADD COLUMN `cross_day` smallint(6) NOT NULL DEFAULT 0 COMMENT ''结束是否次日0/1'' AFTER `end_time`',
  118. 'SELECT 1'
  119. );
  120. PREPARE stmt FROM @sql;
  121. EXECUTE stmt;
  122. DEALLOCATE PREPARE stmt;
  123. -- 4) 回填时段(仅 start_time 为空的历史行)
  124. UPDATE `duty_slot`
  125. SET `start_time` = '08:00:00', `end_time` = '18:00:00', `cross_day` = 0
  126. WHERE `del_flag` = 0 AND `shift_type` = 'morning' AND `start_time` IS NULL;
  127. UPDATE `duty_slot`
  128. SET `start_time` = '18:00:00', `end_time` = '07:00:00', `cross_day` = 1
  129. WHERE `del_flag` = 0 AND `shift_type` = 'evening' AND `start_time` IS NULL;
  130. -- 5) 回填工作安排(整段匹配);其它 remark 保留为备注
  131. UPDATE `duty_slot`
  132. SET `work_plans` = 'peisuan', `remark` = NULL
  133. WHERE `del_flag` = 0
  134. AND TRIM(`remark`) = '配酸'
  135. AND (`work_plans` IS NULL OR `work_plans` = '');
  136. UPDATE `duty_slot`
  137. SET `work_plans` = 'zhuangxie', `remark` = NULL
  138. WHERE `del_flag` = 0
  139. AND TRIM(`remark`) = '装卸'
  140. AND (`work_plans` IS NULL OR `work_plans` = '');
  141. UPDATE `duty_slot`
  142. SET `work_plans` = 'peisuan,zhuangxie', `remark` = NULL
  143. WHERE `del_flag` = 0
  144. AND TRIM(`remark`) IN ('配酸,装卸', '配酸,装卸', '装卸,配酸', '装卸,配酸')
  145. AND (`work_plans` IS NULL OR `work_plans` = '');