change.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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 noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid);
  516. await this.transaction.insert('zh_notice', { pid, type: pushType.change, uid: changeData.uid, status: audit.flow.status.checked, is_read: 0, content: noticeContent });
  517. // 添加短信通知-审批通过提醒功能
  518. // const mobile_array = [];
  519. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeData.cid, changeData.times);
  520. // for (const user of auditList) {
  521. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  522. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  523. // const smsType = JSON.parse(smsUser.sms_type);
  524. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  525. // mobile_array.push(smsUser.auth_mobile);
  526. // }
  527. // }
  528. // }
  529. // if (mobile_array.length > 0) {
  530. // const sms = new SMS(this.ctx);
  531. // const content = '【纵横计量支付】' + changeData.code + '变更,审批通过。';
  532. // sms.send(mobile_array, content);
  533. // }
  534. const users = this._.map(auditList, 'uid');
  535. const sms = new SMS(this.ctx);
  536. const code = await sms.contentChange(changeData.code);
  537. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG,
  538. smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, { biangeng: code, status: SmsAliConst.status.success });
  539. } else {
  540. // 设置下一个审批人为审批状态
  541. const nextAudit_update = {
  542. id: postData.audit_next_id,
  543. status: audit.flow.auditStatus.checking,
  544. };
  545. await this.transaction.update(this.ctx.service.changeAudit.tableName, nextAudit_update);
  546. // 添加短信通知-需要审批提醒功能
  547. const nextAuditData = await this.ctx.service.changeAudit.getDataById(postData.audit_next_id);
  548. const sms = new SMS(this.ctx);
  549. const code = await sms.contentChange(changeData.code);
  550. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAuditData.uid);
  551. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  552. // const smsType = JSON.parse(smsUser.sms_type);
  553. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  554. // const sms = new SMS(this.ctx);
  555. // const code = await sms.contentChange(changeData.code);
  556. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  557. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  558. // sms.send(smsUser.auth_mobile, content);
  559. // }
  560. // }
  561. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  562. await this.ctx.helper.sendAliSms(nextAuditData.uid, smsTypeConst.const.BG,
  563. smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, { biangeng: code, code: shenpiUrl });
  564. }
  565. change_update.total_price = total_price;
  566. const options = {
  567. where: {
  568. cid: postData.change_id,
  569. },
  570. };
  571. await this.transaction.update(this.tableName, change_update, options);
  572. await this.transaction.commit();
  573. result = true;
  574. } catch (error) {
  575. console.log(error);
  576. await this.transaction.rollback();
  577. result = false;
  578. }
  579. return result;
  580. }
  581. /**
  582. * 审批终止
  583. * @param {int} postData - 表单提交的数据
  584. * @return {void}
  585. */
  586. async approvalStop(postData) {
  587. // 初始化事务
  588. this.transaction = await this.db.beginTransaction();
  589. let result = false;
  590. try {
  591. // 设置审批人终止
  592. const audit_update = {
  593. id: postData.audit_id,
  594. sdesc: postData.sdesc,
  595. status: 4,
  596. sin_time: new Date(),
  597. };
  598. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  599. // 设置变更令终止
  600. const change_update = {
  601. w_code: postData.w_code,
  602. status: 4,
  603. cin_time: Date.parse(new Date()) / 1000,
  604. };
  605. const options = {
  606. where: {
  607. cid: postData.change_id,
  608. },
  609. };
  610. await this.transaction.update(this.tableName, change_update, options);
  611. await this.transaction.commit();
  612. result = true;
  613. } catch (error) {
  614. await this.transaction.rollback();
  615. result = false;
  616. }
  617. return result;
  618. }
  619. /**
  620. * 审批退回到原报人
  621. * @param {Number} pid 项目id
  622. * @param {int} postData - 表单提交的数据
  623. * @param {int} changeData - 变更令的数据
  624. * @return {void}
  625. */
  626. async approvalBack(pid, postData, changeData) {
  627. // 初始化事务
  628. this.transaction = await this.db.beginTransaction();
  629. let result = false;
  630. try {
  631. // 获取所有审核人列表
  632. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  633. // 添加到消息推送表
  634. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, postData.audit_id);
  635. const records = [{ pid, type: pushType.change, uid: changeData.uid, status: audit.flow.status.backnew, content: noticeContent }];
  636. auditors.forEach(auditor => {
  637. records.push({ pid, type: pushType.change, uid: auditor.uid, status: audit.flow.status.backnew, content: noticeContent });
  638. });
  639. await this.transaction.insert('zh_notice', records);
  640. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  641. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  642. // 设置审批人退回
  643. const audit_update = {
  644. id: postData.audit_id,
  645. sdesc: postData.sdesc,
  646. status: audit.flow.auditStatus.back,
  647. sin_time: new Date(),
  648. };
  649. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  650. // 新增新一次的审批人列表
  651. // 获取当前次数审批人列表
  652. const auditList = await this.ctx.service.changeAudit.getListGroupByTimes(changeInfo.cid, changeInfo.times);
  653. const lastauditInfo = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 1, 0);
  654. let usort = lastauditInfo.usort + 1;
  655. const newTimes = changeInfo.times + 1;
  656. const insert_audit_array = [];
  657. for (const al of auditList) {
  658. const insert_audit = {
  659. tid: al.tid,
  660. cid: al.cid,
  661. uid: al.uid,
  662. name: al.name,
  663. jobs: al.jobs,
  664. company: al.company,
  665. times: newTimes,
  666. usite: al.usite,
  667. usort,
  668. status: al.usite !== 0 ? audit.flow.auditStatus.uncheck : audit.flow.auditStatus.checking,
  669. };
  670. insert_audit_array.push(insert_audit);
  671. usort++;
  672. }
  673. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit_array);
  674. // 变更金额也退回
  675. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  676. let total_price = 0;
  677. for (const cl of changeList) {
  678. total_price = this.ctx.helper.add(total_price,
  679. this.ctx.helper.mul(cl.unit_price, cl.camount, tenderInfo.decimal.tp));
  680. }
  681. // 设置变更令退回
  682. const change_update = {
  683. w_code: postData.w_code,
  684. status: audit.flow.status.back,
  685. times: newTimes,
  686. cin_time: Date.parse(new Date()) / 1000,
  687. total_price,
  688. };
  689. const options = {
  690. where: {
  691. cid: postData.change_id,
  692. },
  693. };
  694. await this.transaction.update(this.tableName, change_update, options);
  695. await this.transaction.commit();
  696. result = true;
  697. // 添加短信通知-审批退回提醒功能
  698. // const mobile_array = [];
  699. // for (const user of insert_audit_array) {
  700. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.uid);
  701. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  702. // const smsType = JSON.parse(smsUser.sms_type);
  703. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  704. // mobile_array.push(smsUser.auth_mobile);
  705. // }
  706. // }
  707. // }
  708. // if (mobile_array.length > 0) {
  709. // const sms = new SMS(this.ctx);
  710. // const code = await sms.contentChange(changeData.code);
  711. // const content = '【纵横计量支付】' + code + '变更,审批退回。';
  712. // sms.send(mobile_array, content);
  713. // }
  714. const users = this._.map(insert_audit_array, 'uid');
  715. const sms = new SMS(this.ctx);
  716. const code = await sms.contentChange(changeData.code);
  717. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.BG,
  718. smsTypeConst.judge.result.toString(), SmsAliConst.template.change_result, { biangeng: code, status: SmsAliConst.status.back });
  719. } catch (error) {
  720. await this.transaction.rollback();
  721. result = false;
  722. }
  723. return result;
  724. }
  725. /**
  726. * 审批退回到上一个审批人
  727. * @param {Number} pid 项目id
  728. * @param {int} postData - 表单提交的数据
  729. * @param {int} changeData - 变更令的数据
  730. * @return {void}
  731. */
  732. async approvalBackNew(pid, postData, changeData) {
  733. // 初始化事务
  734. this.transaction = await this.db.beginTransaction();
  735. let result = false;
  736. try {
  737. // 获取所有审核人列表
  738. const auditors = await this.ctx.service.changeAudit.getAllAuditors(changeData.tid);
  739. // 添加到消息推送表
  740. const noticeContent = await this.getNoticeContent(pid, changeData.tid, changeData.cid, postData.audit_id);
  741. const records = [{ pid, type: pushType.change, uid: changeData.uid, status: audit.flow.status.back, content: noticeContent }];
  742. auditors.forEach(auditor => {
  743. records.push({ pid, type: pushType.change, uid: auditor.uid, status: audit.flow.status.back, content: noticeContent });
  744. });
  745. await this.transaction.insert('zh_notice', records);
  746. const changeInfo = await this.getDataByCondition({ cid: postData.change_id });
  747. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  748. // 设置审批人退回
  749. const audit_update = {
  750. id: postData.audit_id,
  751. sdesc: postData.sdesc,
  752. status: audit.flow.auditStatus.backnew,
  753. sin_time: new Date(),
  754. };
  755. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  756. // 获取当前审批人信息
  757. const auditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_id);
  758. // 获取当前次数审批人列表
  759. const auditList = await this.ctx.service.changeAudit.getNextAuditList(changeInfo.cid, auditInfo.usort);
  760. let usort = auditInfo.usort + 1;
  761. // 获取上一个审批人信息
  762. const lastauditInfo = await this.ctx.service.changeAudit.getDataById(postData.audit_last_id);
  763. // 新增2个审批人到审批列表中
  764. const insert_audit1 = {
  765. tid: lastauditInfo.tid,
  766. cid: lastauditInfo.cid,
  767. uid: lastauditInfo.uid,
  768. name: lastauditInfo.name,
  769. jobs: lastauditInfo.jobs,
  770. company: lastauditInfo.company,
  771. times: lastauditInfo.times,
  772. usite: lastauditInfo.usite,
  773. usort,
  774. status: audit.flow.auditStatus.checking,
  775. };
  776. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  777. usort++;
  778. // 新增2个审批人到审批列表中
  779. const insert_audit2 = {
  780. tid: auditInfo.tid,
  781. cid: auditInfo.cid,
  782. uid: auditInfo.uid,
  783. name: auditInfo.name,
  784. jobs: auditInfo.jobs,
  785. company: auditInfo.company,
  786. times: auditInfo.times,
  787. usite: auditInfo.usite,
  788. usort,
  789. status: audit.flow.auditStatus.uncheck,
  790. };
  791. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  792. // 把接下未审批的审批人排序都加2
  793. for (const al of auditList) {
  794. const audit_update = {
  795. id: al.id,
  796. usort: al.usort + 2,
  797. };
  798. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  799. }
  800. // 审批列表数据也要回退
  801. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  802. let total_price = 0;
  803. for (const cl of changeList) {
  804. const audit_amount = cl.audit_amount.split(',');
  805. const last_amount = audit_amount[audit_amount.length - 1];
  806. audit_amount.splice(-1, 1);
  807. const list_update = {
  808. id: cl.id,
  809. audit_amount: audit_amount.join(','),
  810. spamount: parseFloat(last_amount),
  811. };
  812. total_price = this.ctx.helper.add(total_price,
  813. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  814. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  815. }
  816. // 设置变更令退回
  817. const change_update = {
  818. w_code: postData.w_code,
  819. status: audit.flow.status.backnew,
  820. cin_time: Date.parse(new Date()) / 1000,
  821. total_price,
  822. };
  823. const options = {
  824. where: {
  825. cid: postData.change_id,
  826. },
  827. };
  828. await this.transaction.update(this.tableName, change_update, options);
  829. await this.transaction.commit();
  830. result = true;
  831. // 添加短信通知-需要审批提醒功能
  832. // const smsUser = await this.ctx.service.projectAccount.getDataById(lastauditInfo.uid);
  833. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  834. // const smsType = JSON.parse(smsUser.sms_type);
  835. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  836. // const sms = new SMS(this.ctx);
  837. // const code = await sms.contentChange(changeData.code);
  838. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  839. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  840. // sms.send(smsUser.auth_mobile, content);
  841. // }
  842. // }
  843. const sms = new SMS(this.ctx);
  844. const code = await sms.contentChange(changeData.code);
  845. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeData.tid + '/change/' + changeData.cid + '/info#shenpi');
  846. await this.ctx.helper.sendAliSms(lastauditInfo.uid, smsTypeConst.const.BG,
  847. smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, { biangeng: code, code: shenpiUrl });
  848. } catch (error) {
  849. await this.transaction.rollback();
  850. result = false;
  851. }
  852. return result;
  853. }
  854. /**
  855. * 查询可用的变更令
  856. * @param bills - 查询的清单
  857. * @param pos - 查询的部位
  858. * @return {Promise<*>} - 可用的变更令列表
  859. */
  860. async getValidChanges(tid, bills, pos) {
  861. const timesLen = 100;
  862. const filter = 'cb.`code` = ' + this.db.escape(bills.b_code) +
  863. ' And cb.`name` = ' + this.db.escape(bills.name) +
  864. ' And cb.`unit` = ' + this.db.escape(bills.unit) +
  865. ' And cb.`unit_price` = ' + this.db.escape(bills.unit_price) +
  866. (pos ? ' And cb.`bwmx` = ' + this.db.escape(pos.name) : '');
  867. 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,' +
  868. ' c.content, c.basis, c.memo, c.type, c.class, c.quality, c.company, c.charge, ' +
  869. ' 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, ' +
  870. ' scb.used_amount' +
  871. ' FROM ' + this.tableName + ' As c ' +
  872. ' Left Join ' + this.ctx.service.changeAuditList.tableName + ' As cb On c.cid = cb.cid ' +
  873. ' Left Join (' +
  874. ' SELECT SUM(sc.qty) As used_amount, sc.cbid' +
  875. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  876. ' INNER JOIN (SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `flow`, cbid, sid ' +
  877. ' FROM ' + this.ctx.service.stageChange.tableName +
  878. ' WHERE tid = ?' +
  879. ' GROUP BY cbid, sid' +
  880. ' ) As MF' +
  881. ' ON (sc.stimes * ' + timesLen + ' + sc.sorder) = MF.flow And sc.cbid = MF.cbid And sc.sid = MF.sid' +
  882. ' GROUP BY sc.cbid' +
  883. ' ) As scb ON cb.id = scb.cbid' +
  884. ' WHERE c.tid = ? And c.status = ? And c.valid And ' + filter +
  885. ' ORDER BY c.in_time';
  886. const sqlParam = [tid, tid, audit.flow.status.checked];
  887. const changes = await this.db.query(sql, sqlParam);
  888. for (const c of changes) {
  889. const aSql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  890. ' FROM ?? As ca ' +
  891. ' Left Join ?? As pa ' +
  892. ' On ca.uid = pa.id ' +
  893. ' Where ca.cid = ?';
  894. const aSqlParam = [this.ctx.service.changeAtt.tableName, this.ctx.service.projectAccount.tableName, c.cid];
  895. c.attachments = await this.db.query(aSql, aSqlParam);
  896. }
  897. return changes;
  898. }
  899. /**
  900. * 查询变更令 + 变更令执行
  901. * @param tid
  902. * @return {Promise<void>}
  903. */
  904. async getChangeAndUsedInfo(tid) {
  905. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  906. let filter;
  907. if (lastStage.id === this.ctx.stage.id) {
  908. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  909. [lastStage.order, lastStage.order, this.ctx.stage.curTimes, this.ctx.stage.curTimes, this.ctx.stage.curOrder]);
  910. } else {
  911. if (lastStage.status === audit.stage.status.uncheck) {
  912. filter = ' And s.order < ' + lastStage.order;
  913. } else if (lastStage.status === audit.stage.status.checked) {
  914. filter = '';
  915. } else if (lastStage.status === audit.stage.status.checkNo) {
  916. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And sChange.`stimes` <= ?))',
  917. [lastStage.order, lastStage.order, lastStage.times]);
  918. } else {
  919. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(lastStage.id, lastStage.times);
  920. filter = this.db.format(' And (s.`order` < ? OR (s.`order` = ? And (sChange.`stimes` < ? OR (sChange.`stimes` = ? And sChange.`sorder` <= ?))))',
  921. [lastStage.order, lastStage.order, lastStage.times, lastStage.times, curAuditor.order - 1]);
  922. }
  923. }
  924. const sql = 'SELECT C.*, Sum(U.utp) As used_tp, Round(Sum(U.utp) / C.total_price * 100, 2) As used_pt' +
  925. ' FROM ' + this.tableName + ' As C' +
  926. ' LEFT JOIN (SELECT sc.tid, sc.cid, sc.cbid, Round(SUM(sc.qty) * cb.unit_price, ?) As utp' +
  927. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sc' +
  928. ' INNER JOIN (' +
  929. ' SELECT MAX(`stimes`) As `stimes`, MAX(`sorder`) As `sorder`, `lid`, `pid`, `cbid`, sChange.`sid` ' +
  930. ' FROM ' + this.ctx.service.stageChange.tableName + ' As sChange ' +
  931. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' As s' +
  932. ' ON sChange.sid = s.id' +
  933. ' WHERE sChange.tid = ?' + filter +
  934. ' GROUP By `lid`, `pid`, `cbid`, `sid`' +
  935. ' ) As m' +
  936. ' 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`' +
  937. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As cb ON sc.cbid = cb.id' +
  938. ' GROUP By sc.`cbid`' +
  939. ' ) As U ON C.cid = U.cid' +
  940. ' WHERE C.tid = ? And C.status = ? And C.valid' +
  941. ' GROUP By C.cid' +
  942. ' ORDER By in_time';
  943. const sqlParam = [this.ctx.tender.info.decimal.tp, tid, tid, audit.flow.status.checked];
  944. return await this.db.query(sql, sqlParam);
  945. }
  946. /**
  947. * 查询可用的变更令
  948. * @param { string } cid - 查询的清单
  949. * @return {Promise<*>} - 可用的变更令列表
  950. */
  951. async delete(cid) {
  952. // 初始化事务
  953. this.transaction = await this.db.beginTransaction();
  954. let result = false;
  955. try {
  956. // 先删除清单,审批人列表
  957. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  958. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  959. // 再删除附件和附件文件ni zuo
  960. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  961. if (attList.length !== 0) {
  962. for (const att of attList) {
  963. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  964. }
  965. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  966. }
  967. // 最后删除变更令
  968. await this.transaction.delete(this.tableName, { cid });
  969. await this.transaction.commit();
  970. result = true;
  971. } catch (e) {
  972. await this.transaction.rollback();
  973. result = false;
  974. }
  975. return result;
  976. }
  977. /**
  978. * 重新审批变更令
  979. * @param { string } cid - 查询的清单
  980. * @return {Promise<*>} - 可用的变更令列表
  981. */
  982. async checkAgain(cid) {
  983. // 初始化事务
  984. this.transaction = await this.db.beginTransaction();
  985. let result = false;
  986. try {
  987. const changeInfo = await this.getDataByCondition({ cid });
  988. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(changeInfo.tid);
  989. // 获取终审
  990. const auditInfo = (await this.ctx.service.changeAudit.getAllDataByCondition({ where: { cid }, orders: [['usort', 'desc']], limit: 1, offset: 0 }))[0];
  991. let usort = auditInfo.usort + 1;
  992. // 新增2个审批状态到审批列表中
  993. const insert_audit1 = {
  994. tid: auditInfo.tid,
  995. cid: auditInfo.cid,
  996. uid: auditInfo.uid,
  997. name: auditInfo.name,
  998. jobs: auditInfo.jobs,
  999. company: auditInfo.company,
  1000. times: auditInfo.times,
  1001. usite: auditInfo.usite,
  1002. usort,
  1003. sin_time: new Date(),
  1004. status: audit.flow.auditStatus.checkAgain,
  1005. };
  1006. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit1);
  1007. usort++;
  1008. // 新增2个审批人到审批列表中
  1009. const insert_audit2 = {
  1010. tid: auditInfo.tid,
  1011. cid: auditInfo.cid,
  1012. uid: auditInfo.uid,
  1013. name: auditInfo.name,
  1014. jobs: auditInfo.jobs,
  1015. company: auditInfo.company,
  1016. times: auditInfo.times,
  1017. usite: auditInfo.usite,
  1018. usort,
  1019. status: audit.flow.auditStatus.checking,
  1020. };
  1021. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insert_audit2);
  1022. // 审批列表数据也要回退
  1023. let total_price = 0;
  1024. const changeList = await this.ctx.service.changeAuditList.getAllDataByCondition({ where: { cid: changeInfo.cid } });
  1025. for (const cl of changeList) {
  1026. const audit_amount = cl.audit_amount.split(',');
  1027. const last_amount = audit_amount[audit_amount.length - 1];
  1028. audit_amount.splice(-1, 1);
  1029. const list_update = {
  1030. id: cl.id,
  1031. audit_amount: audit_amount.join(','),
  1032. samount: '',
  1033. };
  1034. total_price = this.ctx.helper.add(total_price,
  1035. this.ctx.helper.mul(cl.unit_price, parseFloat(last_amount), tenderInfo.decimal.tp));
  1036. await this.transaction.update(this.ctx.service.changeAuditList.tableName, list_update);
  1037. }
  1038. // 设置变更令审批中
  1039. const change_update = {
  1040. p_code: null,
  1041. status: audit.flow.status.checking,
  1042. cin_time: Date.parse(new Date()) / 1000,
  1043. sin_time: null,
  1044. total_price,
  1045. };
  1046. const options = {
  1047. where: {
  1048. cid: changeInfo.cid,
  1049. },
  1050. };
  1051. await this.transaction.update(this.tableName, change_update, options);
  1052. await this.transaction.commit();
  1053. result = true;
  1054. // 添加短信通知-需要审批提醒功能
  1055. // const smsUser = await this.ctx.service.projectAccount.getDataById(auditInfo.uid);
  1056. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  1057. // const smsType = JSON.parse(smsUser.sms_type);
  1058. // if (smsType[smsTypeConst.const.BG] !== undefined && smsType[smsTypeConst.const.BG].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  1059. // const sms = new SMS(this.ctx);
  1060. // const code = await sms.contentChange(changeInfo.code);
  1061. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi');
  1062. // const content = '【纵横计量支付】' + code + '变更需要您审批。' + result;
  1063. // sms.send(smsUser.auth_mobile, content);
  1064. // }
  1065. // }
  1066. const sms = new SMS(this.ctx);
  1067. const code = await sms.contentChange(changeInfo.code);
  1068. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + changeInfo.tid + '/change/' + changeInfo.cid + '/info#shenpi');
  1069. await this.ctx.helper.sendAliSms(auditInfo.uid, smsTypeConst.const.BG,
  1070. smsTypeConst.judge.approval.toString(), SmsAliConst.template.change_check, { biangeng: code, code: shenpiUrl });
  1071. } catch (error) {
  1072. await this.transaction.rollback();
  1073. result = false;
  1074. }
  1075. return result;
  1076. }
  1077. /**
  1078. * 判断是否有重名的变更令
  1079. * @param cid
  1080. * @param code
  1081. * @param tid
  1082. * @return {Promise<void>}
  1083. */
  1084. async isRepeat(cid, code, tid) {
  1085. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE ((`code` = ? AND `status` != ?) OR (`p_code` = ? AND `status` = ?)) AND `cid` != ? AND `tid` = ?';
  1086. const sqlParam = [this.tableName, code, audit.flow.status.checked, code, audit.flow.status.checked, cid, tid];
  1087. const result = await this.db.queryOne(sql, sqlParam);
  1088. return result.count !== 0;
  1089. }
  1090. async getAllCheckedChanges(tid) {
  1091. return await this.getAllDataByCondition({
  1092. where: { tid, status: audit.flow.status.checked },
  1093. orders: [['in_time', 'desc']],
  1094. });
  1095. }
  1096. /**
  1097. * 用于添加推送所需的content内容
  1098. * @param {Number} pid 项目id
  1099. * @param {Number} tid 台账id
  1100. * @param {Number} cid 变更id
  1101. */
  1102. async getNoticeContent(pid, tid, cid) {
  1103. const noticeSql = 'SELECT * FROM (SELECT ' +
  1104. ' t.`id` As `tid`, t.`name`, c.`cid`, c.`code` As `c_code`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1105. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1106. ' LEFT JOIN ?? As c ON c.`cid` = ?' +
  1107. ' LEFT JOIN ?? As pa ON c.`uid` = pa.`id`' +
  1108. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1109. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.change.tableName, cid, this.ctx.service.projectAccount.tableName, pid];
  1110. const content = await this.db.query(noticeSql, noticeSqlParam);
  1111. return content.length ? JSON.stringify(content[0]) : '';
  1112. }
  1113. }
  1114. return Change;
  1115. };