Browse Source

同步更新到审批流程

laiguoran 2 years ago
parent
commit
8411d32ff9
2 changed files with 68 additions and 3 deletions
  1. 61 0
      app/service/shenpi_audit.js
  2. 7 3
      app/service/stage_audit.js

+ 61 - 0
app/service/shenpi_audit.js

@@ -197,6 +197,67 @@ module.exports = app => {
                 throw err;
             }
         }
+
+        // 更新审批流程
+        async updateAuditList(transaction, tenderId, sp_status, sp_type, ids) {
+            if (sp_status === shenpiConst.sp_status.gdspl) {
+                const auditList = await this.getAuditList(tenderId, sp_type, sp_status);
+                const oldIds = this._.map(auditList, 'audit_id');
+                if (this._.isEqual(ids, oldIds)) {
+                    return;
+                }
+                // 更新固定审批流
+                await transaction.delete(this.tableName, { tid: tenderId, sp_type, sp_status });
+                const insertDatas = [];
+                for (const id of ids) {
+                    insertDatas.push({
+                        tid: tenderId,
+                        sp_type,
+                        sp_status,
+                        audit_id: id,
+                    });
+                }
+                await transaction.insert(this.tableName, insertDatas);
+                if (sp_type === shenpiConst.sp_type.stage) {
+                    // 判断哪些audit_id不存在了,哪些audit_为新增
+                    const exist = this._.difference(ids, oldIds);// 判断新增的
+                    const unExist = this._.difference(oldIds, ids);// 判断已删除的
+                    console.log(ids, oldIds, exist, unExist);
+                    if (exist.length > 0) {
+                        const options = {
+                            where: {
+                                tid: this.ctx.tender.id,
+                                user_id: exist,
+                            },
+                        };
+                        const updateData = {
+                            status: 1,
+                        };
+                        await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);
+                    }
+                    if (unExist.length > 0) {
+                        const options2 = {
+                            where: {
+                                tid: this.ctx.tender.id,
+                                user_id: unExist,
+                            },
+                        };
+                        const updateData2 = {
+                            status: 0,
+                        };
+                        await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData2, options2);
+                    }
+                }
+            } else if (sp_status === shenpiConst.sp_status.gdzs) {
+                const audit = await this.getAudit(tenderId, sp_type, sp_status);
+                if (audit && audit.audit_id !== ids[ids.length - 1]) {
+                    // 更换终审
+                    await transaction.update(this.tableName, { audit_id: ids[ids.length - 1] }, { where: { tid: tenderId, sp_type, sp_status } });
+                } else if (!audit) {
+                    await transaction.insert(this.tableName, { tid: tenderId, sp_type, sp_status, audit_id: ids[ids.length - 1] });
+                }
+            }
+        }
     }
 
     return ShenpiAudit;

+ 7 - 3
app/service/stage_audit.js

@@ -1394,7 +1394,7 @@ module.exports = app => {
          * @param auditorId
          * @return {Promise<*>}
          */
-        async getAuditGroupByList(stageId, times) {
+        async getAuditGroupByList(stageId, times, transaction = false) {
             // const sql =
             //     'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
             //     '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
@@ -1406,7 +1406,7 @@ module.exports = app => {
                 ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
                 ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
             const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
-            return await this.db.query(sql, sqlParam);
+            return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
         }
 
         /**
@@ -1736,7 +1736,6 @@ module.exports = app => {
             try {
                 const auditors = await this.getAuditGroupByList(stageId, times);
                 const now_audit = this._.find(auditors, { aid: data.old_aid });
-                console.log(now_audit);
                 if (data.operate === 'add') {
                     if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
                         throw '当前人下无法操作新增';
@@ -1783,6 +1782,11 @@ module.exports = app => {
                         }
                     });
                 }
+                if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
+                    const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
+                    await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, this._.map(newAuditors, 'aid'));
+                }
+                // 更新到审批流程方法
                 await transaction.commit();
             } catch (err) {
                 await transaction.rollback();