change.js 58 KB

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