change.js 45 KB

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