change.js 47 KB

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