change.js 49 KB

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