| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054 | 
							- 'use strict';
 
- /**
 
-  *
 
-  *
 
-  * @author Mai
 
-  * @date 2019/2/27
 
-  * @version
 
-  */
 
- const auditConst = require('../const/audit').stage;
 
- const auditType = require('../const/audit').auditType;
 
- const smsTypeConst = require('../const/sms_type');
 
- const SMS = require('../lib/sms');
 
- const SmsAliConst = require('../const/sms_alitemplate');
 
- const wxConst = require('../const/wechat_template');
 
- const shenpiConst = require('../const/shenpi');
 
- const payConst = require('../const/deal_pay');
 
- const pushType = require('../const/audit').pushType;
 
- const syncApiConst = require('../const/sync_api');
 
- const measureType = require('../const/tender').measureType;
 
- const RevisePrice = require('../lib/revise_price');
 
- const pushOperate = require('../const/spec_3f').pushOperate;
 
- module.exports = app => {
 
-     class StageAudit extends app.BaseService {
 
-         /**
 
-          * 构造函数
 
-          *
 
-          * @param {Object} ctx - egg全局变量
 
-          * @return {void}
 
-          */
 
-         constructor(ctx) {
 
-             super(ctx);
 
-             this.tableName = 'stage_audit';
 
-         }
 
-         /**
 
-          * 获取 审核人信息
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} auditorId - 审核人id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<*>}
 
-          */
 
-         async getAuditor(stageId, auditorId, times = 1) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 'FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
 
-                 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?';
 
-             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
 
-             return await this.db.queryOne(sql, sqlParam);
 
-         }
 
-         async getAuditorByOrder(stageId, order, times) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
 
-                 '    la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '    FROM ' + this.tableName + ' AS la' +
 
-                 '    Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
 
-                 '  ORDER BY `order` DESC';
 
-             const sqlParam = [stageId, order, times ? times: 1];
 
-             return await this.db.queryOne(sql, sqlParam);
 
-         }
 
-         async getAuditorsByOrder(stageId, order, times) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
 
-                 '    la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '    FROM ' + this.tableName + ' AS la' +
 
-                 '    Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
 
-                 '  ORDER BY `order` DESC';
 
-             const sqlParam = [stageId, order, times ? times: 1];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         async getLastestAuditor(stageId, times, status) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
 
-                 '    la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '    FROM ' + this.tableName + ' AS la' +
 
-                 '    Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
 
-                 '  ORDER BY `order` DESC';
 
-             const sqlParam = [stageId, status, times ? times: 1];
 
-             return await this.db.queryOne(sql, sqlParam);
 
-         }
 
-         async getLastestAuditors(stageId, times, status) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
 
-                 '    la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '    FROM ' + this.tableName + ' AS la' +
 
-                 '    Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
 
-                 '  ORDER BY `order` DESC';
 
-             const sqlParam = [stageId, status, times ? times: 1];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 获取 审核列表信息
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} times - 第几次审批
 
-          * @param {Number} order_sort - 列表排序方式
 
-          * @return {Promise<*>}
 
-          */
 
