change_history.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. /**
  3. * 变更新增部位插入记录表
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ChangeHistory extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'change_history';
  20. }
  21. async saveHistory(transaction, data, list) {
  22. // const info_json = {
  23. // code: data.code,
  24. // w_code: data.code,
  25. // p_code: data.p_code,
  26. // name: data.name,
  27. // plan_code: data.plan_code,
  28. // peg: data.peg,
  29. // org_name: data.org_name,
  30. // org_code: data.org_code,
  31. // new_name: data.new_name,
  32. // new_code: data.new_code,
  33. // content: data.content,
  34. // basis: data.basis,
  35. // expr: data.expr,
  36. // memo: data.memo,
  37. // type: data.type,
  38. // class: data.class,
  39. // quality: data.quality,
  40. // company: data.company,
  41. // charge: data.charge,
  42. // };
  43. await transaction.insert(this.tableName, {
  44. tid: data.tid,
  45. cid: data.cid,
  46. info_json: JSON.stringify(data),
  47. list_json: JSON.stringify(list),
  48. });
  49. }
  50. async returnHistory(transaction, cid) {
  51. const data = await transaction.get(this.tableName, { cid });
  52. if (!data) throw '撤销前数据不存在,无法撤销';
  53. const change_update = {};
  54. const oldInfo = JSON.parse(data.info_json);
  55. for (const key in oldInfo) {
  56. if (key !== 'cid' && key !== 'in_time') {
  57. change_update[key] = oldInfo[key];
  58. }
  59. }
  60. const options = {
  61. where: {
  62. cid,
  63. },
  64. };
  65. await transaction.update(this.ctx.service.change.tableName, change_update, options);
  66. const oldList = JSON.parse(data.list_json);
  67. // 更新已调用清单id值
  68. const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? GROUP BY b.cbid';
  69. const sqlParam1 = [this.ctx.service.stageChange.tableName, this.ctx.service.changeAuditList.tableName, cid];
  70. const usedList = await transaction.query(sql1, sqlParam1);
  71. // 先删后插
  72. await transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  73. await transaction.insert(this.ctx.service.changeAuditList.tableName, oldList);
  74. // 可能需要更新stage_change和stage_change_final的cbid
  75. if (usedList.length > 0) {
  76. const updateList = [];
  77. const sql2 = 'SELECT * FROM ?? WHERE `cid` = ? AND `lid` != "0"';
  78. const sqlParam2 = [this.ctx.service.changeAuditList.tableName, cid];
  79. const newList = await transaction.query(sql2, sqlParam2);
  80. // const newList = await transaction.select(this.tableName, { where: { cid: this.ctx.change.cid } });
  81. for (const used of usedList) {
  82. const findFilter = { lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx };
  83. const findFilter2 = { lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx };
  84. if (used.mx_id) findFilter2.mx_id = used.mx_id;
  85. const newone = this._.find(newList, findFilter2) || this._.find(newList, findFilter);
  86. if (newone && newone.id !== used.cbid) {
  87. updateList.push({
  88. row: {
  89. cbid: newone.id,
  90. },
  91. where: {
  92. cid,
  93. cbid: used.id,
  94. },
  95. });
  96. }
  97. }
  98. if (updateList.length > 0) {
  99. await transaction.updateRows(this.ctx.service.stageChange.tableName, updateList);
  100. await transaction.updateRows(this.ctx.service.stageChangeFinal.tableName, updateList);
  101. }
  102. }
  103. }
  104. }
  105. return ChangeHistory;
  106. };