change.js 48 KB

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