-         async getAuditors(stageId, times = 1, order_sort = 'asc') {
 
-             const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
 
-                 '    pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
 
-                 `  FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
 
-                 '  WHERE la.sid = ? AND la.times = ?' +
 
-                 '  ORDER BY la.order ' + order_sort;
 
-             const sqlParam = [stageId, times];
 
-             const result = await this.db.query(sql, sqlParam);
 
-             const max_sort = this._.max(result.map(x => { return x.audit_order; }));
 
-             for (const i in result) {
 
-                 result[i].max_sort = max_sort;
 
-             }
 
-             return result;
 
-         }
 
-         async getAllAuditors(tenderId) {
 
-             const sql =
 
-                 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
 
-                 '  LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
 
-                 '  WHERE t.id = ?' +
 
-                 '  GROUP BY  sa.aid';
 
-             const sqlParam = [tenderId];
 
-             return this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 获取标段审核人最后一位的名称
 
-          *
 
-          * @param {Number} tenderId - 标段id
 
-          * @param {Number} auditorId - 审核人id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<*>}
 
-          */
 
-         async getStatusName(stageId) {
 
-             const sql = 'SELECT pa.`name` FROM ?? AS sa Left Join ?? AS pa On sa.`aid` = pa.`id`' +
 
-                 '  WHERE sa.`sid` = ? and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
 
-             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
 
-             return await this.db.queryOne(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 获取 当前审核人
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<*>}
 
-          */
 
-         async getCurAuditor(stageId, times = 1) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
 
-             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
 
-             return await this.db.queryOne(sql, sqlParam);
 
-         }
 
-         async getCurAuditors(stageId, times = 1) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
 
-                 '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
 
-                 '  WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
 
-             const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 获取 最新审核顺序
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<number>}
 
-          */
 
-         async getNewOrder(stageId, times = 1) {
 
-             const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ' + this.tableName + ' Where `sid` = ? and `times` = ?';
 
-             const sqlParam = [stageId, times];
 
-             const result = await this.db.queryOne(sql, sqlParam);
 
-             return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
 
-         }
 
-         /**
 
-          * 新增审核人
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} auditorId - 审核人id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<number>}
 
-          */
 
-         async addAuditor(stageId, auditorId, times = 1, is_gdzs = 0) {
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 let [newOrder, newAuditOrder] = await this.getNewOrder(stageId, times);
 
-                 // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
 
-                 newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
 
-                 if (is_gdzs) await this._syncOrderByDelete(transaction, stageId, newOrder, times, '+');
 
-                 const data = {
 
-                     tid: this.ctx.tender.id,
 
-                     sid: stageId,
 
-                     aid: auditorId,
 
-                     times,
 
-                     order: newOrder,
 
-                     status: auditConst.status.uncheck,
 
-                     audit_order: newAuditOrder,
 
-                 };
 
-                 const result = await transaction.insert(this.tableName, data);
 
-                 await transaction.commit();
 
-                 return result.effectRows = 1;
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-             return false;
 
-         }
 
-         /**
 
-          * 移除审核人时,同步其后审核人order
 
-          * @param transaction - 事务
 
-          * @param {Number} stageId - 标段id
 
-          * @param {Number} auditorId - 审核人id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<*>}
 
-          * @private
 
-          */
 
-         async _syncOrderByDelete(transaction, stageId, order, times, selfOperate = '-') {
 
-             this.initSqlBuilder();
 
-             this.sqlBuilder.setAndWhere('sid', {
 
-                 value: stageId,
 
-                 operate: '=',
 
-             });
 
-             this.sqlBuilder.setAndWhere('order', {
 
-                 value: order,
 
-                 operate: '>=',
 
-             });
 
-             this.sqlBuilder.setAndWhere('times', {
 
-                 value: times,
 
-                 operate: '=',
 
-             });
 
-             this.sqlBuilder.setUpdateData('order', {
 
-                 value: 1,
 
-                 selfOperate: selfOperate,
 
-             });
 
-             this.sqlBuilder.setUpdateData('audit_order', {
 
-                 value: 1,
 
-                 selfOperate: selfOperate,
 
-             });
 
-             const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
 
-             const data = await transaction.query(sql, sqlParam);
 
-             return data;
 
-         }
 
-         /**
 
-          * 移除审核人
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} auditorId - 审核人id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<boolean>}
 
-          */
 
-         async deleteAuditor(stageId, auditorId, times = 1) {
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const condition = { sid: stageId, aid: auditorId, times };
 
-                 const auditor = await this.getDataByCondition(condition);
 
-                 if (!auditor) {
 
-                     throw '该审核人不存在';
 
-                 }
 
-                 await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
 
-                 await transaction.delete(this.tableName, condition);
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-             return true;
 
-         }
 
-         async _updateTender(transaction) {
 
-             if (!this.ctx.tender) return;
 
-             if (this.ctx.tender.data.measure_type !== measureType.gcl.value) return;
 
-             const sum = await this.ctx.service.ledger.addUp({ tender_id: this.ctx.tender.id/* , is_leaf: true*/ });
 
-             await transaction.update(this.ctx.service.tender.tableName, {
 
-                 id: this.ctx.tender.id, total_price: sum.total_price, deal_tp: sum.deal_tp
 
-             });
 
-             return sum;
 
-         }
 
-         /**
 
-          * 开始审批
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<boolean>}
 
-          */
 
-         async start(stageId, times = 1) {
 
-             const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: 1 } });
 
-             if (audits.length === 0) {
 
-                 if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
 
-                     throw '请联系管理员添加审批人';
 
-                 } else {
 
-                     throw '请先选择审批人,再上报数据';
 
-                 }
 
-             }
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 const begin_time = new Date();
 
-                 const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time } });
 
-                 await transaction.updateRows(this.tableName, updateData);
 
-                 // 计算原报最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 // 复制一份下一审核人数据
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(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);
 
-                 // 更新期数据
 
-                 const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 this.ctx.stage.tp_history.push({ times: this.ctx.stage.curTimes, order: 0, ...stageTp });
 
-                 await this.ctx.service.tenderCache.updateStageCache4Start(transaction, this.ctx.stage, auditConst.status.checking, audits, ledgerTp, stageTp);
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stageId,
 
-                     status: auditConst.status.checking,
 
-                     ...stageTp,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                 });
 
-                 // 多人协同,取消下一审批人存在的锁定
 
-                 await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, audits.map(x => { return x.aid; }), transaction);
 
-                 // 添加短信通知-需要审批提醒功能
 
-                 const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-                 const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                 const auditIds = this._.map(audits, 'aid');
 
-                 const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
 
-                 users.push(...auditIds);
 
-                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 
-                     qi: stageInfo.order,
 
-                     code: shenpiUrl,
 
-                 });
 
-                 // 微信模板通知
 
-                 const wechatData = {
 
-                     wap_url: shenpiUrl,
 
-                     qi: stageInfo.order,
 
-                     status: wxConst.status.check,
 
-                     tips: wxConst.tips.check,
 
-                     code: this.ctx.session.sessionProject.code,
 
-                 };
 
-                 await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 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;
 
-             }
 
-             return true;
 
-         }
 
-         async _checked(pid, stageId, checkData, times) {
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const time = new Date();
 
-             // 整理当前流程审核人状态更新
 
-             const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
 
-             if (audits.length === 0) throw '审核数据错误';
 
-             const selfAudit = audits.find(x => { return x.aid === accountId; });
 
-             if (!selfAudit) throw '当前标段您无权审批';
 
-             const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order } });
 
-             const nextAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order + 1 } });
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-             const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 添加通知
 
-                 const noticeContent = await this.getNoticeContent(pid, stageInfo.tid, stageId, accountId, checkData.opinion);
 
-                 const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
 
-                 const defaultNoticeRecord = {
 
-                     pid,
 
-                     type: pushType.stage,
 
-                     status: auditConst.status.checked,
 
-                     content: noticeContent,
 
-                 };
 
-                 const records = [];
 
-                 auditors.forEach(audit => {
 
-                     records.push({
 
-                         uid: audit.aid,
 
-                         ...defaultNoticeRecord,
 
-                     });
 
-                 });
 
-                 this.ctx.stage.userAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 this.ctx.stage.auditAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 await transaction.insert('zh_notice', records);
 
-                 // 更新本人审批状态
 
-                 await transaction.update(this.tableName, {
 
-                     id: selfAudit.id,
 
-                     status: checkData.checkType,
 
-                     opinion: checkData.opinion,
 
-                     end_time: time,
 
-                 });
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
 
-                 if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
 
-                     // 或签更新他人审批状态
 
-                     if (selfAudit.audit_type === auditType.key.or) {
 
-                         const updateOther = [];
 
-                         for (const audit of audits) {
 
-                             if (audit.aid === selfAudit.aid) continue;
 
-                             updateOther.push({
 
-                                 id: audit.id,
 
-                                 status: auditConst.status.checkSkip,
 
-                                 opinion: '',
 
-                                 end_time: time,
 
-                             });
 
-                         }
 
-                         if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
 
-                     }
 
-                     // 无下一审核人表示,审核结束
 
-                     if (nextAudits.length > 0) {
 
-                         // 复制一份下一审核人数据
 
-                         await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudits[0].order, 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);
 
-                         // 流程至下一审批人
 
-                         const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
 
-                         await transaction.updateRows(this.tableName, updateData);
 
-                         // 同步 期信息
 
-                         await transaction.update(this.ctx.service.stage.tableName, {
 
-                             id: stageId,
 
-                             status: auditConst.status.checking,
 
-                             ...stageTp,
 
-                             tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                             cache_time_r: this.ctx.stage.cache_time_l,
 
-                         });
 
-                         await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, nextAudits, flowAudits, ledgerTp, stageTp);
 
-                         // 多人协同,取消下一审批人存在的锁定
 
-                         await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudits.map(x => { return x.aid; }), transaction);
 
-                         // 添加短信通知-需要审批提醒功能
 
-                         const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                         const nextAuditIds = this._.map(nextAudits, 'aid');
 
-                         const users = this._.map(this._.filter(this.ctx.auditAssists, function (x) { return nextAuditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
 
-                         users.push(...nextAuditIds);
 
-                         await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 
-                             qi: stageInfo.order,
 
-                             code: shenpiUrl,
 
-                         });
 
-                         // 微信模板通知
 
-                         const wechatData = {
 
-                             wap_url: shenpiUrl,
 
-                             qi: stageInfo.order,
 
-                             status: wxConst.status.check,
 
-                             tips: wxConst.tips.check,
 
-                             code: this.ctx.session.sessionProject.code,
 
-                         };
 
-                         await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
-                     } else {
 
-                         await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
 
-                         const his_id = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(this.ctx.stage.tid, this.ctx.stage.id);
 
-                         // 本期结束
 
-                         // 生成截止本期数据 final数据
 
-                         await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
 
-                         await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
 
-                         await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
 
-                         // 同步 期信息
 
-                         await transaction.update(this.ctx.service.stage.tableName, {
 
-                             id: stageId,
 
-                             status: checkData.checkType,
 
-                             ...stageTp,
 
-                             tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                             cache_time_r: this.ctx.stage.cache_time_l,
 
-                             his_id,
 
-                         });
 
-                         await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
 
-                         await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
 
-                         // 添加短信通知-审批通过提醒功能
 
-                         const auditList = await this.getAuditors(stageId, stageInfo.times);
 
-                         const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
 
-                         await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
 
-                             qi: stageInfo.order,
 
-                             status: SmsAliConst.status.success,
 
-                         });
 
-                         // 微信模板通知
 
-                         const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                         const wechatData = {
 
-                             wap_url: shenpiUrl,
 
-                             qi: stageInfo.order,
 
-                             status: wxConst.status.success,
 
-                             tips: wxConst.tips.success,
 
-                             code: this.ctx.session.sessionProject.code,
 
-                         };
 
-                         await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
 
-                         // 审批通过 - 检查三方特殊推送
 
-                         await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
 
-                     }
 
-                     // 上报/审批 - 检查三方特殊推送
 
-                     await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
 
-                 }
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         async _checkNo(pid, stageId, checkData, times) {
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const time = new Date();
 
-             // 整理当前流程审核人状态更新
 
-             const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
 
-             if (!audits) throw '审核数据错误';
 
-             const selfAudit = audits.find(x => { return x.aid === accountId; });
 
-             if (!selfAudit) throw '当前标段您无权审批';
 
-             const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-             const auditors = await this.getUniqAuditor(stageId, times); // 全部参与的审批人
 
-             const newAuditors = auditors.map(x => {
 
-                return {
 
-                    aid: x.aid, tid: selfAudit.tid, sid: selfAudit.sid,
 
-                    times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
 
-                    audit_type: x.audit_type, audit_order: x.audit_order,
 
-                }
 
-             });
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 添加推送
 
-                 const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
 
-                 const defaultNoticeRecord = {
 
-                     pid,
 
-                     type: pushType.stage,
 
-                     status: auditConst.status.checkNo,
 
-                     content: noticeContent,
 
-                 };
 
-                 const records = [
 
-                     {
 
-                         uid: this.ctx.stage.user_id,
 
-                         ...defaultNoticeRecord,
 
-                     },
 
-                 ];
 
-                 auditors.forEach(audit => {
 
-                     records.push({
 
-                         uid: audit.aid,
 
-                         ...defaultNoticeRecord,
 
-                     });
 
-                 });
 
-                 this.ctx.stage.userAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 this.ctx.stage.auditAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 await transaction.insert(this.ctx.service.noticePush.tableName, records);
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 const orgHisIndex = this.ctx.stage.tp_history.findIndex(x => { return x.times === times && x.order === selfAudit.order; });
 
-                 if (orgHisIndex) this.ctx.stage.tp_history.splice(orgHisIndex, 1);
 
-                 this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
 
-                 const updateData = [];
 
-                 audits.forEach(x => {
 
-                     updateData.push({
 
-                         id: x.id,
 
-                         status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
 
-                         opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
 
-                         end_time: x.aid === selfAudit.aid ? time : null,
 
-                     })
 
-                 });
 
-                 await transaction.updateRows(this.tableName, updateData);
 
-                 // 同步 期信息
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stageId,
 
-                     status: checkData.checkType,
 
-                     ...stageTp,
 
-                     times: times + 1,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                 });
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType,
 
-                     [{ aid: this.ctx.stage.user_id, audit_order: 0, audit_type: auditType.key.common, status: auditConst.status.uncheck }], flowAudits, ledgerTp, stageTp);
 
-                 // 拷贝新一次审核流程列表
 
-                 await transaction.insert(this.tableName, newAuditors);
 
-                 // 计算该审批人最终数据
 
-                 await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 // 复制一份最新数据给原报
 
-                 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 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.lockConfirm4CheckNo(this.ctx.stage, audits.map(x => { return x.aid; }), auditors, transaction);
 
-                 // 添加短信通知-审批退回提醒功能
 
-                 // const mobile_array = [];
 
-                 const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-                 const auditList = await this.getAuditors(stageId, stageInfo.times);
 
-                 const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
 
-                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
 
-                     qi: stageInfo.order,
 
-                     status: SmsAliConst.status.back,
 
-                 });
 
-                 // 微信模板通知
 
-                 const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                 const wechatData = {
 
-                     wap_url: shenpiUrl,
 
-                     qi: stageInfo.order,
 
-                     status: wxConst.status.back,
 
-                     tips: wxConst.tips.back,
 
-                     code: this.ctx.session.sessionProject.code,
 
-                 };
 
-                 await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         async _checkNoPre(pid, stageId, checkData, times) {
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const time = new Date();
 
-             // 整理当前流程审核人状态更新
 
-             const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
 
-             if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
 
-             const selfAudit = audits.find(x => { return x.aid === accountId; });
 
-             if (!selfAudit) throw '当前标段您无权审批';
 
-             const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
 
-             // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
 
-             // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
 
-             const auditors2 = await this.getAuditGroupByList(stageId, times);
 
-             const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 添加通知
 
-                 const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
 
-                 const defaultNoticeRecord = {
 
-                     pid,
 
-                     type: pushType.stage,
 
-                     status: auditConst.status.checkNoPre,
 
-                     content: noticeContent,
 
-                 };
 
-                 const records = [
 
-                     {
 
-                         uid: this.ctx.stage.user_id,
 
-                         ...defaultNoticeRecord
 
-                     },
 
-                 ];
 
-                 auditors2.forEach(audit => {
 
-                     records.push({
 
-                         uid: audit.aid,
 
-                         ...defaultNoticeRecord
 
-                     });
 
-                 });
 
-                 this.ctx.stage.userAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 this.ctx.stage.auditAssists.forEach(u => {
 
-                     records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
 
-                 });
 
-                 await transaction.insert('zh_notice', records);
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
 
-                 // 同步 期信息
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stageId,
 
-                     times,
 
-                     ...stageTp,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                 });
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, preAuditors, flowAudits, ledgerTp, stageTp);
 
-                 // 更新同一流程所有审批人状态
 
-                 const updateData = [];
 
-                 for (const audit of audits) {
 
-                     if (audit.aid === selfAudit.aid) {
 
-                         updateData.push({
 
-                             id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
 
-                         });
 
-                     } else {
 
-                         updateData.push({
 
-                             id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
 
-                         });
 
-                     }
 
-                 }
 
-                 await transaction.updateRows(this.tableName, updateData);
 
-                 // 顺移其后审核人流程顺序
 
-                 const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
 
-                 await transaction.query(sql, [stageId, selfAudit.times, selfAudit.order]);
 
-                 // 上一审批人,当前审批人 再次添加至流程
 
-                 const newAuditors = [];
 
-                 preAuditors.forEach(x => {
 
-                     newAuditors.push({
 
-                         tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
 
-                         times: x.times, order: selfAudit.order + 1,
 
-                         status: auditConst.status.checking, begin_time: time,
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 audits.forEach(x => {
 
-                     newAuditors.push({
 
-                         tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
 
-                         times: x.times, order: selfAudit.order + 2,
 
-                         status: auditConst.status.uncheck,
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 await transaction.insert(this.tableName, newAuditors);
 
-                 // 计算该审批人最终数据
 
-                 await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 // 复制一份最新数据给下一人
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, selfAudit.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);
 
-                 const preAuditorIds = preAuditors.map(x => { return x.aid; });
 
-                 // 锁定本人数据,保留锁定数据相关确认状态
 
-                 await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audits.map(x => { return x.aid; }), preAuditorIds, transaction);
 
-                 // 同步 期信息
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stageId,
 
-                     status: checkData.checkType,
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                 });
 
-                 const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-                 const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                 const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id');
 
-                 users.push(...preAuditorIds);
 
-                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 
-                     qi: stageInfo.order,
 
-                     code: shenpiUrl,
 
-                 });
 
-                 // 微信模板通知
 
-                 const wechatData = {
 
-                     wap_url: shenpiUrl,
 
-                     qi: stageInfo.order,
 
-                     status: wxConst.status.check,
 
-                     tips: wxConst.tips.check,
 
-                     code: this.ctx.session.sessionProject.code,
 
-                 };
 
-                 await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 审批
 
-          * @param {Number} stageId - 标段id
 
-          * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<void>}
 
-          */
 
-         async check(stageId, checkData, times = 1) {
 
-             if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
 
-                 throw '提交数据错误';
 
-             }
 
-             // // 整理当前流程审核人状态更新
 
-             // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
 
-             // if (!audit) {
 
-             //     throw '审核数据错误';
 
-             // }
 
-             // const time = new Date();
 
-             const pid = this.ctx.session.sessionProject.id;
 
-             switch (checkData.checkType) {
 
-                 case auditConst.status.checked:
 
-                     await this._checked(pid, stageId, checkData, times);
 
-                     break;
 
-                 case auditConst.status.checkNo:
 
-                     await this._checkNo(pid, stageId, checkData, times);
 
-                     break;
 
-                 case auditConst.status.checkNoPre:
 
-                     await this._checkNoPre(pid, stageId, checkData, times);
 
-                     break;
 
-                 default:
 
-                     throw '无效审批操作';
 
-             }
 
-             // 通知发送 - 第三方更新
 
-             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));
 
-             }
 
-         }
 
-         /**
 
-          * 审批
 
-          * @param {Number} stageId - 标段id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<void>}
 
-          */
 
-         async checkAgain(stageId, times = 1) {
 
-             const time = new Date();
 
-             // 整理当前流程审核人状态更新
 
-             const auditors = await this.getAllDataByCondition({
 
-                 where: { sid: stageId, times },
 
-                 orders: [['order', 'desc']],
 
-             });
 
-             const maxOrder = auditors[auditors.length - 1].order;
 
-             const audits = auditors.filter(x => { return x.order === maxOrder});
 
-             if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
 
-             const selfAudit = audits.find(x => { return x.aid === accountId; });
 
-             if (!selfAudit) throw '当前标段您无权审批';
 
-             const stageInfo = await this.ctx.service.stage.getDataById(stageId);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 当前审批人2次添加至流程中
 
-                 const checkAgainAuditors = [], checkingAuditors = [];
 
-                 audit.forEach(x => {
 
-                     checkAgainAuditors.push({
 
-                         tid: x.tid, sid: x.sid, aid: x.aid,
 
-                         times: x.times, order: x.order + 1,
 
-                         status: x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 audit.forEach(x => {
 
-                     checkingAuditors.push({
 
-                         tid: x.tid, sid: x.sid, aid: x.aid,
 
-                         times: x.times, order: x.order + 2,
 
-                         status: auditConst.status.checking,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 await transaction.insert(this.tableName, checkAgainAuditors);
 
-                 await transaction.insert(this.tableName, checkingAuditors);
 
-                 // 复制一份最新数据给下一人
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
 
-                 await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
 
-                 await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
 
-                 await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
 
-                 await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
 
-                 await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
 
-                 // 同步 期信息
 
-                 const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === selfAudit.order });
 
-                 const stageTp = {
 
-                     contract_tp: his.contract_tp,
 
-                     qc_tp: his.qc_tp,
 
-                     positive_qc_tp: his.positive_qc_tp,
 
-                     negative_qc_tp: his.negative_qc_tp,
 
-                     yf_tp: his.tp,
 
-                     sf_tp: his.tp,
 
-                 };
 
-                 this.ctx.stage.tp_history.push({ times, order: selfAudit.order + 1, ...stageTp });
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stageId,
 
-                     status: auditConst.status.checking,
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                 });
 
-                 // 已经引用到本期的单价变更,全部取消
 
-                 await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
 
-                 // 重算所有单价变更
 
-                 const priceCalc = new RevisePrice(this.ctx);
 
-                 const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, audit.order + 2, transaction);
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, checkingAuditors, checkAgainAuditors, null, stageTp, pcTp);
 
-                 // 添加短信通知-需要审批提醒功能
 
-                 const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
 
-                 const auditorIds = audits.map(x => { return x.aid; });
 
-                 const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
 
-                 users.push(...auditorIds);
 
-                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
 
-                     qi: stageInfo.order,
 
-                     code: shenpiUrl,
 
-                 });
 
-                 // 微信模板通知
 
-                 const wechatData = {
 
-                     wap_url: shenpiUrl,
 
-                     qi: stageInfo.order,
 
-                     status: wxConst.status.check,
 
-                     tips: wxConst.tips.check,
 
-                     code: this.ctx.session.sessionProject.code,
 
-                 };
 
-                 await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 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;
 
-             }
 
-         }
 
-         /**
 
-          * 原报撤回,直接改动审批人状态
 
-          * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
 
-          *
 
-          * 一审 1 A checking  ->  A uncheck status改   pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
 
-          * ...
 
-          *
 
-          * @param stage
 
-          * @returns {Promise<void>}
 
-          * @private
 
-          */
 
-         async _userCheckCancel(stage) {
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 整理当前流程审核人状态更新
 
-                 // 审批人变成待审批状态
 
-                 const updateData = stage.curAuditors.map(x => {
 
-                     return {
 
-                         id: x.id,
 
-                         status: auditConst.status.uncheck,
 
-                         begin_time: null,
 
-                         opinion: null,
 
-                     }
 
-                 });
 
-                 await transaction.updateRows(this.tableName, updateData);
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
 
-                 const stageTp = {
 
-                     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: stage.id,
 
-                     times: stage.times,
 
-                     ...stageTp,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < stage.times; })),
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                     status: stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
 
-                 });
 
-                 // 计量明细:重复数据删除原报,新增数据修改为原报
 
-                 await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
 
-                 await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
 
-                 await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
 
-                 await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, stage.times, 0, stage.time, 1, transaction);
 
-                 // 合同支付明细:保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
 
-                 await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, 0, transaction, this.ctx.stage.times, 1);
 
-                 await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 1, transaction);
 
-                 // 其他台账明细:不需更新历史
 
-                 // 更新标段计量数据缓存
 
-                 const preFlow = stage.times > 1 ? await this.getLastestAuditors(stage.id, stage.times, auditConst.status.checkNo): null;
 
-                 const curFlow = [{ aid: this.ctx.stage.user_id, order: 1, status: auditConst.status.uncheck}];
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
 
-                     curFlow, preFlow, ledgerTp, stageTp);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch(err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 审批人撤回审批通过,插入两条数据
 
-          *
 
-          * 一审 1 A checked             一审 1 A checked
 
-          * 二审 2 B checked   pre ->    二审 2 B checked
 
-          * 三审 3 C checking  cur       二审 3 B checkCancel  增                增extra_his     增tp_his
 
-          * 四审 4 D uncheck             二审 4 B checking     增                增pay_cur
 
-          *                             三审 5 C uncheck      order、status改
 
-          *                             四审 6 D uncheck      order改
 
-          *
 
-          * @param stage
 
-          * @returns {Promise<void>}
 
-          * @private
 
-          */
 
-         async _auditCheckCancel(stage) {
 
-             if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
 
-                 throw '撤回用户数据错误';
 
-             }
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
 
-             if (!selfAuditor) throw '撤回用户数据错误';
 
-             const time = new Date();
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 整理当前流程审核人状态更新
 
-                 // 顺移其后审核人流程顺序
 
-                 const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
 
-                 await transaction.query(sql, [stage.id, selfAuditor.times, stage.curAuditors[0].order]);
 
-                 // 当前审批人2次添加至流程中
 
-                 const checkCancelAuditors = [], checkingAuditors = [];
 
-                 stage.preAuditors.forEach(x => {
 
-                     checkCancelAuditors.push({
 
-                         tid: x.tid, sid: x.sid, aid: x.aid,
 
-                         times: x.times, order: x.order + 1,
 
-                         status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 stage.preAuditors.forEach(x => {
 
-                     checkingAuditors.push({
 
-                         tid: x.tid, sid: x.sid, aid: x.aid,
 
-                         times: x.times, order: x.order + 2,
 
-                         status: auditConst.status.checking,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
 
-                 // 当前审批人变成待审批
 
-                 await transaction.update(this.tableName, { begin_time: null, status: auditConst.status.uncheck, order: stage.curAuditors[0].order + 2 },
 
-                     { where: { sid: stage.sid, times: stage.times, order: stage.curAuditors[0].order } });
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 this.ctx.stage.tp_history.push({ times, order: selfAuditor.order + 1, ...stageTp });
 
-                 // 同步 期信息
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stage.id,
 
-                     times: stage.times,
 
-                     ...stageTp,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                     cache_time_r: this.ctx.stage.cache_time_l,
 
-                 });
 
-                 // 计量明细:勿需修改任何数据
 
-                 // 合同支付明细:复制一份最新数据给下一人
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
 
-                 // 其他台账明细:更新两份历史数据
 
-                 await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 // 更新标段计量数据缓存
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch(err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 审批人撤回审批退回上一人,插入两条数据
 
-          *
 
-          * 一审 1 A checked                   一审 1 A checked
 
-          * 二审 2 B checked                   二审 2 B checked
 
-          * 三审 3 C checkNoPre  pre   ->      三审 3 C checkNoPre
 
-          * 二审 4 B checking    cur           三审 4 C checkCancel   删4B 增4C    增extra_his    增tp_his
 
-          * 三审 3 C uncheck                   三审 5 C checking      status改     增pay_cur
 
-          * 四审 6 D uncheck                   四审 6 D uncheck
 
-          *
 
-          * @param stage
 
-          * @returns {Promise<void>}
 
-          * @private
 
-          */
 
-         async _auditCheckCancelNoPre(stage) {
 
-             if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
 
-                 throw '撤回用户数据错误';
 
-             }
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
 
-             if (!selfAuditor) throw '撤回用户数据错误';
 
-             const time = new Date();
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 整理当前流程审核人状态更新
 
-                 // 删除当前审批人
 
-                 await transaction.delete(this.tableName, { id: stage.curAuditors.map(x => { return x.id; }) });
 
-                 // 添加撤回人到审批流程中
 
-                 const newAuditors = [];
 
-                 stage.preAuditors.forEach(x => {
 
-                     newAuditors.push({
 
-                         tid: x.tid, sid: x.sid, aid: x.aid,
 
-                         times: x.times, order: x.order + 1,
 
-                         status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 await transaction.insert(this.tableName, newAuditors);
 
-                 // 更新上一个人,最新审批状态为审批中
 
-                 await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
 
-                     where: { sid: selfAuditor.sid, times: selfAuditor.times, order: selfAuditor.order + 2 }
 
-                 });
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 stage.tp_history.push({ times, order: selfAuditor.order + 1, ...stageTp });
 
-                 // 同步 期信息
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stage.id,
 
-                     times: stage.times,
 
-                     ...stageTp,
 
-                     status: auditConst.status.checking,
 
-                     tp_history: JSON.stringify(stage.tp_history),
 
-                     cache_time_r: stage.cache_time_l,
 
-                 });
 
-                 // 计量明细:勿需修改任何数据
 
-                 // 合同支付明细:复制一份最新数据给下一人
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(stage, times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
 
-                 // 其他台账明细:更新一份历史数据
 
-                 await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 // 更新标段计量数据缓存
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, newAuditors, stage.preAuditors, ledgerTp, stageTp);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch(err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 审批人撤回审批退回原报
 
-          *
 
-          * 1# 一审 1 A checked              1# 一审 1 A checked
 
-          *    二审 2 B checkNo   pre   ->      二审 2 B checkNo
 
-          *    三审 3 C uncheck                 二审 3 B checkCancel    增       pay: 2#0 -> 1#3   jl: 2#0 -> 1#3   增tp_his   增extra_his
 
-          *                                    二审 4 B checking       增       pay: 2#0 -> 1#4
 
-          *                                    三审 5 C uncheck        order改
 
-          *
 
-          * 2# 一审 1 A uncheck              2#                        删       pay: 2#0删   jl: 2#0删
 
-          *    二审 2 B uncheck
 
-          *    三审 3 C uncheck
 
-          *
 
-          * @param stage
 
-          * @returns {Promise<void>}
 
-          * @private
 
-          */
 
-         async _auditCheckCancelNo(stage) {
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const selfAuditor = stage.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
 
-             if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
 
-             const time = new Date();
 
-             const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 整理上一个流程审核人状态更新
 
-                 // 顺移其后审核人流程顺序
 
-                 const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
 
-                 await transaction.query(sql, [stage.id, selfAuditor.times, selfAuditor.order]);
 
-                 // 当前审批人2次添加至流程中
 
-                 const checkCancelAuditors = [], checkingAuditors = [];
 
-                 stage.preAuditors.forEach(x => {
 
-                     checkCancelAuditors.push({
 
-                         tid: stage.tid, sid: stage.id, aid: x.aid,
 
-                         times: x.times, order: x.order + 1,
 
-                         status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 stage.preAuditors.forEach(x => {
 
-                     checkingAuditors.push({
 
-                         tid: stage.tid, sid: stage.id, aid: x.aid,
 
-                         times: x.times, order: x.order + 2,
 
-                         status: auditConst.status.checking,
 
-                         begin_time: time, end_time: time, opinion: '',
 
-                         audit_type: x.audit_type, audit_order: x.audit_order,
 
-                     });
 
-                 });
 
-                 await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
 
-                 // 删除当前次审批流
 
-                 await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
 
-                 // 计算并合同支付最终数据
 
-                 const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
 
-                 const stageTp = {
 
-                     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,
 
-                 };
 
-                 stage.tp_history.push({ times: selfAuditor.times, order: selfAuditor.order + 1, ...stageTp });
 
-                 await transaction.update(this.ctx.service.stage.tableName, {
 
-                     id: stage.id,
 
-                     times: stage.times - 1,
 
-                     ...stageTp,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history),
 
-                     cache_time_r: stage.cache_time_l,
 
-                     status: auditConst.status.checking,
 
-                 });
 
-                 // 计量明细:重复数据删除原报,新增数据修改为原报
 
-                 await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
 
-                 await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
 
-                 await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
 
-                 await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
 
-                 // 合同支付明细:复制一份最新数据新增的2个审批流,删除最新一次审批数据
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 1, transaction, stage.times, 0);
 
-                 await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 2, transaction, stage.times, 0);
 
-                 await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
 
-                 // 其他台账明细:更新一份历史数据
 
-                 await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
 
-                 // 更新标段计量数据缓存
 
-                 await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, auditConst.status.checking, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
 
-                 // 上报/审批 - 检查三方特殊推送
 
-                 await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
 
-                 await transaction.commit();
 
-             } catch(err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 会签未全部审批通过时,撤回仅修改本人状态
 
-          *
 
-          * @param stage
 
-          * @returns {Promise<void>}
 
-          * @private
 
-          */
 
-         async _auditCheckCancelAnd(stage) {
 
-             const accountId = this.ctx.session.sessionUser.accountId;
 
-             const selfAuditor = stage.flowAuditors.find(x => { return x.aid === accountId; });
 
-             if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 await transaction.update(this.tableName, {
 
-                     id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
 
-                 });
 
-                 await transaction.commit();
 
-             } catch(err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         /**
 
-          * 审批撤回
 
-          * @param {Number} stageId - 标段id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<void>}
 
-          */
 
-         async checkCancel(stage) {
 
-             // 分5种情况,根据ctx.cancancel值判断:
 
-             // 1.原报发起撤回,当前流程删除,并回到待上报
 
-             // 2.审批人撤回审批通过,增加流程,并回到它审批中
 
-             // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
 
-             // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
 
-             // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
 
-             if (stage.cancancel === 5) {
 
-                 await this._auditCheckCancelAnd(stage);
 
-             } else {
 
-                 switch (this.ctx.stage.cancancel) {
 
-                     case 1: await this._userCheckCancel(stage); break;
 
-                     case 2: await this._auditCheckCancel(stage); break;
 
-                     case 3: await this._auditCheckCancelNoPre(stage); break;
 
-                     case 4: await this._auditCheckCancelNo(stage); break;
 
-                     default: throw '不可撤回,请刷新页面重试';
 
-                 }
 
-                 // 通知发送 - 第三方更新
 
-                 if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
 
-                     const base_data = {
 
-                         tid: stage.tid,
 
-                         sid: stage.id,
 
-                         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));
 
-                 }
 
-             }
 
-         }
 
-         /**
 
-          * 获取审核人需要审核的期列表
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         async getAuditStage(auditorId) {
 
-             const sql =
 
-                 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
 
-                 '    s.`order` As `sorder`, s.`status` As `sstatus`,' +
 
-                 '    t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
 
-                 '  FROM ?? AS sa ' +
 
-                 '    Left Join ?? AS s On sa.`sid` = s.`id` ' +
 
-                 '    Left Join ?? As t ON sa.`tid` = t.`id`' +
 
-                 '  WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
 
-                 '  ORDER BY sa.`begin_time` DESC';
 
-             const sqlParam = [
 
-                 this.tableName,
 
-                 this.ctx.service.stage.tableName,
 
-                 this.ctx.service.tender.tableName,
 
-                 auditorId,
 
-                 auditConst.status.checking,
 
-                 auditorId,
 
-                 auditConst.status.checkNo,
 
-                 auditConst.status.checkNo,
 
-             ];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 获取审核人审核的次数
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         async getCountByChecked(auditorId) {
 
-             return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
 
-         }
 
-         /**
 
-          * 获取最近一次审批结束时间
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         async getLastEndTimeByChecked(auditorId) {
 
-             const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
 
-                 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
 
-             const sqlParam = [this.tableName, auditorId];
 
-             const result = await this.db.queryOne(sql, sqlParam);
 
-             return result ? result.end_time : null;
 
-         }
 
-         /**
 
-          * 获取 某时间后 审批进度 更新的期
 
-          * @param {Number} pid - 查询标段
 
-          * @param {Number} uid - 查询人
 
-          * @param {Date} time - 查询时间
 
-          * @return {Promise<*>}
 
-          */
 
-         async getNoticeStage(pid, uid, time) {
 
-             let notice = await this.db.select('zh_notice', {
 
-                 where: { pid, type: pushType.stage, uid },
 
-                 orders: [['create_time', 'desc']],
 
-                 limit: 10,
 
-                 offset: 0,
 
-             });
 
-             notice = notice.map(v => {
 
-                 const extra = JSON.parse(v.content);
 
-                 delete v.content;
 
-                 return { ...v, ...extra };
 
-             });
 
-             return notice;
 
-         }
 
-         /**
 
-          * 用于添加推送所需的content内容
 
-          * @param {Number} pid 项目id
 
-          * @param {Number} tid 台账id
 
-          * @param {Number} sid 期id
 
-          * @param {Number} uid 审核人id
 
-          */
 
-         async getNoticeContent(pid, tid, sid, uid, opinion = '') {
 
-             const noticeSql =
 
-                 'SELECT * FROM (SELECT ' +
 
-                 '  t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
 
-                 '  FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
 
-                 '  LEFT JOIN ?? As s On s.`id` =  ?' +
 
-                 '  LEFT JOIN ?? As pa ON pa.`id` = ?' +
 
-                 '  WHERE  t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
 
-             const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
 
-             const content = await this.db.query(noticeSql, noticeSqlParam);
 
-             if (content.length) {
 
-                 content[0].opinion = opinion;
 
-             }
 
-             return content.length ? JSON.stringify(content[0]) : '';
 
-         }
 
-         /**
 
-          * 获取审核人流程列表
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         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.`order`, la.`status`, la.audit_type, la.audit_order' +
 
-                 ' 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);
 
-         }
 
-         /**
 
-          * 获取审核人流程列表
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         async getAuditGroupByListWithOwner(stageId, times) {
 
-             const result = await this.getAuditGroupByList(stageId, times);
 
-             const sql =
 
-                 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
 
-                 '  FROM ' + this.ctx.service.stage.tableName + ' As s' +
 
-                 '  LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
 
-                 '  ON s.user_id = pa.id' +
 
-                 '  WHERE s.id = ?';
 
-             const sqlParam = [times, stageId, stageId];
 
-             const user = await this.db.queryOne(sql, sqlParam);
 
-             result.unshift(user);
 
-             return result;
 
-         }
 
-         /**
 
-          * 复制上一期的审批人列表给最新一期
 
-          *
 
-          * @param transaction - 新增一期的事务
 
-          * @param {Object} preStage - 上一期
 
-          * @param {Object} newStage - 最新一期
 
-          * @return {Promise<*>}
 
-          */
 
-         async copyPreStageAuditors(transaction, preStage, newStage) {
 
-             const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
 
-             const newAuditors = [];
 
-             for (const a of auditors) {
 
-                 const na = {
 
-                     tid: preStage.tid,
 
-                     sid: newStage.id,
 
-                     aid: a.aid,
 
-                     times: newStage.times,
 
-                     order: a.audit_order,
 
-                     status: auditConst.status.uncheck,
 
-                     audit_type: a.audit_type,
 
-                     audit_order: a.audit_order,
 
-                 };
 
-                 newAuditors.push(na);
 
-             }
 
-             const result = await transaction.insert(this.tableName, newAuditors);
 
-             return (result.effectRows = auditors.length);
 
-         }
 
-         /**
 
-          * 移除审核人
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} status - 期状态
 
-          * @param {Number} status - 期次数
 
-          * @return {Promise<boolean>}
 
-          */
 
-         async getAuditorByStatus(stageId, status, times = 1) {
 
-             let auditor = null;
 
-             let sql = '';
 
-             let sqlParam = '';
 
-             switch (status) {
 
-                 case auditConst.status.checking:
 
-                 case auditConst.status.checked:
 
-                 case auditConst.status.checkNoPre:
 
-                     sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
 
-                           '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
 
-                           '  WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
 
-                     sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
 
-                     auditor = await this.db.queryOne(sql, sqlParam);
 
-                     break;
 
-                 case auditConst.status.checkNo:
 
-                     sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
 
-                           '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
 
-                           '  WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
 
-                     sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
 
-                     auditor = await this.db.queryOne(sql, sqlParam);
 
-                     break;
 
-                 case auditConst.status.uncheck:
 
-                 default:
 
-                     break;
 
-             }
 
-             return auditor;
 
-         }
 
-         async getAuditorsByStatus(stageId, status, times = 1) {
 
-             let auditor = [];
 
-             let sql = '';
 
-             let sqlParam = '';
 
-             let cur;
 
-             switch (status) {
 
-                 case auditConst.status.checking:
 
-                 case auditConst.status.checked:
 
-                 case auditConst.status.checkNoPre:
 
-                     cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, times, status]);
 
-                     if (!cur) return [];
 
-                     sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order ' +
 
-                         '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
 
-                         '  WHERE la.`sid` = ? and la.`order` = ?';
 
-                     sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order];
 
-                     auditor = await this.db.query(sql, sqlParam);
 
-                     break;
 
-                 case auditConst.status.checkNo:
 
-                     cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, parseInt(times) - 1, status]);
 
-                     if (!cur) return [];
 
-                     sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order ' +
 
-                         '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
 
-                         '  WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?';
 
-                     sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, parseInt(times) - 1];
 
-                     auditor = await this.db.queryOne(sql, sqlParam);
 
-                     break;
 
-                 case auditConst.status.uncheck:
 
-                 default:
 
-                     break;
 
-             }
 
-             return auditor;
 
-         }
 
-         /**
 
-          * 取某一期已批准审核信息(报表用)
 
-          *
 
-          * @param {Number} stageId - 期id
 
-          * @param {Number} times - 期次数
 
-          * @return {Promise<boolean>}
 
-          */
 
-         async getStageAudit(stageId, times = 1) {
 
-             const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' + 'FROM ?? AS a1 ' + 'WHERE a1.`sid` = ? and a1.`times` = ? ' + 'ORDER BY a1.order';
 
-             const sqlParam = [this.tableName, stageId, times];
 
-             const rst = await this.db.query(sql, sqlParam);
 
-             return rst;
 
-         }
 
-         /**
 
-          * 取待审批期列表(wap用)
 
-          *
 
-          * @param auditorId
 
-          * @return {Promise<*>}
 
-          */
 
-         async getAuditStageByWap(auditorId) {
 
-             const sql =
 
-                 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
 
-                 // '    s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
 
-                 '    s.*,' +
 
-                 '    t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
 
-                 '    ti.`deal_info` ' +
 
-                 '  FROM ?? AS sa' +
 
-                 '    Left Join ?? AS s On sa.`sid` = s.`id`' +
 
-                 '    Left Join ?? As t On sa.`tid` = t.`id`' +
 
-                 '    Left Join ?? AS ti ON ti.`tid` = t.`id`' +
 
-                 '  WHERE sa.`aid` = ? and sa.`status` = ?';
 
-             const sqlParam = [
 
-                 this.tableName,
 
-                 this.ctx.service.stage.tableName,
 
-                 this.ctx.service.tender.tableName,
 
-                 this.ctx.service.tenderInfo.tableName,
 
-                 auditorId,
 
-                 auditConst.status.checking,
 
-             ];
 
-             return await this.db.query(sql, sqlParam);
 
-         }
 
-         /**
 
-          * 删除 某期 某次 全审批流程
 
-          * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
 
-          * @param {Number} sid - 标段id
 
-          * @param {Number} times - 第几次审批
 
-          * @param transaction - 删除事务
 
-          * @return {Promise<void>}
 
-          */
 
-         async _timesDelete(sid, times, transaction) {
 
-             // 审批流程
 
-             await transaction.delete(this.tableName, { sid, times });
 
-             await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
 
-             await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
 
-             await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
 
-             await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
 
-             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.service.stageAuditAss.tableName, { sid, 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);
 
-             await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
 
-             await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
 
-         }
 
-         /**
 
-          * 删除本次审批流程
 
-          * @param {Number} stageId - 标段id
 
-          * @param {Number} times - 第几次审批
 
-          * @return {Promise<void>}
 
-          */
 
-         async timesDelete() {
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 删除最新一次数据
 
-                 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 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';
 
-                 const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
 
-                 const auditors = await this.db.query(sql, sqlParam);
 
-                 let order = 1;
 
-                 for (const a of auditors) {
 
-                     a.times = nowTimes;
 
-                     a.order = order;
 
-                     a.status = auditConst.status.uncheck;
 
-                     order++;
 
-                 }
 
-                 const ledgerTp = await this._updateTender(transaction);
 
-                 // 拷贝新一次审核流程列表
 
-                 await transaction.insert(this.tableName, auditors);
 
-                 // 计算缓存
 
-                 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.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, 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,
 
-                     positive_qc_tp: tpData.positive_qc_tp,
 
-                     negative_qc_tp: tpData.negative_qc_tp,
 
-                     times: nowTimes,
 
-                     yf_tp: yfPay ? yfPay.tp : null,
 
-                     sf_tp: sfPay ? sfPay.tp : null,
 
-                     tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
 
-                     cache_time_l: time,
 
-                     cache_time_r: time,
 
-                 });
 
-                 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: this.ctx.stage.id,
 
-                         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;
 
-             }
 
-         }
 
-         // 固定审批流-更新
 
-         async updateNewAuditList(stage, newList) {
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 先删除旧的审批流,再添加新的
 
-                 await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
 
-                 await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
 
-                 const newAuditors = [];
 
-                 for (const auditor of newList) {
 
-                     newAuditors.push({
 
-                         tid: stage.tid, sid: stage.id, aid: auditor.audit_id,
 
-                         times: stage.times, order: auditor.audit_order, status: auditConst.status.uncheck,
 
-                         audit_type: auditor.audit_type, audit_order: auditor.audit_order,
 
-                     });
 
-                 }
 
-                 if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         // 固定终审-更新
 
-         async updateLastAudit(stage, auditList, lastId) {
 
-             const transaction = await this.db.beginTransaction();
 
-             try {
 
-                 // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
 
-                 const idList = this._.map(auditList, 'aid');
 
-                 let order = idList.length + 1;
 
-                 if (idList.indexOf(lastId) !== -1) {
 
-                     await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
 
-                     await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times, user_id: lastId });
 
-                     const audit = this._.find(auditList, { 'aid': lastId });
 
-                     const orgAuditors = auditList.filter(x => { return x.order === audit.order; });
 
-                     if (orgAuditors.length === 1) {
 
-                         // 顺移之后审核人流程顺序
 
-                         await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
 
-                         order = order - 1;
 
-                     }
 
-                 }
 
-                 // 添加终审
 
-                 const newAuditor = {
 
-                     tid: stage.tid, sid: stage.id, aid: lastId,
 
-                     times: stage.times, order, status: auditConst.status.uncheck,
 
-                 };
 
-                 await transaction.insert(this.tableName, newAuditor);
 
-                 await transaction.commit();
 
-             } catch (err) {
 
-                 await transaction.rollback();
 
-                 throw err;
 
-             }
 
-         }
 
-         async getFinalAuditGroup(stageId, times) {
 
-             const sql =
 
-                 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`sid`, Max(la.`order`) as max_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];
 
-             const result = await this.db.query(sql, sqlParam);
 
-             for (const r of result) {
 
-                 const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
 
-                 r.status = auditor.status;
 
-                 r.opinion = auditor.opinion;
 
-                 r.begin_time = auditor.begin_time;
 
-                 r.end_time = auditor.end_time;
 
-             }
 
-             return result;
 
-         }
 
-         async getNumByMonth(tid, startMonth, endMonth) {
 
-             const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY sid) AND status = ? AND end_time between ? and ?';
 
-             const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
 
-             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;
 
-             }
 
-         }
 
-         async getAuditorGroup(stageId, times) {
 
-             const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
 
-             return this.ctx.helper.groupAuditors(auditors);
 
-         }
 
-         async getUserGroup(stageId, times) {
 
-             const group = await this.getAuditorGroup(stageId, times);
 
-             const sql =
 
-                 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
 
-                 '  FROM ' + this.ctx.service.stage.tableName + ' As s' +
 
-                 '  LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
 
-                 '  ON s.user_id = pa.id' +
 
-                 '  WHERE s.id = ?';
 
-             const sqlParam = [times, stageId, stageId];
 
-             const user = await this.db.queryOne(sql, sqlParam);
 
-             group.unshift([ user ]);
 
-             return group;
 
-         }
 
-         async getAuditorHistory(stageId, times) {
 
-             const history = [];
 
-             if (times >= 1) {
 
-                 for (let i = 1; i <= times; i++) {
 
-                     const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, i);
 
-                     const group = this.ctx.helper.groupAuditors(auditors);
 
-                     const historyGroup = [];
 
-                     const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
 
-                     for (const g of group) {
 
-                         const his = {
 
-                             beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '',
 
-                             audit_type: g[0].audit_type, audit_order: g[0].audit_order,
 
-                             auditors: g
 
-                         };
 
-                         if (his.audit_type === auditType.key.common) {
 
-                             his.name = g[0].name;
 
-                         } else {
 
-                             his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
 
-                         }
 
-                         his.is_final = his.audit_order === max_order;
 
-                         if (g[0].begin_time) {
 
-                             const beginTime = this.ctx.moment(g[0].begin_time);
 
-                             his.beginYear = beginTime.format('YYYY');
 
-                             his.beginDate = beginTime.format('MM-DD');
 
-                             his.beginTime = beginTime.format('HH:mm:ss');
 
-                         }
 
-                         let end_time;
 
-                         g.forEach(x => {
 
-                             if (x.status === auditConst.status.checkSkip) return;
 
-                             if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
 
-                             if (x.end_time && (!end_time || x.end_time > end_time)) {
 
-                                 end_time = x.end_time;
 
-                                 if (his.status !== auditConst.status.checking) his.status = x.status;
 
-                             }
 
-                         });
 
-                         if (end_time) {
 
-                             const endTime = this.ctx.moment(end_time);
 
-                             his.endYear = endTime.format('YYYY');
 
-                             his.endDate = endTime.format('MM-DD');
 
-                             his.endTime = endTime.format('HH:mm:ss');
 
-                         }
 
-                         historyGroup.push(his);
 
-                     }
 
-                     history.push(historyGroup);
 
-                 }
 
-             }
 
-             return history;
 
-         }
 
-         async getUniqAuditor(stageId, times) {
 
-             const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
 
-             const result = [];
 
-             auditors.forEach(x => {
 
-                 if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === x.audit_order; }) < 0) {
 
-                     result.push(x);
 
-                 }
 
-             });
 
-             return result;
 
-         }
 
-     }
 
-     return StageAudit;
 
- };
 
 
  |