change.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 = ? 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 = ? 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 = ? 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 = ? 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 = ? 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[7],
  321. code: clInfo[0],
  322. name: clInfo[1],
  323. unit: clInfo[2],
  324. unit_price: clInfo[3],
  325. oamount: clInfo[4],
  326. camount: clInfo[5],
  327. samount: '',
  328. detail: clInfo[6],
  329. };
  330. insertCL.push(clArray);
  331. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.accMul(clArray.unit_price, clArray.camount));
  332. }
  333. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
  334. }
  335. // 修改变更令基本数据
  336. const cArray = {
  337. code: postData.code,
  338. name: postData.name,
  339. peg: postData.peg,
  340. org_name: postData.org_name,
  341. org_code: postData.org_code,
  342. new_name: postData.new_name,
  343. new_code: postData.new_code,
  344. content: postData.content,
  345. basis: postData.basis,
  346. memo: postData.memo,
  347. type: postData.type.join(','),
  348. class: postData.class,
  349. quality: postData.quality,
  350. company: postData.company,
  351. charge: postData.charge,
  352. total_price,
  353. };
  354. const options = {
  355. where: {
  356. cid: changeInfo.cid,
  357. },
  358. };
  359. if (change_status) {
  360. cArray.status = 2;
  361. cArray.cin_time = Date.parse(new Date()) / 1000;
  362. }
  363. await this.transaction.update(this.tableName, cArray, options);
  364. await this.transaction.commit();
  365. result = true;
  366. } catch (error) {
  367. await this.transaction.rollback();
  368. result = false;
  369. }
  370. return result;
  371. }
  372. /**
  373. * 审批通过
  374. * @param {int} postData - 表单提交的数据
  375. * @return {void}
  376. */
  377. async approvalSuccess(postData) {
  378. // 初始化事务
  379. this.transaction = await this.db.beginTransaction();
  380. let result = false;
  381. try {
  382. // 设置审批人通过
  383. const audit_update = {
  384. id: postData.audit_id,
  385. sdesc: postData.sdesc,
  386. status: 3,
  387. sin_time: new Date(),
  388. };
  389. const change_update = {
  390. w_code: postData.w_code,
  391. status: 2,
  392. cin_time: Date.parse(new Date()) / 1000,
  393. };
  394. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  395. // 清单数据更新
  396. const bills_list = postData.bills_list.split(',');
  397. let total_price = 0;
  398. for (const bl of bills_list) {
  399. const listInfo = bl.split('_');
  400. const lid = listInfo[0];
  401. const amount = listInfo[1];
  402. const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid);
  403. if (changeListInfo !== undefined) {
  404. total_price += this.ctx.helper.accMul(changeListInfo.unit_price, parseFloat(amount));
  405. const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : [];
  406. audit_amount.push(amount);
  407. const list_update = {
  408. id: lid,
  409. audit_amount: audit_amount.join(','),
  410. };
  411. if (postData.audit_next_id === undefined) {
  412. list_update.samount = amount;
  413. }
  414. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  415. }
  416. }
  417. if (postData.audit_next_id === undefined) {
  418. // 变更令审批完成
  419. change_update.status = 3;
  420. change_update.p_code = postData.p_code;
  421. change_update.sin_time = Date.parse(new Date()) / 1000;
  422. change_update.total_price = total_price;
  423. } else {
  424. // 设置下一个审批人为审批状态
  425. const nextAudit_update = {
  426. id: postData.audit_next_id,
  427. status: 2,
  428. };
  429. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  430. }
  431. const options = {
  432. where: {
  433. cid: postData.change_id,
  434. },
  435. };
  436. await this.transaction.update(this.tableName, change_update, options);
  437. await this.transaction.commit();
  438. result = true;
  439. } catch (error) {
  440. await this.transaction.rollback();
  441. result = false;
  442. }
  443. return result;
  444. }
  445. /**
  446. * 审批终止
  447. * @param {int} postData - 表单提交的数据
  448. * @return {void}
  449. */
  450. async approvalStop(postData) {
  451. // 初始化事务
  452. this.transaction = await this.db.beginTransaction();
  453. let result = false;
  454. try {
  455. // 设置审批人终止
  456. const audit_update = {
  457. id: postData.audit_id,
  458. sdesc: postData.sdesc,
  459. status: 4,
  460. sin_time: new Date(),
  461. };
  462. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  463. // 设置变更令终止
  464. const change_update = {
  465. w_code: postData.w_code,
  466. status: 4,
  467. cin_time: Date.parse(new Date()) / 1000,
  468. };
  469. const options = {
  470. where: {
  471. cid: postData.change_id,
  472. },
  473. };
  474. await this.transaction.update(this.tableName, change_update, options);
  475. await this.transaction.commit();
  476. result = true;
  477. } catch (error) {
  478. await this.transaction.rollback();
  479. result = false;
  480. }
  481. return result;
  482. }
  483. /**
  484. * 审批退回到原报人
  485. * @param {int} postData - 表单提交的数据
  486. * @return {void}
  487. */
  488. async approvalBack(postData) {
  489. // 初始化事务
  490. this.transaction = await this.db.beginTransaction();
  491. let result = false;
  492. try {
  493. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  494. // 设置审批人退回
  495. const audit_update = {
  496. id: postData.audit_id,
  497. sdesc: postData.sdesc,
  498. status: 5,
  499. sin_time: new Date(),
  500. };
  501. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  502. // 新增新一次的审批人列表
  503. // 获取当前次数审批人列表
  504. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  505. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  506. let usort = lastauditInfo.usort + 1;
  507. const newTimes = changeInfo.times + 1;
  508. const insert_audit_array = [];
  509. for (const al of auditList) {
  510. const insert_audit = {
  511. tid: al.tid,
  512. cid: al.cid,
  513. uid: al.uid,
  514. name: al.name,
  515. jobs: al.jobs,
  516. company: al.company,
  517. times: newTimes,
  518. usite: al.usite,
  519. usort,
  520. status: al.usite !== 0 ? 1 : 2,
  521. };
  522. insert_audit_array.push(insert_audit);
  523. usort++;
  524. }
  525. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  526. // 设置变更令退回
  527. const change_update = {
  528. w_code: postData.w_code,
  529. status: 5,
  530. times: newTimes,
  531. cin_time: Date.parse(new Date()) / 1000,
  532. };
  533. const options = {
  534. where: {
  535. cid: postData.change_id,
  536. },
  537. };
  538. await this.transaction.update(this.tableName, change_update, options);
  539. await this.transaction.commit();
  540. result = true;
  541. } catch (error) {
  542. await this.transaction.rollback();
  543. result = false;
  544. }
  545. return result;
  546. }
  547. /**
  548. * 审批退回到上一个审批人
  549. * @param {int} postData - 表单提交的数据
  550. * @return {void}
  551. */
  552. async approvalBackNew(postData) {
  553. // 初始化事务
  554. this.transaction = await this.db.beginTransaction();
  555. let result = false;
  556. try {
  557. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  558. // 设置审批人退回
  559. const audit_update = {
  560. id: postData.audit_id,
  561. sdesc: postData.sdesc,
  562. status: 6,
  563. sin_time: new Date(),
  564. };
  565. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  566. // 获取当前审批人信息
  567. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  568. // 获取当前次数审批人列表
  569. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  570. let usort = auditInfo.usort + 1;
  571. // 获取上一个审批人信息
  572. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  573. // 新增2个审批人到审批列表中
  574. const insert_audit1 = {
  575. tid: lastauditInfo.tid,
  576. cid: lastauditInfo.cid,
  577. uid: lastauditInfo.uid,
  578. name: lastauditInfo.name,
  579. jobs: lastauditInfo.jobs,
  580. company: lastauditInfo.company,
  581. times: lastauditInfo.times,
  582. usite: lastauditInfo.usite,
  583. usort,
  584. status: 2,
  585. };
  586. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  587. usort++;
  588. // 新增2个审批人到审批列表中
  589. const insert_audit2 = {
  590. tid: auditInfo.tid,
  591. cid: auditInfo.cid,
  592. uid: auditInfo.uid,
  593. name: auditInfo.name,
  594. jobs: auditInfo.jobs,
  595. company: auditInfo.company,
  596. times: auditInfo.times,
  597. usite: auditInfo.usite,
  598. usort,
  599. status: 1,
  600. };
  601. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  602. // 把接下未审批的审批人排序都加2
  603. for (const al of auditList) {
  604. const audit_update = {
  605. id: al.id,
  606. usort: al.usort + 2,
  607. };
  608. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  609. }
  610. // 审批列表数据也要回退
  611. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  612. for (const cl of changeList) {
  613. const audit_amount = cl.audit_amount.split(',');
  614. audit_amount.splice(-1, 1);
  615. const list_update = {
  616. id: cl.id,
  617. audit_amount: audit_amount.join(','),
  618. };
  619. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  620. }
  621. // 设置变更令退回
  622. const change_update = {
  623. w_code: postData.w_code,
  624. status: 6,
  625. cin_time: Date.parse(new Date()) / 1000,
  626. };
  627. const options = {
  628. where: {
  629. cid: postData.change_id,
  630. },
  631. };
  632. await this.transaction.update(this.tableName, change_update, options);
  633. await this.transaction.commit();
  634. result = true;
  635. } catch (error) {
  636. await this.transaction.rollback();
  637. result = false;
  638. }
  639. return result;
  640. }
  641. /**
  642. * 查询可用的变更令
  643. * @param bills - 查询的清单
  644. * @param pos - 查询的部位
  645. * @returns {Promise<*>} - 可用的变更令列表
  646. */
  647. async getValidChanges(bills, pos) {
  648. const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) + (pos ? ' And cb.`detail` = ' + this.db.escape(pos.name) : '');
  649. 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,' +
  650. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  651. ' 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 ' +
  652. ' FROM ?? As c ' +
  653. ' Left Join ?? As cb ' +
  654. ' On c.cid = cb.cid ' +
  655. ' WHERE c.status = ? And ' + filter;
  656. const sqlParam = [this.tableName, this.ctx.service.changeAuditList.tableName, audit.flow.status.checked];
  657. const changes = await this.db.query(sql, sqlParam);
  658. for (const c of changes) {
  659. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  660. ' FROM ?? As ca ' +
  661. ' Left Join ?? As pa ' +
  662. ' On ca.uid = pa.id ' +
  663. ' Where ca.cid = ?';
  664. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  665. c.attachments = await this.db.query(aSql, aSqlParam);
  666. }
  667. // const sql = 'SELECT c.* FROM ?? As c' +
  668. // ' WHERE c.`cid` in (' +
  669. // ' SELECT cb.`cid` FROM ?? As cb' +
  670. // ' WHERE ' + filter +
  671. // ' GROUP BY cb.`cid`) ' +
  672. // ' And c.status = ?';
  673. // const sqlParam = [this.tableName, this.ctx.service.changeAuditList.tableName, audit.flow.status.checked];
  674. // const changes = await this.db.query(sql, sqlParam);
  675. // for (const c of changes) {
  676. // c.bills = await this.ctx.service.changeAuditList.getDataByCondition({cid: c.cid, code: bills.b_code});
  677. // }
  678. return changes;
  679. }
  680. /**
  681. * 查询可用的变更令
  682. * @param { string } cid - 查询的清单
  683. * @return {Promise<*>} - 可用的变更令列表
  684. */
  685. async delete(cid) {
  686. // 初始化事务
  687. this.transaction = await this.db.beginTransaction();
  688. let result = false;
  689. try {
  690. // 先删除清单,审批人列表
  691. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  692. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  693. // 再删除附件和附件文件
  694. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  695. if (attList.length !== 0) {
  696. for (const att of attList) {
  697. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  698. }
  699. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  700. }
  701. // 最后删除变更令
  702. await this.transaction.delete(this.tableName, { cid });
  703. await this.transaction.commit();
  704. result = true;
  705. } catch (e) {
  706. await this.transaction.rollback();
  707. result = false;
  708. }
  709. return result;
  710. }
  711. }
  712. return Change;
  713. };