123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const audit = require('../const/audit');
- const fs = require('fs');
- const path = require('path');
- const smsTypeConst = require('../const/sms_type');
- const SMS = require('../lib/sms');
- module.exports = app => {
- class Change extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change';
- }
- /**
- * 查找数据
- *
- * @param {Object} data - 筛选表单中的get数据
- * @return {void}
- */
- searchFilter(data) {
- this.initSqlBuilder();
- // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
- // 'group_id', 'token', 'can_login'];
- data.type = parseInt(data.status);
- if (data.keyword !== undefined) {
- switch (data.type) {
- // 用户名
- case 1:
- this.sqlBuilder.setAndWhere('username', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- // 姓名
- case 2:
- this.sqlBuilder.setAndWhere('real_name', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- // 联系电话
- case 3:
- this.sqlBuilder.setAndWhere('telephone', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- default:
- break;
- }
- }
- // 办事处筛选
- if (data.office !== undefined && data.office !== '') {
- this.sqlBuilder.setAndWhere('office', {
- value: this.db.escape(data.office),
- operate: '=',
- });
- }
- }
- async add(tenderId, userId, code, name) {
- const count = await this.count({ tid: tenderId, code });
- if (count > 0) {
- throw '变更令号重复';
- }
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const cid = this.uuid.v4();
- const change = {
- cid,
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.uncheck,
- times: 1,
- valid: true,
- in_time: new Date(),
- code,
- name,
- };
- const operate = await this.transaction.insert(this.tableName, change);
- if (operate.affectedRows <= 0) {
- throw '新建变更令数据失败';
- }
- // 把提交人信息添加到zh_change_audit
- const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
- const changeaudit = {
- tid: tenderId,
- cid,
- uid: userId,
- name: userInfo.name,
- jobs: userInfo.role,
- company: userInfo.company,
- times: 1,
- usite: 0,
- usort: 0,
- status: 2,
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
- result = change;
- this.transaction.commit();
- } catch (error) {
- console.log(error);
- // 回滚
- await this.transaction.rollback();
- }
- return result;
- }
- async pendingDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checking,
- });
- }
- async uncheckDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.uncheck,
- });
- }
- async checkingDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checking,
- });
- }
- async checkedDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checked,
- });
- }
- async checkNoDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checkNo,
- });
- }
- async checkNoCount(tenderId, userId) {
- return await this.count({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checkNo,
- });
- }
- /**
- * 获取变更令列表
- * @param {int} tenderId - 标段id
- * @param {int} status - 状态
- * @return {object} list - 列表
- */
- async getListByStatus(tenderId, status = 0) {
- let sql = '';
- let sqlParam = '';
- switch (status) {
- case 0:// 包含你的所有变更令
- sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
- '(a.uid = ? OR (a.status != 1 AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid))) ORDER BY a.in_time DESC';
- sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
- this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
- break;
- case 1:// 待处理(你的)
- sql = 'SELECT a.* FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?) ORDER BY in_time DESC';
- sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, 2];
- break;
- case 5:// 待上报(所有的)PS:取未上报和退回的变更令
- sql = 'SELECT a.* FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
- '(a.status = 1 OR a.status = 5) AND a.tid = ? ORDER BY a.in_time DESC';
- sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, tenderId];
- break;
- case 2:// 进行中(所有的)
- case 3:// 已完成(所有的)
- case 4:// 终止(所有的)
- sql = 'SELECT a.* FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND ' +
- 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC';
- sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, status, tenderId];
- break;
- default:
- break;
- }
- const limit = this.app.config.pageSize;
- const offset = limit * (this.ctx.page - 1);
- const limitString = offset >= 0 ? offset + ',' + limit : limit;
- sql += ' LIMIT ' + limitString;
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- /**
- * 获取变更令个数
- * @param {int} tenderId - 标段id
- * @param {int} status - 状态
- * @return {void}
- */
- async getCountByStatus(tenderId, status) {
- switch (status) {
- case 0:// 包含你的所有变更令
- const sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
- '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid))';
- const sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
- this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
- const result = await this.db.query(sql, sqlParam);
- return result[0].count;
- case 1:// 待处理(你的)
- return await this.ctx.service.changeAudit.count({
- tid: tenderId,
- uid: this.ctx.session.sessionUser.accountId,
- status: 2,
- });
- case 5:// 待上报(所有的)PS:取未上报和退回的变更令
- const sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
- 'AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
- const sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, tenderId];
- const result2 = await this.db.query(sql2, sqlParam2);
- return result2[0].count;
- case 2:// 进行中(所有的)
- case 3:// 已完成(所有的)
- case 4:// 终止(所有的)
- const sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
- const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, status, tenderId];
- const result3 = await this.db.query(sql3, sqlParam3);
- return result3[0].count;
- default:
- break;
- }
- }
- /**
- * 上报或重新上报或保存修改功能
- * @param {int} postData - 表单提交的数据
- * @param {int} tenderId - 标段id
- * @return {void}
- */
- async save(postData, tenderId) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- // 变更令信息
- const changeInfo = await this.getDataByCondition({ cid: postData.cid });
- // 该变更令原报人信息
- const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0);
- // 先删除本次原有的变更审批人和清单
- await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times);
- await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid });
- let change_status = false;
- // 获取变更令提交状态
- if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
- change_status = true;
- // 更新原报人审批状态
- await this.transaction.update(this.ctx.service.changeAudit.tableName, { id: lastUser.id, status: audit.flow.auditStatus.checked, sin_time: new Date() });
- }
- // 再插入postData里的变更审批人和清单
- if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
- const changeAudit = postData.changeaudit.split(',');
- const insertCA = [];
- let uSite = 1;
- let uSort = parseInt(lastUser.usort) + 1;
- for (const [index, ca] of changeAudit.entries()) {
- const auditInfo = ca.split('/%/');
- const uStatus = change_status && index === 0 ? audit.flow.auditStatus.checking : audit.flow.auditStatus.uncheck;
- const sin_time = change_status && index === 0 ? new Date() : null;
- const caArray = {
- tid: tenderId,
- cid: changeInfo.cid,
- uid: auditInfo[0],
- name: auditInfo[1],
- jobs: auditInfo[2],
- company: auditInfo[3],
- times: changeInfo.times,
- usite: uSite,
- usort: uSort,
- status: uStatus,
- sin_time,
- };
- uSite++;
- uSort++;
- insertCA.push(caArray);
- // 添加短信通知-需要审批提醒功能
- if (change_status && index === 0) {
- const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo[0]);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const content = '【纵横计量支付】' + changeInfo.code + '变更需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- }
- }
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA);
- }
- let changeList = [];
- if (postData.changelist !== undefined && postData.changelist !== '') {
- changeList = postData.changelist.split('^_^');
- }
- let changeWhiteList = [];
- if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
- changeWhiteList = postData.changewhitelist.split('^_^');
- }
- changeList.push.apply(changeList, changeWhiteList);
- const insertCL = [];
- let total_price = 0;
- if (changeList.length > 0) {
- for (const cl of changeList) {
- const clInfo = cl.split(';');
- const clArray = {
- tid: tenderId,
- cid: changeInfo.cid,
- lid: clInfo[8],
- code: clInfo[0],
- name: clInfo[1],
- bwmx: clInfo[2],
- unit: clInfo[3],
- unit_price: clInfo[4],
- oamount: clInfo[5],
- camount: clInfo[6],
- samount: '',
- detail: clInfo[7],
- };
- insertCL.push(clArray);
- total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.accMul(clArray.unit_price, clArray.camount));
- }
- await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
- }
- // 修改变更令基本数据
- const cArray = {
- code: postData.code,
- name: postData.name,
- peg: postData.peg,
- org_name: postData.org_name,
- org_code: postData.org_code,
- new_name: postData.new_name,
- new_code: postData.new_code,
- content: postData.content,
- basis: postData.basis,
- memo: postData.memo,
- type: postData.type.join(','),
- class: postData.class,
- quality: postData.quality,
- company: postData.company,
- charge: postData.charge,
- total_price,
- };
- const options = {
- where: {
- cid: changeInfo.cid,
- },
- };
- if (change_status) {
- cArray.status = audit.flow.status.checking;
- cArray.cin_time = Date.parse(new Date()) / 1000;
- }
- await this.transaction.update(this.tableName, cArray, options);
- await this.transaction.commit();
- result = true;
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 审批通过
- * @param {int} postData - 表单提交的数据
- * @param {int} changeData - 变更令的数据
- * @return {void}
- */
- async approvalSuccess(postData, changeData) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- // 设置审批人通过
- const audit_update = {
- id: postData.audit_id,
- sdesc: postData.sdesc,
- status: audit.flow.auditStatus.checked,
- sin_time: new Date(),
- };
- const change_update = {
- w_code: postData.w_code,
- status: audit.flow.status.checking,
- cin_time: Date.parse(new Date()) / 1000,
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- // 清单数据更新
- const bills_list = postData.bills_list.split(',');
- let total_price = 0;
- for (const bl of bills_list) {
- const listInfo = bl.split('_');
- const lid = listInfo[0];
- const amount = listInfo[1];
- const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid);
- if (changeListInfo !== undefined) {
- total_price += this.ctx.helper.accMul(changeListInfo.unit_price, parseFloat(amount));
- const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : [];
- audit_amount.push(amount);
- const list_update = {
- id: lid,
- audit_amount: audit_amount.join(','),
- };
- if (postData.audit_next_id === undefined) {
- list_update.samount = amount;
- }
- await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
- }
- }
- if (postData.audit_next_id === undefined) {
- // 变更令审批完成
- change_update.status = audit.flow.status.checked;
- change_update.p_code = postData.p_code;
- change_update.sin_time = Date.parse(new Date()) / 1000;
- change_update.total_price = total_price;
- // 添加短信通知-审批通过提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(changeData.uid);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const content = '【纵横计量支付】' + changeData.code + '变更,审批通过。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- } else {
- // 设置下一个审批人为审批状态
- const nextAudit_update = {
- id: postData.audit_next_id,
- status: audit.flow.auditStatus.checking,
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
- // 添加短信通知-需要审批提醒功能
- const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
- const smsUser = await this.ctx.service.projectAccount.getDataById(nextAuditData.uid);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const code = await sms.contentChange(changeData.code);
- const content = '【纵横计量支付】' + code + '变更需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- }
- const options = {
- where: {
- cid: postData.change_id,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 审批终止
- * @param {int} postData - 表单提交的数据
- * @return {void}
- */
- async approvalStop(postData) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- // 设置审批人终止
- const audit_update = {
- id: postData.audit_id,
- sdesc: postData.sdesc,
- status: 4,
- sin_time: new Date(),
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- // 设置变更令终止
- const change_update = {
- w_code: postData.w_code,
- status: 4,
- cin_time: Date.parse(new Date()) / 1000,
- };
- const options = {
- where: {
- cid: postData.change_id,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 审批退回到原报人
- * @param {int} postData - 表单提交的数据
- * @param {int} changeData - 变更令的数据
- * @return {void}
- */
- async approvalBack(postData, changeData) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
- // 设置审批人退回
- const audit_update = {
- id: postData.audit_id,
- sdesc: postData.sdesc,
- status: audit.flow.auditStatus.back,
- sin_time: new Date(),
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- // 新增新一次的审批人列表
- // 获取当前次数审批人列表
- const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
- const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
- let usort = lastauditInfo.usort + 1;
- const newTimes = changeInfo.times + 1;
- const insert_audit_array = [];
- for (const al of auditList) {
- const insert_audit = {
- tid: al.tid,
- cid: al.cid,
- uid: al.uid,
- name: al.name,
- jobs: al.jobs,
- company: al.company,
- times: newTimes,
- usite: al.usite,
- usort,
- status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
- };
- insert_audit_array.push(insert_audit);
- usort++;
- }
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
- // 设置变更令退回
- const change_update = {
- w_code: postData.w_code,
- status: audit.flow.status.back,
- times: newTimes,
- cin_time: Date.parse(new Date()) / 1000,
- };
- const options = {
- where: {
- cid: postData.change_id,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- // 添加短信通知-审批退回提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(changeData.uid);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const code = await sms.contentChange(changeData.code);
- const content = '【纵横计量支付】' + code + '变更,审批退回。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 审批退回到上一个审批人
- * @param {int} postData - 表单提交的数据
- * @param {int} changeData - 变更令的数据
- * @return {void}
- */
- async approvalBackNew(postData, changeData) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
- // 设置审批人退回
- const audit_update = {
- id: postData.audit_id,
- sdesc: postData.sdesc,
- status: audit.flow.auditStatus.backnew,
- sin_time: new Date(),
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- // 获取当前审批人信息
- const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
- // 获取当前次数审批人列表
- const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
- let usort = auditInfo.usort + 1;
- // 获取上一个审批人信息
- const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
- // 新增2个审批人到审批列表中
- const insert_audit1 = {
- tid: lastauditInfo.tid,
- cid: lastauditInfo.cid,
- uid: lastauditInfo.uid,
- name: lastauditInfo.name,
- jobs: lastauditInfo.jobs,
- company: lastauditInfo.company,
- times: lastauditInfo.times,
- usite: lastauditInfo.usite,
- usort,
- status: audit.flow.auditStatus.checking,
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
- usort++;
- // 新增2个审批人到审批列表中
- const insert_audit2 = {
- tid: auditInfo.tid,
- cid: auditInfo.cid,
- uid: auditInfo.uid,
- name: auditInfo.name,
- jobs: auditInfo.jobs,
- company: auditInfo.company,
- times: auditInfo.times,
- usite: auditInfo.usite,
- usort,
- status: audit.flow.auditStatus.uncheck,
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
- // 把接下未审批的审批人排序都加2
- for (const al of auditList) {
- const audit_update = {
- id: al.id,
- usort: al.usort + 2,
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- }
- // 审批列表数据也要回退
- const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount.split(',');
- audit_amount.splice(-1, 1);
- const list_update = {
- id: cl.id,
- audit_amount: audit_amount.join(','),
- };
- await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
- }
- // 设置变更令退回
- const change_update = {
- w_code: postData.w_code,
- status: audit.flow.status.backnew,
- cin_time: Date.parse(new Date()) / 1000,
- };
- const options = {
- where: {
- cid: postData.change_id,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- // 添加短信通知-需要审批提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(lastauditInfo.uid);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const code = await sms.contentChange(changeData.code);
- const content = '【纵横计量支付】' + code + '变更需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 查询可用的变更令
- * @param bills - 查询的清单
- * @param pos - 查询的部位
- * @returns {Promise<*>} - 可用的变更令列表
- */
- async getValidChanges(tid, bills, pos) {
- const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) + (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
- const sql = 'SELECT c.cid, c.code, c.name, c.w_code, c.p_code, c.peg, c.org_name, c.org_code, c.new_name, c.new_code,' +
- ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
- ' cb.id As cbid, cb.code As b_code, cb.name As b_name, cb.unit As b_unit, cb.samount As b_amount, cb.detail As b_detail, cb.bwmx As b_bwmx, ' +
- ' scb.used_amount' +
- ' FROM ' + this.tableName + ' As c ' +
- ' Left Join ' + this.ctx.service.changeAuditList.tableName +' As cb On c.cid = cb.cid ' +
- ' Left Join (' +
- ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
- ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
- ' INNER JOIN (SELECT Max(stimes) As `stimes`, Max(sorder) As `sorder`, cbid ' +
- ' FROM ' + this.ctx.service.stageChange.tableName +
- ' WHERE tid = ?' +
- ' GROUP BY cbid' +
- ' ) As MF' +
- ' ON sc.stimes = MF.stimes And sc.sorder = MF.sorder And sc.cbid = MF.cbid' +
- ' GROUP BY sc.cbid' +
- ' ) As scb ON cb.id = scb.cbid' +
- ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
- ' ORDER BY c.in_time';
- const sqlParam = [tid, tid, audit.flow.status.checked];
- const changes = await this.db.query(sql, sqlParam);
- for (const c of changes) {
- const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
- ' FROM ?? As ca ' +
- ' Left Join ?? As pa ' +
- ' On ca.uid = pa.id ' +
- ' Where ca.cid = ?';
- const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
- c.attachments = await this.db.query(aSql, aSqlParam);
- }
- return changes;
- }
- /**
- * 查询变更令 + 变更令执行
- * @param tid
- * @returns {Promise<void>}
- */
- async getChangeAndUsedInfo(tid) {
- const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
- let filter;
- if (lastStage.id === this.ctx.stage.id) {
- filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
- [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
- } else {
- if (lastStage.status === audit.stage.status.uncheck) {
- filter = ' And s.order < ' + lastStage.order;
- } else if (lastStage.status === audit.stage.status.checked) {
- filter = '';
- } else if (lastStage.status === audit.stage.status.checkNo) {
- filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))',
- [lastStage.order, lastStage.order, lastStage.times])
- } else {
- const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
- filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
- [lastStage.order, lastStage.order, lastStage.times, lastStage.times, curAuditor.order - 1]);
- }
- }
- const sql = 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
- ' FROM ' + this.tableName + ' As C' +
- ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
- ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
- ' INNER JOIN (' +
- ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
- ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
- ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
- ' ON sChange.sid = s.id' +
- ' WHERE sChange.tid = ?' + filter +
- ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
- ' ) As m' +
- ' ON sc.stimes = m.stimes And sc.sorder = m.sorder And sc.`cbid` = m.`cbid` AND sc.`sid` = m.`sid`' +
- ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As cb ON sc.cbid = cb.id' +
- ' GROUP By sc.`cbid`' +
- ' ) As U ON C.cid = U.cid' +
- ' WHERE C.tid = ? And C.status = ? And C.valid' +
- ' GROUP By C.cid' +
- ' ORDER By in_time';
- const sqlParam = [this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 查询可用的变更令
- * @param { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async delete(cid) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- // 先删除清单,审批人列表
- await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
- await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
- // 再删除附件和附件文件
- const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
- if (attList.length !== 0) {
- for (const att of attList) {
- await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
- }
- await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
- }
- // 最后删除变更令
- await this.transaction.delete(this.tableName, { cid });
- await this.transaction.commit();
- result = true;
- } catch (e) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 重新审批变更令
- * @param { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async checkAgain(cid) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const changeInfo = await this.getDataByCondition({ cid });
- // 获取终审
- const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
- let usort = auditInfo.usort + 1;
- // 新增2个审批状态到审批列表中
- const insert_audit1 = {
- tid: auditInfo.tid,
- cid: auditInfo.cid,
- uid: auditInfo.uid,
- name: auditInfo.name,
- jobs: auditInfo.jobs,
- company: auditInfo.company,
- times: auditInfo.times,
- usite: auditInfo.usite,
- usort,
- sin_time: new Date(),
- status: audit.flow.auditStatus.checkAgain,
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
- usort++;
- // 新增2个审批人到审批列表中
- const insert_audit2 = {
- tid: auditInfo.tid,
- cid: auditInfo.cid,
- uid: auditInfo.uid,
- name: auditInfo.name,
- jobs: auditInfo.jobs,
- company: auditInfo.company,
- times: auditInfo.times,
- usite: auditInfo.usite,
- usort,
- status: audit.flow.auditStatus.checking,
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
- // 审批列表数据也要回退
- let total_price = 0;
- const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
- for (const cl of changeList) {
- const audit_amount = cl.audit_amount.split(',');
- audit_amount.splice(-1, 1);
- const list_update = {
- id: cl.id,
- audit_amount: audit_amount.join(','),
- samount: '',
- };
- total_price += this.ctx.helper.accMul(cl.unit_price, cl.camount);
- await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
- }
- // 设置变更令审批中
- const change_update = {
- p_code: null,
- status: audit.flow.status.checking,
- cin_time: Date.parse(new Date()) / 1000,
- sin_time: null,
- total_price,
- };
- const options = {
- where: {
- cid: changeInfo.cid,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- // 添加短信通知-需要审批提醒功能
- const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo.uid);
- if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
- const smsType = JSON.parse(smsUser.sms_type);
- if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
- const sms = new SMS(this.ctx);
- const code = await sms.contentChange(changeInfo.code);
- const content = '【纵横计量支付】' + code + '变更需要您审批。';
- sms.send(smsUser.auth_mobile, content);
- }
- }
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- }
- return Change;
- };
|