change.js 46 KB

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