change.js 44 KB

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