|  | @@ -0,0 +1,112 @@
 | 
	
		
			
				|  |  | +'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;
 | 
	
		
			
				|  |  | +};
 |