'use strict'; /** * * * @author Mai * @date 2018/8/14 * @version */ const auditConst = require('../const/audit').flow; const pushType = require('../const/audit').pushType; 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'); module.exports = app => { class ChangeAudit extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'change_audit'; } /** * 获取最后一位审批人 * @param {int} cid - 变更令id * @param {int} times - 次数 * @param {int} site - 位置 * @param {int} status - 状态 * @return {void} */ async getLastUser(cid, times, site = 1, status = 1) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('cid', { value: this.db.escape(cid), operate: '=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '=', }); if (status === 1) { this.sqlBuilder.setAndWhere('status', { value: 1, operate: '!=', }); } if (site === 0) { this.sqlBuilder.setAndWhere('usite', { value: site, operate: '=', }); } const u_sort = [['usort', 'DESC']]; this.sqlBuilder.orderBy = u_sort; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const data = await this.db.queryOne(sql, sqlParam); return data; } /** * 获取最后一位退回的审批人 * @param {int} cid - 变更令id * @param {int} times - 次数 * @return {void} */ async getLastBackUser(cid, times) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('cid', { value: this.db.escape(cid), operate: '=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '=', }); this.sqlBuilder.setAndWhere('status', { value: 6, operate: '=', }); const u_sort = [['usort', 'DESC']]; this.sqlBuilder.orderBy = u_sort; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const data = await this.db.queryOne(sql, sqlParam); return data; } /** * 获取当前审批人查看info时的状态 * @param {Object} change - 变更令数据 * @return {void} */ async getStatusByChange(change) { const statusConst = auditConst.status; const auditStatusConst = auditConst.auditStatus; const uid = this.ctx.session.sessionUser.accountId; const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }); if (!change.status === statusConst.checked && (changeAuditInfo === null || changeAuditInfo[0] === undefined) && !ctx.tender.isTourist) { // 无权限查看此变更令 return 0; } if (change.status === statusConst.uncheck && uid === change.uid) { // 待上报 return 1; } else if (change.status === statusConst.back && uid === change.uid) { // 待重新上报 return 2; } else if (change.status === statusConst.revise && uid === change.uid) { // 修订上报 return 9; } else if ((change.status === statusConst.back || change.status === statusConst.revise) && uid !== change.uid) { // 被退回或修订中但你不是原报人 return 3; } else if (change.status === statusConst.checked) { // 已完成 return 4; } else if (change.status === statusConst.checkNo) { // 已终止 return 5; } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status === auditStatusConst.checking) { // 待你审批 return 6; } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo.length > 0 && changeAuditInfo[0].status !== auditStatusConst.checking) { // 审批中但你未到你审批或你已审批 return 7; } else if (this.ctx.tender.isTourist) { // 游客模式,只预览不能操作 return 8; } // 无权限查看此变更令 return 0; } /** * 根据用户查看此变更状态获取审批人列表 * @param {Object} change - 变更令 * @param {int} status - 状态 * @return {object} list - 列表 */ async getListByStatus(change, status) { let sql = ''; let sqlParam = ''; switch (status) { case 1:// 待上报 case 2:// 待重新上报 case 9:// 待修订 sql = 'SELECT * FROM ?? WHERE ' + 'cid = ? AND times = ? GROUP BY usite ORDER BY usort asc'; sqlParam = [this.tableName, change.cid, change.times]; break; case 3: // 被退回但你不是原报人 case 4:// 已完成 case 5:// 已终止 case 7:// 审批中但你未到你审批或你已审批 case 8:// 游客 // 获取完整的审批顺序 sql = 'SELECT * FROM ?? WHERE ' + 'cid = ? ORDER BY usort'; sqlParam = [this.tableName, change.cid]; break; case 6: // 审批中 sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' + 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' + 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite asc'; sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times]; // sql = 'SELECT * FROM ?? WHERE ' + // 'cid = ? AND times = ? ORDER BY usort'; // sqlParam = [this.tableName, change.cid, change.times]; break; default:// 无权限查看此变更令 break; } const list = await this.db.query(sql, sqlParam); return list; } /** * 删除变更令审批人列表 * @param {Object} transaction - 事务 * @param {Object} cid - 变更令id * @param {int} times - 次数 * @return {object} 返回结果 */ async deleteAuditData(transaction, cid, times) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('cid', { value: this.db.escape(cid), operate: '=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '=', }); this.sqlBuilder.setAndWhere('usite', { value: 0, operate: '!=', }); const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete'); const result = await transaction.query(sql, sqlParam); return result; } /** * 获取不重复列表 * @param {Object} cid - 变更令id * @param {int} times - 次数 * @return {object} 返回结果 */ async getListGroupByTimes(cid, times) { const sql = 'SELECT * FROM ?? where id in (SELECT MAX(id) FROM ?? WHERE ' + 'cid = ? AND times = ? GROUP BY usite) ORDER BY usite asc'; const sqlParam = [this.tableName, this.tableName, cid, times]; const list = await this.db.query(sql, sqlParam); return list; } /** * 获取不重复列表 * @param {Object} cid - 变更令id * @param {int} times - 次数 * @return {object} 返回结果 */ async getListGroupByWithoutYB(cid, times, transaction = null) { const sql = 'SELECT * FROM ?? where cid = ? AND times = ? AND usite != 0 AND usort in (SELECT MAX(usort) FROM ?? WHERE ' + 'cid = ? AND times = ? AND usite != 0 GROUP BY usite) ORDER BY usite asc'; const sqlParam = [this.tableName, cid, times, this.tableName, cid, times]; const list = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam); return list; } async getListOrderByTimes(cid, times) { const sql = 'SELECT * FROM ?? WHERE ' + 'cid = ? AND times = ? ORDER BY usort'; const sqlParam = [this.tableName, cid, times]; const list = await this.db.query(sql, sqlParam); return list; } async getListGroupByTimesWithDetail(cid, times) { const sql = 'SELECT *, pa.name, pa.company, pa.role, pa.mobile, pa.telephone FROM ' + this.tableName + ' ca ' + ' Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On ca.uid = pa.id' + ' where id in (SELECT MAX(id) FROM ' + this.tableName +' WHERE cid = ? AND times = ? GROUP BY usite)' + ' ORDER BY usite asc'; const sqlParam = [cid, times]; const list = await this.db.query(sql, sqlParam); return list; } /** * 获取比sort大的审批人列表 * @param {Object} cid - 变更令id * @param {int} usort - 排序 * @return {object} 返回结果 */ async getNextAuditList(cid, usort) { const sql = 'SELECT * FROM ?? WHERE ' + 'cid = ? AND usort > ?'; const sqlParam = [this.tableName, cid, usort]; const list = await this.db.query(sql, sqlParam); return list; } /** * 获取除当前次数的审批人列表 * @param {Object} cid - 变更令id * @param {int} times - 次数 * @return {object} 返回结果 */ async getListByBack(cid, times) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('cid', { value: this.db.escape(cid), operate: '=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '!=', }); this.sqlBuilder.orderBy = [['usort', 'ASC']]; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const result = await this.db.query(sql, sqlParam); return result; } /** * 获取 审核人 待审批的() 变更令列表 * @param uid * @return {Promise} */ async getAuditChange(uid) { const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' + ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, ' + ' t.`name`, t.`type`, t.`user_id` ' + ' FROM ?? AS ca, ?? AS c, ?? As t ' + ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ?' + ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` ORDER BY ca.`sin_time` DESC'; const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2, auditConst.status.uncheck]; const changes = await this.db.query(sql, sqlParam); for (const c of changes) { if (c.cstatus === auditConst.status.back) { const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' + ' WHERE ca.cid = ? and ca.times = ? and ca.status = ? and ca.uid = pa.id'; const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times - 1, c.cstatus]; c.pre = await this.db.queryOne(preSql, preSqlParam); } else { const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' + ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id'; const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1]; c.pre = await this.db.queryOne(preSql, preSqlParam); } } return changes; } /** * 获取审核人审核的次数 * * @param auditorId * @return {Promise<*>} */ async getCountByChecked(auditorId) { const sql = 'Select count(*) as count FROM ?? WHERE uid = ? AND usite != 0 AND status in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.backnew]) +')'; const sqlParam = [this.tableName, auditorId]; 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) { const sql = 'SELECT `sin_time` FROM ?? WHERE `uid` = ? AND usite != 0 ' + 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.backnew]) + ') ORDER BY `sin_time` DESC'; const sqlParam = [this.tableName, auditorId]; const result = await this.db.queryOne(sql, sqlParam); return result ? result.sin_time : null; } /** * 获取 某时间后 审批进度更新的 变更令 * @param {Number} pid - 查询标段 * @param {Number} uid - 查询人 * @param {Date} time - 查询时间 * @return {Promise<*>} */ async getNoticeChange(pid, uid, time) { // const sql = 'SELECT * FROM (SELECT t.`id`, t.`name`, t.`type`, t.`user_id`, ' + // ' ca.`cid`, c.`code` As `c_code`, c.name As `c_name`, ' + // ' ca.`uid`, ca.`sin_time` As `cu_time`, ca.`status` As `cu_status`, ca.`name` As `cu_name`, ca.`jobs` As `cu_jobs`, ca.company As `cu_company`' + // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `uid` = ? GROUP BY `tid`)) As t' + // ' LEFT JOIN ?? As c ON c.`tid` = t.`id`' + // ' LEFT JOIN ?? As ca ON ca.`cid` = c.`cid`' + // ' WHERE t.`project_id` = ? and `ca`.`sin_time` > ? and `ca`.`usite` != 0 and `ca`.`status` != ?' + // ' ORDER By ca.`sin_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' + // ' ORDER BY new_t.`cu_time`'; // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.change.tableName, this.tableName, pid, time, auditConst.status.checking]; // return await this.db.query(sql, sqlParam); let notice = await this.db.select('zh_notice', { where: { pid, type: pushType.change, uid }, orders: [['create_time', 'desc']], limit: 10, offset: 0, }); notice = notice.map(v => { const extra = JSON.parse(v.content); delete v.content; return { ...v, ...extra }; }); return notice; } async getAllAuditors(tenderId) { const sql = 'SELECT ca.uid, ca.tid FROM ' + this.tableName + ' ca' + ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ca.tid = t.id' + ' WHERE t.id = ?' + ' GROUP BY ca.uid'; const sqlParam = [tenderId]; return this.db.query(sql, sqlParam); } /** * 取待审批变更列表(wap用) * * @param auditorId * @return {Promise<*>} */ async getAuditChangeByWap(uid) { const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' + ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, c.`quality`, c.`total_price`,' + ' t.`name`, t.`type`, t.`user_id`, ' + ' ti.`deal_info`, ti.`decimal` ' + ' FROM ?? AS ca, ?? AS c, ?? As t, ?? AS ti ' + ' WHERE ca.`uid` = ? and ca.`status` = ? and (c.`status` = ? or c.`status` = ?)' + ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` and ti.`tid` = t.`id`'; const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, uid, auditConst.auditStatus.checking, auditConst.status.checking, auditConst.status.backnew]; const changes = await this.db.query(sql, sqlParam); for (const c of changes) { const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' + ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id'; const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1]; c.pre = await this.db.queryOne(preSql, preSqlParam); } return await this.db.query(sql, sqlParam); } async updateNewAuditList(change, newIdList) { const transaction = await this.db.beginTransaction(); try { // 先删除旧的审批流(除了原报),再添加新的 const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `usite` != 0'; const sqlParam = [this.tableName, change.cid, change.times]; await transaction.query(sql, sqlParam); const newAuditors = []; let order = 1; let uSort = await transaction.count(this.tableName, { cid: change.cid }); for (const aid of newIdList) { const accountInfo = await this.ctx.service.projectAccount.getDataById(aid); newAuditors.push({ tid: change.tid, cid: change.cid, uid: aid, name: accountInfo.name, jobs: accountInfo.role, company: accountInfo.company, times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck, }); order++; uSort++; } if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async updateLastAudit(change, auditList, lastId) { const transaction = await this.db.beginTransaction(); try { // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order const idList = this._.map(auditList, 'uid'); let order = idList.length + 1; if (idList.indexOf(lastId) !== -1) { const sql = 'DELETE FROM ?? WHERE `cid`= ? AND `times` = ? AND `uid` = ? AND `usite` != 0'; const sqlParam = [this.tableName, change.cid, change.times, lastId]; await transaction.query(sql, sqlParam); // await transaction.delete(this.tableName, { cid: change.cid, times: change.times, uid: lastId, usite: 0 }); const user = this._.find(auditList, { 'uid': lastId }); // 顺移之后审核人流程顺序 await this._syncOrderByDelete(transaction, change.cid, user.usite, user.usort, change.times); order = order - 1; } // 添加终审 const userInfo = await this.ctx.service.projectAccount.getDataById(lastId); let uSort = await transaction.count(this.tableName, { cid: change.cid }); const newAuditor = { tid: change.tid, cid: change.cid, uid: lastId, name: userInfo.name, jobs: userInfo.role, company: userInfo.company, times: change.times, usite: order, usort: uSort, status: auditConst.auditStatus.uncheck, }; await transaction.insert(this.tableName, newAuditor); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } /** * 移除审核人时,同步其后审核人order * @param transaction - 事务 * @param {Number} changeId - 变更令id * @param {Number} usite - 审核人id * @param {Number} usort - 审核人id * @param {Number} times - 第几次审批 * @return {Promise<*>} * @private */ async _syncOrderByDelete(transaction, changeId, usite, usort, times, selfOperate = '-') { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('cid', { value: this.db.escape(changeId), operate: '=', }); this.sqlBuilder.setAndWhere('usite', { value: usite, operate: '>=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '=', }); this.sqlBuilder.setUpdateData('usite', { value: 1, selfOperate: selfOperate, }); this.sqlBuilder.setUpdateData('usort', { value: 1, selfOperate: selfOperate, }); const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update'); const data = await transaction.query(sql, sqlParam); return data; } /** * 获取 当前审核人 * * @param {Number} cid - 期id * @param {Number} times - 第几次审批 * @return {Promise<*>} */ async getCurAuditor(cid, times = 1) { const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `status` = ? and `times` = ? and usite != 0'; const sqlParam = [this.tableName, cid, auditConst.status.checking, times]; return await this.db.queryOne(sql, sqlParam); } /** * 获取 审核人列表(除去原报) * * @param {Number} cid - 变更id * @param {Number} times - 第几次审批 * @param {Boolean} includeOR - 是否包含原报 * @return {Promise<*>} */ async getAuditors(cid, times = 1, includeOR = false) { const sql = `SELECT * FROM ?? WHERE cid = ? and times = ?${includeOR ? '' : ' and usite != 0'}`; // const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and usite != 0'; const sqlParam = [this.tableName, cid, times]; return await this.db.query(sql, sqlParam); } /** * 新增审核人 * * @param {Number} cid - 变更令id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise} */ async addAuditor(cid, auditorId, times = 1, is_gdzs = 0) { const transaction = await this.db.beginTransaction(); try { let newOrder = await transaction.count(this.tableName, { cid, times }); let uSort = await transaction.count(this.tableName, { cid }); // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1 newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder; uSort = is_gdzs === 1 ? uSort - 1 : uSort; if (is_gdzs) await this._syncOrderByDelete(transaction, cid, newOrder, uSort, times, '+'); const userInfo = await this.ctx.service.projectAccount.getDataById(auditorId); const newAuditor = { tid: this.ctx.tender.id, cid: cid, uid: auditorId, name: userInfo.name, jobs: userInfo.role, company: userInfo.company, times: times, usite: newOrder, usort: uSort, status: auditConst.auditStatus.uncheck, }; const result = await transaction.insert(this.tableName, newAuditor); await transaction.commit(); return result.effectRows = 1; } catch (err) { await transaction.rollback(); throw err; } return false; } /** * 移除审核人 * * @param {Number} cid - 变更令id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise} */ async deleteAuditor(cid, auditorId, times = 1) { const transaction = await this.db.beginTransaction(); try { const sql = 'SELECT * FROM ?? WHERE `cid` = ? and `times` = ? and `uid` = ? and `usite` != 0 ORDER BY `usort` DESC'; const sqlParam = [this.tableName, cid, times, auditorId]; const auditor = await transaction.queryOne(sql, sqlParam); if (!auditor) { throw '该审核人不存在'; } await this._syncOrderByDelete(transaction, cid, auditor.usite, auditor.usort, times); await transaction.delete(this.tableName, { id: auditor.id }); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } /** * 开始审批 * @param {Number} cid - 变更令id * @param {Number} times - 第几次审批 * @return {Promise} */ async start(cid, times = 1) { const audit = await this.getDataByCondition({ cid, times, usite: 1 }); const yBAudit = await this.getDataByCondition({ cid, times, usite: 0 }); if (!audit) { if(this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) { throw '请联系管理员添加审批人'; } else { throw '请先选择审批人,再上报数据'; } } const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, { id: audit.id, status: auditConst.auditStatus.checking, sin_time: new Date() }); // 更新原报人审批状态 await transaction.update(this.tableName, { id: yBAudit.id, status: auditConst.auditStatus.checked, sin_time: new Date(), }); const changeList = await this.ctx.service.changeAuditList.getList(cid); let total_price = 0; // 更新清单spamount的值 const updateListData = []; for (const cl of changeList) { total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, this.ctx.tender.info.decimal.tp)); if(cl.camount !== cl.spamount) { const uld = { id: cl.id, spamount: cl.camount, }; updateListData.push(uld); } } if(updateListData.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateListData); const options = { where: { cid: cid, }, }; const updateData = { total_price, tp_decimal: this.ctx.tender.info.decimal.tp, up_decimal: this.ctx.tender.info.decimal.up, status: auditConst.status.checking, }; await transaction.update(this.ctx.service.change.tableName, updateData, options); // 清空audit_amount if(times > 1) await transaction.update(this.ctx.service.changeAuditList.tableName, { audit_amount: null }, { where: { cid: cid } }); // 添加短信通知-需要审批提醒功能 const sms = new SMS(this.ctx); const code = await sms.contentChange(this.ctx.change.code); const shenpiUrl = await this.ctx.helper.urlToShort( this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.change.tid + '/change/' + cid + '/information#shenpi' ); await this.ctx.helper.sendAliSms(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, { biangeng: code, code: shenpiUrl, }); // 微信模板通知 const wechatData = { wap_url: shenpiUrl, 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(audit.uid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData); // 重新发送配置 await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, { pid: this.ctx.session.sessionProject.id, tid: this.ctx.change.tid, uid: audit.uid, sp_type: 'change', sp_id: audit.id, table_name: this.tableName, template: wxConst.template.change, wx_data: wechatData, }); await transaction.delete(this.ctx.service.changeHistory.tableName, { cid }); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } async getNumByMonth(tid, startMonth, endMonth) { const sql = 'SELECT COUNT(*) as num FROM ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? AND usite != 0 GROUP BY id ORDER BY usort DESC) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.sin_time between ? and ?'; // const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT b.id FROM (SELECT * FROM ?? WHERE tid = ? AND usite != 0 GROUP BY id ORDER BY usort DESC) as b GROUP BY b.cid) AND status = ? AND sin_time between ? and ?'; const sqlParam = [this.tableName, this.tableName, tid, auditConst.auditStatus.checked, startMonth, endMonth]; const result = await this.db.queryOne(sql, sqlParam); return result ? result.num : 0; } /** * 审批撤回 * @param {Number} stageId - 标段id * @param {Number} times - 第几次审批 * @return {Promise} */ async checkCancel(change) { // 分4种情况,根据ctx.cancancel值判断: // 1.原报发起撤回,当前流程删除,并回到待上报 // 2.审批人撤回审批通过,增加流程,并回到它审批中 // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中 // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中 switch (change.cancancel) { case 1: await this._userCheckCancel(change); break; case 2: await this._auditCheckCancel(change); break; case 3: await this._auditCheckCancelNoPre(change); break; case 4: await this._auditCheckCancelNo(change); break; default: throw '不可撤回,请刷新页面重试'; } // if (stage.cancancel === 5) { // await this._auditCheckCancelAnd(stage); // } else { // switch (this.ctx.stage.cancancel) { // case 1: await this._userCheckCancel(stage); break; // case 2: await this._auditCheckCancel(stage); break; // case 3: await this._auditCheckCancelNoPre(stage); break; // case 4: await this._auditCheckCancelNo(stage); break; // default: throw '不可撤回,请刷新页面重试'; // } // // 通知发送 - 第三方更新 // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) { // const base_data = { // tid: stage.tid, // sid: stage.id, // op: 'update', // }; // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data)); // base_data.op = 'update'; // base_data.sid = -1; // this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data)); // } // } } /** * 原报撤回,直接改动审批人状态 * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人 * * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1 * ... * * @param stage * @returns {Promise} * @private */ async _userCheckCancel(change) { const transaction = await this.db.beginTransaction(); try { // 整理当前流程审核人状态更新 const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.auditStatus.checking }); // // 审批人变成待审批状态 await transaction.update(this.tableName, { id: curAudit.id, status: auditConst.auditStatus.uncheck, sin_time: null, sdesc: null, }); await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id); // 原报变成checking状态 const ybAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, usite: 0, status: auditConst.auditStatus.checked }); await transaction.update(this.tableName, { id: ybAudit.id, status: auditConst.auditStatus.checking, sin_time: new Date(), }); // 变更令变成待上报状态 const options = { where: { cid: change.cid, }, }; await transaction.update(this.ctx.service.change.tableName, { status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back, }, options); 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} * @private */ async _auditCheckCancel(change) { const time = new Date(); const transaction = await this.db.beginTransaction(); try { // 整理当前流程审核人状态更新 const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking }); const preAudit = change.preAudit; if (!curAudit || curAudit.usite <= 1 || !preAudit) { throw '撤回用户数据错误'; } // 顺移其后审核人流程顺序 const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?'; await transaction.query(sql, [change.cid, change.times, curAudit.usort]); // 当前审批人2次添加至流程中 const newAuditors = []; // 先入撤回记录 newAuditors.push({ tid: change.tid, cid: change.cid, uid: preAudit.uid, name: preAudit.name, jobs: preAudit.jobs, company: preAudit.company, times: change.times, usite: preAudit.usite, usort: curAudit.usort, status: auditConst.auditStatus.checkCancel, sin_time: time, sdesc: '', }); newAuditors.push({ tid: change.tid, cid: change.cid, uid: preAudit.uid, name: preAudit.name, jobs: preAudit.jobs, company: preAudit.company, times: change.times, usite: preAudit.usite, usort: curAudit.usort + 1, status: auditConst.auditStatus.checking, sin_time: time, }); await transaction.insert(this.tableName, newAuditors); // 当前审批人变成待审批 await transaction.update(this.tableName, { id: curAudit.id, usort: curAudit.usort + 2, sin_time: null, status: auditConst.auditStatus.uncheck }); await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id); // 审批列表数据也要回退 const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid }, }); let total_price = 0; const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp; const updateList = []; for (const cl of changeList) { const audit_amount = cl.audit_amount.split(','); const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0; audit_amount.splice(-1, 1); const list_update = { id: cl.id, audit_amount: audit_amount.join(','), spamount: parseFloat(last_amount), }; total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tp_decimal)); updateList.push(list_update) } if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList); // 变更令变成待上报状态 const options = { where: { cid: change.cid, }, }; await transaction.update(this.ctx.service.change.tableName, { status: auditConst.status.checking, total_price, cin_time: Date.parse(time) / 1000, }, options); await transaction.commit(); } catch(err) { await transaction.rollback(); throw err; } } /** * 审批人撤回审批退回上一人,插入两条数据 * * 一审 1 A checked 一审 1 A checked * 二审 2 B checked 二审 2 B checked * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur * 四审 6 D uncheck 四审 6 D uncheck * * @param stage * @returns {Promise} * @private */ async _auditCheckCancelNoPre(change) { const time = new Date(); const transaction = await this.db.beginTransaction(); try { const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times, status: auditConst.status.checking }); const preAudit = change.preAudit; if (!curAudit || curAudit.order <= 1 || !preAudit) { throw '撤回用户数据错误'; } // 整理当前流程审核人状态更新 // 删除当前审批人 await transaction.delete(this.tableName, { id: curAudit.id }); await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id); // 添加撤回人到审批流程中 const newAuditors = []; newAuditors.push({ tid: change.tid, cid: change.cid, uid: preAudit.uid, name: preAudit.name, jobs: preAudit.jobs, company: preAudit.company, times: change.times, usite: preAudit.usite, usort: curAudit.usort, status: auditConst.auditStatus.checkCancel, sin_time: time, sdesc: '', }); await transaction.insert(this.tableName, newAuditors); // 更新上一个人,最新审批状态为审批中 await transaction.update(this.tableName, { sin_time: time, status: auditConst.status.checking }, { where: { cid: change.cid, times: change.times, usort: curAudit.usort + 1 } }); // 回退spamount值数据 const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: change.cid }, }); let total_price = 0; const tp_decimal = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp; const updateList = []; for (const cl of changeList) { const audit_amount = cl.audit_amount !== '' ? cl.audit_amount.split(',') : []; // const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0; audit_amount.push(cl.spamount); const list_update = { id: cl.id, audit_amount: audit_amount.join(','), }; total_price = this.ctx.helper.add(total_price, this.ctx.helper.mul(cl.unit_price, parseFloat(cl.spamount), tp_decimal)); updateList.push(list_update); } if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changeAuditList.tableName, updateList); // 变更令变成待上报状态 const options = { where: { cid: change.cid, }, }; await transaction.update(this.ctx.service.change.tableName, { status: auditConst.status.checking, total_price, cin_time: Date.parse(time) / 1000, }, options); 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} * @private */ async _auditCheckCancelNo(change) { const time = new Date(); const transaction = await this.db.beginTransaction(); try { // const curAudit = await this.getDataByCondition({ cid: change.cid, times: change.times - 1, status: auditConst.auditStatus.back }); const curAudit = await this.getAuditorByStatus(change.cid, change.times - 1, auditConst.auditStatus.back); // 整理上一个流程审核人状态更新 // 顺移其后审核人流程顺序 const sql = 'UPDATE ' + this.tableName + ' SET `usort` = `usort` + 2 WHERE cid = ? AND times = ? AND `usort` > ?'; await transaction.query(sql, [change.cid, change.times - 1, curAudit.usort]); // 当前审批人2次添加至流程中 const newAuditors = []; newAuditors.push({ tid: change.tid, cid: change.cid, uid: curAudit.uid, name: curAudit.name, jobs: curAudit.jobs, company: curAudit.company, times: curAudit.times, usite: curAudit.usite, usort: curAudit.usort + 1, status: auditConst.auditStatus.checkCancel, sin_time: time, sdesc: '', }); newAuditors.push({ tid: change.tid, cid: change.cid, uid: curAudit.uid, name: curAudit.name, jobs: curAudit.jobs, company: curAudit.company, times: curAudit.times, usite: curAudit.usite, usort: curAudit.usort + 2, status: auditConst.auditStatus.checking, sin_time: time, }); await transaction.insert(this.tableName, newAuditors); // 删除当前次审批流 await transaction.delete(this.tableName, { cid: change.cid, times: change.times }); // 回退数据 await this.ctx.service.changeHistory.returnHistory(transaction, change.cid); await transaction.delete(this.ctx.service.changeHistory.tableName, { cid: change.cid }); await transaction.commit(); } catch(err) { await transaction.rollback(); throw err; } } async getAuditorByStatus(cid, times, status, transaction= null) { const sql = 'SELECT * FROM ?? WHERE `cid` = ? AND `times` = ? AND `status` = ? ORDER BY `usort` DESC'; const sqlParam = [this.tableName, cid, times, status]; return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam); } async saveAudit(cid, times, data) { const transaction = await this.db.beginTransaction(); try { const auditors = await this.getListGroupByWithoutYB(cid, times); const now_audit = this._.find(auditors, { uid: data.old_aid }); if (data.operate === 'add') { if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) { throw '当前人下无法操作新增'; } const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid); const newAudit = { tid: this.ctx.tender.id, cid, uid: data.new_aid, name: nowAuditInfo.name, company: nowAuditInfo.company, jobs: nowAuditInfo.role, usort: now_audit.usort+1, usite: now_audit.usite+1, times: times, status: auditConst.auditStatus.uncheck, }; // order+1 await this._syncOrderByDelete(transaction, cid, now_audit.usite+1, now_audit.usort+1, times, '+'); await transaction.insert(this.tableName, newAudit); // 更新审批流程页数据,如果存在 } else if (data.operate === 'del') { if (now_audit.status !== auditConst.status.uncheck) { throw '当前人无法操作删除'; } await transaction.delete(this.tableName, { cid, times, uid: now_audit.uid, usite: now_audit.usite }); await this._syncOrderByDelete(transaction, cid, now_audit.usite, now_audit.usort, 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, times, uid: now_audit.uid, usite: now_audit.usite }); if (now_audit.status !== auditConst.status.uncheck || !nowAudit) { throw '当前人无法操作替换'; } const nowAuditInfo = await this.ctx.service.projectAccount.getDataById(data.new_aid); nowAudit.uid = data.new_aid; nowAudit.name = nowAuditInfo.name; nowAudit.company = nowAuditInfo.company; nowAudit.jobs = nowAuditInfo.role; 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 || this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) { const newAuditors = await this.getListGroupByWithoutYB(cid, 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, 'uid')); } // 更新到审批流程方法 await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } } return ChangeAudit; };