123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 'use strict';
- /**
- * 变更新增部位插入记录表
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class ChangeHistory extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_history';
- }
- async saveHistory(transaction, data, list) {
- // const info_json = {
- // code: data.code,
- // w_code: data.code,
- // p_code: data.p_code,
- // name: data.name,
- // plan_code: data.plan_code,
- // peg: data.peg,
- // org_name: data.org_name,
- // org_code: data.org_code,
- // new_name: data.new_name,
- // new_code: data.new_code,
- // content: data.content,
- // basis: data.basis,
- // expr: data.expr,
- // memo: data.memo,
- // type: data.type,
- // class: data.class,
- // quality: data.quality,
- // company: data.company,
- // charge: data.charge,
- // };
- await transaction.insert(this.tableName, {
- tid: data.tid,
- cid: data.cid,
- info_json: JSON.stringify(data),
- list_json: JSON.stringify(list),
- });
- }
- async returnHistory(transaction, cid) {
- const data = await transaction.get(this.tableName, { cid });
- if (!data) throw '撤销前数据不存在,无法撤销';
- const change_update = {};
- const oldInfo = JSON.parse(data.info_json);
- for (const key in oldInfo) {
- if (key !== 'cid' && key !== 'in_time') {
- change_update[key] = oldInfo[key];
- }
- }
- const options = {
- where: {
- cid,
- },
- };
- await transaction.update(this.ctx.service.change.tableName, change_update, options);
- const oldList = JSON.parse(data.list_json);
- // 更新已调用清单id值
- const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? GROUP BY b.cbid';
- const sqlParam1 = [this.ctx.service.stageChange.tableName, this.ctx.service.changeAuditList.tableName, cid];
- const usedList = await transaction.query(sql1, sqlParam1);
- // 先删后插
- await transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
- await transaction.insert(this.ctx.service.changeAuditList.tableName, oldList);
- // 可能需要更新stage_change和stage_change_final的cbid
- if (usedList.length > 0) {
- const updateList = [];
- const sql2 = 'SELECT * FROM ?? WHERE `cid` = ? AND `lid` != "0"';
- const sqlParam2 = [this.ctx.service.changeAuditList.tableName, cid];
- const newList = await transaction.query(sql2, sqlParam2);
- // const newList = await transaction.select(this.tableName, { where: { cid: this.ctx.change.cid } });
- for (const used of usedList) {
- const findFilter = { lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx };
- const findFilter2 = { lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx };
- if (used.mx_id) findFilter2.mx_id = used.mx_id;
- const newone = this._.find(newList, findFilter2) || this._.find(newList, findFilter);
- if (newone && newone.id !== used.cbid) {
- updateList.push({
- row: {
- cbid: newone.id,
- },
- where: {
- cid,
- cbid: used.id,
- },
- });
- }
- }
- if (updateList.length > 0) {
- await transaction.updateRows(this.ctx.service.stageChange.tableName, updateList);
- await transaction.updateRows(this.ctx.service.stageChangeFinal.tableName, updateList);
- }
- }
- }
- }
- return ChangeHistory;
- };
|