change.js 54 KB

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