change.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?))';
  74. const sqlParam = [this.tableName, tenderId, code, audit.flow.status.checked, code, audit.flow.status.checked];
  75. const codeCount = await this.db.queryOne(sql, sqlParam);
  76. const count = codeCount.count;
  77. if (count > 0) {
  78. throw '变更令号重复';
  79. }
  80. // 初始化事务
  81. this.transaction = await this.db.beginTransaction();
  82. let result = false;
  83. try {
  84. const cid = this.uuid.v4();
  85. const change = {
  86. cid,
  87. tid: tenderId,
  88. uid: userId,
  89. status: audit.flow.status.uncheck,
  90. times: 1,
  91. valid: true,
  92. in_time: new Date(),
  93. code,
  94. name,
  95. };
  96. const operate = await this.transaction.insert(this.tableName, change);
  97. if (operate.affectedRows <= 0) {
  98. throw '新建变更令数据失败';
  99. }
  100. // 把提交人信息添加到zh_change_audit
  101. const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
  102. const changeaudit = {
  103. tid: tenderId,
  104. cid,
  105. uid: userId,
  106. name: userInfo.name,
  107. jobs: userInfo.role,
  108. company: userInfo.company,
  109. times: 1,
  110. usite: 0,
  111. usort: 0,
  112. status: 2,
  113. };
  114. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
  115. result = change;
  116. this.transaction.commit();
  117. } catch (error) {
  118. console.log(error);
  119. // 回滚
  120. await this.transaction.rollback();
  121. }
  122. return result;
  123. }
  124. async pendingDatas(tenderId, userId) {
  125. return await this.getAllDataByCondition({
  126. tid: tenderId,
  127. uid: userId,
  128. status: audit.flow.status.checking,
  129. });
  130. }
  131. async uncheckDatas(tenderId, userId) {
  132. return await this.getAllDataByCondition({
  133. tid: tenderId,
  134. uid: userId,
  135. status: audit.flow.status.uncheck,
  136. });
  137. }
  138. async checkingDatas(tenderId, userId) {
  139. return await this.getAllDataByCondition({
  140. tid: tenderId,
  141. uid: userId,
  142. status: audit.flow.status.checking,
  143. });
  144. }
  145. async checkedDatas(tenderId, userId) {
  146. return await this.getAllDataByCondition({
  147. tid: tenderId,
  148. uid: userId,
  149. status: audit.flow.status.checked,
  150. });
  151. }
  152. async checkNoDatas(tenderId, userId) {
  153. return await this.getAllDataByCondition({
  154. tid: tenderId,
  155. uid: userId,
  156. status: audit.flow.status.checkNo,
  157. });
  158. }
  159. async checkNoCount(tenderId, userId) {
  160. return await this.count({
  161. tid: tenderId,
  162. uid: userId,
  163. status: audit.flow.status.checkNo,
  164. });
  165. }
  166. /**
  167. * 获取变更令列表
  168. * @param {int} tenderId - 标段id
  169. * @param {int} status - 状态
  170. * @return {object} list - 列表
  171. */
  172. async getListByStatus(tenderId, status = 0) {
  173. let sql = '';
  174. let sqlParam = '';
  175. switch (status) {
  176. case 0:// 包含你的所有变更令
  177. sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  178. '(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';
  179. sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  180. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  181. break;
  182. case 1:// 待处理(你的)
  183. 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';
  184. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, 2];
  185. break;
  186. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  187. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  188. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  189. '(a.status = 1 OR a.status = 5) AND a.tid = ? ORDER BY a.in_time DESC';
  190. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  191. this.ctx.session.sessionUser.accountId, tenderId];
  192. break;
  193. case 2:// 进行中(所有的)
  194. case 3:// 已完成(所有的)
  195. case 4:// 终止(所有的)
  196. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  197. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) AND ' +
  198. 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC';
  199. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  200. this.ctx.session.sessionUser.accountId, status, tenderId];
  201. break;
  202. default:
  203. break;
  204. }
  205. const limit = this.app.config.pageSize;
  206. const offset = limit * (this.ctx.page - 1);
  207. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  208. sql += ' LIMIT ' + limitString;
  209. const list = await this.db.query(sql, sqlParam);
  210. return list;
  211. }
  212. /**
  213. * 获取变更令个数
  214. * @param {int} tenderId - 标段id
  215. * @param {int} status - 状态
  216. * @return {void}
  217. */
  218. async getCountByStatus(tenderId, status) {
  219. switch (status) {
  220. case 0:// 包含你的所有变更令
  221. const sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  222. '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid))';
  223. const sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  224. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  225. const result = await this.db.query(sql, sqlParam);
  226. return result[0].count;
  227. case 1:// 待处理(你的)
  228. return await this.ctx.service.changeAudit.count({
  229. tid: tenderId,
  230. uid: this.ctx.session.sessionUser.accountId,
  231. status: 2,
  232. });
  233. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  234. const sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  235. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? AND a.times = b.times GROUP BY b.cid) ' +
  236. 'AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
  237. const sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName,
  238. this.ctx.session.sessionUser.accountId, tenderId];
  239. const result2 = await this.db.query(sql2, sqlParam2);
  240. return result2[0].count;
  241. case 2:// 进行中(所有的)
  242. case 3:// 已完成(所有的)
  243. case 4:// 终止(所有的)
  244. const sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  245. '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 = ?';
  246. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName,
  247. this.ctx.session.sessionUser.accountId, status, tenderId];
  248. const result3 = await this.db.query(sql3, sqlParam3);
  249. return result3[0].count;
  250. default:
  251. break;
  252. }
  253. }
  254. /**
  255. * 上报或重新上报或保存修改功能
  256. * @param {int} postData - 表单提交的数据
  257. * @param {int} tenderId - 标段id
  258. * @return {void}
  259. */
  260. async save(postData, tenderId) {
  261. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tenderId);
  262. // 初始化事务
  263. this.transaction = await this.db.beginTransaction();
  264. let result = false;
  265. try {
  266. // 变更令信息
  267. const changeInfo = await this.getDataByCondition({ cid: postData.cid });
  268. // 该变更令原报人信息
  269. const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0);
  270. // 先删除本次原有的变更审批人和清单
  271. await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times);
  272. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid });
  273. let change_status = false;
  274. // 获取变更令提交状态
  275. if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
  276. change_status = true;
  277. // 更新原报人审批状态
  278. await this.transaction.update(this.ctx.service.changeAudit.tableName, { id: lastUser.id, status: audit.flow.auditStatus.checked, sin_time: new Date() });
  279. }
  280. // 再插入postData里的变更审批人和清单
  281. if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
  282. const changeAudit = postData.changeaudit.split(',');
  283. const insertCA = [];
  284. let uSite = 1;
  285. let uSort = parseInt(lastUser.usort) + 1;
  286. for (const [index, ca] of changeAudit.entries()) {
  287. const auditInfo = ca.split('/%/');
  288. const uStatus = change_status && index === 0 ? audit.flow.auditStatus.checking : audit.flow.auditStatus.uncheck;
  289. const sin_time = change_status && index === 0 ? new Date() : null;
  290. const caArray = {
  291. tid: tenderId,
  292. cid: changeInfo.cid,
  293. uid: auditInfo[0],
  294. name: auditInfo[1],
  295. jobs: auditInfo[2],
  296. company: auditInfo[3],
  297. times: changeInfo.times,
  298. usite: uSite,
  299. usort: uSort,
  300. status: uStatus,
  301. sin_time,
  302. };
  303. uSite++;
  304. uSort++;
  305. insertCA.push(caArray);
  306. // 添加短信通知-需要审批提醒功能
  307. if (change_status && index === 0) {
  308. const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo[0]);
  309. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  310. const smsType = JSON.parse(smsUser.sms_type);
  311. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  312. const sms = new SMS(this.ctx);
  313. const content = '【纵横计量支付】' + changeInfo.code + '变更需要您审批。';
  314. sms.send(smsUser.auth_mobile, content);
  315. }
  316. }
  317. }
  318. }
  319. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA);
  320. }
  321. let changeList = [];
  322. if (postData.changelist !== undefined && postData.changelist !== '') {
  323. changeList = postData.changelist.split('^_^');
  324. }
  325. let changeWhiteList = [];
  326. if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
  327. changeWhiteList = postData.changewhitelist.split('^_^');
  328. }
  329. changeList.push.apply(changeList, changeWhiteList);
  330. const insertCL = [];
  331. let total_price = 0;
  332. if (changeList.length > 0) {
  333. for (const cl of changeList) {
  334. const clInfo = cl.split(';');
  335. const clArray = {
  336. tid: tenderId,
  337. cid: changeInfo.cid,
  338. lid: clInfo[8],
  339. code: clInfo[0],
  340. name: clInfo[1],
  341. bwmx: clInfo[2],
  342. unit: clInfo[3],
  343. unit_price: clInfo[4],
  344. oamount: clInfo[5],
  345. camount: clInfo[6],
  346. samount: '',
  347. detail: clInfo[7],
  348. spamount: clInfo[6],
  349. };
  350. if (clInfo[4] === '') {
  351. delete clArray.unit_price;
  352. }
  353. insertCL.push(clArray);
  354. total_price = this.ctx.helper.accAdd(total_price,
  355. this.ctx.helper.mul(clArray.unit_price, clArray.spamount, tenderInfo.decimal.tp));
  356. }
  357. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
  358. }
  359. // 修改变更令基本数据
  360. const cArray = {
  361. code: postData.code,
  362. name: postData.name,
  363. peg: postData.peg,
  364. org_name: postData.org_name,
  365. org_code: postData.org_code,
  366. new_name: postData.new_name,
  367. new_code: postData.new_code,
  368. content: postData.content,
  369. basis: postData.basis,
  370. expr: postData.expr,
  371. memo: postData.memo,
  372. type: postData.type.join(','),
  373. class: postData.class,
  374. quality: postData.quality,
  375. company: postData.company,
  376. charge: postData.charge,
  377. total_price,
  378. };
  379. const options = {
  380. where: {
  381. cid: changeInfo.cid,
  382. },
  383. };
  384. if (change_status) {
  385. cArray.status = audit.flow.status.checking;
  386. cArray.cin_time = Date.parse(new Date()) / 1000;
  387. }
  388. await this.transaction.update(this.tableName, cArray, options);
  389. await this.transaction.commit();
  390. result = true;
  391. } catch (error) {
  392. await this.transaction.rollback();
  393. result = false;
  394. }
  395. return result;
  396. }
  397. /**
  398. * 审批通过
  399. * @param {int} postData - 表单提交的数据
  400. * @param {int} changeData - 变更令的数据
  401. * @return {void}
  402. */
  403. async approvalSuccess(postData, changeData) {
  404. let tenderInfo;
  405. // 初始化事务
  406. this.transaction = await this.db.beginTransaction();
  407. let result = false;
  408. try {
  409. // 设置审批人通过
  410. const audit_update = {
  411. id: postData.audit_id,
  412. sdesc: postData.sdesc,
  413. status: audit.flow.auditStatus.checked,
  414. sin_time: new Date(),
  415. };
  416. const change_update = {
  417. w_code: postData.w_code,
  418. status: audit.flow.status.checking,
  419. cin_time: Date.parse(new Date()) / 1000,
  420. };
  421. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  422. // 清单数据更新
  423. const bills_list = postData.bills_list.split(',');
  424. let total_price = 0;
  425. for (const bl of bills_list) {
  426. const listInfo = bl.split('_');
  427. const lid = listInfo[0];
  428. const amount = listInfo[1];
  429. const changeListInfo = await this.ctx.service.changeAuditList.getDataById(lid);
  430. if (!tenderInfo) {
  431. tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeListInfo.tid);
  432. }
  433. if (changeListInfo !== undefined) {
  434. total_price = this.ctx.helper.add(total_price,
  435. this.ctx.helper.mul(changeListInfo.unit_price, amount, tenderInfo.decimal.tp));
  436. const audit_amount = changeListInfo.audit_amount !== null && changeListInfo.audit_amount !== '' ? changeListInfo.audit_amount.split(',') : [];
  437. audit_amount.push(amount);
  438. const list_update = {
  439. id: lid,
  440. audit_amount: audit_amount.join(','),
  441. spamount: parseFloat(amount),
  442. };
  443. if (postData.audit_next_id === undefined) {
  444. list_update.samount = amount;
  445. }
  446. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  447. }
  448. }
  449. if (postData.audit_next_id === undefined) {
  450. // 变更令审批完成
  451. change_update.status = audit.flow.status.checked;
  452. change_update.p_code = postData.p_code;
  453. change_update.sin_time = Date.parse(new Date()) / 1000;
  454. // 添加短信通知-审批通过提醒功能
  455. const mobile_array = [];
  456. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  457. for (const user of auditList) {
  458. const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  459. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  460. const smsType = JSON.parse(smsUser.sms_type);
  461. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  462. mobile_array.push(smsUser.auth_mobile);
  463. }
  464. }
  465. }
  466. if (mobile_array.length > 0) {
  467. const sms = new SMS(this.ctx);
  468. const content = '【纵横计量支付】' + changeData.code + '变更,审批通过。';
  469. sms.send(mobile_array, content);
  470. }
  471. } else {
  472. // 设置下一个审批人为审批状态
  473. const nextAudit_update = {
  474. id: postData.audit_next_id,
  475. status: audit.flow.auditStatus.checking,
  476. };
  477. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  478. // 添加短信通知-需要审批提醒功能
  479. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
  480. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAuditData.uid);
  481. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  482. const smsType = JSON.parse(smsUser.sms_type);
  483. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  484. const sms = new SMS(this.ctx);
  485. const code = await sms.contentChange(changeData.code);
  486. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  487. sms.send(smsUser.auth_mobile, content);
  488. }
  489. }
  490. }
  491. change_update.total_price = total_price;
  492. const options = {
  493. where: {
  494. cid: postData.change_id,
  495. },
  496. };
  497. await this.transaction.update(this.tableName, change_update, options);
  498. await this.transaction.commit();
  499. result = true;
  500. } catch (error) {
  501. console.log(error);
  502. await this.transaction.rollback();
  503. result = false;
  504. }
  505. return result;
  506. }
  507. /**
  508. * 审批终止
  509. * @param {int} postData - 表单提交的数据
  510. * @return {void}
  511. */
  512. async approvalStop(postData) {
  513. // 初始化事务
  514. this.transaction = await this.db.beginTransaction();
  515. let result = false;
  516. try {
  517. // 设置审批人终止
  518. const audit_update = {
  519. id: postData.audit_id,
  520. sdesc: postData.sdesc,
  521. status: 4,
  522. sin_time: new Date(),
  523. };
  524. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  525. // 设置变更令终止
  526. const change_update = {
  527. w_code: postData.w_code,
  528. status: 4,
  529. cin_time: Date.parse(new Date()) / 1000,
  530. };
  531. const options = {
  532. where: {
  533. cid: postData.change_id,
  534. },
  535. };
  536. await this.transaction.update(this.tableName, change_update, options);
  537. await this.transaction.commit();
  538. result = true;
  539. } catch (error) {
  540. await this.transaction.rollback();
  541. result = false;
  542. }
  543. return result;
  544. }
  545. /**
  546. * 审批退回到原报人
  547. * @param {int} postData - 表单提交的数据
  548. * @param {int} changeData - 变更令的数据
  549. * @return {void}
  550. */
  551. async approvalBack(postData, changeData) {
  552. // 初始化事务
  553. this.transaction = await this.db.beginTransaction();
  554. let result = false;
  555. try {
  556. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  557. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  558. // 设置审批人退回
  559. const audit_update = {
  560. id: postData.audit_id,
  561. sdesc: postData.sdesc,
  562. status: audit.flow.auditStatus.back,
  563. sin_time: new Date(),
  564. };
  565. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  566. // 新增新一次的审批人列表
  567. // 获取当前次数审批人列表
  568. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  569. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  570. let usort = lastauditInfo.usort + 1;
  571. const newTimes = changeInfo.times + 1;
  572. const insert_audit_array = [];
  573. for (const al of auditList) {
  574. const insert_audit = {
  575. tid: al.tid,
  576. cid: al.cid,
  577. uid: al.uid,
  578. name: al.name,
  579. jobs: al.jobs,
  580. company: al.company,
  581. times: newTimes,
  582. usite: al.usite,
  583. usort,
  584. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  585. };
  586. insert_audit_array.push(insert_audit);
  587. usort++;
  588. }
  589. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  590. // 变更金额也退回
  591. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  592. let total_price = 0;
  593. for (const cl of changeList) {
  594. total_price = this.ctx.helper.add(total_price,
  595. this.ctx.helper.mul(cl.unit_price, cl.camount, tenderInfo.decimal.tp));
  596. }
  597. // 设置变更令退回
  598. const change_update = {
  599. w_code: postData.w_code,
  600. status: audit.flow.status.back,
  601. times: newTimes,
  602. cin_time: Date.parse(new Date()) / 1000,
  603. total_price,
  604. };
  605. const options = {
  606. where: {
  607. cid: postData.change_id,
  608. },
  609. };
  610. await this.transaction.update(this.tableName, change_update, options);
  611. await this.transaction.commit();
  612. result = true;
  613. // 添加短信通知-审批退回提醒功能
  614. const mobile_array = [];
  615. for (const user of insert_audit_array) {
  616. const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  617. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  618. const smsType = JSON.parse(smsUser.sms_type);
  619. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  620. mobile_array.push(smsUser.auth_mobile);
  621. }
  622. }
  623. }
  624. if (mobile_array.length > 0) {
  625. const sms = new SMS(this.ctx);
  626. const code = await sms.contentChange(changeData.code);
  627. const content = '【纵横计量支付】' + code + '变更,审批退回。';
  628. sms.send(mobile_array, content);
  629. }
  630. } catch (error) {
  631. await this.transaction.rollback();
  632. result = false;
  633. }
  634. return result;
  635. }
  636. /**
  637. * 审批退回到上一个审批人
  638. * @param {int} postData - 表单提交的数据
  639. * @param {int} changeData - 变更令的数据
  640. * @return {void}
  641. */
  642. async approvalBackNew(postData, changeData) {
  643. // 初始化事务
  644. this.transaction = await this.db.beginTransaction();
  645. let result = false;
  646. try {
  647. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  648. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  649. // 设置审批人退回
  650. const audit_update = {
  651. id: postData.audit_id,
  652. sdesc: postData.sdesc,
  653. status: audit.flow.auditStatus.backnew,
  654. sin_time: new Date(),
  655. };
  656. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  657. // 获取当前审批人信息
  658. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  659. // 获取当前次数审批人列表
  660. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  661. let usort = auditInfo.usort + 1;
  662. // 获取上一个审批人信息
  663. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  664. // 新增2个审批人到审批列表中
  665. const insert_audit1 = {
  666. tid: lastauditInfo.tid,
  667. cid: lastauditInfo.cid,
  668. uid: lastauditInfo.uid,
  669. name: lastauditInfo.name,
  670. jobs: lastauditInfo.jobs,
  671. company: lastauditInfo.company,
  672. times: lastauditInfo.times,
  673. usite: lastauditInfo.usite,
  674. usort,
  675. status: audit.flow.auditStatus.checking,
  676. };
  677. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  678. usort++;
  679. // 新增2个审批人到审批列表中
  680. const insert_audit2 = {
  681. tid: auditInfo.tid,
  682. cid: auditInfo.cid,
  683. uid: auditInfo.uid,
  684. name: auditInfo.name,
  685. jobs: auditInfo.jobs,
  686. company: auditInfo.company,
  687. times: auditInfo.times,
  688. usite: auditInfo.usite,
  689. usort,
  690. status: audit.flow.auditStatus.uncheck,
  691. };
  692. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  693. // 把接下未审批的审批人排序都加2
  694. for (const al of auditList) {
  695. const audit_update = {
  696. id: al.id,
  697. usort: al.usort + 2,
  698. };
  699. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  700. }
  701. // 审批列表数据也要回退
  702. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  703. let total_price = 0;
  704. for (const cl of changeList) {
  705. const audit_amount = cl.audit_amount.split(',');
  706. const last_amount = audit_amount[audit_amount.length - 1];
  707. audit_amount.splice(-1, 1);
  708. const list_update = {
  709. id: cl.id,
  710. audit_amount: audit_amount.join(','),
  711. spamount: parseFloat(last_amount),
  712. };
  713. total_price = this.ctx.helper.add(total_price,
  714. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  715. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  716. }
  717. // 设置变更令退回
  718. const change_update = {
  719. w_code: postData.w_code,
  720. status: audit.flow.status.backnew,
  721. cin_time: Date.parse(new Date()) / 1000,
  722. total_price,
  723. };
  724. const options = {
  725. where: {
  726. cid: postData.change_id,
  727. },
  728. };
  729. await this.transaction.update(this.tableName, change_update, options);
  730. await this.transaction.commit();
  731. result = true;
  732. // 添加短信通知-需要审批提醒功能
  733. const smsUser = await this.ctx.service.projectAccount.getDataById(lastauditInfo.uid);
  734. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  735. const smsType = JSON.parse(smsUser.sms_type);
  736. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  737. const sms = new SMS(this.ctx);
  738. const code = await sms.contentChange(changeData.code);
  739. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  740. sms.send(smsUser.auth_mobile, content);
  741. }
  742. }
  743. } catch (error) {
  744. await this.transaction.rollback();
  745. result = false;
  746. }
  747. return result;
  748. }
  749. /**
  750. * 查询可用的变更令
  751. * @param bills - 查询的清单
  752. * @param pos - 查询的部位
  753. * @returns {Promise<*>} - 可用的变更令列表
  754. */
  755. async getValidChanges(tid, bills, pos) {
  756. const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) + (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
  757. 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,' +
  758. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  759. ' 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, ' +
  760. ' scb.used_amount' +
  761. ' FROM ' + this.tableName + ' As c ' +
  762. ' Left Join ' + this.ctx.service.changeAuditList.tableName +' As cb On c.cid = cb.cid ' +
  763. ' Left Join (' +
  764. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  765. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  766. ' INNER JOIN (SELECT Max(stimes) As `stimes`, Max(sorder) As `sorder`, cbid ' +
  767. ' FROM ' + this.ctx.service.stageChange.tableName +
  768. ' WHERE tid = ?' +
  769. ' GROUP BY cbid' +
  770. ' ) As MF' +
  771. ' ON sc.stimes = MF.stimes And sc.sorder = MF.sorder And sc.cbid = MF.cbid' +
  772. ' GROUP BY sc.cbid' +
  773. ' ) As scb ON cb.id = scb.cbid' +
  774. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  775. ' ORDER BY c.in_time';
  776. const sqlParam = [tid, tid, audit.flow.status.checked];
  777. const changes = await this.db.query(sql, sqlParam);
  778. for (const c of changes) {
  779. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  780. ' FROM ?? As ca ' +
  781. ' Left Join ?? As pa ' +
  782. ' On ca.uid = pa.id ' +
  783. ' Where ca.cid = ?';
  784. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  785. c.attachments = await this.db.query(aSql, aSqlParam);
  786. }
  787. return changes;
  788. }
  789. /**
  790. * 查询变更令 + 变更令执行
  791. * @param tid
  792. * @returns {Promise<void>}
  793. */
  794. async getChangeAndUsedInfo(tid) {
  795. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  796. let filter;
  797. if (lastStage.id === this.ctx.stage.id) {
  798. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  799. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  800. } else {
  801. if (lastStage.status === audit.stage.status.uncheck) {
  802. filter = ' And s.order < ' + lastStage.order;
  803. } else if (lastStage.status === audit.stage.status.checked) {
  804. filter = '';
  805. } else if (lastStage.status === audit.stage.status.checkNo) {
  806. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))',
  807. [lastStage.order, lastStage.order, lastStage.times])
  808. } else {
  809. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  810. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  811. [lastStage.order, lastStage.order, lastStage.times, lastStage.times, curAuditor.order - 1]);
  812. }
  813. }
  814. const sql = 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
  815. ' FROM ' + this.tableName + ' As C' +
  816. ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
  817. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  818. ' INNER JOIN (' +
  819. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  820. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
  821. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
  822. ' ON sChange.sid = s.id' +
  823. ' WHERE sChange.tid = ?' + filter +
  824. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  825. ' ) As m' +
  826. ' ON sc.stimes = m.stimes And sc.sorder = m.sorder And sc.`cbid` = m.`cbid` AND sc.`sid` = m.`sid`' +
  827. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As cb ON sc.cbid = cb.id' +
  828. ' GROUP By sc.`cbid`' +
  829. ' ) As U ON C.cid = U.cid' +
  830. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  831. ' GROUP By C.cid' +
  832. ' ORDER By in_time';
  833. const sqlParam = [this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
  834. return await this.db.query(sql, sqlParam);
  835. }
  836. /**
  837. * 查询可用的变更令
  838. * @param { string } cid - 查询的清单
  839. * @return {Promise<*>} - 可用的变更令列表
  840. */
  841. async delete(cid) {
  842. // 初始化事务
  843. this.transaction = await this.db.beginTransaction();
  844. let result = false;
  845. try {
  846. // 先删除清单,审批人列表
  847. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  848. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  849. // 再删除附件和附件文件
  850. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  851. if (attList.length !== 0) {
  852. for (const att of attList) {
  853. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  854. }
  855. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  856. }
  857. // 最后删除变更令
  858. await this.transaction.delete(this.tableName, { cid });
  859. await this.transaction.commit();
  860. result = true;
  861. } catch (e) {
  862. await this.transaction.rollback();
  863. result = false;
  864. }
  865. return result;
  866. }
  867. /**
  868. * 重新审批变更令
  869. * @param { string } cid - 查询的清单
  870. * @return {Promise<*>} - 可用的变更令列表
  871. */
  872. async checkAgain(cid) {
  873. // 初始化事务
  874. this.transaction = await this.db.beginTransaction();
  875. let result = false;
  876. try {
  877. const changeInfo = await this.getDataByCondition({ cid });
  878. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  879. // 获取终审
  880. const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
  881. let usort = auditInfo.usort + 1;
  882. // 新增2个审批状态到审批列表中
  883. const insert_audit1 = {
  884. tid: auditInfo.tid,
  885. cid: auditInfo.cid,
  886. uid: auditInfo.uid,
  887. name: auditInfo.name,
  888. jobs: auditInfo.jobs,
  889. company: auditInfo.company,
  890. times: auditInfo.times,
  891. usite: auditInfo.usite,
  892. usort,
  893. sin_time: new Date(),
  894. status: audit.flow.auditStatus.checkAgain,
  895. };
  896. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  897. usort++;
  898. // 新增2个审批人到审批列表中
  899. const insert_audit2 = {
  900. tid: auditInfo.tid,
  901. cid: auditInfo.cid,
  902. uid: auditInfo.uid,
  903. name: auditInfo.name,
  904. jobs: auditInfo.jobs,
  905. company: auditInfo.company,
  906. times: auditInfo.times,
  907. usite: auditInfo.usite,
  908. usort,
  909. status: audit.flow.auditStatus.checking,
  910. };
  911. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  912. // 审批列表数据也要回退
  913. let total_price = 0;
  914. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  915. for (const cl of changeList) {
  916. const audit_amount = cl.audit_amount.split(',');
  917. const last_amount = audit_amount[audit_amount.length - 1];
  918. audit_amount.splice(-1, 1);
  919. const list_update = {
  920. id: cl.id,
  921. audit_amount: audit_amount.join(','),
  922. samount: '',
  923. };
  924. total_price = this.ctx.helper.add(total_price,
  925. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  926. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  927. }
  928. // 设置变更令审批中
  929. const change_update = {
  930. p_code: null,
  931. status: audit.flow.status.checking,
  932. cin_time: Date.parse(new Date()) / 1000,
  933. sin_time: null,
  934. total_price,
  935. };
  936. const options = {
  937. where: {
  938. cid: changeInfo.cid,
  939. },
  940. };
  941. await this.transaction.update(this.tableName, change_update, options);
  942. await this.transaction.commit();
  943. result = true;
  944. // 添加短信通知-需要审批提醒功能
  945. const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo.uid);
  946. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  947. const smsType = JSON.parse(smsUser.sms_type);
  948. if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  949. const sms = new SMS(this.ctx);
  950. const code = await sms.contentChange(changeInfo.code);
  951. const content = '【纵横计量支付】' + code + '变更需要您审批。';
  952. sms.send(smsUser.auth_mobile, content);
  953. }
  954. }
  955. } catch (error) {
  956. await this.transaction.rollback();
  957. result = false;
  958. }
  959. return result;
  960. }
  961. /**
  962. * 判断是否有重名的变更令
  963. * @param cid
  964. * @param code
  965. * @param tid
  966. * @returns {Promise<void>}
  967. */
  968. async isRepeat(cid, code, tid) {
  969. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?';
  970. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid];
  971. const result = await this.db.queryOne(sql, sqlParam);
  972. return result.count !== 0;
  973. }
  974. }
  975. return Change;
  976. };