|  | @@ -85,11 +85,11 @@ module.exports = app => {
 | 
	
		
			
				|  |  |           */
 | 
	
		
			
				|  |  |          async getAuditors(stageId, times = 1, order_sort = 'asc') {
 | 
	
		
			
				|  |  |              const sql =
 | 
	
		
			
				|  |  | -                'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.sign_path, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
 | 
	
		
			
				|  |  | -                'FROM ?? AS la, ?? AS pa, (SELECT sa.`aid`,(@i:=@i+1) as `sort` FROM (SELECT * FROM ?? ORDER BY `order` ASC) sa, (select @i:=0) as it WHERE sa.`sid` = ? AND sa.`times` = ? GROUP BY sa.`aid`) as g ' +
 | 
	
		
			
				|  |  | +                'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.sign_path, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.`is_old`, g.`sort` ' +
 | 
	
		
			
				|  |  | +                'FROM ?? AS la, ?? AS pa, (SELECT sa.`aid`,(@i:=@i+1) as `sort` FROM (SELECT * FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid` ORDER BY `order` ASC) sa, (select @i:=0) as it WHERE sa.`sid` = ? AND sa.`times` = ? GROUP BY sa.`aid`) as g ' +
 | 
	
		
			
				|  |  |                  'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order` ' +
 | 
	
		
			
				|  |  |                  order_sort;
 | 
	
		
			
				|  |  | -            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, stageId, times, stageId, times];
 | 
	
		
			
				|  |  | +            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, stageId, times, stageId, times, stageId, times];
 | 
	
		
			
				|  |  |              const result = await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  |              const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as a';
 | 
	
		
			
				|  |  |              const sqlParam2 = [this.tableName, stageId, times];
 | 
	
	
		
			
				|  | @@ -489,6 +489,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                          cache_time_r: this.ctx.stage.cache_time_l,
 | 
	
		
			
				|  |  |                          his_id,
 | 
	
		
			
				|  |  |                      });
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                      // 添加短信通知-审批通过提醒功能
 | 
	
		
			
				|  |  |                      const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 | 
	
	
		
			
				|  | @@ -775,7 +776,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
 | 
	
		
			
				|  |  |                  const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
 | 
	
		
			
				|  |  |                  const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return x.user_id === preAuditor.aid}), 'ass_user_id');
 | 
	
		
			
				|  |  | -                user.push(preAuditor.aid);
 | 
	
		
			
				|  |  | +                users.push(preAuditor.aid);
 | 
	
		
			
				|  |  |                  await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 | 
	
		
			
				|  |  |                      qi: stageInfo.order,
 | 
	
		
			
				|  |  |                      code: shenpiUrl,
 | 
	
	
		
			
				|  | @@ -939,7 +940,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |                  // }
 | 
	
		
			
				|  |  |                  const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
 | 
	
		
			
				|  |  |                  const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
 | 
	
		
			
				|  |  | -                const users = this._.map(this.ctx.stage.auditAssist.filter(x => { return x.user_id == audit.aid; }), 'ass_user_id');
 | 
	
		
			
				|  |  | +                const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return x.user_id == audit.aid; }), 'ass_user_id');
 | 
	
		
			
				|  |  |                  users.push(audit.aid);
 | 
	
		
			
				|  |  |                  await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 | 
	
		
			
				|  |  |                      qi: stageInfo.order,
 | 
	
	
		
			
				|  | @@ -977,6 +978,319 @@ module.exports = app => {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          /**
 | 
	
		
			
				|  |  | +         * 审批撤回
 | 
	
		
			
				|  |  | +         * @param {Number} stageId - 标段id
 | 
	
		
			
				|  |  | +         * @param {Number} times - 第几次审批
 | 
	
		
			
				|  |  | +         * @return {Promise<void>}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async checkCancel(stageId, times = 1) {
 | 
	
		
			
				|  |  | +            // 分3种情况,根据ctx.cancancel值判断:
 | 
	
		
			
				|  |  | +            // 1.原报发起撤回,当前流程删除,并回到待上报
 | 
	
		
			
				|  |  | +            // 2.审批人撤回审批通过,增加流程,并回到它审批中
 | 
	
		
			
				|  |  | +            // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
 | 
	
		
			
				|  |  | +            // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
 | 
	
		
			
				|  |  | +            const transaction = await this.db.beginTransaction();
 | 
	
		
			
				|  |  | +            const time = new Date();
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                if (this.ctx.stage.cancancel === 1) {
 | 
	
		
			
				|  |  | +                    // 原报撤回,判断是否为多次,多次则为退回状态
 | 
	
		
			
				|  |  | +                    // 整理当前流程审核人状态更新
 | 
	
		
			
				|  |  | +                    const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
 | 
	
		
			
				|  |  | +                    // // 审批人变成待审批状态
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, {
 | 
	
		
			
				|  |  | +                        id: curAudit.id,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.uncheck,
 | 
	
		
			
				|  |  | +                        begin_time: null,
 | 
	
		
			
				|  |  | +                        opinion: null,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    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.tp_history.push({
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        yf_tp: yfPay.tp,
 | 
	
		
			
				|  |  | +                        sf_tp: sfPay.tp,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    await transaction.update(this.ctx.service.stage.tableName, {
 | 
	
		
			
				|  |  | +                        id: stageId,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        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,
 | 
	
		
			
				|  |  | +                        status: times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    // 计算该审批人最终数据
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    // 复制一份最新数据给下一人
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                } else if (this.ctx.stage.cancancel === 2) {
 | 
	
		
			
				|  |  | +                    const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 | 
	
		
			
				|  |  | +                    // 整理当前流程审核人状态更新
 | 
	
		
			
				|  |  | +                    const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
 | 
	
		
			
				|  |  | +                    const preAudit = this.ctx.stage.preAudit;
 | 
	
		
			
				|  |  | +                    if (!curAudit || curAudit.order <= 1) {
 | 
	
		
			
				|  |  | +                        throw '撤回用户数据错误';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    // 顺移气候审核人流程顺序
 | 
	
		
			
				|  |  | +                    this.initSqlBuilder();
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
 | 
	
		
			
				|  |  | +                    const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
 | 
	
		
			
				|  |  | +                    const data = await transaction.query(sql, sqlParam);
 | 
	
		
			
				|  |  | +                    // 当前审批人2次添加至流程中
 | 
	
		
			
				|  |  | +                    const newAuditors = [];
 | 
	
		
			
				|  |  | +                    newAuditors.push({
 | 
	
		
			
				|  |  | +                        tid: curAudit.tid,
 | 
	
		
			
				|  |  | +                        sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                        aid: preAudit.aid,
 | 
	
		
			
				|  |  | +                        times: curAudit.times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checkCancel,
 | 
	
		
			
				|  |  | +                        begin_time: time,
 | 
	
		
			
				|  |  | +                        end_time: time,
 | 
	
		
			
				|  |  | +                        opinion: '',
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    newAuditors.push({
 | 
	
		
			
				|  |  | +                        tid: curAudit.tid,
 | 
	
		
			
				|  |  | +                        sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                        aid: preAudit.aid,
 | 
	
		
			
				|  |  | +                        times: curAudit.times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order + 1,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checking,
 | 
	
		
			
				|  |  | +                        begin_time: time,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    await transaction.insert(this.tableName, newAuditors);
 | 
	
		
			
				|  |  | +                    // 当前审批人变成待审批
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
 | 
	
		
			
				|  |  | +                    // 计算并合同支付最终数据
 | 
	
		
			
				|  |  | +                    const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    this.ctx.stage.tp_history.push({
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        yf_tp: yfPay.tp,
 | 
	
		
			
				|  |  | +                        sf_tp: sfPay.tp,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    // 同步 期信息
 | 
	
		
			
				|  |  | +                    await transaction.update(this.ctx.service.stage.tableName, {
 | 
	
		
			
				|  |  | +                        id: stageId,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        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,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    // 计算该审批人最终数据
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    // 复制一份最新数据给下一人
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, curAudit.order + 1, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    // 锁定本人数据,保留锁定数据相关确认状态
 | 
	
		
			
				|  |  | +                    // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
 | 
	
		
			
				|  |  | +                } else if (this.ctx.stage.cancancel === 3) {
 | 
	
		
			
				|  |  | +                    const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 | 
	
		
			
				|  |  | +                    // 整理当前流程审核人状态更新
 | 
	
		
			
				|  |  | +                    const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
 | 
	
		
			
				|  |  | +                    const preAudit = this.ctx.stage.preAudit;
 | 
	
		
			
				|  |  | +                    if (!curAudit || curAudit.order <= 1) {
 | 
	
		
			
				|  |  | +                        throw '撤回用户数据错误';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    // // 顺移气候审核人流程顺序
 | 
	
		
			
				|  |  | +                    // this.initSqlBuilder();
 | 
	
		
			
				|  |  | +                    // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
 | 
	
		
			
				|  |  | +                    // this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
 | 
	
		
			
				|  |  | +                    // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
 | 
	
		
			
				|  |  | +                    // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
 | 
	
		
			
				|  |  | +                    // const data = await transaction.query(sql, sqlParam);
 | 
	
		
			
				|  |  | +                    // 添加撤回人到审批流程中
 | 
	
		
			
				|  |  | +                    const newAuditors = [];
 | 
	
		
			
				|  |  | +                    newAuditors.push({
 | 
	
		
			
				|  |  | +                        tid: curAudit.tid,
 | 
	
		
			
				|  |  | +                        sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                        aid: preAudit.aid,
 | 
	
		
			
				|  |  | +                        times: curAudit.times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checkCancel,
 | 
	
		
			
				|  |  | +                        begin_time: time,
 | 
	
		
			
				|  |  | +                        end_time: time,
 | 
	
		
			
				|  |  | +                        opinion: '',
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    await transaction.insert(this.tableName, newAuditors);
 | 
	
		
			
				|  |  | +                    // 删除当前审批人
 | 
	
		
			
				|  |  | +                    await transaction.delete(this.tableName, { id: curAudit.id });
 | 
	
		
			
				|  |  | +                    // 更新上一个人为审批中
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
 | 
	
		
			
				|  |  | +                        where: {
 | 
	
		
			
				|  |  | +                            sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                            times: curAudit.times,
 | 
	
		
			
				|  |  | +                            order: curAudit.order + 1,
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    // 修改stage状态为审批中
 | 
	
		
			
				|  |  | +                    await transaction.update(this.ctx.service.stage.tableName, { id: this.ctx.stage.id, status: auditConst.status.checking });
 | 
	
		
			
				|  |  | +                    // 计算并合同支付最终数据
 | 
	
		
			
				|  |  | +                    const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    this.ctx.stage.tp_history.push({
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        yf_tp: yfPay.tp,
 | 
	
		
			
				|  |  | +                        sf_tp: sfPay.tp,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    // 同步 期信息
 | 
	
		
			
				|  |  | +                    await transaction.update(this.ctx.service.stage.tableName, {
 | 
	
		
			
				|  |  | +                        id: stageId,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        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,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    // 计算该审批人最终数据
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    // 复制一份最新数据给下一人
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, curAudit.order + 1, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    // 锁定本人数据,保留锁定数据相关确认状态
 | 
	
		
			
				|  |  | +                    // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
 | 
	
		
			
				|  |  | +                } else if (this.ctx.stage.cancancel === 4) {
 | 
	
		
			
				|  |  | +                    // 原报撤回,判断是否为多次,多次则为退回状态
 | 
	
		
			
				|  |  | +                    // 整理上一个流程审核人状态更新
 | 
	
		
			
				|  |  | +                    const curAudit = await this.getDataByCondition({ sid: stageId, times: times - 1, status: auditConst.status.checkNo });
 | 
	
		
			
				|  |  | +                    // 顺移气候审核人流程顺序
 | 
	
		
			
				|  |  | +                    this.initSqlBuilder();
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
 | 
	
		
			
				|  |  | +                    this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
 | 
	
		
			
				|  |  | +                    const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
 | 
	
		
			
				|  |  | +                    const data = await transaction.query(sql, sqlParam);
 | 
	
		
			
				|  |  | +                    // 当前审批人2次添加至流程中
 | 
	
		
			
				|  |  | +                    const newAuditors = [];
 | 
	
		
			
				|  |  | +                    newAuditors.push({
 | 
	
		
			
				|  |  | +                        tid: curAudit.tid,
 | 
	
		
			
				|  |  | +                        sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                        aid: curAudit.aid,
 | 
	
		
			
				|  |  | +                        times: curAudit.times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order + 1,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checkCancel,
 | 
	
		
			
				|  |  | +                        begin_time: time,
 | 
	
		
			
				|  |  | +                        end_time: time,
 | 
	
		
			
				|  |  | +                        opinion: '',
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    newAuditors.push({
 | 
	
		
			
				|  |  | +                        tid: curAudit.tid,
 | 
	
		
			
				|  |  | +                        sid: curAudit.sid,
 | 
	
		
			
				|  |  | +                        aid: curAudit.aid,
 | 
	
		
			
				|  |  | +                        times: curAudit.times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order + 2,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checking,
 | 
	
		
			
				|  |  | +                        begin_time: time,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    await transaction.insert(this.tableName, newAuditors);
 | 
	
		
			
				|  |  | +                    // 删除当前次审批流
 | 
	
		
			
				|  |  | +                    await transaction.delete(this.tableName, { sid: stageId, times });
 | 
	
		
			
				|  |  | +                    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.tp_history.push({
 | 
	
		
			
				|  |  | +                        times,
 | 
	
		
			
				|  |  | +                        order: curAudit.order,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        yf_tp: yfPay.tp,
 | 
	
		
			
				|  |  | +                        sf_tp: sfPay.tp,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    await transaction.update(this.ctx.service.stage.tableName, {
 | 
	
		
			
				|  |  | +                        id: stageId,
 | 
	
		
			
				|  |  | +                        contract_tp: tpData.contract_tp,
 | 
	
		
			
				|  |  | +                        qc_tp: tpData.qc_tp,
 | 
	
		
			
				|  |  | +                        positive_qc_tp: tpData.positive_qc_tp,
 | 
	
		
			
				|  |  | +                        negative_qc_tp: tpData.negative_qc_tp,
 | 
	
		
			
				|  |  | +                        times: times - 1,
 | 
	
		
			
				|  |  | +                        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,
 | 
	
		
			
				|  |  | +                        status: auditConst.status.checking,
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                    // 计算该审批人最终数据
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    // 复制一份最新数据给下一人
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 1, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 2, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                    await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                // 上报/审批 - 检查三方特殊推送
 | 
	
		
			
				|  |  | +                await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
 | 
	
		
			
				|  |  | +                await transaction.commit();
 | 
	
		
			
				|  |  | +                // 通知发送 - 第三方更新
 | 
	
		
			
				|  |  | +                if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
 | 
	
		
			
				|  |  | +                    const base_data = {
 | 
	
		
			
				|  |  | +                        tid: this.ctx.tender.id,
 | 
	
		
			
				|  |  | +                        sid: stageId,
 | 
	
		
			
				|  |  | +                        op: 'update',
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
 | 
	
		
			
				|  |  | +                    base_data.op = 'update';
 | 
	
		
			
				|  |  | +                    base_data.sid = -1;
 | 
	
		
			
				|  |  | +                    this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } catch (err) {
 | 
	
		
			
				|  |  | +                await transaction.rollback();
 | 
	
		
			
				|  |  | +                throw err;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  |           * 获取审核人需要审核的期列表
 | 
	
		
			
				|  |  |           *
 | 
	
		
			
				|  |  |           * @param auditorId
 | 
	
	
		
			
				|  | @@ -1080,13 +1394,19 @@ 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` ' +
 | 
	
		
			
				|  |  | +            //     '  WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
 | 
	
		
			
				|  |  | +            // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
 | 
	
		
			
				|  |  |              const sql =
 | 
	
		
			
				|  |  | -                'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
 | 
	
		
			
				|  |  | -                '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
 | 
	
		
			
				|  |  | -                '  WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
 | 
	
		
			
				|  |  | -            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
 | 
	
		
			
				|  |  | -            return await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  | +                'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`' +
 | 
	
		
			
				|  |  | +                ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
 | 
	
		
			
				|  |  | +                ' 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 transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          /**
 | 
	
	
		
			
				|  | @@ -1232,7 +1552,7 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
 | 
	
		
			
				|  |  |              await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
 | 
	
		
			
				|  |  |              await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
 | 
	
		
			
				|  |  | -            await transaction.delete(this.ctx.serivce.stageAuditAss.tableName, { sid, times });
 | 
	
		
			
				|  |  | +            await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
 | 
	
		
			
				|  |  |              // 其他台账
 | 
	
		
			
				|  |  |              await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
 | 
	
		
			
				|  |  |              await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
 | 
	
	
		
			
				|  | @@ -1403,6 +1723,76 @@ module.exports = app => {
 | 
	
		
			
				|  |  |              const result = await this.db.queryOne(sql, sqlParam);
 | 
	
		
			
				|  |  |              return result ? result.num : 0;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        /**
 | 
	
		
			
				|  |  | +         * 删除本次审批流程
 | 
	
		
			
				|  |  | +         * @param {Number} stageId - 标段id
 | 
	
		
			
				|  |  | +         * @param {Number} times - 第几次审批
 | 
	
		
			
				|  |  | +         * @param {Object} data - 更改参数
 | 
	
		
			
				|  |  | +         * @return {Promise<void>}
 | 
	
		
			
				|  |  | +         */
 | 
	
		
			
				|  |  | +        async saveAudit(stageId, times, data) {
 | 
	
		
			
				|  |  | +            const transaction = await this.db.beginTransaction();
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                const auditors = await this.getAuditGroupByList(stageId, times);
 | 
	
		
			
				|  |  | +                const now_audit = this._.find(auditors, { aid: data.old_aid });
 | 
	
		
			
				|  |  | +                if (data.operate === 'add') {
 | 
	
		
			
				|  |  | +                    if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
 | 
	
		
			
				|  |  | +                        throw '当前人下无法操作新增';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    const newAudit = {
 | 
	
		
			
				|  |  | +                        tid: this.ctx.tender.id,
 | 
	
		
			
				|  |  | +                        sid: stageId,
 | 
	
		
			
				|  |  | +                        aid: data.new_aid,
 | 
	
		
			
				|  |  | +                        order: now_audit.order+1,
 | 
	
		
			
				|  |  | +                        times: times,
 | 
	
		
			
				|  |  | +                        status: 1
 | 
	
		
			
				|  |  | +                    };
 | 
	
		
			
				|  |  | +                    // order+1
 | 
	
		
			
				|  |  | +                    await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
 | 
	
		
			
				|  |  | +                    await transaction.insert(this.tableName, newAudit);
 | 
	
		
			
				|  |  | +                    // 更新审批流程页数据,如果存在
 | 
	
		
			
				|  |  | +                } else if (data.operate === 'del') {
 | 
	
		
			
				|  |  | +                    if (now_audit.status !== auditConst.status.uncheck) {
 | 
	
		
			
				|  |  | +                        throw '当前人无法操作删除';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
 | 
	
		
			
				|  |  | +                    await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
 | 
	
		
			
				|  |  | +                    // 旧的更新为is_old为1
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, { is_old: 1 }, {
 | 
	
		
			
				|  |  | +                        where: {
 | 
	
		
			
				|  |  | +                            sid: stageId,
 | 
	
		
			
				|  |  | +                            times,
 | 
	
		
			
				|  |  | +                            aid: data.old_aid,
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                } else if (data.operate === 'change') {
 | 
	
		
			
				|  |  | +                    const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
 | 
	
		
			
				|  |  | +                    if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
 | 
	
		
			
				|  |  | +                        throw '当前人无法操作替换';
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    nowAudit.aid = data.new_aid;
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, nowAudit);
 | 
	
		
			
				|  |  | +                    // 旧的更新为is_old为1
 | 
	
		
			
				|  |  | +                    await transaction.update(this.tableName, { is_old: 1 }, {
 | 
	
		
			
				|  |  | +                        where: {
 | 
	
		
			
				|  |  | +                            sid: stageId,
 | 
	
		
			
				|  |  | +                            times,
 | 
	
		
			
				|  |  | +                            aid: data.old_aid,
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    });
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                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();
 | 
	
		
			
				|  |  | +                throw err;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      return StageAudit;
 |