123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- 'use strict';
- /**
- * 台账审批流程表
- *
- * @author Mai
- * @date 2018/5/25
- * @version
- */
- const auditConst = require('../const/audit').ledger;
- const smsTypeConst = require('../const/sms_type');
- const SMS = require('../lib/sms');
- const SmsAliConst = require('../const/sms_alitemplate');
- const wxConst = require('../const/wechat_template');
- const shenpiConst = require('../const/shenpi');
- const pushType = require('../const/audit').pushType;
- const pushOperate = require('../const/spec_3f').pushOperate;
- module.exports = app => {
- class LedgerAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'ledger_audit';
- }
- /**
- * 获取标段审核人信息
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getAuditor(tenderId, auditorId, times = 1) {
- const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
- ' FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- async getAuditorByOrder(tenderId, order, times = 1) {
- const sql = 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
- ' FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, order, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- async getLastestAuditor(tenderId, times, status) {
- const sql =
- 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
- ' la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
- ' FROM ' + this.tableName + ' AS la' +
- ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
- ' ORDER BY `audit_order` DESC';
- const sqlParam = [tenderId, status, times ? times : 1];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取标段审核人最后一位的名称
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getStatusName(tenderId, times) {
- const sql = 'SELECT pa.`name` FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`status` != ? ORDER BY la.`times` DESC, la.`audit_order` DESC';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.uncheck];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取标段审核列表信息
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getAuditors(tenderId, times = 1) {
- const sql =
- 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
- 'FROM ?? AS la, ?? AS pa, (SELECT t1.`audit_id`,(@i:=@i+1) as `sort` FROM (SELECT t.`audit_id`, t.`audit_order` FROM (select `audit_id`, `audit_order` from ?? WHERE `tender_id` = ? AND `times` = ? ORDER BY `audit_order` LIMIT 200) t GROUP BY t.`audit_id` ORDER BY t.`audit_order`) t1, (select @i:=0) as it) as g ' +
- 'WHERE la.`tender_id` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`audit_order`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, tenderId, times, tenderId, times];
- const result = await this.db.query(sql, sqlParam);
- const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `tender_id` = ? AND `times` = ? GROUP BY `audit_id`) as a';
- const sqlParam2 = [this.tableName, tenderId, times];
- const count = await this.db.queryOne(sql2, sqlParam2);
- for (const i in result) {
- result[i].max_sort = count.num;
- }
- return result;
- }
- async getFinalAuditGroup(tenderId, times = 1) {
- const sql =
- 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`tender_id`, Max(la.`audit_order`) as max_order ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, times];
- const result = await this.db.query(sql, sqlParam);
- for (const r of result) {
- const auditor = await this.getDataByCondition({tender_id: tenderId, times: r.times, audit_order: r.max_order});
- r.status = auditor.status;
- r.opinion = auditor.opinion;
- r.begin_time = auditor.begin_time;
- r.end_time = auditor.end_time;
- }
- return result;
- }
- /**
- * 获取标段当前审核人
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- */
- async getCurAuditor(tenderId, times = 1) {
- const sql =
- 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取最新审核顺序
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<number>}
- */
- async getNewOrder(tenderId, times = 1) {
- const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
- const sqlParam = ['audit_order', this.tableName, tenderId, times];
- const result = await this.db.queryOne(sql, sqlParam);
- return result && result.max_order ? result.max_order + 1 : 1;
- }
- /**
- * 新增审核人
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<number>}
- */
- async addAuditor(tenderId, auditorId, times = 1, is_gdzs = 0) {
- const transaction = await this.db.beginTransaction();
- try {
- let newOrder = await this.getNewOrder(tenderId, times);
- // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
- newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
- if (is_gdzs) await this._syncOrderByDelete(transaction, tenderId, newOrder, times, '+');
- const data = {
- tender_id: tenderId,
- audit_id: auditorId,
- times,
- audit_order: newOrder,
- status: auditConst.status.uncheck,
- };
- const result = await transaction.insert(this.tableName, data);
- await transaction.commit();
- return (result.effectRows = 1);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return false;
- }
- /**
- * 移除审核人时,同步其后审核人order
- * @param transaction - 事务
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<*>}
- * @private
- */
- async _syncOrderByDelete(transaction, tenderId, order, times, selfOperate = '-') {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('tender_id', {
- value: tenderId,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('audit_order', {
- value: order,
- operate: '>=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '=',
- });
- this.sqlBuilder.setUpdateData('audit_order', {
- value: 1,
- selfOperate: selfOperate,
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
- const data = await transaction.query(sql, sqlParam);
- return data;
- }
- /**
- * 移除审核人
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @return {Promise<boolean>}
- */
- async deleteAuditor(tenderId, auditorId, times = 1) {
- const transaction = await this.db.beginTransaction();
- try {
- const condition = { tender_id: tenderId, audit_id: auditorId, times };
- const auditor = await this.getDataByCondition(condition);
- if (!auditor) {
- throw '该审核人不存在';
- }
- await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
- await transaction.delete(this.tableName, condition);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 开始审批
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<boolean>}
- */
- async start(tenderId, times = 1) {
- const audit = await this.getDataByCondition({ tender_id: tenderId, times, audit_order: 1 });
- if (!audit) {
- if(this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdspl) {
- throw '请联系管理员添加审批人';
- } else {
- throw '请先选择审批人,再上报数据';
- }
- }
- const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId /* , is_leaf: true*/ });
- // 拷贝备份台账数据
- const his_id = await this.ctx.service.ledgerHistory.backupLedgerHistory(this.ctx.tender.data);
- 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.tender.tableName, {
- id: tenderId,
- ledger_status: auditConst.status.checking,
- total_price: sum.total_price,
- deal_tp: sum.deal_tp,
- his_id: his_id,
- });
- await this.ctx.service.tenderCache.updateLedgerCache4Start(transaction, tenderId, auditConst.status.checking, audit, sum);
- // 添加短信通知-需要审批提醒功能
- await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
- // 微信模板通知
- const wechatData = {
- status: wxConst.status.check,
- tips: wxConst.tips.check,
- begin_time: Date.parse(new Date()),
- };
- await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
- await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
- pid: this.ctx.session.sessionProject.id,
- tid: tenderId,
- uid: audit.audit_id,
- sp_type: 'ledger',
- sp_id: audit.id,
- table_name: this.tableName,
- template: wxConst.template.ledger,
- wx_data: wechatData,
- });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 审批
- * @param {Number} tenderId - 标段id
- * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
- * @param {String} opinion 审批意见
- * @param {Number} times - 第几次审批
- * @return {Promise<void>}
- */
- async check(tenderId, checkType, opinion, times = 1) {
- if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
- throw '提交数据错误';
- }
- const pid = this.ctx.session.sessionProject.id;
- const transaction = await this.db.beginTransaction();
- try {
- // 整理当前流程审核人状态更新
- const time = new Date();
- const audit = await this.getDataByCondition({
- tender_id: tenderId,
- times,
- status: auditConst.status.checking,
- });
- if (!audit) {
- throw '审核数据错误';
- }
- // 获取审核人列表
- const auditList = await this.getAuditors(tenderId, times);
- // 添加推送
- const noticeContent = await this.getNoticeContent(audit.tender_id, pid, audit.audit_id, opinion);
- const records = [
- {
- pid,
- type: pushType.ledger,
- uid: this.ctx.tender.data.user_id,
- status: checkType,
- content: noticeContent,
- },
- ];
- auditList.forEach(audit => {
- records.push({
- pid,
- type: pushType.ledger,
- uid: audit.audit_id,
- status: checkType,
- content: noticeContent,
- });
- });
- await transaction.insert('zh_notice', records);
- // 更新当前审核流程
- await transaction.update(this.tableName, { id: audit.id, status: checkType, opinion, end_time: time });
- await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
- const begin_audit = await this.getDataByCondition({
- tender_id: tenderId,
- audit_order: 1,
- });
- const tenderMsg = await this.ctx.service.tender.getDataById(tenderId);
- const users = this._.uniq(this._.concat(this._.map(auditList, 'audit_id'), tenderMsg.user_id));
- if (checkType === auditConst.status.checked) {
- const nextAudit = await this.getDataByCondition({
- tender_id: tenderId,
- times,
- audit_order: audit.audit_order + 1,
- });
- // 无下一审核人表示,审核结束
- if (nextAudit) {
- await transaction.update(this.tableName, {
- id: nextAudit.id,
- status: auditConst.status.checking,
- begin_time: time,
- });
- await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, checkType, nextAudit);
- await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
- // 微信模板通知
- const wechatData = {
- status: wxConst.status.check,
- tips: wxConst.tips.check,
- begin_time: Date.parse(begin_audit.begin_time),
- };
- await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
- await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
- pid: this.ctx.session.sessionProject.id,
- tid: tenderId,
- uid: nextAudit.audit_id,
- sp_type: 'ledger',
- sp_id: nextAudit.id,
- table_name: this.tableName,
- template: wxConst.template.ledger,
- wx_data: wechatData,
- });
- } else {
- // 同步标段信息
- await transaction.update(this.ctx.service.tender.tableName, {
- id: tenderId,
- ledger_status: checkType,
- });
- await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, checkType, checkType);
- await this.ctx.service.tenderTag.saveTenderTag(tenderId, {ledger_time: new Date()}, transaction);
- // const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
- await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
- status: SmsAliConst.status.success,
- });
- // 微信模板通知
- const wechatData = {
- status: wxConst.status.success,
- tips: wxConst.tips.success,
- begin_time: Date.parse(begin_audit.begin_time),
- };
- await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
- // 审批通过 - 检查三方特殊推送
- await this.ctx.service.specMsg.addLedgerMsg(transaction, pid, this.ctx.tender, pushOperate.ledger.checked);
- }
- } else {
- // 同步标段信息
- await transaction.update(this.ctx.service.tender.tableName, {
- id: tenderId,
- ledger_times: times + 1,
- ledger_status: checkType,
- });
- await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, checkType, checkType, { audit_id: this.ctx.tender.data.user_id });
- // 拷贝新一次审核流程列表
- const auditors = await this.getAllDataByCondition({
- where: { tender_id: tenderId, times },
- columns: ['tender_id', 'audit_order', 'audit_id'],
- });
- const insertAuditors = [];
- for (const a of auditors) {
- if (insertAuditors.find(x => { return x.audit_id === a.audit_id; })) continue;
- a.audit_order = insertAuditors.length + 1;
- a.times = times + 1;
- a.status = auditConst.status.uncheck;
- insertAuditors.push(a)
- }
- await transaction.insert(this.tableName, insertAuditors);
- // const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
- await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
- status: SmsAliConst.status.back,
- });
- // 微信模板通知
- const wechatData = {
- status: wxConst.status.back,
- tips: wxConst.tips.back,
- begin_time: Date.parse(begin_audit.begin_time),
- };
- await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 重新审批
- * @param {Number} stageId - 标段id
- * @param {Number} times - 第几次审批
- * @return {Promise<void>}
- */
- async checkAgain(tenderId, times = 1) {
- const time = new Date();
- // 整理当前流程审核人状态更新
- const auditors = await this.getAllDataByCondition({
- where: { tender_id: tenderId, times },
- orders: [['audit_order', 'desc']],
- });
- if (auditors.length <= 0) throw '台账审核数据错误';
- const selfAudit = auditors[0];
- if (selfAudit.audit_id !== this.ctx.session.sessionUser.accountId) throw '当前台账您无权重审';
- const tender = this.ctx.service.tender.getDataById(tenderId);
- if (!tender) throw '标段数据错误';
- let otherAuditIds = [tender.user_id, ...auditors.map(x => { return x.audit_id })];
- otherAuditIds = this._.uniq(otherAuditIds).filter(x => { return x !== selfAudit.audit_id; });
- const checkAgainAuditor = {
- tender_id: tenderId, times, audit_order: selfAudit.audit_order + 1, audit_id: selfAudit.audit_id,
- begin_time: time, end_time: time, opinion: '', status: auditConst.status.checkAgain,
- };
- const checkingAuditor = {
- tender_id: tenderId, times, audit_order: selfAudit.audit_order + 2, audit_id: selfAudit.audit_id,
- begin_time: time, end_time: null, opinion: '', status: auditConst.status.checking,
- };
- const transaction = await this.db.beginTransaction();
- try {
- // 当前审批人2次添加至流程中
- await transaction.insert(this.tableName, checkAgainAuditor);
- const checking_result = await transaction.insert(this.tableName, checkingAuditor);
- // 同步标段信息
- await transaction.update(this.ctx.service.tender.tableName, {
- id: tenderId,
- ledger_status: auditConst.status.checking,
- });
- await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checkAgain, checkingAuditor);
- if (otherAuditIds.length > 0) {
- await this.ctx.helper.sendAliSms(otherAuditIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
- // 微信模板通知
- const wechatData = {
- status: wxConst.status.check,
- tips: wxConst.tips.check,
- begin_time: Date.parse(time),
- };
- await this.ctx.helper.sendWechat(selfAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
- await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
- pid: this.ctx.session.sessionProject.id,
- tid: this.ctx.tender.id,
- uid: selfAudit.audit_id,
- sp_type: 'ledger',
- sp_id: checking_result.insertId,
- table_name: this.tableName,
- template: wxConst.template.ledger,
- wx_data: wechatData,
- });
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 获取审核人需要审核的标段列表
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditTender(auditorId) {
- const sql =
- 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`begin_time`, la.`end_time`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`ledger_status` ' +
- ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
- ' WHERE ((la.`audit_id` = ? and la.`status` = ?) OR (t.`user_id` = ? and t.`ledger_status` = ? and la.`status` = ? and la.`times` = (t.`ledger_times`-1)))' +
- ' ORDER BY la.`begin_time` DESC';
- const sqlParam = [
- this.tableName,
- this.ctx.service.tender.tableName,
- auditorId,
- auditConst.status.checking,
- auditorId,
- auditConst.status.checkNo,
- auditConst.status.checkNo,
- ];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取审核人审核的次数
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getCountByChecked(auditorId) {
- return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
- }
- /**
- * 获取最近一次审批结束时间
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getLastEndTimeByChecked(auditorId) {
- const sql = 'SELECT `end_time` FROM ?? WHERE `audit_id` = ? ' +
- 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, 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} id 标段id
- * @param {Number} pid 项目id
- * @param {Number} uid 审核人id
- */
- async getNoticeContent(id, pid, uid, opinion = '') {
- const noticeSql =
- 'SELECT * FROM (SELECT ' +
- ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
- ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
- ' 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, id, 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} tender_id - 标段id
- * @param {Number} times 审核次数
- * @return {Promise<Array>} 查询结果集
- */
- async getAuditGroupByList(tender_id, times = 1) {
- const sql =
- 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
- ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
- ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取审核人流程列表(包括原报)
- * @param {Number} tender_id - 标段id
- * @param {Number} times 审核次数
- * @return {Promise<Array>} 查询结果集(包括原报)
- */
- async getAuditorsWithOwner(tender_id, times = 1) {
- const result = await this.getAuditGroupByList(tender_id, times);
- const sql =
- 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As `audit_order`' +
- ' FROM ' + this.ctx.service.tender.tableName + ' As s' +
- ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
- ' ON s.user_id = pa.id' +
- ' WHERE s.id = ?';
- const sqlParam = [times, tender_id, tender_id];
- const user = await this.db.queryOne(sql, sqlParam);
- result.unshift(user);
- return result;
- }
- async updateNewAuditList(tender, newIdList) {
- const transaction = await this.db.beginTransaction();
- try {
- // 先删除旧的审批流,再添加新的
- await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times });
- const newAuditors = [];
- let order = 1;
- for (const aid of newIdList) {
- newAuditors.push({
- tender_id: tender.id, audit_id: aid,
- times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
- });
- order++;
- }
- if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateLastAudit(tender, auditList, lastId) {
- const transaction = await this.db.beginTransaction();
- try {
- // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
- const idList = this._.map(auditList, 'audit_id');
- let order = idList.length + 1;
- if (idList.indexOf(lastId) !== -1) {
- await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times, audit_id: lastId });
- const audit = this._.find(auditList, { 'audit_id': lastId });
- // 顺移之后审核人流程顺序
- await this._syncOrderByDelete(transaction, tender.id, audit.audit_order, tender.ledger_times);
- order = order - 1;
- }
- // 添加终审
- const newAuditor = {
- tender_id: tender.id, audit_id: lastId,
- times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
- };
- await transaction.insert(this.tableName, newAuditor);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return LedgerAudit;
- };
|