123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2019/2/27
- * @version
- */
- const auditConst = require('../const/audit').stage;
- 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 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);
- }
- /**
- * 获取 审核列表信息
- *
- * @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.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.sign_path, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.`is_old`, g.`sort` ' +
- 'FROM ?? AS la, ?? AS pa, (SELECT sa.`aid`,(@i:=@i+1) as `sort` FROM (SELECT * FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid` ORDER BY `order` ASC) sa, (select @i:=0) as it WHERE sa.`sid` = ? AND sa.`times` = ? GROUP BY sa.`aid`) as g ' +
- 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order` ' +
- order_sort;
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, stageId, times, stageId, times, stageId, times];
- const result = await this.db.query(sql, sqlParam);
- const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as a';
- const sqlParam2 = [this.tableName, stageId, times];
- const count = await this.db.queryOne(sql2, sqlParam2);
- for (const i in result) {
- result[i].max_sort = count.num;
- }
- 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);
- }
- /**
- * 获取 最新审核顺序
- *
- * @param {Number} stageId - 期id
- * @param {Number} times - 第几次审批
- * @return {Promise<number>}
- */
- async getNewOrder(stageId, times = 1) {
- const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
- const sqlParam = ['order', this.tableName, stageId, times];
- const result = await this.db.queryOne(sql, sqlParam);
- return result && result.max_order ? result.max_order + 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 = 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,
- };
- 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,
- });
- 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 audit = await this.getDataByCondition({ sid: stageId, times, order: 1 });
- if (!audit) {
- 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);
- await transaction.update(this.tableName, {
- id: audit.id,
- status: auditConst.status.checking,
- begin_time: new Date(),
- });
- // 计算原报最终数据
- 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, audit, 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, audit.aid, transaction);
- // 添加短信通知-需要审批提醒功能
- const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
- const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
- const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return x.user_id === audit.aid }), 'ass_user_id');
- users.push(audit.aid);
- await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
- qi: stageInfo.order,
- 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);
- // todo 更新标段tender状态 ?
- 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 time = new Date();
- // 整理当前流程审核人状态更新
- const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- if (!audit) {
- throw '审核数据错误';
- }
- const nextAudit = await this.getDataByCondition({ sid: stageId, times, order: 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, audit.tid, stageId, audit.aid, 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: audit.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: audit.order, ...stageTp });
- // 无下一审核人表示,审核结束
- if (nextAudit) {
- // 复制一份下一审核人数据
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.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);
- // 流程至下一审批人
- await transaction.update(this.tableName, {
- id: nextAudit.id,
- status: auditConst.status.checking,
- begin_time: time,
- });
- // 同步 期信息
- 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, nextAudit, audit, ledgerTp, stageTp);
- // 多人协同,取消下一审批人存在的锁定
- await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudit.aid, transaction);
- // 添加短信通知-需要审批提醒功能
- const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
- 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._.filter(this.ctx.auditAssists, function (x) { return x.user_id === nextAudit.id; }), 'ass_user_id');
- users.push(nextAudit.aid);
- 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, nextAudit, audit, ledgerTp, stageTp);
- await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
- // 添加短信通知-审批通过提醒功能
- 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.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 time = new Date();
- // 整理当前流程审核人状态更新
- const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- if (!audit) {
- throw '审核数据错误';
- }
- const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
- const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
- const sqlParam = [this.tableName, stageId, times];
- const auditors = await this.db.query(sql, sqlParam);
- let order = 1;
- for (const a of auditors) {
- a.times = times + 1;
- a.order = order;
- a.status = auditConst.status.uncheck;
- order++;
- }
- const transaction = await this.db.beginTransaction();
- try {
- const ledgerTp = await this._updateTender(transaction);
- // 添加推送
- const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.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,
- };
- this.ctx.stage.tp_history.push({ times, order: audit.order, ...stageTp });
- await transaction.update(this.tableName, {
- id: audit.id,
- status: checkData.checkType,
- opinion: checkData.opinion,
- end_time: time,
- });
- // 同步 期信息
- 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, order: 0, status: auditConst.status.uncheck }, audit, ledgerTp, stageTp);
- // 拷贝新一次审核流程列表
- await transaction.insert(this.tableName, auditors);
- // 计算该审批人最终数据
- 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, audit.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 time = new Date();
- // 整理当前流程审核人状态更新
- const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- if (!audit || audit.order <= 1) {
- throw '审核数据错误';
- }
- // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
- // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
- const auditors2 = await this.getAuditGroupByList(stageId, times);
- const auditorIndex = await auditors2.findIndex(function(item) {
- return item.aid === audit.aid;
- });
- const preAuditor = auditors2[auditorIndex - 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, audit.tid, stageId, audit.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: audit.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, preAuditor, audit, ledgerTp, stageTp);
- await transaction.update(this.tableName, {
- id: audit.id,
- status: checkData.checkType,
- opinion: checkData.opinion,
- end_time: time,
- });
- // 顺移其后审核人流程顺序
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
- this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
- this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- // 上一审批人,当前审批人 再次添加至流程
- const newAuditors = [];
- newAuditors.push({
- tid: audit.tid,
- sid: audit.sid,
- aid: preAuditor.aid,
- times: audit.times,
- order: audit.order + 1,
- status: auditConst.status.checking,
- begin_time: time,
- });
- newAuditors.push({
- tid: audit.tid,
- sid: audit.sid,
- aid: audit.aid,
- times: audit.times,
- order: audit.order + 2,
- status: auditConst.status.uncheck,
- });
- 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, audit.order + 1, transaction);
- await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
- await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
- await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
- await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
- await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
- // 锁定本人数据,保留锁定数据相关确认状态
- await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audit.aid, preAuditor.aid, 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(audit.sid);
- 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 x.user_id === preAuditor.aid}), 'ass_user_id');
- users.push(preAuditor.aid);
- await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
- qi: stageInfo.order,
- code: shenpiUrl,
- });
- // 微信模板通知
- 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 audit = (
- await this.getAllDataByCondition({
- where: { sid: stageId, times },
- orders: [['order', 'desc']],
- limit: 1,
- offset: 0,
- })
- )[0];
- if (!audit || audit.order < 1) {
- throw '审核数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 当前审批人2次添加至流程中
- const newAuditors = [];
- newAuditors.push({
- tid: audit.tid,
- sid: audit.sid,
- aid: audit.aid,
- times: audit.times,
- order: audit.order + 1,
- status: auditConst.status.checkAgain,
- begin_time: time,
- end_time: time,
- opinion: '',
- });
- newAuditors.push({
- tid: audit.tid,
- sid: audit.sid,
- aid: audit.aid,
- times: audit.times,
- order: audit.order + 2,
- status: auditConst.status.checking,
- begin_time: time,
- });
- await transaction.insert(this.tableName, newAuditors);
- // 复制一份最新数据给下一人
- 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 === audit.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: audit.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, newAuditors[1], newAuditors[0], null, stageTp, pcTp);
- // 添加短信通知-需要审批提醒功能
- // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
- // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
- // const smsType = JSON.parse(smsUser.sms_type);
- // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
- // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
- // const sms = new SMS(this.ctx);
- // const tenderName = await sms.contentChange(tenderInfo.name);
- // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
- // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
- // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
- // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
- // sms.send(smsUser.auth_mobile, content);
- // }
- // }
- const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
- const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
- const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return x.user_id == audit.aid; }), 'ass_user_id');
- users.push(audit.aid);
- await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
- qi: stageInfo.order,
- 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;
- }
- }
- /**
- * 审批撤回
- * @param {Number} stageId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<void>}
- */
- async checkCancel(stageId, times = 1) {
- // 分4种情况,根据ctx.cancancel值判断:
- // 1.原报发起撤回,当前流程删除,并回到待上报
- // 2.审批人撤回审批通过,增加流程,并回到它审批中
- // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
- // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
- const transaction = await this.db.beginTransaction();
- const time = new Date();
- try {
- const ledgerTp = await this._updateTender(transaction);
- let newTimes, newOrder, newSaid, oldTimes, oldOrder, oldSaid = '';
- if (this.ctx.stage.cancancel === 1) {
- // 原报撤回,判断是否为多次,多次则为退回状态
- // 整理当前流程审核人状态更新
- const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- // // 审批人变成待审批状态
- await transaction.update(this.tableName, {
- id: curAudit.id,
- status: auditConst.status.uncheck,
- begin_time: null,
- opinion: null,
- });
- const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
- // 计算并合同支付最终数据
- const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- 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: stageId,
- times,
- ...stageTp,
- tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < times; })),
- cache_time_r: this.ctx.stage.cache_time_l,
- status: times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
- });
- const preFlow = times > 1 ? await this.getLastestAuditor(stageId, 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, times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
- curFlow, preFlow, ledgerTp, stageTp);
- // 计算该审批人最终数据
- await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- // 复制一份最新数据给下一人
- // 保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
- await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction);
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction, this.ctx.stage.times, 1);
- await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
- newTimes = this.ctx.stage.times;
- newOrder = 0;
- newSaid = this.ctx.stage.user_id;
- oldTimes = this.ctx.stage.times;
- oldOrder = 1;
- oldSaid = curAudit.aid;
- } else if (this.ctx.stage.cancancel === 2) {
- const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
- // 整理当前流程审核人状态更新
- const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- const preAudit = this.ctx.stage.preAudit;
- if (!curAudit || curAudit.order <= 1) {
- throw '撤回用户数据错误';
- }
- // 顺移气候审核人流程顺序
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
- this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
- this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- // 当前审批人2次添加至流程中
- const newAuditors = [];
- newAuditors.push({
- tid: curAudit.tid,
- sid: curAudit.sid,
- aid: preAudit.aid,
- times: curAudit.times,
- order: curAudit.order,
- status: auditConst.status.checkCancel,
- begin_time: time,
- end_time: time,
- opinion: '',
- });
- newAuditors.push({
- tid: curAudit.tid,
- sid: curAudit.sid,
- aid: preAudit.aid,
- times: curAudit.times,
- order: curAudit.order + 1,
- status: auditConst.status.checking,
- begin_time: time,
- });
- await transaction.insert(this.tableName, newAuditors);
- // 当前审批人变成待审批
- await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
- // 计算并合同支付最终数据
- const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- 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: curAudit.order, ...stageTp }, { times, order: curAudit.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, this.ctx.stage.status, newAuditors[1], newAuditors[0], ledgerTp, stageTp);
- // 计算该审批人最终数据
- await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- // 复制一份最新数据给下一人
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, curAudit.order + 1, transaction, this.ctx.stage.times, this.ctx.stage.curOrder + 1);
- newTimes = this.ctx.stage.times;
- newOrder = curAudit.order + 1;
- newSaid = preAudit.aid;
- oldTimes = this.ctx.stage.times;
- oldOrder = curAudit.order;
- oldSaid = curAudit.aid;
- // 锁定本人数据,保留锁定数据相关确认状态
- // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
- } else if (this.ctx.stage.cancancel === 3) {
- const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
- // 整理当前流程审核人状态更新
- const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
- const preAudit = this.ctx.stage.preAudit;
- if (!curAudit || curAudit.order <= 1) {
- throw '撤回用户数据错误';
- }
- // 添加撤回人到审批流程中
- const newAuditors = [];
- newAuditors.push({
- tid: curAudit.tid,
- sid: curAudit.sid,
- aid: preAudit.aid,
- times: curAudit.times,
- order: curAudit.order,
- status: auditConst.status.checkCancel,
- begin_time: time,
- end_time: time,
- opinion: '',
- });
- await transaction.insert(this.tableName, newAuditors);
- // 删除当前审批人
- await transaction.delete(this.tableName, { id: curAudit.id });
- // 更新上一个人为审批中
- await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
- where: {
- sid: curAudit.sid,
- times: curAudit.times,
- order: curAudit.order + 1,
- }
- });
- // 计算并合同支付最终数据
- 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: curAudit.order, ...stageTp });
- // 同步 期信息
- await transaction.update(this.ctx.service.stage.tableName, {
- id: stageId,
- times,
- ...stageTp,
- status: auditConst.status.checking,
- tp_history: JSON.stringify(this.ctx.stage.tp_history),
- cache_time_r: this.ctx.stage.cache_time_l,
- });
- const cur = {tid: curAudit.tid, sid: curAudit.sid, aid: preAudit.aid, times: curAudit.times, order: curAudit.order + 1, status: auditConst.status.checking};
- await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, this.ctx.stage.status, cur, newAuditors[0], ledgerTp, stageTp);
- // 计算该审批人最终数据
- await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- // 复制一份最新数据给下一人
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, curAudit.order + 1, transaction, this.ctx.stage.times, this.ctx.stage.curOrder + 1);
- newTimes = this.ctx.stage.times;
- newOrder = curAudit.order + 1;
- newSaid = preAudit.aid;
- oldTimes = this.ctx.stage.times;
- oldOrder = curAudit.order;
- oldSaid = curAudit.aid;
- // 锁定本人数据,保留锁定数据相关确认状态
- // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
- } else if (this.ctx.stage.cancancel === 4) {
- // 原报撤回,判断是否为多次,多次则为退回状态
- // 整理上一个流程审核人状态更新
- const curAudit = await this.getDataByCondition({ sid: stageId, times: times - 1, status: auditConst.status.checkNo });
- // 顺移气候审核人流程顺序
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
- this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
- this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- // 当前审批人2次添加至流程中
- const newAuditors = [];
- newAuditors.push({
- tid: curAudit.tid,
- sid: curAudit.sid,
- aid: curAudit.aid,
- times: curAudit.times,
- order: curAudit.order + 1,
- status: auditConst.status.checkCancel,
- begin_time: time,
- end_time: time,
- opinion: '',
- });
- newAuditors.push({
- tid: curAudit.tid,
- sid: curAudit.sid,
- aid: curAudit.aid,
- times: curAudit.times,
- order: curAudit.order + 2,
- status: auditConst.status.checking,
- begin_time: time,
- });
- await transaction.insert(this.tableName, newAuditors);
- // 删除当前次审批流
- await transaction.delete(this.tableName, { sid: stageId, times });
- const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
- // 计算并合同支付最终数据
- const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- 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: curAudit.times, order: curAudit.order + 1, ...stageTp }, { times: curAudit.times, order: curAudit.order + 2, ...stageTp });
- await transaction.update(this.ctx.service.stage.tableName, {
- id: stageId,
- times: times - 1,
- ...stageTp,
- tp_history: JSON.stringify(this.ctx.stage.tp_history),
- cache_time_r: this.ctx.stage.cache_time_l,
- status: auditConst.status.checking,
- });
- await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, newAuditors[1], newAuditors[0], ledgerTp, stageTp);
- // 计算该审批人最终数据
- await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
- // 复制一份最新数据给下一人
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 1, transaction, this.ctx.stage.times, 0);
- await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 2, transaction, this.ctx.stage.times, 0);
- await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction);
- newTimes = this.ctx.stage.times - 1;
- newOrder = curAudit.order + 2;
- newSaid = curAudit.aid;
- oldTimes = this.ctx.stage.times;
- oldOrder = 0;
- oldSaid = this.ctx.stage.user_id;
- }
- // 先覆盖再删除
- await this.ctx.service.stageBills.updateStageBills4CheckCancel(stageId, newSaid, newTimes, newOrder, oldSaid, oldTimes, oldOrder, transaction);
- await this.ctx.service.stagePos.updateStagePos4CheckCancel(stageId, newSaid, newTimes, newOrder, oldSaid, oldTimes, oldOrder, transaction);
- await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stageId, newTimes, newOrder, oldTimes, oldOrder, transaction);
- await this.ctx.service.stageJgcl.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
- await this.ctx.service.stageBonus.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
- await this.ctx.service.stageOther.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
- await this.ctx.service.stageSafeProd.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
- await this.ctx.service.stageTempLand.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
- // 上报/审批 - 检查三方特殊推送
- await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
- await transaction.commit();
- // 通知发送 - 第三方更新
- if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
- const base_data = {
- tid: this.ctx.tender.id,
- sid: stageId,
- op: 'update',
- };
- this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
- base_data.op = 'update';
- base_data.sid = -1;
- this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
- }
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 获取审核人需要审核的期列表
- *
- * @param auditorId
- * @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`' +
- ' 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: newAuditors.length + 1,
- status: auditConst.status.uncheck,
- };
- 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;
- }
- /**
- * 取某一期已批准审核信息(报表用)
- *
- * @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, newIdList) {
- 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 = [];
- let order = 1;
- for (const aid of newIdList) {
- newAuditors.push({
- tid: stage.tid, sid: stage.id, aid,
- times: stage.times, order, status: auditConst.status.uncheck,
- });
- 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 });
- // 顺移之后审核人流程顺序
- 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;
- }
- }
- }
- return StageAudit;
- };
|