change.js 50 KB

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