change.js 59 KB

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