change.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. const fs = require('fs');
  11. const path = require('path');
  12. const smsTypeConst = require('../const/sms_type');
  13. const SMS = require('../lib/sms');
  14. module.exports = app => {
  15. class Change extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'change';
  25. }
  26. /**
  27. * 查找数据
  28. *
  29. * @param {Object} data - 筛选表单中的get数据
  30. * @return {void}
  31. */
  32. searchFilter(data) {
  33. this.initSqlBuilder();
  34. // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
  35. // 'group_id', 'token', 'can_login'];
  36. data.type = parseInt(data.status);
  37. if (data.keyword !== undefined) {
  38. switch (data.type) {
  39. // 用户名
  40. case 1:
  41. this.sqlBuilder.setAndWhere('username', {
  42. value: this.db.escape(data.keyword + '%'),
  43. operate: 'like',
  44. });
  45. break;
  46. // 姓名
  47. case 2:
  48. this.sqlBuilder.setAndWhere('real_name', {
  49. value: this.db.escape(data.keyword + '%'),
  50. operate: 'like',
  51. });
  52. break;
  53. // 联系电话
  54. case 3:
  55. this.sqlBuilder.setAndWhere('telephone', {
  56. value: this.db.escape(data.keyword + '%'),
  57. operate: 'like',
  58. });
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. // 办事处筛选
  65. if (data.office !== undefined && data.office !== '') {
  66. this.sqlBuilder.setAndWhere('office', {
  67. value: this.db.escape(data.office),
  68. operate: '=',
  69. });
  70. }
  71. }
  72. async add(tenderId, userId, code, name) {
  73. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?))';
  74. const sqlParam = [this.tableName, tenderId, code, audit.flow.status.checked, code, audit.flow.status.checked];
  75. const codeCount = await this.db.queryOne(sql, sqlParam);
  76. const count = codeCount.count;
  77. if (count > 0) {
  78. throw '变更令号重复';
  79. }
  80. // 初始化事务
  81. this.transaction = await this.db.beginTransaction();
  82. let result = false;
  83. try {
  84. const cid = this.uuid.v4();
  85. const change = {
  86. cid,
  87. tid: tenderId,
  88. uid: userId,
  89. status: audit.flow.status.uncheck,
  90. times: 1,
  91. valid: true,
  92. in_time: new Date(),
  93. code,
  94. name,
  95. };
  96. const operate = await this.transaction.insert(this.tableName, change);
  97. if (operate.affectedRows <= 0) {
  98. throw '新建变更令数据失败';
  99. }
  100. // 把提交人信息添加到zh_change_audit
  101. const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
  102. const changeaudit = [{
  103. tid: tenderId,
  104. cid,
  105. uid: userId,
  106. name: userInfo.name,
  107. jobs: userInfo.role,
  108. company: userInfo.company,
  109. times: 1,
  110. usite: 0,
  111. usort: 0,
  112. status: 2,
  113. }];
  114. // 并把之前存在的变更令审批人添加到zh_change_audit
  115. // 先找出标段最近存在的变更令审批人的变更令info
  116. const changeInfo = await this.ctx.service.change.getHaveAuditLastInfo(tenderId);
  117. if (changeInfo) {
  118. // 再获取非原报审批人
  119. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  120. let sort = 1;
  121. for (const audit of auditList) {
  122. if (audit.usite !== 0) {
  123. const oneaudit = {
  124. tid: tenderId,
  125. cid,
  126. uid: audit.uid,
  127. name: audit.name,
  128. jobs: audit.jobs,
  129. company: audit.company,
  130. times: 1,
  131. usite: audit.usite,
  132. usort: sort++,
  133. status: 1,
  134. };
  135. changeaudit.push(oneaudit);
  136. }
  137. }
  138. }
  139. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
  140. result = change;
  141. this.transaction.commit();
  142. } catch (error) {
  143. console.log(error);
  144. // 回滚
  145. await this.transaction.rollback();
  146. }
  147. return result;
  148. }
  149. async getHaveAuditLastInfo(tenderId) {
  150. const sql = 'SELECT * FROM ?? as a LEFT JOIN ?? as b ON a.`cid` = b.`cid` WHERE a.`tid` = ? AND b.`usite` > 0 ORDER BY a.`in_time` DESC';
  151. const sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId];
  152. return await this.db.queryOne(sql, sqlParam);
  153. }
  154. async pendingDatas(tenderId, userId) {
  155. return await this.getAllDataByCondition({
  156. tid: tenderId,
  157. uid: userId,
  158. status: audit.flow.status.checking,
  159. });
  160. }
  161. async uncheckDatas(tenderId, userId) {
  162. return await this.getAllDataByCondition({
  163. tid: tenderId,
  164. uid: userId,
  165. status: audit.flow.status.uncheck,
  166. });
  167. }
  168. async checkingDatas(tenderId, userId) {
  169. return await this.getAllDataByCondition({
  170. tid: tenderId,
  171. uid: userId,
  172. status: audit.flow.status.checking,
  173. });
  174. }
  175. async checkedDatas(tenderId, userId) {
  176. return await this.getAllDataByCondition({
  177. tid: tenderId,
  178. uid: userId,
  179. status: audit.flow.status.checked,
  180. });
  181. }
  182. async checkNoDatas(tenderId, userId) {
  183. return await this.getAllDataByCondition({
  184. tid: tenderId,
  185. uid: userId,
  186. status: audit.flow.status.checkNo,
  187. });
  188. }
  189. async checkNoCount(tenderId, userId) {
  190. return await this.count({
  191. tid: tenderId,
  192. uid: userId,
  193. status: audit.flow.status.checkNo,
  194. });
  195. }
  196. /**
  197. * 获取变更令列表
  198. * @param {int} tenderId - 标段id
  199. * @param {int} status - 状态
  200. * @return {object} list - 列表
  201. */
  202. async getListByStatus(tenderId, status = 0) {
  203. let sql = '';
  204. let sqlParam = '';
  205. switch (status) {
  206. case 0:// 包含你的所有变更令
  207. sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  208. '(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';
  209. sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  210. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  211. break;
  212. case 1:// 待处理(你的)
  213. 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';
  214. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, 2];
  215. break;
  216. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  217. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  218. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  219. '(a.status = 1 OR a.status = 5) AND a.tid = ? ORDER BY a.in_time DESC';
  220. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  221. this.ctx.session.sessionUser.accountId, tenderId];
  222. break;
  223. case 2:// 进行中(所有的)
  224. case 4:// 终止(所有的)
  225. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  226. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND ' +
  227. 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC';
  228. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  229. this.ctx.session.sessionUser.accountId, status, tenderId];
  230. break;
  231. case 3:// 已完成(所有的)
  232. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  233. 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC';
  234. sqlParam = [this.tableName, status, tenderId];
  235. break;
  236. default:
  237. break;
  238. }
  239. const limit = this.app.config.pageSize;
  240. const offset = limit * (this.ctx.page - 1);
  241. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  242. sql += ' LIMIT ' + limitString;
  243. const list = await this.db.query(sql, sqlParam);
  244. return list;
  245. }
  246. /**
  247. * 获取变更令个数
  248. * @param {int} tenderId - 标段id
  249. * @param {int} status - 状态
  250. * @return {void}
  251. */
  252. async getCountByStatus(tenderId, status) {
  253. switch (status) {
  254. case 0:// 包含你的所有变更令
  255. const sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  256. '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid))';
  257. const sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  258. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  259. const result = await this.db.query(sql, sqlParam);
  260. return result[0].count;
  261. case 1:// 待处理(你的)
  262. return await this.ctx.service.changeAudit.count({
  263. tid: tenderId,
  264. uid: this.ctx.session.sessionUser.accountId,
  265. status: 2,
  266. });
  267. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  268. const sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  269. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
  270. 'AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
  271. const sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName,
  272. this.ctx.session.sessionUser.accountId, tenderId];
  273. const result2 = await this.db.query(sql2, sqlParam2);
  274. return result2[0].count;
  275. case 2:// 进行中(所有的)
  276. case 4:// 终止(所有的)
  277. const sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  278. '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 = ?';
  279. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName,
  280. this.ctx.session.sessionUser.accountId, status, tenderId];
  281. const result3 = await this.db.query(sql3, sqlParam3);
  282. return result3[0].count;
  283. case 3:// 已完成(所有的)
  284. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  285. const sqlParam4 = [this.tableName, status, tenderId];
  286. const result4 = await this.db.query(sql4, sqlParam4);
  287. return result4[0].count;
  288. default:
  289. break;
  290. }
  291. }
  292. /**
  293. * 上报或重新上报或保存修改功能
  294. * @param {int} postData - 表单提交的数据
  295. * @param {int} tenderId - 标段id
  296. * @return {void}
  297. */
  298. async save(postData, tenderId) {
  299. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tenderId);
  300. // 初始化事务
  301. this.transaction = await this.db.beginTransaction();
  302. let result = false;
  303. try {
  304. // 变更令信息
  305. const changeInfo = await this.getDataByCondition({ cid: postData.cid });
  306. // 该变更令原报人信息
  307. const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0);
  308. // 先删除本次原有的变更审批人和清单
  309. await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times);
  310. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid });
  311. let change_status = false;
  312. // 获取变更令提交状态
  313. if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
  314. change_status = true;
  315. // 更新原报人审批状态
  316. await this.transaction.update(this.ctx.service.changeAudit.tableName, { id: lastUser.id, status: audit.flow.auditStatus.checked, sin_time: new Date() });
  317. }
  318. // 再插入postData里的变更审批人和清单
  319. if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
  320. const changeAudit = postData.changeaudit.split(',');
  321. const insertCA = [];
  322. let uSite = 1;
  323. let uSort = parseInt(lastUser.usort) + 1;
  324. for (const [index, ca] of changeAudit.entries()) {
  325. const auditInfo = ca.split('/%/');
  326. const uStatus = change_status && index === 0 ? audit.flow.auditStatus.checking : audit.flow.auditStatus.uncheck;
  327. const sin_time = change_status && index === 0 ? new Date() : null;
  328. const caArray = {
  329. tid: tenderId,
  330. cid: changeInfo.cid,
  331. uid: auditInfo[0],
  332. name: auditInfo[1],
  333. jobs: auditInfo[2],
  334. company: auditInfo[3],
  335. times: changeInfo.times,
  336. usite: uSite,
  337. usort: uSort,
  338. status: uStatus,
  339. sin_time,
  340. };
  341. uSite++;
  342. uSort++;
  343. insertCA.push(caArray);
  344. // 添加短信通知-需要审批提醒功能
  345. if (change_status && index === 0) {
  346. const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo[0]);
  347. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  348. const smsType = JSON.parse(smsUser.sms_type);
  349. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  350. const sms = new SMS(this.ctx);
  351. const content = '【纵横计量支付】' + changeInfo.code + '变更需要您审批。';
  352. sms.send(smsUser.auth_mobile, content);
  353. }
  354. }
  355. }
  356. }
  357. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA);
  358. }
  359. let changeList = [];
  360. if (postData.changelist !== undefined && postData.changelist !== '') {
  361. changeList = postData.changelist.split('^_^');
  362. }
  363. let changeWhiteList = [];
  364. if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
  365. changeWhiteList = postData.changewhitelist.split('^_^');
  366. }
  367. changeList.push.apply(changeList, changeWhiteList);
  368. const insertCL = [];
  369. let total_price = 0;
  370. if (changeList.length > 0) {
  371. for (const cl of changeList) {
  372. const clInfo = cl.split(';');
  373. const clArray = {
  374. tid: tenderId,
  375. cid: changeInfo.cid,
  376. lid: clInfo[8],
  377. code: clInfo[0],
  378. name: clInfo[1],
  379. bwmx: clInfo[2],
  380. unit: clInfo[3],
  381. unit_price: clInfo[4],
  382. oamount: clInfo[5],
  383. camount: clInfo[6],
  384. samount: '',
  385. detail: clInfo[7],
  386. spamount: clInfo[6],
  387. };
  388. if (clInfo[4] === '') {
  389. delete clArray.unit_price;
  390. }
  391. insertCL.push(clArray);
  392. total_price = this.ctx.helper.accAdd(total_price,
  393. this.ctx.helper.mul(clArray.unit_price, clArray.spamount, tenderInfo.decimal.tp));
  394. }
  395. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
  396. }
  397. // 修改变更令基本数据
  398. const cArray = {
  399. code: postData.code,
  400. name: postData.name,
  401. peg: postData.peg,
  402. org_name: postData.org_name,
  403. org_code: postData.org_code,
  404. new_name: postData.new_name,
  405. new_code: postData.new_code,
  406. content: postData.content,
  407. basis: postData.basis,
  408. expr: postData.expr,
  409. memo: postData.memo,
  410. type: postData.type.join(','),
  411. class: postData.class,
  412. quality: postData.quality,
  413. company: postData.company,
  414. charge: postData.charge,
  415. total_price,
  416. };
  417. const options = {
  418. where: {
  419. cid: changeInfo.cid,
  420. },
  421. };
  422. if (change_status) {
  423. cArray.status = audit.flow.status.checking;
  424. cArray.cin_time = Date.parse(new Date()) / 1000;
  425. }
  426. await this.transaction.update(this.tableName, cArray, options);
  427. await this.transaction.commit();
  428. result = true;
  429. } catch (error) {
  430. await this.transaction.rollback();
  431. result = false;
  432. }
  433. return result;
  434. }
  435. /**
  436. * 审批通过
  437. * @param {int} postData - 表单提交的数据
  438. * @param {int} changeData - 变更令的数据
  439. * @return {void}
  440. */
  441. async approvalSuccess(postData, changeData) {
  442. let tenderInfo;
  443. // 初始化事务
  444. this.transaction = await this.db.beginTransaction();
  445. let result = false;
  446. try {
  447. // 设置审批人通过
  448. const audit_update = {
  449. id: postData.audit_id,
  450. sdesc: postData.sdesc,
  451. status: audit.flow.auditStatus.checked,
  452. sin_time: new Date(),
  453. };
  454. const change_update = {
  455. w_code: postData.w_code,
  456. status: audit.flow.status.checking,
  457. cin_time: Date.parse(new Date()) / 1000,
  458. };
  459. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  460. // 清单数据更新
  461. const bills_list = postData.bills_list.split(',');
  462. let total_price = 0;
  463. for (const bl of bills_list) {
  464. const listInfo = bl.split('_');
  465. const lid = listInfo[0];
  466. const amount = listInfo[1];
  467. const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid);
  468. if (!tenderInfo) {
  469. tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeListInfo.tid);
  470. }
  471. if (changeListInfo !== undefined) {
  472. total_price = this.ctx.helper.add(total_price,
  473. this.ctx.helper.mul(changeListInfo.unit_price, amount, tenderInfo.decimal.tp));
  474. const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : [];
  475. audit_amount.push(amount);
  476. const list_update = {
  477. id: lid,
  478. audit_amount: audit_amount.join(','),
  479. spamount: parseFloat(amount),
  480. };
  481. if (postData.audit_next_id === undefined) {
  482. list_update.samount = amount;
  483. }
  484. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  485. }
  486. }
  487. if (postData.audit_next_id === undefined) {
  488. // 变更令审批完成
  489. change_update.status = audit.flow.status.checked;
  490. change_update.p_code = postData.p_code;
  491. change_update.sin_time = Date.parse(new Date()) / 1000;
  492. // 添加短信通知-审批通过提醒功能
  493. const mobile_array = [];
  494. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  495. for (const user of auditList) {
  496. const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  497. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  498. const smsType = JSON.parse(smsUser.sms_type);
  499. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  500. mobile_array.push(smsUser.auth_mobile);
  501. }
  502. }
  503. }
  504. if (mobile_array.length > 0) {
  505. const sms = new SMS(this.ctx);
  506. const content = '【纵横计量支付】' + changeData.code + '变更,审批通过。';
  507. sms.send(mobile_array, content);
  508. }
  509. } else {
  510. // 设置下一个审批人为审批状态
  511. const nextAudit_update = {
  512. id: postData.audit_next_id,
  513. status: audit.flow.auditStatus.checking,
  514. };
  515. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  516. // 添加短信通知-需要审批提醒功能
  517. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
  518. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAuditData.uid);
  519. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  520. const smsType = JSON.parse(smsUser.sms_type);
  521. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  522. const sms = new SMS(this.ctx);
  523. const code = await sms.contentChange(changeData.code);
  524. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  525. sms.send(smsUser.auth_mobile, content);
  526. }
  527. }
  528. }
  529. change_update.total_price = total_price;
  530. const options = {
  531. where: {
  532. cid: postData.change_id,
  533. },
  534. };
  535. await this.transaction.update(this.tableName, change_update, options);
  536. await this.transaction.commit();
  537. result = true;
  538. } catch (error) {
  539. console.log(error);
  540. await this.transaction.rollback();
  541. result = false;
  542. }
  543. return result;
  544. }
  545. /**
  546. * 审批终止
  547. * @param {int} postData - 表单提交的数据
  548. * @return {void}
  549. */
  550. async approvalStop(postData) {
  551. // 初始化事务
  552. this.transaction = await this.db.beginTransaction();
  553. let result = false;
  554. try {
  555. // 设置审批人终止
  556. const audit_update = {
  557. id: postData.audit_id,
  558. sdesc: postData.sdesc,
  559. status: 4,
  560. sin_time: new Date(),
  561. };
  562. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  563. // 设置变更令终止
  564. const change_update = {
  565. w_code: postData.w_code,
  566. status: 4,
  567. cin_time: Date.parse(new Date()) / 1000,
  568. };
  569. const options = {
  570. where: {
  571. cid: postData.change_id,
  572. },
  573. };
  574. await this.transaction.update(this.tableName, change_update, options);
  575. await this.transaction.commit();
  576. result = true;
  577. } catch (error) {
  578. await this.transaction.rollback();
  579. result = false;
  580. }
  581. return result;
  582. }
  583. /**
  584. * 审批退回到原报人
  585. * @param {int} postData - 表单提交的数据
  586. * @param {int} changeData - 变更令的数据
  587. * @return {void}
  588. */
  589. async approvalBack(postData, changeData) {
  590. // 初始化事务
  591. this.transaction = await this.db.beginTransaction();
  592. let result = false;
  593. try {
  594. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  595. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  596. // 设置审批人退回
  597. const audit_update = {
  598. id: postData.audit_id,
  599. sdesc: postData.sdesc,
  600. status: audit.flow.auditStatus.back,
  601. sin_time: new Date(),
  602. };
  603. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  604. // 新增新一次的审批人列表
  605. // 获取当前次数审批人列表
  606. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  607. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  608. let usort = lastauditInfo.usort + 1;
  609. const newTimes = changeInfo.times + 1;
  610. const insert_audit_array = [];
  611. for (const al of auditList) {
  612. const insert_audit = {
  613. tid: al.tid,
  614. cid: al.cid,
  615. uid: al.uid,
  616. name: al.name,
  617. jobs: al.jobs,
  618. company: al.company,
  619. times: newTimes,
  620. usite: al.usite,
  621. usort,
  622. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  623. };
  624. insert_audit_array.push(insert_audit);
  625. usort++;
  626. }
  627. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  628. // 变更金额也退回
  629. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  630. let total_price = 0;
  631. for (const cl of changeList) {
  632. total_price = this.ctx.helper.add(total_price,
  633. this.ctx.helper.mul(cl.unit_price, cl.camount, tenderInfo.decimal.tp));
  634. }
  635. // 设置变更令退回
  636. const change_update = {
  637. w_code: postData.w_code,
  638. status: audit.flow.status.back,
  639. times: newTimes,
  640. cin_time: Date.parse(new Date()) / 1000,
  641. total_price,
  642. };
  643. const options = {
  644. where: {
  645. cid: postData.change_id,
  646. },
  647. };
  648. await this.transaction.update(this.tableName, change_update, options);
  649. await this.transaction.commit();
  650. result = true;
  651. // 添加短信通知-审批退回提醒功能
  652. const mobile_array = [];
  653. for (const user of insert_audit_array) {
  654. const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  655. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  656. const smsType = JSON.parse(smsUser.sms_type);
  657. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  658. mobile_array.push(smsUser.auth_mobile);
  659. }
  660. }
  661. }
  662. if (mobile_array.length > 0) {
  663. const sms = new SMS(this.ctx);
  664. const code = await sms.contentChange(changeData.code);
  665. const content = '【纵横计量支付】' + code + '变更,审批退回。';
  666. sms.send(mobile_array, content);
  667. }
  668. } catch (error) {
  669. await this.transaction.rollback();
  670. result = false;
  671. }
  672. return result;
  673. }
  674. /**
  675. * 审批退回到上一个审批人
  676. * @param {int} postData - 表单提交的数据
  677. * @param {int} changeData - 变更令的数据
  678. * @return {void}
  679. */
  680. async approvalBackNew(postData, changeData) {
  681. // 初始化事务
  682. this.transaction = await this.db.beginTransaction();
  683. let result = false;
  684. try {
  685. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  686. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  687. // 设置审批人退回
  688. const audit_update = {
  689. id: postData.audit_id,
  690. sdesc: postData.sdesc,
  691. status: audit.flow.auditStatus.backnew,
  692. sin_time: new Date(),
  693. };
  694. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  695. // 获取当前审批人信息
  696. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  697. // 获取当前次数审批人列表
  698. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  699. let usort = auditInfo.usort + 1;
  700. // 获取上一个审批人信息
  701. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  702. // 新增2个审批人到审批列表中
  703. const insert_audit1 = {
  704. tid: lastauditInfo.tid,
  705. cid: lastauditInfo.cid,
  706. uid: lastauditInfo.uid,
  707. name: lastauditInfo.name,
  708. jobs: lastauditInfo.jobs,
  709. company: lastauditInfo.company,
  710. times: lastauditInfo.times,
  711. usite: lastauditInfo.usite,
  712. usort,
  713. status: audit.flow.auditStatus.checking,
  714. };
  715. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  716. usort++;
  717. // 新增2个审批人到审批列表中
  718. const insert_audit2 = {
  719. tid: auditInfo.tid,
  720. cid: auditInfo.cid,
  721. uid: auditInfo.uid,
  722. name: auditInfo.name,
  723. jobs: auditInfo.jobs,
  724. company: auditInfo.company,
  725. times: auditInfo.times,
  726. usite: auditInfo.usite,
  727. usort,
  728. status: audit.flow.auditStatus.uncheck,
  729. };
  730. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  731. // 把接下未审批的审批人排序都加2
  732. for (const al of auditList) {
  733. const audit_update = {
  734. id: al.id,
  735. usort: al.usort + 2,
  736. };
  737. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  738. }
  739. // 审批列表数据也要回退
  740. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  741. let total_price = 0;
  742. for (const cl of changeList) {
  743. const audit_amount = cl.audit_amount.split(',');
  744. const last_amount = audit_amount[audit_amount.length - 1];
  745. audit_amount.splice(-1, 1);
  746. const list_update = {
  747. id: cl.id,
  748. audit_amount: audit_amount.join(','),
  749. spamount: parseFloat(last_amount),
  750. };
  751. total_price = this.ctx.helper.add(total_price,
  752. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  753. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  754. }
  755. // 设置变更令退回
  756. const change_update = {
  757. w_code: postData.w_code,
  758. status: audit.flow.status.backnew,
  759. cin_time: Date.parse(new Date()) / 1000,
  760. total_price,
  761. };
  762. const options = {
  763. where: {
  764. cid: postData.change_id,
  765. },
  766. };
  767. await this.transaction.update(this.tableName, change_update, options);
  768. await this.transaction.commit();
  769. result = true;
  770. // 添加短信通知-需要审批提醒功能
  771. const smsUser = await this.ctx.service.projectAccount.getDataById(lastauditInfo.uid);
  772. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  773. const smsType = JSON.parse(smsUser.sms_type);
  774. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  775. const sms = new SMS(this.ctx);
  776. const code = await sms.contentChange(changeData.code);
  777. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  778. sms.send(smsUser.auth_mobile, content);
  779. }
  780. }
  781. } catch (error) {
  782. await this.transaction.rollback();
  783. result = false;
  784. }
  785. return result;
  786. }
  787. /**
  788. * 查询可用的变更令
  789. * @param bills - 查询的清单
  790. * @param pos - 查询的部位
  791. * @returns {Promise<*>} - 可用的变更令列表
  792. */
  793. async getValidChanges(tid, bills, pos) {
  794. const timesLen = 100;
  795. const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) + (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
  796. 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,' +
  797. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  798. ' 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, ' +
  799. ' scb.used_amount' +
  800. ' FROM ' + this.tableName + ' As c ' +
  801. ' Left Join ' + this.ctx.service.changeAuditList.tableName +' As cb On c.cid = cb.cid ' +
  802. ' Left Join (' +
  803. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  804. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  805. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  806. ' FROM ' + this.ctx.service.stageChange.tableName +
  807. ' WHERE tid = ?' +
  808. ' GROUP BY cbid, sid' +
  809. ' ) As MF' +
  810. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  811. ' GROUP BY sc.cbid' +
  812. ' ) As scb ON cb.id = scb.cbid' +
  813. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  814. ' ORDER BY c.in_time';
  815. const sqlParam = [tid, tid, audit.flow.status.checked];
  816. const changes = await this.db.query(sql, sqlParam);
  817. for (const c of changes) {
  818. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  819. ' FROM ?? As ca ' +
  820. ' Left Join ?? As pa ' +
  821. ' On ca.uid = pa.id ' +
  822. ' Where ca.cid = ?';
  823. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  824. c.attachments = await this.db.query(aSql, aSqlParam);
  825. }
  826. return changes;
  827. }
  828. /**
  829. * 查询变更令 + 变更令执行
  830. * @param tid
  831. * @returns {Promise<void>}
  832. */
  833. async getChangeAndUsedInfo(tid) {
  834. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  835. let filter;
  836. if (lastStage.id === this.ctx.stage.id) {
  837. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  838. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  839. } else {
  840. if (lastStage.status === audit.stage.status.uncheck) {
  841. filter = ' And s.order < ' + lastStage.order;
  842. } else if (lastStage.status === audit.stage.status.checked) {
  843. filter = '';
  844. } else if (lastStage.status === audit.stage.status.checkNo) {
  845. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))',
  846. [lastStage.order, lastStage.order, lastStage.times])
  847. } else {
  848. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  849. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  850. [lastStage.order, lastStage.order, lastStage.times, lastStage.times, curAuditor.order - 1]);
  851. }
  852. }
  853. const sql = 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
  854. ' FROM ' + this.tableName + ' As C' +
  855. ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
  856. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  857. ' INNER JOIN (' +
  858. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  859. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
  860. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
  861. ' ON sChange.sid = s.id' +
  862. ' WHERE sChange.tid = ?' + filter +
  863. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  864. ' ) As m' +
  865. ' ON sc.stimes = m.stimes And sc.sorder = m.sorder And sc.`cbid` = m.`cbid` AND sc.`sid` = m.`sid` And sc.`lid` = m.`lid` And sc.`pid` = m.`pid`' +
  866. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As cb ON sc.cbid = cb.id' +
  867. ' GROUP By sc.`cbid`' +
  868. ' ) As U ON C.cid = U.cid' +
  869. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  870. ' GROUP By C.cid' +
  871. ' ORDER By in_time';
  872. const sqlParam = [this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
  873. return await this.db.query(sql, sqlParam);
  874. }
  875. /**
  876. * 查询可用的变更令
  877. * @param { string } cid - 查询的清单
  878. * @return {Promise<*>} - 可用的变更令列表
  879. */
  880. async delete(cid) {
  881. // 初始化事务
  882. this.transaction = await this.db.beginTransaction();
  883. let result = false;
  884. try {
  885. // 先删除清单,审批人列表
  886. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  887. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  888. // 再删除附件和附件文件
  889. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  890. if (attList.length !== 0) {
  891. for (const att of attList) {
  892. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  893. }
  894. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  895. }
  896. // 最后删除变更令
  897. await this.transaction.delete(this.tableName, { cid });
  898. await this.transaction.commit();
  899. result = true;
  900. } catch (e) {
  901. await this.transaction.rollback();
  902. result = false;
  903. }
  904. return result;
  905. }
  906. /**
  907. * 重新审批变更令
  908. * @param { string } cid - 查询的清单
  909. * @return {Promise<*>} - 可用的变更令列表
  910. */
  911. async checkAgain(cid) {
  912. // 初始化事务
  913. this.transaction = await this.db.beginTransaction();
  914. let result = false;
  915. try {
  916. const changeInfo = await this.getDataByCondition({ cid });
  917. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  918. // 获取终审
  919. const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
  920. let usort = auditInfo.usort + 1;
  921. // 新增2个审批状态到审批列表中
  922. const insert_audit1 = {
  923. tid: auditInfo.tid,
  924. cid: auditInfo.cid,
  925. uid: auditInfo.uid,
  926. name: auditInfo.name,
  927. jobs: auditInfo.jobs,
  928. company: auditInfo.company,
  929. times: auditInfo.times,
  930. usite: auditInfo.usite,
  931. usort,
  932. sin_time: new Date(),
  933. status: audit.flow.auditStatus.checkAgain,
  934. };
  935. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  936. usort++;
  937. // 新增2个审批人到审批列表中
  938. const insert_audit2 = {
  939. tid: auditInfo.tid,
  940. cid: auditInfo.cid,
  941. uid: auditInfo.uid,
  942. name: auditInfo.name,
  943. jobs: auditInfo.jobs,
  944. company: auditInfo.company,
  945. times: auditInfo.times,
  946. usite: auditInfo.usite,
  947. usort,
  948. status: audit.flow.auditStatus.checking,
  949. };
  950. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  951. // 审批列表数据也要回退
  952. let total_price = 0;
  953. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  954. for (const cl of changeList) {
  955. const audit_amount = cl.audit_amount.split(',');
  956. const last_amount = audit_amount[audit_amount.length - 1];
  957. audit_amount.splice(-1, 1);
  958. const list_update = {
  959. id: cl.id,
  960. audit_amount: audit_amount.join(','),
  961. samount: '',
  962. };
  963. total_price = this.ctx.helper.add(total_price,
  964. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  965. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  966. }
  967. // 设置变更令审批中
  968. const change_update = {
  969. p_code: null,
  970. status: audit.flow.status.checking,
  971. cin_time: Date.parse(new Date()) / 1000,
  972. sin_time: null,
  973. total_price,
  974. };
  975. const options = {
  976. where: {
  977. cid: changeInfo.cid,
  978. },
  979. };
  980. await this.transaction.update(this.tableName, change_update, options);
  981. await this.transaction.commit();
  982. result = true;
  983. // 添加短信通知-需要审批提醒功能
  984. const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo.uid);
  985. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  986. const smsType = JSON.parse(smsUser.sms_type);
  987. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  988. const sms = new SMS(this.ctx);
  989. const code = await sms.contentChange(changeInfo.code);
  990. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  991. sms.send(smsUser.auth_mobile, content);
  992. }
  993. }
  994. } catch (error) {
  995. await this.transaction.rollback();
  996. result = false;
  997. }
  998. return result;
  999. }
  1000. /**
  1001. * 判断是否有重名的变更令
  1002. * @param cid
  1003. * @param code
  1004. * @param tid
  1005. * @returns {Promise<void>}
  1006. */
  1007. async isRepeat(cid, code, tid) {
  1008. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?';
  1009. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid];
  1010. const result = await this.db.queryOne(sql, sqlParam);
  1011. return result.count !== 0;
  1012. }
  1013. async getAllCheckedChanges(tid) {
  1014. return await this.getAllDataByCondition({
  1015. where: {tid: tid, status: audit.flow.status.checked},
  1016. orders: [['in_time', 'desc']]
  1017. })
  1018. }
  1019. }
  1020. return Change;
  1021. };