change.js 58 KB

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