| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const auditConst = require('../const/audit').contract;
- const auditType = require('../const/audit').auditType;
- // const pushType = require('../const/audit').pushType;
- const shenpiConst = require('../const/sp_shenpi');
- const contractConst = require('../const/contract');
- module.exports = app => {
- class ContractSpAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'contract_sp_audit';
- }
- async checkShenpi(contract) {
- const cid = contract.cid ? contract.cid : contract.id;
- const cpid = contract.cid ? contract.id : null;
- const change_type = contract.cid ? contractConst.typeMap[contract.contract_type] : 'contract';
- const tableName = contract.cid ? this.ctx.service.contractPay : this.ctx.service.contract;
- const status = auditConst.status;
- let shenpi = '';
- if (this.ctx.contract_tender) {
- const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(this.ctx.contract.id);
- shenpi = tenderInfo ? tenderInfo.shenpi : '';
- } else {
- shenpi = this.ctx.subProject.shenpi;
- }
- const shenpi_status = shenpi.contract;
- if ((contract.status === status.uncheck || contract.status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
- // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
- const auditList = await this.getAllDataByCondition({ where: { cid, cpid, times: contract.times }, orders: [['order', 'asc']] });
- // auditList.shift();
- if (shenpi_status === shenpiConst.sp_status.gdspl) {
- // 判断并获取审批组
- const group = await this.ctx.service.shenpiGroup.getSelectGroupByChangeType(this.ctx.contract.id, shenpiConst.sp_type.contract, change_type, contract.sp_group);
- if ((group && contract.sp_group !== group.id) || (!group && contract.sp_group !== 0)) {
- contract.sp_group = group ? group.id : 0;
- await tableName.defaultUpdate({ id: contract.id, sp_group: contract.sp_group });
- }
- const condition = this._.assign({ sp_type: shenpiConst.sp_type.contract, sp_status: shenpi_status, sp_group: contract.sp_group }, this.ctx.contractOptions);
- const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: condition, orders: [['audit_order', 'asc']] });
- if (contract.sp_group !== 0 || shenpiList.length !== 0) {
- // const shenpiIdList = _.map(shenpiList, 'audit_id');
- // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
- let sameAudit = auditList.length === shenpiList.length;
- if (sameAudit) {
- for (const audit of auditList) {
- const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
- if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
- sameAudit = false;
- break;
- }
- }
- }
- if (!sameAudit) {
- await this.updateNewAuditList(contract, shenpiList);
- await this.loadUser(contract);
- await this.loadAuditViewData(contract);
- }
- }
- } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
- const shenpiInfo = await this.service.shenpiAudit.getDataByCondition(this._.assign({ sp_type: shenpiConst.sp_type.contract, sp_status: shenpi_status }, this.ctx.contractOptions));
- // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
- const lastAuditors = auditList.filter(x => { return x.audit_order === auditList.length - 1; });
- if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
- await this.updateLastAudit(contract, auditList, shenpiInfo.audit_id);
- } else if (!shenpiInfo) {
- // 不存在终审人的状态下这里恢复为授权审批人
- shenpi.contract = shenpiConst.sp_status.sqspr;
- }
- }
- }
- }
- async loadUser(contract) {
- const status = auditConst.status;
- const accountId = this.ctx.session.sessionUser.accountId;
- contract.user = await this.ctx.service.projectAccount.getAccountInfoById(contract.uid);
- contract.auditors = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, contract.times); // 全部参与的审批人
- contract.auditorIds = this._.map(contract.auditors, 'aid');
- contract.curAuditors = contract.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
- contract.curAuditorIds = this._.map(contract.curAuditors, 'aid');
- contract.flowAuditors = contract.curAuditors.length > 0 ? contract.auditors.filter(x => { return x.order === contract.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
- contract.flowAuditorIds = this._.map(contract.flowAuditors, 'aid');
- contract.nextAuditors = contract.curAuditors.length > 0 ? contract.auditors.filter(x => { return x.order === contract.curAuditors[0].order + 1; }) : [];
- contract.nextAuditorIds = this._.map(contract.nextAuditors, 'aid');
- contract.auditorGroups = this.ctx.helper.groupAuditors(contract.auditors);
- contract.userGroups = this.ctx.helper.groupAuditorsUniq(contract.auditorGroups);
- contract.finalAuditorIds = this._.map(contract.userGroups[contract.userGroups.length - 1], 'aid');
- }
- async loadAuditViewData(contract) {
- const times = contract.status === auditConst.status.checkNo ? contract.times - 1 : contract.times;
- if (!contract.user) contract.user = await this.ctx.service.projectAccount.getAccountInfoById(contract.uid);
- contract.auditHistory = await this.getAuditorHistory(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, times);
- // 获取审批流程中左边列表
- // if ((contract.status === auditConst.status.checkNo) && !(contract.uid === this.ctx.session.sessionUser.accountId || this.ctx.contract_audit_permission.permission_edit_contract || this.ctx.session.sessionUser.is_admin)) {
- const auditors = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, times); // 全部参与的审批人
- const auditorGroups = this.ctx.helper.groupAuditors(auditors);
- contract.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
- // } else {
- // contract.auditors2 = contract.userGroups;
- // }
- if (contract.status === auditConst.status.uncheck || contract.status === auditConst.status.checkNo) {
- contract.auditorList = await this.getAuditors(contract.cid ? contract.cid : contract.id, contract.cid ? contract.id : null, contract.times);
- }
- }
- /**
- * 获取 审核列表信息
- *
- * @param {Number} cpId - 变更立项id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getAuditors(cid, cpid = null, 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 cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- 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.cid = ?' + cpidSql + ' AND la.times = ?' + ybSql +
- ' ORDER BY la.order ' + order_sort;
- const sqlParam = [cid, 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(cid, cpid = null, times = 1) {
- const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- 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.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ? and la.`audit_order` != 0';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checking, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- async getCurAuditors(cid, cpid = null, times = 1) {
- const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- 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.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ? and la.`audit_order` != 0';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checking, times];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取审核人流程列表
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- // async getAuditGroupByList(cid, cpid = null, times, noYB = true, transaction = false) {
- // const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, 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.`cid` = ?' + cpidSql + ' and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
- // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, times];
- // return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
- // }
- /**
- * 获取审核人流程列表(除去原报)
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditGroupByList(cid, cpid = null, 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 cpidSql = cpid ? ' AND la.`cpid` = ' + cpid : ' AND la.`cpid` is null';
- const sql =
- 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
- ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `cid` = ? and `cpid` ' + (cpid ? '= ' + cpid : 'is null') + ' and `times` = ? and `audit_order` != 0 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.`cid` = ?' + cpidSql + ' and la.`times` = ? and la.`audit_order` != 0 order BY la.`order`';
- const sqlParam = [this.tableName, cid, times, this.tableName, this.ctx.service.projectAccount.tableName, cid, times];
- 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(cid, cpid = null, status, times = 1) {
- const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- let auditor = null;
- let sql = '';
- let sqlParam = '';
- switch (status) {
- case auditConst.status.checking :
- case auditConst.status.checked :
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
- ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? ' +
- ' ORDER BY la.`times` desc, la.`order` desc';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, 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.`cid`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
- ' WHERE la.`cid` = ?' + cpidSql + ' and la.`status` = ? and la.`times` = ?' +
- ' ORDER BY la.`times` desc, la.`order` desc';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, auditConst.status.checkNo, parseInt(times) - 1];
- auditor = await this.db.queryOne(sql, sqlParam);
- break;
- case auditConst.status.uncheck :
- break;
- default:break;
- }
- return auditor;
- }
- async getAuditorsByStatus(cid, cpid = null, status, times = 1) {
- const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- let auditor = [];
- let sql = '';
- let sqlParam = '';
- let cur;
- switch (status) {
- case auditConst.status.checking :
- case auditConst.status.checked :
- case auditConst.status.checkNoPre:
- cur = await this.db.queryOne('SELECT * From ?? where cid = ? AND cpid ' + (cpid ? '= ' + cpid : 'is null') + ' AND times = ? AND status = ? AND audit_order != 0 ORDER By times DESC, `order` DESC', [this.tableName, cid, times, status]);
- if (!cur) return [];
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, 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.`cid` = ?' + cpidSql + ' and la.`order` = ? and times = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, cur.order, times];
- auditor = await this.db.query(sql, sqlParam);
- break;
- case auditConst.status.checkNo :
- cur = await this.db.queryOne('SELECT * From ?? where cid = ? AND cpid ' + (cpid ? '= ' + cpid : 'is null') + ' AND times = ? AND status = ? AND audit_order != 0 ORDER By times DESC, `order` DESC', [this.tableName, cid, parseInt(times) - 1, status]);
- if (!cur) return [];
- sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cid`, 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.`cid` = ?' + cpidSql + ' and la.`order` = ? and la.`times` = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid, cur.order, parseInt(times) - 1];
- auditor = await this.db.query(sql, sqlParam);
- break;
- case auditConst.status.uncheck:
- default:
- break;
- }
- return auditor;
- }
- async getLastAudit(cid, cpid = null, times, transaction = null) {
- const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
- const sql = 'SELECT * FROM ?? WHERE `cid` = ?' + cpidSql + ' AND `times` = ? ORDER BY `order` DESC';
- const sqlParam = [this.tableName, cid, 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(cid, cpid = null, times = 1) {
- // const cpidSql = cpid ? ' AND la.cpid = ' + cpid : ' AND la.cpid is null';
- const result = await this.getAuditGroupByList(cid, 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.changePlan.tableName +
- // ' As s' +
- // ' LEFT JOIN ' +
- // this.ctx.service.projectAccount.tableName +
- // ' As pa' +
- // ' ON s.uid = pa.id' +
- // ' WHERE s.id = ?';
- // const sqlParam = [times, cid, cid];
- // 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(options, cid, cpid = null, auditorId, times = 1, is_gdzs = 0) {
- const transaction = await this.db.beginTransaction();
- let flag = false;
- try {
- let [newOrder, newAuditOrder] = await this.getNewOrder(cid, 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, cid, cpid, newOrder, times, '+');
- const data = {
- spid: options.spid || null,
- tid: options.tid || null,
- cid,
- cpid,
- aid: auditorId,
- times,
- order: newOrder,
- status: auditConst.status.uncheck,
- audit_order: newAuditOrder,
- };
- const result = await transaction.insert(this.tableName, data);
- await this.ctx.service.contractAudit.saveAudits(options, [{ id: auditorId }], transaction);
- 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(cid, cpid = null, times = 1) {
- const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
- const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `cid` = ?' + cpidSql + ' and `times` = ?';
- const sqlParam = [this.tableName, cid, 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(cid, cpid = null, auditorId, times = 1) {
- const transaction = await this.db.beginTransaction();
- try {
- const cpidSql = cpid ? ' AND cpid = ' + cpid : ' AND cpid is null';
- const sql = 'SELECT * FROM ?? WHERE `cid` = ?' + cpidSql + ' and `times` = ? and `aid` = ? and `audit_order` != 0 ORDER BY `order` DESC';
- const sqlParam = [this.tableName, cid, times, auditorId];
- const auditor = await transaction.queryOne(sql, sqlParam);
- // const condition = { cid, cpid, aid: auditorId, times };
- // const auditor = await this.getDataByCondition(condition);
- if (!auditor) {
- throw '该审核人不存在';
- }
- await transaction.delete(this.tableName, { cid, cpid, order: auditor.order, times });
- await this._syncOrderByDelete(transaction, cid, 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, cid, cpid, order, times, selfOperate = '-') {
- // this.initSqlBuilder();
- // this.sqlBuilder.setAndWhere('cid', {
- // value: this.db.escape(cid),
- // 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);
- const sql = 'UPDATE ?? SET `order` = `order` ' + selfOperate + ' ?, `audit_order` = `audit_order` ' + selfOperate + ' ? WHERE `cid` = ? AND `cpid` ' + (cpid ? '= ' + cpid : 'is null') + ' AND `order` >= ? AND `times` = ?';
- const sqlParam = [this.tableName, 1, 1, cid, order, times];
- const data = await transaction.query(sql, sqlParam);
- return data;
- }
- /**
- * 开始审批
- * @param {Number} cpId - 方案id
- * @param {Number} times - 第几次审批
- * @return {Promise<boolean>}
- */
- async start(options, shenpi, cid, cpid = null, times = 1) {
- const audits = await this.getAllDataByCondition({ where: { cid, cpid, times, order: 1 } });
- if (audits.length === 0) {
- if (shenpi.contract === shenpiConst.sp_status.gdspl) {
- throw '请联系管理员添加审批人';
- } else {
- throw '请先选择审批人,再上报数据';
- }
- }
- const transaction = await this.db.beginTransaction();
- try {
- const begin_time = new Date();
- const data = {
- spid: options.spid || null,
- tid: options.tid || null,
- cid,
- cpid,
- aid: this.ctx.session.sessionUser.accountId,
- times,
- order: 0,
- status: auditConst.status.checked,
- begin_time,
- end_time: begin_time,
- audit_order: 0,
- };
- const result = await transaction.insert(this.tableName, data);
- const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time }; });
- await transaction.updateRows(this.tableName, updateData);
- if (cpid) {
- await transaction.update(this.ctx.service.contractPay.tableName, {
- id: cpid, status: auditConst.status.checking,
- });
- } else {
- await transaction.update(this.ctx.service.contract.tableName, {
- id: cid, status: auditConst.status.checking,
- });
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 获取审核人需要审核的期列表
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditList(options, auditorId) {
- const optionsSql = options.spid ? 'c.spid = "' + options.spid + '"' : 'c.tid = ' + options.tid;
- const sql = 'SELECT c.`id`, c.`uid`, c.`status`' +
- ' FROM ?? AS c LEFT JOIN ?? AS ca ON c.`id` = ca.`cid`' +
- ' WHERE ' + optionsSql + ' AND ca.`aid` = ?';
- const sqlParam = [this.ctx.service.contract.tableName, this.tableName, auditorId];
- const result = await this.db.query(sql, sqlParam);
- // 过滤result中存在重复sid的值, 保留最新的一条
- const filterResult = [];
- const idArr = [];
- for (const r of result) {
- if (idArr.indexOf(r.id) === -1 && r.status !== auditConst.status.uncheck) {
- filterResult.push(r);
- idArr.push(r.id);
- }
- }
- return filterResult;
- }
- /**
- * 获取审核人审核的次数
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getCountByChecked(auditorId, spid = '') {
- if (spid) {
- const sql = 'SELECT count(*) AS count FROM ?? AS a LEFT JOIN ?? AS t ON a.tid = t.id WHERE a.`aid` = ? AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') AND t.`spid` = ?';
- const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.count ? result.count : 0;
- }
- return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
- }
- /**
- * 获取最近一次审批结束时间
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getLastEndTimeByChecked(auditorId, spid = '') {
- const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
- const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
- 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
- ' ORDER BY a.`end_time` DESC';
- const sqlParam = [this.tableName, this.ctx.service.tender.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.changePlan.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(contract, checkData) {
- if (!this._.includes([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre], checkData.checkType)) {
- throw '提交数据错误';
- }
- const pid = this.ctx.session.sessionProject.id;
- switch (checkData.checkType) {
- case auditConst.status.checked:
- await this._checked(pid, contract, checkData);
- break;
- case auditConst.status.checkNo:
- await this._checkNo(pid, contract, checkData);
- break;
- case auditConst.status.checkNoPre:
- await this._checkNoPre(pid, contract, checkData);
- break;
- default:
- throw '无效审批操作';
- }
- }
- async _checked(pid, contract, checkData) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
- // 整理当前流程审核人状态更新
- if (contract.curAuditorIds.length === 0) throw '审核数据错误';
- if (!this._.includes(contract.curAuditorIds, accountId)) throw '当前标段您无权审批';
- const flowAudits = contract.flowAuditors;
- const selfAudit = this._.find(flowAudits, { aid: accountId });
- const nextAudits = contract.nextAuditors;
- 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 = [];
- // const auditors = await this.getAuditorsWithOwner(cpId, times);
- // auditors.forEach(audit => {
- // records.push({ pid, tid: selfAudit.tid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
- // });
- // await transaction.insert('zh_notice', records);
- if (contract.curAuditors.length === 1 || selfAudit.audit_type !== auditType.key.and) {
- // 或签更新他人审批状态
- if (selfAudit.audit_type === auditType.key.or) {
- const updateOther = [];
- for (const audit of flowAudits) {
- 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(tableName, {
- id: contract.id, status: auditConst.status.checking,
- });
- } else {
- // 本期结束
- // 生成截止本期数据 final数据
- // 同步 期信息
- const final_auditor_str = flowAudits[0].audit_type === auditType.key.common
- ? flowAudits[0].name + (flowAudits[0].role ? '-' + flowAudits[0].role : '')
- : this.ctx.helper.transFormToChinese(flowAudits[0].audit_order) + '审';
- await transaction.update(tableName, {
- id: contract.id, status: checkData.checkType, final_auditor_str,
- });
- }
- } else {
- await transaction.update(tableName, {
- id: contract.id, status: auditConst.status.checking,
- });
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async _checkNo(pid, contract, checkData) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
- const audits = contract.curAuditors;
- if (audits.length === 0) throw '审核数据错误';
- const selfAudit = audits.find(x => { return x.aid === accountId; });
- if (!selfAudit) throw '当前标段您无权审批';
- const auditors = await this.getUniqAuditor(contract.cid || contract.id, contract.cid ? contract.id : null, contract.times, true); // 全部参与的审批人
- const newAuditors = auditors.map(x => {
- return {
- aid: x.aid, spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null,
- times: contract.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, tid: selfAudit.tid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
- // auditors.forEach(audit => {
- // records.push({ pid, tid: selfAudit.tid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
- // });
- // await transaction.insert(this.ctx.service.noticePush.tableName, records);
- // 同步期信息
- await transaction.update(tableName, {
- id: contract.id, status: checkData.checkType, times: contract.times + 1,
- });
- // 拷贝新一次审核流程列表
- await transaction.insert(this.tableName, newAuditors);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 审批退回到上一个审批人
- * @param {Number} pid 项目id
- * @param {int} postData - 表单提交的数据
- * @param {int} changeData - 变更令的数据
- * @return {void}
- */
- async _checkNoPre(pid, contract, checkData) {
- const accountId = this.ctx.session.sessionUser.accountId;
- const time = new Date();
- const tableName = contract.cid !== undefined ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
- // 整理当前流程审核人状态更新
- const audits = contract.curAuditors;
- if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
- const selfAudit = audits.find(x => { return x.aid === accountId; });
- if (!selfAudit) throw '当前标段您无权审批';
- const flowAudits = contract.flowAuditors;
- // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
- // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
- const auditors2 = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, contract.times);
- const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
- const transaction = await this.db.beginTransaction();
- try {
- // 添加通知
- // const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, inspection.id, selfAudit.aid, checkData.opinion);
- // const defaultNoticeRecord = {
- // pid,
- // tid: selfAudit.tid,
- // type: pushType.inspection,
- // status: auditConst.status.checkNoPre,
- // content: noticeContent,
- // };
- // const records = [
- // {
- // uid: inspection.uid,
- // ...defaultNoticeRecord
- // },
- // ];
- // auditors2.forEach(audit => {
- // records.push({
- // uid: audit.aid,
- // ...defaultNoticeRecord
- // });
- // });
- // await transaction.insert('zh_notice', records);
- // 更新同一流程所有审批人状态
- const updateData = [];
- for (const audit of audits) {
- if (audit.aid === selfAudit.aid) {
- updateData.push({
- id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
- });
- } else {
- updateData.push({
- id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
- });
- }
- }
- await transaction.updateRows(this.tableName, updateData);
- // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
- // 顺移其后审核人流程顺序
- const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cid = ? AND cpid' + (contract.cid !== undefined ? ' = ' + contract.id : ' is null') + ' AND times = ? AND `order` > ?';
- await transaction.query(sql, [contract.cid ? contract.cid : contract.id, selfAudit.times, selfAudit.order]);
- // 上一审批人,当前审批人 再次添加至流程
- const newAuditors = [];
- preAuditors.forEach(x => {
- newAuditors.push({
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null, aid: x.aid,
- times: x.times, order: selfAudit.order + 1,
- status: auditConst.status.checking, begin_time: time,
- audit_type: x.audit_type, audit_order: x.audit_order,
- });
- });
- // 获取ids值
- const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
- // 获取刚批量添加的所有list
- for (let j = 0; j < preAuditors.length; j++) {
- newAuditors[j].id = newAuditors_result.insertId + j;
- }
- const newFlowAuditors = [];
- flowAudits.forEach(x => {
- newFlowAuditors.push({
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid ? contract.cid : contract.id, cpid: contract.cid ? contract.id : null, aid: x.aid,
- times: x.times, order: selfAudit.order + 2,
- status: auditConst.status.uncheck,
- audit_type: x.audit_type, audit_order: x.audit_order,
- });
- });
- await transaction.insert(this.tableName, newFlowAuditors);
- // 同步 期信息
- await transaction.update(tableName, {
- id: contract.id,
- status: checkData.checkType,
- });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async getAuditorGroup(cid, cpid, times) {
- const auditors = await this.getAuditors(cid, cpid, times); // 全部参与的审批人
- return this.ctx.helper.groupAuditors(auditors, 'order');
- }
- async getUserGroup(cid, cpid, times) {
- const group = await this.getAuditorGroup(cid, 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.changePlan.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(cid, cpid, times) {
- const group = await this.getAuditorGroup(cid, 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.changePlan.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(cid, cpid, times, reverse = false) {
- const history = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- const auditors = await this.getAuditors(cid, 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(cid, cpid, times, noYb = false) {
- const auditors = await this.getAuditors(cid, cpid, times, 'asc', noYb); // 全部参与的审批人
- 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 makeAudits(transaction, options, cid, cpid = null, uid) {
- // 创建审批人列表
- const lastContractInfo = await this.getHaveAuditLastInfo(options, cid, cpid, uid);
- if (lastContractInfo) {
- // 再获取非原报审批人
- const auditList = await this.getUniqAuditor(lastContractInfo.cid ? lastContractInfo.cid : lastContractInfo.id, lastContractInfo.cid ? lastContractInfo.id : null, lastContractInfo.times, true); // 全部参与的审批人
- if (auditList.length > 0) {
- const spAudits = [];
- for (const x of auditList) {
- if (x.audit_order !== 0) {
- spAudits.push({
- spid: options.spid || null,
- tid: options.tid || null,
- cid, cpid, aid: x.aid,
- times: 1, order: x.audit_order, status: auditConst.status.uncheck,
- audit_type: x.audit_type, audit_order: x.audit_order,
- });
- }
- }
- if (spAudits.length > 0) await transaction.insert(this.ctx.service.contractSpAudit.tableName, spAudits);
- }
- }
- }
- async getHaveAuditLastInfo(options, cid, cpid, uid) {
- const optionsSql = options.spid ? 'c.spid = "' + options.spid + '"' : 'c.tid = ' + options.tid;
- const tableName = cpid ? this.ctx.service.contractPay.tableName : this.ctx.service.contract.tableName;
- const joinSql = cpid ? 'ca.cpid' : 'ca.cid';
- const cpidSql = cpid ? 'AND ca.`cpid` is not null' : 'AND ca.`cpid` is null';
- const sql = 'SELECT c.* FROM ?? as c LEFT JOIN ?? as ca ON c.`id` = ' + joinSql + ' WHERE ' + optionsSql + ' AND c.`contract_type` = ? AND c.`uid` = ? ' + cpidSql + ' AND ca.`audit_order` > 0 ORDER BY c.`create_time` DESC';
- const sqlParam = [tableName, this.tableName, options.contract_type, uid];
- return await this.db.queryOne(sql, sqlParam);
- }
- async saveAudit(contract, times, sp_group, data) {
- const transaction = await this.db.beginTransaction();
- try {
- const auditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, 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 '该审核人已存在,请勿重复添加';
- const groupAuditors = this._.flattenDeep(contract.userGroups);
- const exist = this._.find(groupAuditors, { 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 = {
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, 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, contract.cid || contract.id, contract.cid ? contract.id : null, 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 = {
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, 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, { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times, aid: now_audit.aid, order: now_audit.order });
- if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, contract.cid || contract.id, contract.cid ? contract.id : null, 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({ cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, 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.contract_tender) {
- const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(this.ctx.contract.id);
- if (tenderInfo.shenpi.contract === shenpiConst.sp_status.gdspl) {
- const newAuditors = await transaction.select(this.tableName, { where: { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times }, orders: [['order', 'asc']] });
- newAuditors.shift();
- const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order');
- const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
- await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.contract.id, tenderInfo.shenpi.contract, shenpiConst.sp_type.contract, uniqNewAuditorGroup, sp_group);
- } else if (tenderInfo.shenpi.contract === shenpiConst.sp_status.gdzs) {
- const newAuditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, times, transaction);
- await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.contract.id, tenderInfo.shenpi.contract, shenpiConst.sp_type.contract, this._.map(newAuditors, 'aid'));
- }
- } else {
- if (this.ctx.subProject.shenpi.contract === shenpiConst.sp_status.gdspl) {
- const newAuditors = await transaction.select(this.tableName, { where: { cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, times }, orders: [['order', 'asc']] });
- newAuditors.shift();
- const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order');
- const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
- await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.subProject.id, this.ctx.subProject.shenpi.contract, shenpiConst.sp_type.contract, uniqNewAuditorGroup, sp_group);
- } else if (this.ctx.subProject.shenpi.contract === shenpiConst.sp_status.gdzs) {
- const newAuditors = await this.getAuditGroupByList(contract.cid || contract.id, contract.cid ? contract.id : null, times, transaction);
- await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.subProject.id, this.ctx.subProject.shenpi.contract, shenpiConst.sp_type.contract, this._.map(newAuditors, 'aid'));
- }
- }
- // 更新到审批流程方法
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async changeSpGroup(contract, sp_group) {
- const transaction = await this.db.beginTransaction();
- try {
- const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
- if (!group) {
- throw '该固定审批组不存在,请刷新页面重新获取';
- }
- const condition = { sp_type: shenpiConst.sp_type.contract, sp_status: shenpiConst.sp_status.gdspl, sp_group: group.id };
- if (this.ctx.contract_tender) {
- condition.tid = contract.tid;
- } else {
- condition.spid = contract.spid;
- }
- const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: condition });
- // await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
- await this.updateNewAuditors(contract, shenpiList, transaction);
- if (contract.cid === undefined) {
- await transaction.update(this.ctx.service.contract.tableName, { id: contract.id, sp_group: group.id });
- } else {
- await transaction.update(this.ctx.service.contractPay.tableName, { id: contract.id, sp_group: group.id });
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async updateNewAuditList(contract, newList) {
- const transaction = await this.db.beginTransaction();
- try {
- await this.updateNewAuditors(contract, newList, transaction);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateNewAuditors(contract, newList, transaction) {
- const condition = { times: contract.times };
- if (contract.cid !== undefined) {
- condition.cid = contract.cid;
- condition.cpid = contract.id;
- } else {
- condition.cid = contract.id;
- condition.cpid = null;
- }
- // 先删除旧的审批流,再添加新的
- await transaction.delete(this.tableName, condition);
- const newAuditors = [];
- for (const auditor of newList) {
- newAuditors.push({
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: auditor.audit_id,
- times: contract.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);
- // 增加到权限表
- const options = {};
- if (contract.spid) options.spid = contract.spid;
- if (contract.tid) options.tid = contract.tid;
- const auditorIds = this._.map(newAuditors, 'aid');
- const list = [];
- for (const aid of auditorIds) {
- list.push({ id: aid });
- }
- await this.ctx.service.contractAudit.saveAudits(options, list, transaction);
- }
- }
- async updateLastAudit(contract, 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) {
- const condition = { times: contract.times, aid: lastId };
- if (contract.cid !== undefined) {
- condition.cid = contract.cid;
- condition.cpid = contract.id;
- } else {
- condition.cid = contract.id;
- condition.cpid = null;
- }
- await transaction.delete(this.tableName, condition);
- 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(this.tableName, updateData);
- }
- order = order - 1;
- }
- }
- // 添加终审
- const newAuditor = {
- spid: contract.spid || null, tid: contract.tid || null, cid: contract.cid || contract.id, cpid: contract.cid ? contract.id : null, aid: lastId,
- times: contract.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;
- }
- }
- /**
- * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- // async getDonesByAudit(auditorId, spid = '') {
- // const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
- // const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre];
- // const sql =
- // 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`cid`, la.`cpid`, t.`id`, cp.`code`, t.`name`, t.`spid` ' +
- // ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS cp ON la.`cpid` = cp.`id`' +
- // ' WHERE la.`aid` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) + ')' + spSql +
- // ' ORDER BY la.`end_time` DESC';
- // const sqlParam = [
- // this.tableName,
- // this.ctx.service.tender.tableName,
- // this.ctx.service.contract.tableName,
- // auditorId,
- // ];
- // return await this.db.query(sql, sqlParam);
- // }
- }
- return ContractSpAudit;
- };
|