123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const auditConst = require('../const/audit').changeProject;
- const pushType = require('../const/audit').pushType;
- const pushOperate = require('../const/spec_3f').pushOperate;
- const shenpiConst = require('../const/shenpi');
- const smsTypeConst = require('../const/sms_type');
- const SMS = require('../lib/sms');
- const SmsAliConst = require('../const/sms_alitemplate');
- const wxConst = require('../const/wechat_template');
- const auditType = require('../const/audit').auditType;
- module.exports = app => {
- class ChangeProjectAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_project_audit';
- }
- /**
- * 获取 审核列表信息
- *
- * @param {Number} cpId - 变更立项id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getAuditors(cpId, times = 1, order_sort = 'asc', noYB = false) {
- // 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`, g.`sort` ' +
- // 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
- // 'WHERE la.`cpid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
- // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, cpId, times, cpId, times];
- // const result = await this.db.query(sql, sqlParam);
- // const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) as a';
- // const sqlParam2 = [this.tableName, cpId, times];
- // const count = await this.db.queryOne(sql2, sqlParam2);
- // for (const i in result) {
- // result[i].max_sort = count.num;
- // }
- const ybSql = noYB ? ' AND la.audit_order != 0' : '';
- 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' +
- ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
- ' WHERE la.cpid = ? AND la.times = ?' + ybSql +
- ' ORDER BY la.order ' + order_sort;
- const sqlParam = [cpId, 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;
- }
- /**
- * 获取 当前审核人
- *
- * @param {Number} cpId - 变更立项id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getCurAuditor(cpId, 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`, la.audit_type, la.audit_order ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
- ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- async getCurAuditors(cpId, 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`, la.audit_type, la.audit_order ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
- ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取审核人流程列表
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditGroupByList(changeId, times, transaction = false) {
- const sql = 'SELECT pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
- ' WHERE la.`cpid` = ? and la.`times` = ? and la.`status` != ? and la.`status` != ? GROUP BY la.`aid` ORDER BY la.`order`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, auditConst.status.revise, auditConst.status.cancelRevise];
- // const sql =
- // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
- // ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `cpid` = ? 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.`cpid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
- // const sqlParam = [this.tableName, changeId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, 0];
- return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
- }
- /**
- * 移除审核人
- *
- * @param {Number} materialId - 材料调差期id
- * @param {Number} status - 期状态
- * @param {Number} status - 期次数
- * @return {Promise<boolean>}
- */
- async getAuditorByStatus(cpId, status, times = 1) {
- let auditor = null;
- let sql = '';
- let sqlParam = '';
- switch (status) {
- case auditConst.status.checking :
- case auditConst.status.checked :
- case auditConst.status.checkNo :
- case auditConst.status.revise :
- case auditConst.status.cancelRevise :
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
- ' WHERE la.`cpid` = ? and la.`status` = ? ' +
- ' ORDER BY la.`times` desc, la.`order` desc';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, status];
- auditor = await this.db.queryOne(sql, sqlParam);
- break;
- case auditConst.status.back :
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
- ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?' +
- ' ORDER BY la.`times` desc, la.`order` desc';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.back, parseInt(times) - 1];
- auditor = await this.db.queryOne(sql, sqlParam);
- break;
- case auditConst.status.uncheck :
- break;
- default:break;
- }
- return auditor;
- }
- async getAuditorsByStatus(cpId, status, times = 1) {
- let auditor = [];
- let sql = '';
- let sqlParam = '';
- let cur;
- switch (status) {
- case auditConst.status.checking :
- case auditConst.status.checked :
- case auditConst.status.checkNo :
- case auditConst.status.revise :
- case auditConst.status.cancelRevise :
- cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, times, status]);
- if (!cur) return [];
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
- ' WHERE la.`cpid` = ? and la.`order` = ? and times = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, times];
- auditor = await this.db.query(sql, sqlParam);
- break;
- case auditConst.status.back :
- cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, parseInt(times) - 1, status]);
- if (!cur) return [];
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
- ' WHERE la.`cpid` = ? and la.`order` = ? and la.`times` = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, parseInt(times) - 1];
- auditor = await this.db.query(sql, sqlParam);
- break;
- case auditConst.status.uncheck:
- default:
- break;
- }
- return auditor;
- }
- async getLastAudit(cpid, times, transaction = null) {
- const sql = 'SELECT * FROM ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` DESC';
- const sqlParam = [this.tableName, cpid, times];
- return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取审核人流程列表(包括原报)
- * @param {Number} materialId 调差id
- * @param {Number} times 审核次数
- * @return {Promise<Array>} 查询结果集(包括原报)
- */
- async getAuditorsWithOwner(cpId, times = 1) {
- const result = await this.getAuditGroupByList(cpId, times);
- const sql =
- 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
- ' FROM ' +
- this.ctx.service.changeProject.tableName +
- ' As s' +
- ' LEFT JOIN ' +
- this.ctx.service.projectAccount.tableName +
- ' As pa' +
- ' ON s.uid = pa.id' +
- ' WHERE s.id = ?';
- const sqlParam = [times, cpId, cpId];
- const user = await this.db.queryOne(sql, sqlParam);
- result.unshift(user);
- return result;
- }
- /**
- * 新增审核人
- *
- * @param {Number} cpId - 立项书id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<number>}
- */
- async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) {
- const transaction = await this.db.beginTransaction();
- let flag = false;
- try {
- let [newOrder, newAuditOrder] = await this.getNewOrder(cpId, times);
- // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
- newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
- newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
- if (is_gdzs) await this._syncOrderByDelete(transaction, cpId, newOrder, times, '+');
- const data = {
- tid: this.ctx.tender.id,
- cpid: cpId,
- aid: auditorId,
- times,
- order: newOrder,
- status: auditConst.status.uncheck,
- audit_order: newAuditOrder,
- };
- const result = await transaction.insert(this.tableName, data);
- await transaction.commit();
- flag = result.effectRows = 1;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return flag;
- }
- /**
- * 获取 最新审核顺序
- *
- * @param {Number} cpId - 立项书id
- * @param {Number} times - 第几次审批
- * @return {Promise<number>}
- */
- async getNewOrder(cpId, times = 1) {
- const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `cpid` = ? and `times` = ?';
- const sqlParam = [this.tableName, cpId, 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} cpId - 变更立项书id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<boolean>}
- */
- async deleteAuditor(cpId, auditorId, times = 1) {
- const transaction = await this.db.beginTransaction();
- try {
- const condition = { cpid: cpId, aid: auditorId, times };
- const auditor = await this.getDataByCondition(condition);
- if (!auditor) {
- throw '该审核人不存在';
- }
- // 移除整个流程的人
- await transaction.delete(this.tableName, { cpid: cpId, order: auditor.order, times });
- await this._syncOrderByDelete(transaction, cpId, auditor.order, times);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 移除审核人时,同步其后审核人order
- * @param transaction - 事务
- * @param {Number} cpId - 变更立项书id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- * @private
- */
- async _syncOrderByDelete(transaction, cpId, order, times, selfOperate = '-') {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cpid', {
- value: cpId,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('order', {
- value: order,
- operate: '>=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '=',
- });
- this.sqlBuilder.setUpdateData('order', {
- value: 1,
- selfOperate,
- });
- this.sqlBuilder.setUpdateData('audit_order', {
- value: 1,
- selfOperate,
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- return data;
- }
- /**
- * 开始审批
- * @param {Number} cpId - 立项书id
- * @param {Number} times - 第几次审批
- * @return {Promise<boolean>}
- */
- async start(cpId, times = 1) {
- const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: 1 } });
- if (audits.length === 0) {
- if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
- throw '请联系管理员添加审批人';
- } else {
- throw '请先选择审批人,再上报数据';
- }
- }
- const transaction = await this.db.beginTransaction();
- try {
- 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);
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: cpId, status: auditConst.status.checking,
- });
- // 微信模板通知
- const wechatData = {
- type: 'project',
- status: wxConst.status.check,
- tips: wxConst.tips.check,
- code: this.ctx.session.sessionProject.code,
- c_name: this.ctx.change.name,
- };
- await this.ctx.helper.sendWechat(this._.map(audits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
- for (const audit of audits) {
- await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
- pid: this.ctx.session.sessionProject.id,
- tid: this.ctx.tender.id,
- uid: audit.aid,
- sp_type: 'change',
- sp_id: audit.id,
- table_name: this.tableName,
- template: wxConst.template.change,
- wx_data: wechatData,
- });
- }
- await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: cpId });
- // todo 更新标段tender状态 ?
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 获取审核人需要审核的期列表
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditChangeProject(auditorId) {
- const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
- ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
- ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
- ' FROM ?? AS ma, ?? AS m, ?? As t ' +
- ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
- ' and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
- const sqlParam = [this.tableName, this.ctx.service.changeProject.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.back, auditConst.status.back];
- const result = await this.db.query(sql, sqlParam);
- // 过滤result中存在重复cpid的值, 保留最新的一条
- const filterResult = [];
- const cpidArr = [];
- for (const r of result) {
- if (cpidArr.indexOf(r.cpid) === -1) {
- filterResult.push(r);
- cpidArr.push(r.cpid);
- }
- }
- return filterResult;
- }
- /**
- * 获取审核人审核的次数
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getCountByChecked(auditorId) {
- return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo] });
- }
- /**
- * 获取最近一次审批结束时间
- *
- * @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.back, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
- const sqlParam = [this.tableName, auditorId];
- const result = await this.db.queryOne(sql, sqlParam);
- return result ? result.end_time : null;
- }
- /**
- * 用于添加推送所需的content内容
- * @param {Number} pid 项目id
- * @param {Number} tid 台账id
- * @param {Number} cpId 立项书id
- * @param {Number} uid 审批人id
- */
- async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
- const noticeSql = 'SELECT * FROM (SELECT ' +
- ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
- ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
- ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
- ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
- ' 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.changeProject.tableName, cpId, this.tableName, 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 {Number} cpId - 立项书id
- * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
- * @param {Number} times - 第几次审批
- * @return {Promise<void>}
- */
- async check(cpId, checkData, times = 1) {
- if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.back) {
- throw '提交数据错误';
- }
- const pid = this.ctx.session.sessionProject.id;
- switch (checkData.checkType) {
- case auditConst.status.checked:
- await this._checked(pid, cpId, checkData, times);
- break;
- case auditConst.status.back:
- await this._back(pid, cpId, checkData, times);
- break;
- case auditConst.status.checkNo:
- await this._checkNo(pid, cpId, checkData, times);
- break;
- default:
- throw '无效审批操作';
- }
- }
- async _checked(pid, cpId, checkData, times) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- // 整理当前流程审核人状态更新
- const audits = await this.getAllDataByCondition({ where: { cpid: cpId, 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: { cpid: cpId, times, order: selfAudit.order } });
- const nextAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order + 1 } });
- // 获取审核人列表
- // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
- // const sqlParam = [this.tableName, cpId, times];
- // const auditors = await this.db.query(sql, sqlParam);
- //
- // const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
- const transaction = await this.db.beginTransaction();
- try {
- // 更新本人审批状态
- await transaction.update(this.tableName, {
- id: selfAudit.id,
- status: checkData.checkType,
- opinion: checkData.opinion,
- end_time: time,
- });
- await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
- // 获取推送必要信息
- const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
- // 添加推送
- // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
- const records = [];
- const auditors = await this.getAuditorsWithOwner(cpId, times);
- auditors.forEach(audit => {
- records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
- });
- await transaction.insert('zh_notice', records);
- 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,
- });
- await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
- }
- if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
- }
- // 无下一审核人表示,审核结束
- if (nextAudits.length > 0) {
- // 流程至下一审批人
- 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.changeProject.tableName, {
- id: cpId, status: auditConst.status.checking,
- });
- // 微信模板通知
- const wechatData = {
- type: 'project',
- status: wxConst.status.check,
- tips: wxConst.tips.check,
- code: this.ctx.session.sessionProject.code,
- c_name: this.ctx.change.name,
- };
- await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
- for (const nextAudit of nextAudits) {
- await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
- pid: this.ctx.session.sessionProject.id,
- tid: this.ctx.tender.id,
- uid: nextAudit.aid,
- sp_type: 'change',
- sp_id: nextAudit.id,
- table_name: this.tableName,
- template: wxConst.template.change,
- wx_data: wechatData,
- });
- }
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
- } else {
- // 本期结束
- // 生成截止本期数据 final数据
- // 同步 期信息
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: cpId, status: checkData.checkType,
- });
- // 微信模板通知
- const users = this._.uniq(this._.map(auditors, 'aid'));
- const wechatData = {
- type: 'project',
- status: wxConst.status.success,
- tips: wxConst.tips.success,
- code: this.ctx.session.sessionProject.code,
- c_name: this.ctx.change.name,
- };
- await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.checked);
- }
- } else {
- // 同步 期信息
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: cpId,
- status: auditConst.status.checking,
- });
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _back(pid, cpId, checkData, times) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- const changeData = await this.ctx.service.changeProject.getDataById(cpId);
- // 整理当前流程审核人状态更新
- const audits = await this.getAllDataByCondition({ where: { cpid: cpId, 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: { cpid: cpId, times: selfAudit.times, order: selfAudit.order } });
- const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
- const newAuditors = auditors.map(x => {
- return {
- aid: x.aid, tid: selfAudit.tid, cpid: selfAudit.cpid,
- 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 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 this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
- // 添加到消息推送表
- const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
- const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.back, content: noticeContent }];
- auditors.forEach(audit => {
- records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.back, content: noticeContent });
- });
- await transaction.insert(this.ctx.service.noticePush.tableName, records);
- // 同步期信息
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: cpId, status: checkData.checkType,
- times: times + 1,
- });
- // 拷贝新一次审核流程列表
- await transaction.insert(this.tableName, newAuditors);
- // 微信模板通知
- const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
- const wechatData = {
- type: 'project',
- status: wxConst.status.back,
- tips: wxConst.tips.back,
- code: this.ctx.session.sessionProject.code,
- c_name: this.ctx.change.name,
- };
- await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
- // 生成内容保存表至zh_change_project_history中,用于撤回
- await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _checkNo(pid, cpId, checkData, times) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- // 整理当前流程审核人状态更新
- const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
- if (!audits) throw '审核数据错误';
- const selfAudit = audits.find(x => { return x.aid === accountId; });
- if (!selfAudit) throw '当前标段您无权终止';
- const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
- // 获取审核人列表
- // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
- // const sqlParam = [this.tableName, cpId, times];
- // const auditors = await this.db.query(sql, sqlParam);
- const transaction = await this.db.beginTransaction();
- try {
- 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 this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
- // 获取推送必要信息
- const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
- // 添加推送
- const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
- auditors.forEach(audit => {
- records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
- });
- await transaction.insert('zh_notice', records);
- // 本期结束
- // 生成截止本期数据 final数据
- // 同步 期信息
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: cpId, status: checkData.checkType,
- });
- // 微信模板通知
- const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
- const wechatData = {
- type: 'project',
- status: wxConst.status.stop,
- tips: wxConst.tips.stop,
- code: this.ctx.session.sessionProject.code,
- c_name: this.ctx.change.name,
- };
- await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 复制上一期的审批人列表给最新一期
- *
- * @param transaction - 新增一期的事务
- * @param {Object} preMaterial - 上一期
- * @param {Object} newaMaterial - 最新一期
- * @return {Promise<*>}
- */
- async copyPreChangeProjectAuditors(transaction, preChange, newChange) {
- // 再获取非原报审批人
- const auditList = await this.getUniqAuditor(preChange.id, preChange.times); // 全部参与的审批人
- const newAuditors = [];
- for (const x of auditList) {
- if (x.audit_order !== 0) {
- newAuditors.push({
- tid: preChange.tid, cpid: newChange.id, aid: x.aid,
- times: 1, order: x.audit_order, status: auditConst.status.uncheck,
- audit_type: x.audit_type, audit_order: x.audit_order,
- });
- }
- }
- const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
- return result ? result.affectedRows === newAuditors.length : true;
- }
- async getAllAuditors(tenderId) {
- const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
- ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
- ' WHERE t.id = ?' +
- ' GROUP BY ma.aid';
- const sqlParam = [tenderId];
- return this.db.query(sql, sqlParam);
- }
- /**
- * 审批撤回
- * @param {Number} stageId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<void>}
- */
- async checkCancel(change) {
- // 分4种情况,根据ctx.cancancel值判断:
- // 1.原报发起撤回,当前流程删除,并回到待上报
- // 2.审批人撤回审批通过,增加流程,并回到它审批中
- // 3.审批人撤回审批终止,增加流程,并回到它审批中,并更新立项状态为审批中
- // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
- // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
- switch (change.cancancel) {
- case 1: await this._userCheckCancel(change); break;
- case 2: await this._auditCheckCancel(change); break;
- case 3: await this._auditCheckCancelStop(change); break;
- case 4: await this._auditCheckCancelNo(change); break;
- case 5: await this._auditCheckCancelAnd(change); break;
- default: throw '不可撤回,请刷新页面重试';
- }
- }
- /**
- * 原报撤回,直接改动审批人状态
- * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
- *
- * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
- * ...
- *
- * @param stage
- * @returns {Promise<void>}
- * @private
- */
- async _userCheckCancel(change) {
- const transaction = await this.db.beginTransaction();
- try {
- // 整理当前流程审核人状态更新
- // 审批人变成待审批状态
- const updateData = change.curAuditors.map(x => {
- return {
- id: x.id,
- status: auditConst.status.uncheck,
- begin_time: null, opinion: null,
- };
- });
- await transaction.updateRows(this.tableName, updateData);
- await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
- // 变成待上报状态
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: change.id,
- status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
- });
- 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(change) {
- if (change.curAuditors.length === 0 || change.curAuditors[0].order <= 1) {
- throw '撤回用户数据错误';
- }
- const accountId = this.ctx.session.sessionUser.accountId;
- const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId; });
- if (!selfAuditor) throw '撤回用户数据错误';
- const time = new Date();
- const transaction = await this.db.beginTransaction();
- try {
- // 顺移其后审核人流程顺序
- const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
- await transaction.query(sql, [change.id, selfAuditor.times, change.curAuditors[0].order]);
- // 当前审批人2次添加至流程中
- const checkCancelAuditors = [],
- checkingAuditors = [];
- change.preAuditors.forEach(x => {
- checkCancelAuditors.push({
- tid: change.tid, cpid: change.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,
- });
- });
- change.preAuditors.forEach(x => {
- checkingAuditors.push({
- tid: change.tid, cpid: change.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.updateRows(this.tableName, change.curAuditors.map(x => {
- return { id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2 };
- }));
- await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(change.curAuditors, 'id'));
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 审批人撤回审批终止,插入两条数据
- *
- * @param stage
- * @returns {Promise<void>}
- * @private
- */
- async _auditCheckCancelStop(change) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
- if (!selfAuditor) throw '该变更立项由他人审批终止,您不可撤回';
- const time = new Date();
- const transaction = await this.db.beginTransaction();
- try {
- // 顺移其后审核人流程顺序
- const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
- await transaction.query(sql, [change.id, change.times, selfAuditor.order]);
- // 当前审批人2次添加至流程中
- const checkCancelAuditors = [],
- checkingAuditors = [];
- change.preAuditors.forEach(x => {
- checkCancelAuditors.push({
- tid: change.tid, cpid: change.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,
- });
- });
- change.preAuditors.forEach(x => {
- checkingAuditors.push({
- tid: change.tid, cpid: change.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.update(this.ctx.service.changeProject.tableName, {
- id: change.id,
- status: auditConst.status.checking,
- });
- 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(change) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.back; });
- if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
- const time = new Date();
- const transaction = await this.db.beginTransaction();
- try {
- // 整理上一个流程审核人状态更新
- // 顺移其后审核人流程顺序
- const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
- await transaction.query(sql, [change.id, selfAuditor.times, selfAuditor.order]);
- // 当前审批人2次添加至流程中
- const checkCancelAuditors = [],
- checkingAuditors = [];
- change.preAuditors.forEach(x => {
- checkCancelAuditors.push({
- tid: change.tid, cpid: change.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,
- });
- });
- change.preAuditors.forEach(x => {
- checkingAuditors.push({
- tid: change.tid, cpid: change.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, { cpid: change.id, times: change.times });
- // 回退数据
- await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
- await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
- // // 设置变更立项为审批中
- // await transaction.update(this.ctx.service.changeProject.tableName, {
- // id: change.id,
- // time: change.times - 1,
- // status: auditConst.status.checking,
- // });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 会签未全部审批通过时,撤回仅修改本人状态
- *
- * @param stage
- * @returns {Promise<void>}
- * @private
- */
- async _auditCheckCancelAnd(change) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const selfAuditor = change.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 { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async checkRevise(change) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- // 初始化事务
- const transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const pid = this.ctx.session.sessionProject.id;
- // 获取审核人列表
- const auditors = await this.getUniqAuditor(change.id, change.times); // 全部参与的审批人
- // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
- // const sqlParam = [this.tableName, change.id, change.times];
- // const auditors = await this.db.query(sql, sqlParam);
- // 添加到消息推送表
- const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
- const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.revise, content: noticeContent }];
- auditors.forEach(auditor => {
- records.push({
- pid,
- type: pushType.changeProject,
- uid: auditor.aid,
- status: auditConst.status.revise,
- content: noticeContent,
- });
- });
- await transaction.insert('zh_notice', records);
- const order = change.auditors.length > 0 ? change.auditors.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
- // 当前审批人添加至流程中
- const checkReviseAuditor = {
- tid: change.tid, cpid: change.id, aid: change.uid,
- times: change.times, order,
- status: auditConst.status.revise,
- begin_time: time, end_time: time, opinion: '',
- audit_type: auditType.key.common, audit_order: 0,
- };
- await transaction.insert(this.tableName, checkReviseAuditor);
- // 修订人加入审批流程
- const newAuditors = auditors.map(x => {
- return {
- aid: x.aid, tid: change.tid, cpid: change.id,
- times: change.times + 1, order: x.audit_order, status: auditConst.status.uncheck,
- audit_type: x.audit_type, audit_order: x.audit_order,
- };
- });
- await transaction.insert(this.tableName, newAuditors);
- // 生成内容保存表至zh_change_history中,用于撤销修订回退
- const changeData = await transaction.get(this.ctx.service.changeProject.tableName, { id: change.id });
- await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
- // 设置变更立项修订状态
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: change.id,
- status: auditConst.status.revise,
- times: change.times + 1,
- });
- await transaction.commit();
- result = true;
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 撤销修订变更令
- * @param { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async cancelRevise(change) {
- const time = new Date();
- // 初始化事务
- const transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const pid = this.ctx.session.sessionProject.id;
- // 获取审核人列表
- const auditors = await this.getUniqAuditor(change.id, change.times - 1); // 全部参与的审批人
- // 添加到消息推送表
- const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
- const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.cancelRevise, content: noticeContent }];
- auditors.forEach(auditor => {
- records.push({
- pid,
- type: pushType.changeProject,
- uid: auditor.aid,
- status: auditConst.status.cancelRevise,
- content: noticeContent,
- });
- });
- await transaction.insert('zh_notice', records);
- const lastAudit = await this.getLastAudit(change.id, change.times - 1);
- // 新增一个撤销修订状态到审批流程中
- const revise_audit = {
- tid: change.tid, cpid: change.id, aid: this.ctx.session.sessionUser.accountId,
- times: change.times - 1, order: lastAudit.order + 1, status: auditConst.status.cancelRevise,
- begin_time: time, end_time: time, opinion: '',
- audit_type: lastAudit.audit_type, audit_order: lastAudit.audit_order,
- };
- await transaction.insert(this.tableName, revise_audit);
- await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
- await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
- await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
- await transaction.commit();
- result = true;
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 重新审批变更立项
- * @param { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async checkAgain(change) {
- const accountId = this.ctx.session.sessionUser.accountId;
- // 初始化事务
- const time = new Date();
- const transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const noYbAuditors = change.auditors.filter(x => { return x.audit_order !== 0; });
- const noYbMaxOrder = noYbAuditors[noYbAuditors.length - 1].order;
- const maxOrder = change.auditors[change.auditors.length - 1].order;
- const audits = change.auditors.filter(x => { return x.order === noYbMaxOrder; });
- if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
- const selfAudit = audits.find(x => { return x.aid === accountId; });
- if (!selfAudit) throw '当前标段您无权审批';
- // 当前审批人2次添加至流程中
- const checkAgainAuditors = [];
- audits.forEach(x => {
- checkAgainAuditors.push({
- tid: change.tid, cpid: change.id, aid: x.aid,
- times: x.times, order: maxOrder + 1,
- status: !selfAudit || 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,
- });
- });
- const checkingAuditors = [];
- audits.forEach(x => {
- checkingAuditors.push({
- tid: change.tid, cpid: change.id, aid: x.aid,
- times: x.times, order: maxOrder + 2,
- status: auditConst.status.checking,
- begin_time: time,
- audit_type: x.audit_type, audit_order: x.audit_order,
- });
- });
- await transaction.insert(this.tableName, checkAgainAuditors);
- const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
- // 获取刚批量添加的所有list
- // for (let j = 0; j < checkingAuditors.length; j++) {
- // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
- // }
- // 设置变更令审批中
- await transaction.update(this.ctx.service.changeProject.tableName, {
- id: change.id,
- status: auditConst.status.checking,
- });
- // 检查三方特殊推送
- await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
- await transaction.commit();
- result = true;
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- result = false;
- }
- return result;
- }
- async getAuditorGroup(cpId, times) {
- const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
- return this.ctx.helper.groupAuditors(auditors, 'order', true);
- }
- async getUserGroup(cpId, times) {
- const group = await this.getAuditorGroup(cpId, times);
- const sql =
- 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
- ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
- ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
- ' ON s.uid = pa.id' +
- ' WHERE s.id = ?';
- const sqlParam = [times, cpId, cpId];
- const user = await this.db.queryOne(sql, sqlParam);
- user.audit_order = 0;
- group.unshift([ user ]);
- return group;
- }
- async getUniqUserGroup(cpId, times) {
- const group = await this.getAuditorGroup(cpId, times);
- const sql =
- 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
- ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
- ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
- ' ON s.uid = pa.id' +
- ' WHERE s.id = ?';
- const sqlParam = [times, cpId, cpId];
- const user = await this.db.queryOne(sql, sqlParam);
- user.audit_order = 0;
- group.unshift([ user ]);
- return this.ctx.helper.groupAuditorsUniq(group);
- }
- async getAuditorHistory(cpId, times, reverse = false) {
- const history = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- const auditors = await this.getAuditors(cpId, i);
- const group = this.ctx.helper.groupAuditors(auditors);
- const historyGroup = [];
- // 找出group里audit_order最大值
- const max_info = group.length > 0 ? this._.maxBy(group, function(item) {
- return item && item[0] && item[0].audit_order;
- }) : null;
- const max_order = max_info ? max_info[0].audit_order : -1;
- for (const g of group) {
- const his = {
- beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
- 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) {
- his.begin_time = 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) {
- his.end_time = 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);
- }
- if (reverse) {
- history.push(historyGroup.reverse());
- } else {
- history.push(historyGroup);
- }
- }
- }
- return history;
- }
- async getUniqAuditor(cpId, times) {
- const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
- const result = [];
- auditors.forEach(x => {
- if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
- result.push(x);
- }
- });
- return result;
- }
- async saveAudit(cpId, times, sp_group, data) {
- const transaction = await this.db.beginTransaction();
- try {
- const auditors = await this.getAuditGroupByList(cpId, times);
- const now_audit = this._.find(auditors, { aid: data.old_aid });
- if (data.operate !== 'del') {
- const exist = await this.getDataByCondition({ cpid: cpId, times, aid: data.new_aid });
- if (exist) throw '该审核人已存在,请勿重复添加';
- }
- 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, cpid: cpId, aid: data.new_aid,
- order: now_audit.order + 1,
- audit_order: now_audit.audit_order + 1, audit_type: auditType.key.common,
- times, status: auditConst.status.uncheck,
- };
- // order+1
- await this._syncOrderByDelete(transaction, cpId, now_audit.order + 1, times, '+');
- await transaction.insert(this.tableName, newAudit);
- // 更新审批流程页数据,如果存在
- } else if (data.operate === 'add-sibling') {
- if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
- throw '当前人下无法操作新增';
- }
- const newAudit = {
- tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
- order: now_audit.order,
- audit_order: now_audit.audit_order, audit_type: now_audit.audit_type,
- times, status: auditConst.status.uncheck,
- };
- await transaction.insert(this.tableName, newAudit);
- } else if (data.operate === 'del') {
- if (now_audit.status !== auditConst.status.uncheck) {
- throw '当前人无法操作删除';
- }
- const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
- await transaction.delete(this.tableName, { cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
- if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, cpId, 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({ cpid: cpId, 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.change === shenpiConst.sp_status.gdspl) {
- const newAuditors = await transaction.select(this.tableName, { where: { cpid: cpId, times }, orders: [['order', 'asc']] });
- const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order', true);
- const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
- await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, uniqNewAuditorGroup, sp_group);
- } else if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
- const newAuditors = await this.getAuditGroupByList(cpId, times, transaction);
- await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, this._.map(newAuditors, 'aid'));
- }
- // 更新到审批流程方法
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async changeSpGroup(change, sp_group) {
- const transaction = await this.db.beginTransaction();
- try {
- const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
- if (!group) {
- throw '该固定审批组不存在,请刷新页面重新获取';
- }
- const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdspl, sp_group: group.id } });
- await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
- await this.updateNewAuditors(change, shenpiList, transaction);
- await transaction.update(this.ctx.service.changeProject.tableName, { id: change.id, sp_group: group.id });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async updateNewAuditList(change, newList) {
- const transaction = await this.db.beginTransaction();
- try {
- await this.updateNewAuditors(change, newList, transaction);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateNewAuditors(change, newList, transaction) {
- // 先删除旧的审批流,再添加新的
- await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
- const newAuditors = [];
- for (const auditor of newList) {
- newAuditors.push({
- tid: change.tid, cpid: change.id, aid: auditor.audit_id,
- times: change.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);
- }
- async updateLastAudit(change, auditList, lastId) {
- const transaction = await this.db.beginTransaction();
- try {
- // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
- const existAudit = auditList.find(x => { return x.aid === lastId; });
- let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
- if (existAudit) {
- await transaction.delete(this.tableName, { cpid: change.id, times: change.times, aid: lastId });
- const sameOrder = auditList.filter(x => { return x.order === existAudit.order; });
- if (sameOrder.length === 1) {
- const updateData = [];
- auditList.forEach(x => {
- if (x.order <= existAudit.order) return;
- updateData.push({ id: x.id, order: x.order - 1, audit_order: x.audit_order - 1 });
- });
- if (updateData.length > 0) {
- await transaction.updateRows(updateData);
- }
- order = order - 1;
- }
- }
- // 添加终审
- const newAuditor = {
- tid: change.tid, cpid: change.id, aid: lastId,
- times: change.times, order, status: auditConst.status.uncheck,
- audit_type: auditType.key.common, audit_order: order,
- };
- await transaction.insert(this.tableName, newAuditor);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return ChangeProjectAudit;
- };
|