change.js 46 KB

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