change.js 51 KB

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