change.js 47 KB

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