'use strict'; /** * * * @author Mai * @date 2018/8/14 * @version */ const auditConst = require('../const/audit').changeProject; 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 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) { 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 `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) 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; } 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` ' + ' 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); } /** * 获取审核人流程列表 * * @param auditorId * @return {Promise<*>} */ async getAuditGroupByList(changeId, times) { const 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.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times]; return await this.db.query(sql, sqlParam); } /** * 移除审核人 * * @param {Number} materialId - 材料调差期id * @param {Number} status - 期状态 * @param {Number} status - 期次数 * @return {Promise} */ 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 : 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; } /** * 获取审核人流程列表(包括原报) * @param {Number} materialId 调差id * @param {Number} times 审核次数 * @return {Promise} 查询结果集(包括原报) */ 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} */ async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) { const transaction = await this.db.beginTransaction(); let flag = false; try { let newOrder = await this.getNewOrder(cpId, times); // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1 newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder; 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, }; 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} */ async getNewOrder(cpId, times = 1) { const sql = 'SELECT Max(??) As max_order FROM ?? Where `cpid` = ? and `times` = ?'; const sqlParam = ['order', this.tableName, cpId, times]; const result = await this.db.queryOne(sql, sqlParam); return result && result.max_order ? result.max_order + 1 : 1; } /** * 移除审核人 * * @param {Number} cpId - 变更立项书id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise} */ 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 this._syncOrderByDelete(transaction, cpId, auditor.order, times); await transaction.delete(this.tableName, condition); 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, }); 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} */ async start(cpId, times = 1) { const audit = await this.getDataByCondition({ cpid: cpId, times, order: 1 }); if (!audit) { // if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) { // throw '请联系管理员添加审批人'; // } else { throw '请先选择审批人,再上报数据'; // } } const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() }); 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(audit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData); // todo 更新标段tender状态 ? 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]; return await this.db.query(sql, sqlParam); } /** * 获取审核人审核的次数 * * @param auditorId * @return {Promise<*>} */ async getCountByChecked(auditorId) { return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.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} */ 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 time = new Date(); // 整理当前流程审核人状态更新 const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking }); if (!audit) { throw '审核数据错误'; } // 获取审核人列表 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: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time }); // 获取推送必要信息 const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion); // 添加推送 const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }]; 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 (nextAudit) { // 流程至下一审批人 await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time }); // 同步 期信息 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(nextAudit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData); } else { // 本期结束 // 生成截止本期数据 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.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 transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async _back(pid, cpId, checkData, times) { const time = new Date(); // 整理当前流程审核人状态更新 const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking }); if (!audit) { throw '审核数据错误'; } 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); let order = 1; for (const a of auditors) { a.times = times + 1; a.order = order; a.status = auditConst.status.uncheck; order++; } const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time }); // 添加到消息推送表 const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.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, auditors); // 微信模板通知 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); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async _checkNo(pid, cpId, checkData, times) { const time = new Date(); // 整理当前流程审核人状态更新 const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking }); if (!audit) { throw '审核数据错误'; } // 获取审核人列表 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 { await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time }); // 获取推送必要信息 const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.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 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 auditors = await this.getAuditGroupByList(preChange.id, preChange.times); const newAuditors = []; for (const a of auditors) { const na = { tid: preChange.tid, cpid: newChange.id, aid: a.aid, times: newChange.times, order: newAuditors.length + 1, status: auditConst.status.uncheck, }; newAuditors.push(na); } const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null; return result ? result.affectedRows === auditors.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); } } return ChangeProjectAudit; };