123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- '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');
- 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 - 第几次审批
- * @returns {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, ?? AS pa ' +
- 'WHERE la.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?' +
- ' and la.`audit_id` = pa.`id`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取标段审核人最后一位的名称
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- */
- async getStatusName(tenderId) {
- const sql = 'SELECT pa.`name` ' +
- 'FROM ?? AS la, ?? AS pa ' +
- 'WHERE la.`tender_id` = ?' +
- ' and la.`audit_id` = pa.`id` and la.`status` != ? ORDER BY la.`id` 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 - 第几次审批
- * @returns {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 `audit_id`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `tender_id` = ? AND `times` = ? GROUP BY `audit_id`) 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;
- }
- /**
- * 获取标段当前审核人
- *
- * @param {Number} tenderId - 标段id
- * @param {Number} times - 第几次审批
- * @returns {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, ?? AS pa ' +
- 'WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
- ' and la.`audit_id` = pa.`id`';
- 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 - 第几次审批
- * @returns {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 - 第几次审批
- * @returns {Promise<number>}
- */
- async addAuditor(tenderId, auditorId, times = 1) {
- const newOrder = await this.getNewOrder(tenderId, times);
- const data = {
- tender_id: tenderId,
- audit_id: auditorId,
- times: times,
- audit_order: newOrder,
- status: auditConst.status.uncheck,
- };
- const result = await this.db.insert(this.tableName, data);
- return result.effectRows = 1;
- }
- /**
- * 移除审核人时,同步其后审核人order
- * @param transaction - 事务
- * @param {Number} tenderId - 标段id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- * @private
- */
- async _syncOrderByDelete(transaction, tenderId, order, times) {
- 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: '-',
- });
- 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 - 第几次审批
- * @returns {Promise<boolean>}
- */
- async deleteAuditor(tenderId, auditorId, times = 1) {
- const transaction = await this.db.beginTransaction();
- try {
- const condition = {tender_id: tenderId, audit_id: auditorId, times: 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 - 第几次审批
- * @returns {Promise<boolean>}
- */
- async start(tenderId, times = 1) {
- const audit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: 1});
- if (!audit) {
- throw '审核人信息错误';
- }
- const sum = await this.ctx.service.ledger.addUp({tender_id: tenderId/*, is_leaf: true*/});
- 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,
- });
- // 添加短信通知-需要审批提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(audit.audit_id);
- if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
- const sms = new SMS(this.ctx);
- const tenderName = await sms.contentChange(tenderInfo.name);
- const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- 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 {Number} times - 第几次审批
- * @returns {Promise<void>}
- */
- async check(tenderId, checkType, opinion, times = 1) {
- if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
- throw '提交数据错误';
- }
- const transaction = await this.db.beginTransaction();
- try {
- // 整理当前流程审核人状态更新
- const time = new Date();
- const audit = await this.getDataByCondition({tender_id: tenderId, times: times, status: auditConst.status.checking});
- if (!audit) {
- throw '审核数据错误';
- }
- // 更新当前审核流程
- await transaction.update(this.tableName, {id: audit.id, status: checkType, opinion: opinion, end_time: time});
- if (checkType === auditConst.status.checked) {
- const nextAudit = await this.getDataByCondition({tender_id: tenderId, times: times, audit_order: audit.audit_order + 1});
- // 无下一审核人表示,审核结束
- if (nextAudit) {
- await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
- // 添加短信通知-需要审批提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.audit_id);
- if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
- const sms = new SMS(this.ctx);
- const tenderName = await sms.contentChange(tenderInfo.name);
- const content = '【纵横计量支付】' + tenderName + '台帐需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- } else {
- // 同步标段信息
- await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_status: checkType});
- // 添加短信通知-审批通过提醒功能
- const mobile_array = [];
- const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
- const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
- if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
- const smsType = JSON.parse(smsUser1.sms_type);
- if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- mobile_array.push(smsUser1.auth_mobile);
- }
- }
- const auditList = await this.getAuditors(tenderId, times);
- for (const user of auditList) {
- const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
- if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
- const smsType = JSON.parse(smsUser.sms_type);
- if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- mobile_array.push(smsUser.auth_mobile);
- }
- }
- }
- if (mobile_array.length > 0) {
- const sms = new SMS(this.ctx);
- const tenderName = await sms.contentChange(tenderInfo.name);
- const content = '【纵横计量支付】' + tenderName + '台账审批通过,请登录系统处理。';
- sms.send(mobile_array, content);
- }
- }
- } else {
- // 同步标段信息
- await transaction.update(this.ctx.service.tender.tableName, {id: tenderId, ledger_times: times+1, ledger_status: checkType});
- // 拷贝新一次审核流程列表
- const auditors = await this.getAllDataByCondition({
- where: {tender_id: tenderId, times: times},
- columns: ['tender_id', 'audit_order', 'audit_id']
- });
- for (const a of auditors) {
- a.times = times + 1;
- a.status = auditConst.status.uncheck;
- }
- await transaction.insert(this.tableName, auditors);
- // 添加短信通知-审批退回提醒功能
- const mobile_array = [];
- const tenderInfo = await this.ctx.service.tender.getDataById(tenderId);
- const smsUser1 = await this.ctx.service.projectAccount.getDataById(tenderInfo.user_id);
- if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
- const smsType = JSON.parse(smsUser1.sms_type);
- if (smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- mobile_array.push(smsUser1.auth_mobile);
- }
- }
- for (const user of auditors) {
- const smsUser = await this.ctx.service.projectAccount.getDataById(user.audit_id);
- if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
- const smsType = JSON.parse(smsUser.sms_type);
- if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.TZ] !== undefined && smsType[smsTypeConst.const.TZ].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- mobile_array.push(smsUser.auth_mobile);
- }
- }
- }
- if (mobile_array.length > 0) {
- const sms = new SMS(this.ctx);
- const tenderName = await sms.contentChange(tenderInfo.name);
- const content = '【纵横计量支付】' + tenderName + '台账审批退回,请登录系统处理。';
- sms.send(mobile_array, content);
- }
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 获取审核人需要审核的标段列表
- *
- * @param auditorId
- * @returns {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, ?? AS t ' +
- '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)))' +
- ' and la.`tender_id` = t.`id`';
- 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 {Integer} projectId - 项目id
- * @param {Integer} auditorId - 查询人id
- * @param {Date} noticeTime - 查询事件
- * @returns {Promise<*>}
- */
- async getNoticeTender(projectId, auditorId, noticeTime) {
- const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`end_time`, la.`status`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
- ' pa.name As `lu_name`, pa.role As `lu_role`, pa.company As `lu_company`' +
- ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t ' +
- ' LEFT JOIN ?? As la ON la.`tender_id` = t.`id`' +
- ' LEFT JOIN ?? As pa ON la.`audit_id` = pa.`id`' +
- ' WHERE la.`end_time` > ? and t.`project_id` = ?' +
- ' ORDER By la.`end_time` DESC';
- const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, this.tableName, this.ctx.service.projectAccount.tableName,
- noticeTime, projectId];
- return await this.db.query(sql, sqlParam);
- }
- }
- return LedgerAudit;
- };
|