change.js 56 KB

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