123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit').revise;
- const smsTypeConst = require('../const/sms_type');
- module.exports = app => {
- class ReviseAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'revise_audit';
- }
- /**
- * 获取标段审核人信息
- *
- * @param {Number} reviseId - 修订id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- */
- async getAuditor(reviseId, 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.`rid` = ? and la.`audit_id` = ? and la.`times` = ?' +
- ' and la.`audit_id` = pa.`id`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditorId, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取标段审核列表信息
- *
- * @param {Number} reviseId - 修订id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- */
- async getAuditors(reviseId, times = 1) {
- const sql = 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`,' +
- ' pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`' +
- ' FROM ' + this.tableName + ' AS la ' +
- ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id`' +
- ' ORDER BY la.`audit_order`';
- const sqlParam = [reviseId, times];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取标段当前审核人
- *
- * @param {Number} reviseId - 修订id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- */
- async getCurAuditor(reviseId, 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.`rid` = ? and la.`status` = ? and la.`times` = ?' +
- ' and la.`audit_id` = pa.`id`';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditConst.status.checking, times];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取最新审核顺序
- *
- * @param {Number} reviseId - 修订id
- * @param {Number} times - 第几次审批
- * @returns {Promise<number>}
- */
- async getNewOrder(reviseId, times = 1) {
- const sql = 'SELECT Max(??) As max_order FROM ?? Where `rid` = ? and `times` = ?';
- const sqlParam = ['audit_order', this.tableName, reviseId, times];
- const result = await this.db.queryOne(sql, sqlParam);
- return result && result.max_order ? result.max_order + 1 : 1;
- }
- /**
- * 新增审核人
- *
- * @param {Object} revise - 修订
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<number>}
- */
- async addAuditor(revise, auditorId) {
- const times = revise.times ? revise.times : 1;
- const newOrder = await this.getNewOrder(revise.id, times);
- const data = {
- tender_id: revise.tid,
- audit_id: auditorId,
- times: times,
- audit_order: newOrder,
- status: auditConst.status.uncheck,
- rid: revise.id,
- in_time: new Date(),
- };
- const result = await this.db.insert(this.tableName, data);
- return result.effectRows = 1;
- }
- /**
- * 移除审核人时,同步其后审核人order
- * @param transaction - 事务
- * @param {Number} reviseId - 修订id
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<*>}
- * @private
- */
- async _syncOrderByDelete(transaction, reviseId, order, times) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('rid', {
- value: this.db.escape(reviseId),
- 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 {Object} revise - 修订
- * @param {Number} auditorId - 审核人id
- * @param {Number} times - 第几次审批
- * @returns {Promise<boolean>}
- */
- async deleteAuditor(revise, auditorId) {
- const times = revise.times ? revise.times : 1;
- const transaction = await this.db.beginTransaction();
- try {
- const condition = {rid: revise.id, audit_id: auditorId, times: times};
- const auditor = await this.getDataByCondition(condition);
- if (!auditor) {
- throw '该审核人不存在';
- }
- await this._syncOrderByDelete(transaction, revise.id, auditor.audit_order, times);
- await transaction.delete(this.tableName, condition);
- await transaction.commit();
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- /**
- * 开始审批
- *
- * @param {Object} revise - 修订
- * @param {Number} times - 第几次审批
- * @returns {Promise<boolean>}
- */
- async start(revise, times = 1) {
- const audit = await this.getDataByCondition({rid: revise.id, times: times, audit_order: 1});
- if (!audit) throw '审核人信息错误';
- const time = new Date();
- // 拷贝备份台账数据
- const [billsHis, posHis] = await this.ctx.service.ledgerRevise.backupReviseHistoryFile(revise);
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, {
- id: audit.id, status: auditConst.status.checking, begin_time: time,
- bills_file: revise.bills_file, pos_file: revise.pos_file,
- });
- const reviseData = {
- id: revise.id, status: auditConst.status.checking,
- bills_file: billsHis, pos_file: posHis,
- };
- if (revise.times === 1) {
- reviseData.begin_time = time;
- }
- await transaction.update(this.ctx.service.ledgerRevise.tableName, reviseData);
- // 添加短信通知-需要审批提醒功能
- // 下一人
- await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.XD,
- smsTypeConst.judge.approval.toString(), '台帐修订需要您审批,请登录系统处理。');
- // 其他参与人
- const auditList = await this.getAuditors(revise.tid, times);
- const users = this._.pull(this._.map(auditList, 'user_id'), audit.id);
- await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
- smsTypeConst.judge.result.toString(), '台账修订已上报。');
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async _replaceLedgerByRevise(transaction, revise) {
- const sqlParam = [revise.tid];
- await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: revise.tid});
- const bSql = 'Insert Into ' + this.ctx.service.ledger.tableName +
- ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
- ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
- ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id)' +
- ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
- ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
- ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id' +
- ' From ' + this.ctx.service.reviseBills.tableName +
- ' Where `tender_id` = ?';
- await transaction.query(bSql, sqlParam);
- await transaction.delete(this.ctx.service.pos.tableName, {tid: revise.tid});
- const pSql = 'Insert Into ' + this.ctx.service.pos.tableName +
- ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
- ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder)' +
- ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
- ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder' +
- ' From ' + this.ctx.service.revisePos.tableName +
- ' Where `tid` = ?';
- await transaction.query(pSql, sqlParam);
- }
- /**
- * 审批
- * @param {Object} revise - 修订
- * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
- * @param {String} opinion - 审批意见
- * @param {Number} times - 第几次审批
- * @returns {Promise<void>}
- */
- async check(revise, checkType, opinion, times = 1) {
- if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) throw '提交数据错误';
- const audit = await this.getDataByCondition({rid: revise.id, times: times, status: auditConst.status.checking});
- if (!audit) throw '审核数据错误';
- const transaction = await this.db.beginTransaction();
- try {
- // 整理当前流程审核人状态更新
- const time = new Date();
- // 更新当前审核流程
- 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({rid: revise.id, 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});
- // 短信通知-需要审批提醒功能
- // 下一人
- await this.ctx.helper.sendUserSms(nextAudit.user_id, smsTypeConst.const.XD,
- smsTypeConst.judge.approval.toString(), '台帐修订需要您审批,请登录系统处理。');
- // 其他参与人
- const auditList = await this.getAuditors(revise.tid, times);
- const users = this._.pull(this._.map(auditList, 'user_id'), audit.id);
- users.push(revise.uid);
- await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
- smsTypeConst.judge.result.toString(), '台账修订审批通过。');
- } else {
- // 同步修订信息
- await transaction.update(this.ctx.service.ledgerRevise.tableName, {id: revise.id, status: checkType, end_time: time});
- // 拷贝修订数据至台账
- await this._replaceLedgerByRevise(transaction, revise);
- const sum = await this.ctx.service.reviseBills.addUp({tender_id: revise.tid, is_leaf: true});
- await transaction.update(this.ctx.service.tender.tableName, {
- id: revise.tid, total_price: sum.total_price, deal_tp: sum.deal_tp
- });
- // 短信通知-审批通过提醒功能
- // 下一人
- const msg = '台账修订审批通过,请登录系统处理。';
- await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
- smsTypeConst.judge.result.toString(), msg);
- // 其他参与人
- const auditList = await this.getAuditors(revise.tid, times);
- const users = this._.pull(this._.map(auditList, 'user_id'), audit.id);
- await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
- smsTypeConst.judge.result.toString(), '台账修订审批通过。');
- }
- } else {
- // 同步修订信息
- await transaction.update(this.ctx.service.ledgerRevise.tableName, {id: revise.id, times: times+1, status: checkType});
- // 拷贝新一次审核流程列表
- const auditors = await this.getAllDataByCondition({
- where: {rid: revise.id, times: times},
- columns: ['tender_id', 'rid', 'audit_order', 'audit_id']
- });
- for (const a of auditors) {
- a.times = times + 1;
- a.status = auditConst.status.uncheck;
- a.in_time = time;
- }
- await transaction.insert(this.tableName, auditors);
- // 短信通知-审批退回提醒功能
- // 下一人
- await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
- smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
- // 其他参与人
- await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
- smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
- }
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 获取审核人需要审核的标段列表
- *
- * @param auditorId
- * @returns {Promise<*>}
- */
- async getAuditRevise(auditorId) {
- const sql = 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
- ' r.id, r.corder, r.uid, r.status, r.content,' +
- ' t.id As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, t.`status` As t_status, ' +
- ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
- ' FROM ' + this.tableName + ' AS ra' +
- ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
- ' Left Join '+ this.ctx.service.tender.tableName +' AS t On r.tid = t.id' +
- ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
- ' WHERE ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
- ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1)))';
- const sqlParam = [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 getNoticeRevise(projectId, auditorId, noticeTime) {
- const sql = 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
- ' r.id, r.corder, r.uid, r.status, r.content, ' +
- ' t.`id` As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, ' +
- ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
- ' FROM ' + this.tableName + ' As ra' +
- ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON ra.rid = r.id' +
- ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' As t ON r.`tid` = t.`id`' +
- ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
- ' WHERE ra.audit_id <> ? and ra.end_time > ? and t.project_id = ?' +
- ' GROUP By t.`id`' +
- ' ORDER By ra.`end_time`';
- const sqlParam = [auditorId, noticeTime, projectId];
- return await this.db.query(sql, sqlParam);
- }
- }
- return ReviseAudit;
- };
|