change.js 35 KB

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