2026-10-17-protocol-status-change-log.sql 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. -- 采集协议单 / 科研捐赠:状态变更历史表(追加写,同类型不覆盖)
  2. -- 对齐 docs/superpowers/plans/2026-10-17-protocol-status-change-history.md
  3. CREATE TABLE IF NOT EXISTS biz_protocol_status_change (
  4. id VARCHAR(64) NOT NULL COMMENT '主键ID',
  5. collection_protocol_id VARCHAR(64) NOT NULL COMMENT '采集协议单ID',
  6. change_type VARCHAR(32) NOT NULL COMMENT '变更类型 activate/expire/void/to_research',
  7. from_status VARCHAR(16) DEFAULT NULL COMMENT '变更前状态码',
  8. to_status VARCHAR(16) NOT NULL COMMENT '变更后状态码',
  9. change_time DATETIME NOT NULL COMMENT '业务变更时间',
  10. change_desc VARCHAR(512) DEFAULT NULL COMMENT '变更描述',
  11. void_type VARCHAR(32) DEFAULT NULL COMMENT '作废类型(字典 xyzflx)',
  12. operator_user_id VARCHAR(64) DEFAULT NULL COMMENT '操作人用户ID',
  13. tenant_no VARCHAR(64) NOT NULL DEFAULT 'zkxw' COMMENT '租户编号',
  14. order_num INT DEFAULT NULL COMMENT '排序号',
  15. remark VARCHAR(512) DEFAULT NULL COMMENT '备注',
  16. create_by VARCHAR(64) DEFAULT NULL COMMENT '创建人id',
  17. create_dept_id VARCHAR(64) NOT NULL DEFAULT '' COMMENT '创建人部门ID',
  18. create_time DATETIME NOT NULL COMMENT '创建时间',
  19. update_by VARCHAR(64) DEFAULT NULL COMMENT '更新人id',
  20. update_time DATETIME NOT NULL COMMENT '更新时间',
  21. del_flag SMALLINT NOT NULL DEFAULT 0 COMMENT '删除标记',
  22. PRIMARY KEY (id),
  23. KEY idx_bpsc_protocol_time (tenant_no, collection_protocol_id, del_flag, change_time)
  24. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='采集协议单状态变更记录';
  25. CREATE TABLE IF NOT EXISTS biz_donation_status_change (
  26. id VARCHAR(64) NOT NULL COMMENT '主键ID',
  27. research_donation_id VARCHAR(64) NOT NULL COMMENT '科研捐赠单ID',
  28. change_type VARCHAR(32) NOT NULL COMMENT '变更类型 activate/expire/void/to_storage',
  29. from_status VARCHAR(16) DEFAULT NULL COMMENT '变更前状态码',
  30. to_status VARCHAR(16) NOT NULL COMMENT '变更后状态码',
  31. change_time DATETIME NOT NULL COMMENT '业务变更时间',
  32. change_desc VARCHAR(512) DEFAULT NULL COMMENT '变更描述',
  33. void_type VARCHAR(32) DEFAULT NULL COMMENT '作废类型(字典 xyzflx)',
  34. operator_user_id VARCHAR(64) DEFAULT NULL COMMENT '操作人用户ID',
  35. tenant_no VARCHAR(64) NOT NULL DEFAULT 'zkxw' COMMENT '租户编号',
  36. order_num INT DEFAULT NULL COMMENT '排序号',
  37. remark VARCHAR(512) DEFAULT NULL COMMENT '备注',
  38. create_by VARCHAR(64) DEFAULT NULL COMMENT '创建人id',
  39. create_dept_id VARCHAR(64) NOT NULL DEFAULT '' COMMENT '创建人部门ID',
  40. create_time DATETIME NOT NULL COMMENT '创建时间',
  41. update_by VARCHAR(64) DEFAULT NULL COMMENT '更新人id',
  42. update_time DATETIME NOT NULL COMMENT '更新时间',
  43. del_flag SMALLINT NOT NULL DEFAULT 0 COMMENT '删除标记',
  44. PRIMARY KEY (id),
  45. KEY idx_bdsc_donation_time (tenant_no, research_donation_id, del_flag, change_time)
  46. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='科研捐赠状态变更记录';
  47. -- 回填:主表已有快照各插 1 条(幂等:同协议同 change_type 尚无历史时)
  48. INSERT INTO biz_protocol_status_change (
  49. id, collection_protocol_id, change_type, from_status, to_status,
  50. change_time, change_desc, void_type, operator_user_id,
  51. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  52. )
  53. SELECT
  54. UPPER(MD5(CONCAT(p.id, '|activate'))),
  55. p.id,
  56. 'activate',
  57. NULL,
  58. 'sxz',
  59. p.effective_time,
  60. p.effective_desc,
  61. NULL,
  62. p.update_by,
  63. p.tenant_no,
  64. 0,
  65. NULL,
  66. p.update_by,
  67. '',
  68. IFNULL(p.effective_time, NOW()),
  69. p.update_by,
  70. IFNULL(p.effective_time, NOW()),
  71. 0
  72. FROM biz_collection_protocol p
  73. WHERE p.del_flag = 0
  74. AND p.effective_time IS NOT NULL
  75. AND NOT EXISTS (
  76. SELECT 1 FROM biz_protocol_status_change c
  77. WHERE c.collection_protocol_id = p.id
  78. AND c.change_type = 'activate'
  79. AND c.del_flag = 0
  80. );
  81. INSERT INTO biz_protocol_status_change (
  82. id, collection_protocol_id, change_type, from_status, to_status,
  83. change_time, change_desc, void_type, operator_user_id,
  84. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  85. )
  86. SELECT
  87. UPPER(MD5(CONCAT(p.id, '|expire'))),
  88. p.id,
  89. 'expire',
  90. NULL,
  91. 'ygq',
  92. p.expire_time,
  93. p.expire_desc,
  94. NULL,
  95. p.update_by,
  96. p.tenant_no,
  97. 0,
  98. NULL,
  99. p.update_by,
  100. '',
  101. IFNULL(p.expire_time, NOW()),
  102. p.update_by,
  103. IFNULL(p.expire_time, NOW()),
  104. 0
  105. FROM biz_collection_protocol p
  106. WHERE p.del_flag = 0
  107. AND p.expire_time IS NOT NULL
  108. AND NOT EXISTS (
  109. SELECT 1 FROM biz_protocol_status_change c
  110. WHERE c.collection_protocol_id = p.id
  111. AND c.change_type = 'expire'
  112. AND c.del_flag = 0
  113. );
  114. INSERT INTO biz_protocol_status_change (
  115. id, collection_protocol_id, change_type, from_status, to_status,
  116. change_time, change_desc, void_type, operator_user_id,
  117. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  118. )
  119. SELECT
  120. UPPER(MD5(CONCAT(p.id, '|void'))),
  121. p.id,
  122. 'void',
  123. NULL,
  124. 'yzf',
  125. p.void_time,
  126. p.void_desc,
  127. p.void_type,
  128. p.update_by,
  129. p.tenant_no,
  130. 0,
  131. NULL,
  132. p.update_by,
  133. '',
  134. IFNULL(p.void_time, NOW()),
  135. p.update_by,
  136. IFNULL(p.void_time, NOW()),
  137. 0
  138. FROM biz_collection_protocol p
  139. WHERE p.del_flag = 0
  140. AND p.void_time IS NOT NULL
  141. AND NOT EXISTS (
  142. SELECT 1 FROM biz_protocol_status_change c
  143. WHERE c.collection_protocol_id = p.id
  144. AND c.change_type = 'void'
  145. AND c.del_flag = 0
  146. );
  147. INSERT INTO biz_protocol_status_change (
  148. id, collection_protocol_id, change_type, from_status, to_status,
  149. change_time, change_desc, void_type, operator_user_id,
  150. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  151. )
  152. SELECT
  153. UPPER(MD5(CONCAT(p.id, '|to_research'))),
  154. p.id,
  155. 'to_research',
  156. NULL,
  157. 'zky',
  158. p.to_research_time,
  159. p.to_research_desc,
  160. NULL,
  161. p.update_by,
  162. p.tenant_no,
  163. 0,
  164. NULL,
  165. p.update_by,
  166. '',
  167. IFNULL(p.to_research_time, NOW()),
  168. p.update_by,
  169. IFNULL(p.to_research_time, NOW()),
  170. 0
  171. FROM biz_collection_protocol p
  172. WHERE p.del_flag = 0
  173. AND p.to_research_time IS NOT NULL
  174. AND NOT EXISTS (
  175. SELECT 1 FROM biz_protocol_status_change c
  176. WHERE c.collection_protocol_id = p.id
  177. AND c.change_type = 'to_research'
  178. AND c.del_flag = 0
  179. );
  180. INSERT INTO biz_donation_status_change (
  181. id, research_donation_id, change_type, from_status, to_status,
  182. change_time, change_desc, void_type, operator_user_id,
  183. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  184. )
  185. SELECT
  186. UPPER(MD5(CONCAT(d.id, '|activate'))),
  187. d.id,
  188. 'activate',
  189. NULL,
  190. 'sxz',
  191. d.effective_time,
  192. d.effective_desc,
  193. NULL,
  194. d.update_by,
  195. d.tenant_no,
  196. 0,
  197. NULL,
  198. d.update_by,
  199. '',
  200. IFNULL(d.effective_time, NOW()),
  201. d.update_by,
  202. IFNULL(d.effective_time, NOW()),
  203. 0
  204. FROM biz_research_donation d
  205. WHERE d.del_flag = 0
  206. AND d.effective_time IS NOT NULL
  207. AND NOT EXISTS (
  208. SELECT 1 FROM biz_donation_status_change c
  209. WHERE c.research_donation_id = d.id
  210. AND c.change_type = 'activate'
  211. AND c.del_flag = 0
  212. );
  213. INSERT INTO biz_donation_status_change (
  214. id, research_donation_id, change_type, from_status, to_status,
  215. change_time, change_desc, void_type, operator_user_id,
  216. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  217. )
  218. SELECT
  219. UPPER(MD5(CONCAT(d.id, '|expire'))),
  220. d.id,
  221. 'expire',
  222. NULL,
  223. 'ygq',
  224. d.expire_time,
  225. d.expire_desc,
  226. NULL,
  227. d.update_by,
  228. d.tenant_no,
  229. 0,
  230. NULL,
  231. d.update_by,
  232. '',
  233. IFNULL(d.expire_time, NOW()),
  234. d.update_by,
  235. IFNULL(d.expire_time, NOW()),
  236. 0
  237. FROM biz_research_donation d
  238. WHERE d.del_flag = 0
  239. AND d.expire_time IS NOT NULL
  240. AND NOT EXISTS (
  241. SELECT 1 FROM biz_donation_status_change c
  242. WHERE c.research_donation_id = d.id
  243. AND c.change_type = 'expire'
  244. AND c.del_flag = 0
  245. );
  246. INSERT INTO biz_donation_status_change (
  247. id, research_donation_id, change_type, from_status, to_status,
  248. change_time, change_desc, void_type, operator_user_id,
  249. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  250. )
  251. SELECT
  252. UPPER(MD5(CONCAT(d.id, '|void'))),
  253. d.id,
  254. 'void',
  255. NULL,
  256. 'yzf',
  257. d.void_time,
  258. d.void_desc,
  259. d.void_type,
  260. d.update_by,
  261. d.tenant_no,
  262. 0,
  263. NULL,
  264. d.update_by,
  265. '',
  266. IFNULL(d.void_time, NOW()),
  267. d.update_by,
  268. IFNULL(d.void_time, NOW()),
  269. 0
  270. FROM biz_research_donation d
  271. WHERE d.del_flag = 0
  272. AND d.void_time IS NOT NULL
  273. AND NOT EXISTS (
  274. SELECT 1 FROM biz_donation_status_change c
  275. WHERE c.research_donation_id = d.id
  276. AND c.change_type = 'void'
  277. AND c.del_flag = 0
  278. );
  279. INSERT INTO biz_donation_status_change (
  280. id, research_donation_id, change_type, from_status, to_status,
  281. change_time, change_desc, void_type, operator_user_id,
  282. tenant_no, order_num, remark, create_by, create_dept_id, create_time, update_by, update_time, del_flag
  283. )
  284. SELECT
  285. UPPER(MD5(CONCAT(d.id, '|to_storage'))),
  286. d.id,
  287. 'to_storage',
  288. NULL,
  289. 'zckc',
  290. d.to_storage_time,
  291. d.to_storage_desc,
  292. NULL,
  293. d.update_by,
  294. d.tenant_no,
  295. 0,
  296. NULL,
  297. d.update_by,
  298. '',
  299. IFNULL(d.to_storage_time, NOW()),
  300. d.update_by,
  301. IFNULL(d.to_storage_time, NOW()),
  302. 0
  303. FROM biz_research_donation d
  304. WHERE d.del_flag = 0
  305. AND d.to_storage_time IS NOT NULL
  306. AND NOT EXISTS (
  307. SELECT 1 FROM biz_donation_status_change c
  308. WHERE c.research_donation_id = d.id
  309. AND c.change_type = 'to_storage'
  310. AND c.del_flag = 0
  311. );