change.js 57 KB

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