Jelajahi Sumber

删除本期审批,删除数据相关

MaiXinRong 4 tahun lalu
induk
melakukan
90daa1ec60

+ 69 - 17
app/service/stage_audit.js

@@ -12,6 +12,7 @@ const auditConst = require('../const/audit').stage;
 const smsTypeConst = require('../const/sms_type');
 const SMS = require('../lib/sms');
 const SmsAliConst = require('../const/sms_alitemplate');
+const payConst = require('../const/deal_pay');
 
 module.exports = app => {
     class StageAudit extends app.BaseService {
@@ -985,6 +986,45 @@ module.exports = app => {
         }
 
         /**
+         * 删除 某期 某次 全审批流程
+         * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
+         * @param {Number} sid - 标段id
+         * @param {Number} times - 第几次审批
+         * @param transaction - 删除事务
+         * @return {Promise<void>}
+         */
+        async _timesDelete(sid, times, transaction) {
+            console.log(sid, times);
+            // 审批流程
+            await transaction.delete(this.tableName, { sid: sid, times: times });
+            await transaction.delete(this.ctx.service.pos.tableName, {add_stage: sid, add_times: times});
+            await transaction.delete(this.ctx.service.stageBills.tableName, { sid: sid, times: times });
+            await transaction.delete(this.ctx.service.stagePos.tableName, { sid: sid, times: times });
+            await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: sid, times: times });
+            await transaction.delete(this.ctx.service.stageChange.tableName, { sid: sid, stimes: times });
+            // 合同支付
+            const payList = await this.ctx.service.stagePay.getAllDataByCondition({ where: { sid: sid, stimes: times } });
+            if (payList) {
+                for (const pt of payList) {
+                    if (pt.attachment !== null && pt.attachment !== '') {
+                        const payAttList = JSON.parse(pt.attachment);
+                        for (const pat of payAttList) {
+                            if (fs.existsSync(path.join(this.app.baseDir, pat.filepath))) {
+                                await fs.unlinkSync(path.join(this.app.baseDir, pat.filepath));
+                            }
+                        }
+                    }
+                }
+            }
+            await transaction.delete(this.ctx.service.stagePay.tableName, { sid: sid, stimes: times });
+            await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
+            // 其他台账
+            await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
+            await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
+            await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
+        }
+
+        /**
          * 删除本次审批流程
          * @param {Number} stageId - 标段id
          * @param {Number} times - 第几次审批
@@ -993,12 +1033,13 @@ module.exports = app => {
         async timesDelete() {
             const transaction = await this.db.beginTransaction();
             try {
-                // 判断当前期是否是重新上报状态,决定删除times数并获取最新的times
-                await transaction.delete(this.tableName, { sid: this.ctx.stage.id, times: this.ctx.stage.times });
+                // 删除最新一次数据
+                await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
+                // 审批退回,未重新上报时,需删除最新两次数据
                 const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
                 const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
                 if (isCheckNo) {
-                    await transaction.delete(this.tableName, { sid: this.ctx.stage.id, times: nowTimes });
+                    await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
                 }
                 // 添加上一次审批人
                 const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
@@ -1015,24 +1056,35 @@ module.exports = app => {
                 // 拷贝新一次审核流程列表
                 await transaction.insert(this.tableName, auditors);
 
-                // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
-                // // 计算并合同支付最终数据
-                // const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
+                // 计算缓存
+                this.ctx.stage.curTimes = this.ctx.stage.nowTimes;
+                this.ctx.stage.curOrder = 0;
+                const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
+                // 计算并合同支付最终数据
+                const lastAudit = await this.getDataByCondition({sid: this.ctx.stage.id, times: nowTimes - 1, status: auditConst.status.checkNo});
+                if (!lastAudit) throw '审批数据错误';
+                await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, lastAudit.times, lastAudit.order, transaction);
+                const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
+                const yfPay = stagePay.find(function (x) {
+                    return x.ptype === payConst.payType.yf;
+                });
+                const sfPay = stagePay.find(function (x) {
+                    return x.ptype === payConst.payType.sf;
+                });
                 // 同步 期信息
+                const time = new Date();
                 await transaction.update(this.ctx.service.stage.tableName, {
-                    id: this.ctx.stage.id, status: auditConst.status.checkNo,
-                    // contract_tp: tpData.contract_tp,
-                    // qc_tp: tpData.qc_tp,
+                    id: this.ctx.stage.id,
+                    status: auditConst.status.checkNo,
+                    contract_tp: tpData.contract_tp,
+                    qc_tp: tpData.qc_tp,
                     times: nowTimes,
-                    // yf_tp: yfPay.tp,
-                    // sf_tp: sfPay.tp,
-                    // tp_history: JSON.stringify(this.ctx.stage.tp_history),
-                    // cache_time_r: this.ctx.stage.cache_time_l,
+                    yf_tp: yfPay.tp,
+                    sf_tp: sfPay.tp,
+                    tp_history: JSON.stringify(this.ctx.stage.tp_history),
+                    cache_time_l: time,
+                    cache_time_r: time,
                 });
-                // 复制一份最新数据给原报
-                // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
-                // await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
-                // await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
                 await transaction.commit();
             } catch (err) {
                 await transaction.rollback();

+ 24 - 1
app/service/stage_bonus.js

@@ -167,7 +167,7 @@ module.exports = app => {
             const updateDatas = [];
             for (const d of datas) {
                 const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
-                const his = datas.find(function (x) {
+                const his = history.find(function (x) {
                     return x.stimes && x.stimes === this.ctx.stage.curTimes
                         && x.sorder && x.sorder === this.ctx.stage.curOrder;
                 });
@@ -180,6 +180,29 @@ module.exports = app => {
             }
             await transaction.updateRows(this.tableName, updateDatas);
         }
+
+
+        async deleteStageTimesData(sid, times, transaction) {
+            const datas = await this.getAllDataByCondition({where: { sid: sid }});
+            if (datas.length === 0) return;
+
+            const updateDatas = [];
+            for (const d of datas) {
+                const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
+                const his = history.filter(function (x) {
+                    return x.stimes && x.stimes < times;
+                });
+                his.sort(function (x, y) {
+                    return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
+                });
+                updateDatas.push({
+                    id: d.id,
+                    shistory: JSON.stringify(his),
+                    tp: his.length > 0 ? his[his.length - 1].tp : null,
+                });
+            }
+            await transaction.updateRows(this.tableName, updateDatas);
+        }
     }
 
     return StageBonus;

+ 26 - 1
app/service/stage_jgcl.js

@@ -188,7 +188,7 @@ module.exports = app => {
             const updateDatas = [];
             for (const d of datas) {
                 const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
-                const his = datas.find(function (x) {
+                const his = history.find(function (x) {
                     return x.stimes && x.stimes === this.ctx.stage.curTimes
                         && x.sorder && x.sorder === this.ctx.stage.curOrder;
                 });
@@ -228,6 +228,31 @@ module.exports = app => {
                 return true;
             }
         }
+
+        async deleteStageTimesData(sid, times, transaction) {
+            const datas = await this.getAllDataByCondition({where: { sid: sid }});
+            if (datas.length === 0) return;
+
+            const updateDatas = [];
+            for (const d of datas) {
+                const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
+                const his = history.filter(function (x) {
+                    return x.stimes && x.stimes < times;
+                });
+                his.sort(function (x, y) {
+                    return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
+                });
+                updateDatas.push({
+                    id: d.id,
+                    shistory: JSON.stringify(his),
+                    arrive_qty: his.length > 0 ? his[his.length - 1].arrive_qty : null,
+                    arrive_tp: his.length > 0 ? his[his.length - 1].arrive_tp : null,
+                    deduct_qty: his.length > 0 ? his[his.length - 1].deduct_qty : null,
+                    deduct_tp: his.length > 0 ? his[his.length - 1].deduct_tp : null,
+                });
+            }
+            await transaction.updateRows(this.tableName, updateDatas);
+        }
     }
 
     return StageJgcl;

+ 23 - 1
app/service/stage_other.js

@@ -161,7 +161,7 @@ module.exports = app => {
             const updateDatas = [];
             for (const d of datas) {
                 const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
-                const his = datas.find(function (x) {
+                const his = history.find(function (x) {
                     return x.stimes && x.stimes === this.ctx.stage.curTimes
                         && x.sorder && x.sorder === this.ctx.stage.curOrder;
                 });
@@ -199,6 +199,28 @@ module.exports = app => {
             }
 
         }
+
+        async deleteStageTimesData(sid, times, transaction) {
+            const datas = await this.getAllDataByCondition({where: { sid: sid }});
+            if (datas.length === 0) return;
+
+            const updateDatas = [];
+            for (const d of datas) {
+                const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
+                const his = history.filter(function (x) {
+                    return x.stimes && x.stimes < times;
+                });
+                his.sort(function (x, y) {
+                    return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
+                });
+                updateDatas.push({
+                    id: d.id,
+                    shistory: JSON.stringify(his),
+                    tp: his.length > 0 ? his[his.length - 1].tp : null,
+                });
+            }
+            await transaction.updateRows(this.tableName, updateDatas);
+        }
     }
 
     return StageOther;

+ 4 - 0
sql/update.sql

@@ -102,3 +102,7 @@ ADD COLUMN `stage_select`  text CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL
 
 ALTER TABLE `calculation`.`zh_material_file`
 ADD COLUMN `s_order` VARCHAR(255) NOT NULL  COMMENT '期数order' AFTER `file_name`;
+
+-- ----------------------------
+-- 以上于2020-7-23 15:00 因测试需求更新至uat,尚未更新prod,如需增加sql,请一定在其后添加
+-- ----------------------------