'use strict'; /** * 质量管理 - 巡检单 * * @author Mai * @date 2024/7/22 * @version */ const auditConst = require('../const/audit').inspection; const pushType = require('../const/audit').pushType; const auditType = require('../const/audit').auditType; const shenpiConst = require('../const/shenpi'); module.exports = app => { class SafeInspectionAudit extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'safe_inspection_audit'; } /** * 获取 审核列表信息 * * @param {Number} materialId - 材料调差期id * @param {Number} times - 第几次审批 * @return {Promise<*>} */ async getAuditors(inspectionId, times = 1, order_sort = 'asc') { const sql = 'SELECT la.id, la.tid, la.aid, la.times, la.order, la.status, la.opinion, la.is_rectification, la.is_old, la.begin_time, la.end_time, la.audit_type, la.audit_order,' + ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' + ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` + ' WHERE la.qiid = ? AND la.times = ?' + ' ORDER BY la.order ' + order_sort; const sqlParam = [inspectionId, 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; } async getCurAuditors(inspectionId, times = 1) { const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' + ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' + ' WHERE la.`qiid` = ? and la.`status` = ? and la.`times` = ?'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, inspectionId, auditConst.status.checking, times]; return await this.db.query(sql, sqlParam); } /** * 获取 最新审核顺序 * * @param {Number} materialId - 材料调差期id * @param {Number} times - 第几次审批 * @return {Promise} */ async getNewOrder(inspectionId, times = 1) { const sql = 'SELECT Max(??) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `qiid` = ? and `times` = ?'; const sqlParam = ['order', this.tableName, inspectionId, 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} materialId - 材料调差期id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise} */ async addAuditor(inspectionId, auditorId, times = 1, is_gdzs = 0) { const transaction = await this.db.beginTransaction(); try { let [newOrder, newAuditOrder] = await this.getNewOrder(inspectionId, 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, inspectionId, newOrder, times, '+'); const data = { tid: this.ctx.tender.id, qiid: inspectionId, aid: auditorId, times, order: newOrder, status: auditConst.status.uncheck, audit_order: newAuditOrder, }; const result = await transaction.insert(this.tableName, data); const auditList = await this.ctx.service.tenderPermission.getPartsPermission(this.ctx.tender.id, ['safe_inspection']); const addAid = this._.includes(this._.map(auditList, 'uid'), auditorId); if (!addAid) { const insert_members = [{ uid: auditorId, inspection: ['1'], }]; await this.ctx.service.tenderPermission.saveOnePermission(this.ctx.tender.id, [auditorId], insert_members, ['safe_inspection'], transaction); } await transaction.commit(); return (result.effectRows = 1); } catch (err) { await transaction.rollback(); throw err; } return false; } /** * 移除审核人时,同步其后审核人order * @param transaction - 事务 * @param {Number} materialId - 材料调差期id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise<*>} * @private */ async _syncOrderByDelete(transaction, inspectionId, order, times, selfOperate = '-') { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('qiid', { value: inspectionId, operate: '=', }); this.sqlBuilder.setAndWhere('order', { value: order, operate: '>=', }); this.sqlBuilder.setAndWhere('times', { value: times, operate: '=', }); this.sqlBuilder.setUpdateData('order', { value: 1, selfOperate, }); this.sqlBuilder.setUpdateData('audit_order', { value: 1, selfOperate, }); const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update'); const data = await transaction.query(sql, sqlParam); return data; } /** * 移除审核人 * * @param {Number} inspectionId - 质量巡检id * @param {Number} auditorId - 审核人id * @param {Number} times - 第几次审批 * @return {Promise} */ async deleteAuditor(inspectionId, auditorId, times = 1) { const transaction = await this.db.beginTransaction(); try { const condition = { qiid: inspectionId, aid: auditorId, times }; const auditor = await this.getDataByCondition(condition); if (!auditor) { throw '该审核人不存在'; } await transaction.delete(this.tableName, { qiid: inspectionId, order: auditor.order, times }); await this._syncOrderByDelete(transaction, inspectionId, auditor.order, times); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } /** * 获取审核人需要审核的期列表 * * @param auditorId * @return {Promise<*>} */ async getAuditInspection(auditorId, spid = '') { const spSql = spid ? ' and t.`spid` = "' + spid + '"' : ''; const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`qiid`,' + ' m.`code` As `mcode`, m.`status` As `mstatus`,' + ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid` ' + ' FROM ?? AS ma' + ' LEFT JOIN ?? AS m On ma.qiid = m.id' + ' LEFT JOIN ?? As t On m.tid = t.id' + ' WHERE ((ma.`aid` = ? and (ma.`status` = ? OR ma.`status` = ?)) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql + ' ORDER BY ma.`begin_time` DESC'; const sqlParam = [this.tableName, this.ctx.service.safeInspection.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditConst.status.rectification, auditorId, auditConst.status.checkNo, auditConst.status.checkNo]; const result = await this.db.query(sql, sqlParam); // 过滤result中存在重复sid的值, 保留最新的一条 const filterResult = []; const qiidArr = []; for (const r of result) { if (qiidArr.indexOf(r.qiid) === -1) { filterResult.push(r); qiidArr.push(r.qiid); } } 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, auditConst.status.checkNoPre, auditConst.status.checkStop]) + ') 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, auditConst.status.checkNoPre, auditConst.status.checkStop] }); } /** * 获取最近一次审批结束时间 * * @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, auditConst.status.checkStop]) + ')' + 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; } /** * 获取审核人流程列表 * * @param auditorId * @return {Promise<*>} */ async getAuditGroupByList(inspectionId, times) { const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`qiid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' + ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `qiid` = ? and `times` = ? and `is_old` = ? and `is_rectification` = ? 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.`qiid` = ? and la.`times` = ? and la.`is_old` = ? and la.`is_rectification` = ? order BY la.`order`'; const sqlParam = [this.tableName, inspectionId, times, 0, 0, this.tableName, this.ctx.service.projectAccount.tableName, inspectionId, times, 0, 0]; return await this.db.query(sql, sqlParam); } /** * 获取审核人流程列表(包括原报) * @param {Number} materialId 调差id * @param {Number} times 审核次数 * @return {Promise} 查询结果集(包括原报) */ async getAuditorsWithOwner(inspectionId, times = 1) { const result = await this.getAuditGroupByList(inspectionId, times); const sql = 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As qiid, 0 As `order`' + ' FROM ' + this.ctx.service.safeInspection.tableName + ' As s' + ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' + ' ON s.uid = pa.id' + ' WHERE s.id = ?'; const sqlParam = [times, inspectionId, inspectionId]; const user = await this.db.queryOne(sql, sqlParam); result.unshift(user); return result; } 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); } async updateNewAuditList(inspection, newList) { const transaction = await this.db.beginTransaction(); try { // 先删除旧的审批流,再添加新的 await transaction.delete(this.tableName, { qiid: inspection.id, times: inspection.times }); const newAuditors = []; for (const auditor of newList) { newAuditors.push({ tid: inspection.tid, qiid: inspection.id, aid: auditor.audit_id, times: inspection.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); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async updateLastAudit(inspection, 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 // const idList = this._.map(auditList, 'aid'); // let order = idList.length + 1; if (existAudit) { await transaction.delete(this.tableName, {qiid: inspection.id, times: inspection.times, aid: lastId}); const sameOrder = auditList.filter(x => { return x.order === existAudit.order }); if (sameOrder.length === 1) { const updateData = []; auditList.forEach(x => { if (x.order <= existAudit.order) return; updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1}); }); if (updateData.length > 0) { await transaction.updateRows(updateData); } order = order - 1; } } // 添加终审 const newAuditor = { tid: inspection.tid, qiid: inspection.id, aid: lastId, times: inspection.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; } } async getAuditorGroup(inspectionId, times) { const auditors = await this.getAuditors(inspectionId, times); // 全部参与的审批人 const newAuditors = auditors.filter(x => { return x.is_old === 0 && x.is_rectification === 0; }); return this.ctx.helper.groupAuditors(newAuditors); } async getUserGroup(inspectionId, times) { const group = await this.getAuditorGroup(inspectionId, times); const sql = 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As qiid, 0 As `order`, 1 As audit_type, 0 As audit_order' + ' FROM ' + this.ctx.service.safeInspection.tableName + ' As m' + ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' + ' ON m.uid = pa.id' + ' WHERE m.id = ?'; const sqlParam = [times, inspectionId, inspectionId]; const user = await this.db.queryOne(sql, sqlParam); group.unshift([user]); return group; } async getUniqUserGroup(inspectionId, times) { const group = await this.getAuditorGroup(inspectionId, times); const sql = 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As qiid, 0 As `order`, 1 As audit_type, 0 As audit_order' + ' FROM ' + this.ctx.service.safeInspection.tableName + ' As m' + ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' + ' ON m.uid = pa.id' + ' WHERE m.id = ?'; const sqlParam = [times, inspectionId, inspectionId]; const user = await this.db.queryOne(sql, sqlParam); user.audit_order = 0; group.unshift([user]); return this.ctx.helper.groupAuditorsUniq(group); } async getAuditorHistory(inspectionId, times, reverse = false) { const history = []; if (times >= 1) { for (let i = 1; i <= times; i++) { const auditors = await this.getAuditors(inspectionId, i); const group = this.ctx.helper.groupAuditors(auditors); const historyGroup = []; const max_order = group.length > 0 && group[group.length - 1].length > 0 ? (group[group.length - 1][0].is_rectification && group[group.length - 2].length > 0 ? group[group.length - 2][0].audit_order : group[group.length - 1][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_rectification = !!g[0].is_rectification; his.is_final = !his.is_rectification ? his.audit_order === max_order : false; 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(inspectionId, times) { const auditors = await this.getAuditors(inspectionId, times); // 全部参与的审批人 const result = []; auditors.forEach(x => { if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) { result.push(x); } }); return result; } /** * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等) * * @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, auditConst.status.checkStop]; const sql = 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`qiid`, t.`id`, m.`code` As `code`, t.`name`, t.`spid` ' + ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS m ON la.`qiid` = m.`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.safeInspection.tableName, auditorId, ]; return await this.db.query(sql, sqlParam); } /** * 复制上一期的审批人列表给最新一期 * * @param transaction - 新增一期的事务 * @param {Object} preMaterial - 上一期 * @param {Object} newaMaterial - 最新一期 * @return {Promise<*>} */ async copyPreAuditors(transaction, preInspection, newInspection) { const auditList = await this.getUniqAuditor(preInspection.id, preInspection.times); // 全部参与的审批人 const newAuditors = []; for (const x of auditList) { if (x.audit_order !== 0 && x.is_rectification === 0) { newAuditors.push({ tid: preInspection.tid, qiid: newInspection.id, aid: x.aid, times: 1, order: x.audit_order, status: auditConst.status.uncheck, audit_type: x.audit_type, audit_order: x.audit_order, }); } } const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null; return result ? result.affectedRows === newAuditors.length : true; } /** * 开始审批 * @param {Number} cpId - 方案id * @param {Number} times - 第几次审批 * @return {Promise} */ async start(qiid, times = 1) { const audits = await this.getAllDataByCondition({ where: { qiid, times, order: 1 } }); if (audits.length === 0) { if (this.ctx.tender.info.shenpi.safe_inspection === shenpiConst.sp_status.gdspl) { throw '请联系管理员添加审批人'; } else { throw '请先选择审批人,再上报数据'; } } const transaction = await this.db.beginTransaction(); try { const begin_time = new Date(); const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time }; }); await transaction.updateRows(this.tableName, updateData); await transaction.update(this.ctx.service.safeInspection.tableName, { id: qiid, status: auditConst.status.checking, }); // 微信模板通知 // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'plan', // 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(this._.map(audits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData); // for (const audit of audits) { // await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, { // pid: this.ctx.session.sessionProject.id, // tid: this.ctx.tender.id, // uid: audit.aid, // sp_type: 'change', // sp_id: audit.id, // table_name: this.tableName, // template: wxConst.template.change, // wx_data: wechatData, // }); // } // todo 更新标段tender状态 ? await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } /** * 审批 * @param {Object} inspection - 质量巡检对象 * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果 * @return {Promise} */ async check(inspection, checkData) { if (!this._.includes([auditConst.status.checked, auditConst.status.checkNoPre, auditConst.status.checkNo, auditConst.status.checkStop], checkData.checkType)) { throw '提交数据错误'; } const pid = this.ctx.session.sessionProject.id; switch (checkData.checkType) { case auditConst.status.checked: await this._checked(pid, inspection, checkData); break; case auditConst.status.checkNoPre: await this._checkNoPre(pid, inspection, checkData); break; case auditConst.status.checkNo: await this._checkNo(pid, inspection, checkData); break; case auditConst.status.checkStop: await this._checkStop(pid, inspection, checkData); break; default: throw '无效审批操作'; } } async _checked(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 if (inspection.curAuditorIds.length === 0) throw '审核数据错误'; if (!this._.includes(inspection.curAuditorIds, accountId)) throw '当前标段您无权审批'; const flowAudits = inspection.flowAuditors; const selfAudit = this._.find(flowAudits, { aid: accountId }); const nextAudits = inspection.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, inspection.id, selfAudit.aid, checkData.opinion); // 添加推送 // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }]; const records = []; const auditors = await this.getAuditorsWithOwner(inspection.id, inspection.times); auditors.forEach(audit => { records.push({ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: audit.aid, status: auditConst.status.checked, content: noticeContent }); }); await transaction.insert('zh_notice', records); if (flowAudits.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(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: auditConst.status.checking, }); // 微信模板通知 // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'project', // 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(this._.map(nextAudits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData); // for (const nextAudit of nextAudits) { // await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, { // pid: this.ctx.session.sessionProject.id, // tid: this.ctx.tender.id, // uid: nextAudit.aid, // sp_type: 'change', // sp_id: nextAudit.id, // table_name: this.tableName, // template: wxConst.template.change, // wx_data: wechatData, // }); // } } else { // 本期结束 // 生成截止本期数据 final数据 // 同步 期信息 if (!checkData.rectification_uid) throw '请指定整改人'; await transaction.insert(this.tableName, { tid: inspection.tid, qiid: inspection.id, aid: checkData.rectification_uid, times: inspection.times, order: selfAudit.order + 1, status: auditConst.status.rectification, is_rectification: 1, begin_time: time, audit_order: selfAudit.audit_order + 1, }); const auditList = await this.ctx.service.tenderPermission.getPartsPermission(inspection.tid, ['inspection']); const addAid = this._.includes(this._.map(auditList, 'uid'), checkData.rectification_uid); if (!addAid) { const insert_members = [{ uid: checkData.rectification_uid, inspection: ['1'], }]; await this.ctx.service.tenderPermission.saveOnePermission(inspection.tid, [checkData.rectification_uid], insert_members, ['inspection'], transaction); } await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: auditConst.status.rectification, rectification_uid: checkData.rectification_uid, }); // 微信模板通知 // const users = this._.uniq(this._.map(auditors, 'aid')); // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'project', // wap_url: shenpiUrl, // 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); } } else { // 同步 期信息 await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: auditConst.status.checking, rectification_uid: checkData.rectification_uid || null, }); } await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async _checkNo(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 const audits = inspection.curAuditors; if (audits.length === 0) throw '审核数据错误'; const selfAudit = audits.find(x => { return x.aid === accountId; }); if (!selfAudit) throw '当前标段您无权审批'; const auditors = await this.getUniqAuditor(inspection.id, inspection.times); // 全部参与的审批人 const newAuditors = this._.filter(auditors, { is_rectification: 0 }).map(x => { return { aid: x.aid, tid: inspection.tid, qiid: inspection.id, times: inspection.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, inspection.id, selfAudit.aid, checkData.opinion); const records = [{ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: inspection.uid, status: auditConst.status.checkNoPre, content: noticeContent }]; auditors.forEach(audit => { records.push({ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent }); }); await transaction.insert(this.ctx.service.noticePush.tableName, records); // 同步期信息 await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: checkData.checkType, times: inspection.times + 1, rectification_uid: null, }); // 拷贝新一次审核流程列表 await transaction.insert(this.tableName, newAuditors); // 微信模板通知 // const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid)); // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'project', // wap_url: shenpiUrl, // 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 _checkNoPre(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 const audits = inspection.curAuditors; if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误'; const selfAudit = audits.find(x => { return x.aid === accountId; }); if (!selfAudit) throw '当前标段您无权审批'; const flowAudits = inspection.flowAuditors; // 添加重新审批后,不能用order-1,取groupby值里的上一个才对 // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1}); const auditors2 = await this.getAuditGroupByList(inspection.id, inspection.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.safeInspection, 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 qiid = ? AND times = ? AND `order` > ?'; await transaction.query(sql, [inspection.id, selfAudit.times, selfAudit.order]); // 上一审批人,当前审批人 再次添加至流程 const newAuditors = []; preAuditors.forEach(x => { newAuditors.push({ tid: inspection.tid, qiid: inspection.id, 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({ tid: inspection.tid, qiid: inspection.id, 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(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: checkData.checkType, rectification_uid: null, }); // const preAuditorIds = preAuditors.map(x => { return x.aid; }); // const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order); // const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id'); // users.push(...preAuditorIds); // await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { // qi: stageInfo.order, // code: shenpiUrl, // }); // // 微信模板通知 // const wechatData = { // wap_url: shenpiUrl, // qi: stageInfo.order, // status: wxConst.status.check, // tips: wxConst.tips.check, // code: this.ctx.session.sessionProject.code, // }; // await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData); // 重新发送配置 // for (const a of newAuditors) { // await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, { // pid: this.ctx.session.sessionProject.id, // tid: this.ctx.tender.id, // uid: a.aid, // sp_type: 'stage', // sp_id: a.id, // table_name: this.tableName, // template: wxConst.template.stage, // wx_data: wechatData, // }); // } await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async _checkStop(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 const audits = inspection.curAuditors; if (!audits) throw '审核数据错误'; const selfAudit = audits.find(x => { return x.aid === accountId; }); if (!selfAudit) throw '当前标段您无权终止'; const auditors = await this.getUniqAuditor(inspection.id, inspection.times); // 全部参与的审批人 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, inspection.id, selfAudit.aid, checkData.opinion); // 添加推送 const records = [{ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: inspection.uid, status: auditConst.status.checkStop, content: noticeContent }]; auditors.forEach(audit => { records.push({ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: audit.aid, status: auditConst.status.checkStop, content: noticeContent }); }); await transaction.insert('zh_notice', records); // 本期结束 // 生成截止本期数据 final数据 // 同步 期信息 await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: checkData.checkType, }); // 微信模板通知 // const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid)); // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'project', // wap_url: shenpiUrl, // 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 {Object} inspection - 质量巡检对象 * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果 * @return {Promise} */ async rectification(inspection, checkData) { if (!this._.includes([auditConst.status.checked, auditConst.status.checkNoPre], checkData.checkType)) { throw '提交数据错误'; } const pid = this.ctx.session.sessionProject.id; switch (checkData.checkType) { case auditConst.status.checked: await this._rectificationChecked(pid, inspection, checkData); break; case auditConst.status.checkNoPre: await this._rectificationCheckNoPre(pid, inspection, checkData); break; default: throw '无效审批操作'; } } async _rectificationChecked(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 if (inspection.curAuditorIds.length === 0) throw '审核数据错误'; if (!this._.includes(inspection.curAuditorIds, accountId)) throw '当前标段您无权审批'; const flowAudits = inspection.flowAuditors; const selfAudit = this._.find(flowAudits, { aid: accountId }); 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, inspection.id, selfAudit.aid, checkData.opinion); // 添加推送 // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }]; const records = []; const auditors = await this.getAuditorsWithOwner(inspection.id, inspection.times); auditors.forEach(audit => { records.push({ pid, tid: selfAudit.tid, type: pushType.safeInspection, uid: audit.aid, status: auditConst.status.checked, content: noticeContent }); }); await transaction.insert('zh_notice', records); // 本期结束 await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: auditConst.status.checked, }); // 微信模板通知 // const users = this._.uniq(this._.map(auditors, 'aid')); // const shenpiUrl = await this.ctx.helper.urlToShort( // this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi' // ); // const wechatData = { // type: 'project', // wap_url: shenpiUrl, // 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 _rectificationCheckNoPre(pid, inspection, checkData) { const accountId = this.ctx.session.sessionUser.accountId; const time = new Date(); // 整理当前流程审核人状态更新 const audits = inspection.curAuditors; if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误'; const selfAudit = audits.find(x => { return x.aid === accountId; }); if (!selfAudit) throw '当前标段您无权审批'; const auditors2 = await this.getAuditGroupByList(inspection.id, inspection.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.safeInspection, 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, }); } } await transaction.updateRows(this.tableName, updateData); // await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id')); // 上一审批人,当前审批人 再次添加至流程 const newAuditors = []; preAuditors.forEach(x => { newAuditors.push({ tid: inspection.tid, qiid: inspection.id, 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); // 同步 期信息 await transaction.update(this.ctx.service.safeInspection.tableName, { id: inspection.id, status: checkData.checkType, rectification_item: '', rectification_date: null, }); // const preAuditorIds = preAuditors.map(x => { return x.aid; }); // const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order); // const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id'); // users.push(...preAuditorIds); // await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { // qi: stageInfo.order, // code: shenpiUrl, // }); // // 微信模板通知 // const wechatData = { // wap_url: shenpiUrl, // qi: stageInfo.order, // status: wxConst.status.check, // tips: wxConst.tips.check, // code: this.ctx.session.sessionProject.code, // }; // await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData); // 重新发送配置 // for (const a of newAuditors) { // await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, { // pid: this.ctx.session.sessionProject.id, // tid: this.ctx.tender.id, // uid: a.aid, // sp_type: 'stage', // sp_id: a.id, // table_name: this.tableName, // template: wxConst.template.stage, // wx_data: wechatData, // }); // } await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } /** * 用于添加推送所需的content内容 * @param {Number} pid 项目id * @param {Number} tid 台账id * @param {Number} cpId 方案id * @param {Number} uid 审批人id */ async getNoticeContent(pid, tid, qiid, uid, opinion = '') { const noticeSql = 'SELECT * FROM (SELECT ' + ' t.`id` As `tid`, t.`spid` As `spid`, ma.`qiid`, 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.`qiid`' + ' 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.safeInspection.tableName, qiid, 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]) : ''; } } return SafeInspectionAudit; };