change_plan_audit.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').changePlan;
  10. const pushType = require('../const/audit').pushType;
  11. const pushOperate = require('../const/spec_3f').pushOperate;
  12. const shenpiConst = require('../const/shenpi');
  13. const smsTypeConst = require('../const/sms_type');
  14. const SMS = require('../lib/sms');
  15. const SmsAliConst = require('../const/sms_alitemplate');
  16. const wxConst = require('../const/wechat_template');
  17. module.exports = app => {
  18. class ChangePlanAudit 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_plan_audit';
  28. }
  29. /**
  30. * 获取 审核列表信息
  31. *
  32. * @param {Number} cpId - 变更立项id
  33. * @param {Number} times - 第几次审批
  34. * @return {Promise<*>}
  35. */
  36. async getAuditors(cpId, times = 1) {
  37. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  38. 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
  39. 'WHERE la.`cpid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  40. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, cpId, times, cpId, times];
  41. const result = await this.db.query(sql, sqlParam);
  42. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) as a';
  43. const sqlParam2 = [this.tableName, cpId, times];
  44. const count = await this.db.queryOne(sql2, sqlParam2);
  45. for (const i in result) {
  46. result[i].max_sort = count.num;
  47. }
  48. return result;
  49. }
  50. /**
  51. * 获取 当前审核人
  52. *
  53. * @param {Number} cpId - 变更立项id
  54. * @param {Number} times - 第几次审批
  55. * @return {Promise<*>}
  56. */
  57. async getCurAuditor(cpId, times = 1) {
  58. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  59. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  60. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
  61. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
  62. return await this.db.queryOne(sql, sqlParam);
  63. }
  64. /**
  65. * 获取审核人流程列表
  66. *
  67. * @param auditorId
  68. * @return {Promise<*>}
  69. */
  70. async getAuditGroupByList(changeId, times) {
  71. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
  72. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  73. ' WHERE la.`cpid` = ? and la.`times` = ? and la.`status` != ? and la.`status` != ? GROUP BY la.`aid` ORDER BY la.`order`';
  74. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, auditConst.status.revise, auditConst.status.cancelRevise];
  75. return await this.db.query(sql, sqlParam);
  76. }
  77. /**
  78. * 移除审核人
  79. *
  80. * @param {Number} materialId - 材料调差期id
  81. * @param {Number} status - 期状态
  82. * @param {Number} status - 期次数
  83. * @return {Promise<boolean>}
  84. */
  85. async getAuditorByStatus(cpId, status, times = 1) {
  86. let auditor = null;
  87. let sql = '';
  88. let sqlParam = '';
  89. switch (status) {
  90. case auditConst.status.checking :
  91. case auditConst.status.checked :
  92. case auditConst.status.revise :
  93. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  94. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  95. ' WHERE la.`cpid` = ? and la.`status` = ? ' +
  96. ' ORDER BY la.`times` desc, la.`order` desc';
  97. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, status];
  98. auditor = await this.db.queryOne(sql, sqlParam);
  99. break;
  100. case auditConst.status.checkNo :
  101. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  102. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  103. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?' +
  104. ' ORDER BY la.`times` desc, la.`order` desc';
  105. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checkNo, parseInt(times) - 1];
  106. auditor = await this.db.queryOne(sql, sqlParam);
  107. break;
  108. case auditConst.status.uncheck :
  109. break;
  110. default:break;
  111. }
  112. return auditor;
  113. }
  114. async getLastAudit(cpid, times, transaction = null) {
  115. const sql = 'SELECT * FROM ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` DESC';
  116. const sqlParam = [this.tableName, cpid, times];
  117. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  118. }
  119. /**
  120. * 获取审核人流程列表(包括原报)
  121. * @param {Number} materialId 调差id
  122. * @param {Number} times 审核次数
  123. * @return {Promise<Array>} 查询结果集(包括原报)
  124. */
  125. async getAuditorsWithOwner(cpId, times = 1) {
  126. const result = await this.getAuditGroupByList(cpId, times);
  127. const sql =
  128. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
  129. ' FROM ' +
  130. this.ctx.service.changePlan.tableName +
  131. ' As s' +
  132. ' LEFT JOIN ' +
  133. this.ctx.service.projectAccount.tableName +
  134. ' As pa' +
  135. ' ON s.uid = pa.id' +
  136. ' WHERE s.id = ?';
  137. const sqlParam = [times, cpId, cpId];
  138. const user = await this.db.queryOne(sql, sqlParam);
  139. result.unshift(user);
  140. return result;
  141. }
  142. /**
  143. * 新增审核人
  144. *
  145. * @param {Number} cpId - 方案id
  146. * @param {Number} auditorId - 审核人id
  147. * @param {Number} times - 第几次审批
  148. * @return {Promise<number>}
  149. */
  150. async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) {
  151. const transaction = await this.db.beginTransaction();
  152. let flag = false;
  153. try {
  154. let newOrder = await this.getNewOrder(cpId, times);
  155. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  156. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  157. if (is_gdzs) await this._syncOrderByDelete(transaction, cpId, newOrder, times, '+');
  158. const data = {
  159. tid: this.ctx.tender.id,
  160. cpid: cpId,
  161. aid: auditorId,
  162. times,
  163. order: newOrder,
  164. status: auditConst.status.uncheck,
  165. };
  166. const result = await transaction.insert(this.tableName, data);
  167. await transaction.commit();
  168. flag = result.effectRows = 1;
  169. } catch (err) {
  170. await transaction.rollback();
  171. throw err;
  172. }
  173. return flag;
  174. }
  175. /**
  176. * 获取 最新审核顺序
  177. *
  178. * @param {Number} cpId - 方案id
  179. * @param {Number} times - 第几次审批
  180. * @return {Promise<number>}
  181. */
  182. async getNewOrder(cpId, times = 1) {
  183. const sql = 'SELECT Max(??) As max_order FROM ?? Where `cpid` = ? and `times` = ?';
  184. const sqlParam = ['order', this.tableName, cpId, times];
  185. const result = await this.db.queryOne(sql, sqlParam);
  186. return result && result.max_order ? result.max_order + 1 : 1;
  187. }
  188. /**
  189. * 移除审核人
  190. *
  191. * @param {Number} cpId - 变更方案id
  192. * @param {Number} auditorId - 审核人id
  193. * @param {Number} times - 第几次审批
  194. * @return {Promise<boolean>}
  195. */
  196. async deleteAuditor(cpId, auditorId, times = 1) {
  197. const transaction = await this.db.beginTransaction();
  198. try {
  199. const condition = { cpid: cpId, aid: auditorId, times };
  200. const auditor = await this.getDataByCondition(condition);
  201. if (!auditor) {
  202. throw '该审核人不存在';
  203. }
  204. await this._syncOrderByDelete(transaction, cpId, auditor.order, times);
  205. await transaction.delete(this.tableName, condition);
  206. await transaction.commit();
  207. } catch (err) {
  208. await transaction.rollback();
  209. throw err;
  210. }
  211. return true;
  212. }
  213. /**
  214. * 移除审核人时,同步其后审核人order
  215. * @param transaction - 事务
  216. * @param {Number} cpId - 变更方案id
  217. * @param {Number} auditorId - 审核人id
  218. * @param {Number} times - 第几次审批
  219. * @return {Promise<*>}
  220. * @private
  221. */
  222. async _syncOrderByDelete(transaction, cpId, order, times, selfOperate = '-') {
  223. this.initSqlBuilder();
  224. this.sqlBuilder.setAndWhere('cpid', {
  225. value: cpId,
  226. operate: '=',
  227. });
  228. this.sqlBuilder.setAndWhere('order', {
  229. value: order,
  230. operate: '>=',
  231. });
  232. this.sqlBuilder.setAndWhere('times', {
  233. value: times,
  234. operate: '=',
  235. });
  236. this.sqlBuilder.setUpdateData('order', {
  237. value: 1,
  238. selfOperate,
  239. });
  240. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  241. const data = await transaction.query(sql, sqlParam);
  242. return data;
  243. }
  244. /**
  245. * 开始审批
  246. * @param {Number} cpId - 方案id
  247. * @param {Number} times - 第几次审批
  248. * @return {Promise<boolean>}
  249. */
  250. async start(cpId, times = 1) {
  251. const audit = await this.getDataByCondition({ cpid: cpId, times, order: 1 });
  252. if (!audit) {
  253. // if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  254. // throw '请联系管理员添加审批人';
  255. // } else {
  256. throw '请先选择审批人,再上报数据';
  257. // }
  258. }
  259. const transaction = await this.db.beginTransaction();
  260. try {
  261. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  262. await transaction.update(this.ctx.service.changePlan.tableName, {
  263. id: cpId, status: auditConst.status.checking,
  264. });
  265. // 微信模板通知
  266. const shenpiUrl = await this.ctx.helper.urlToShort(
  267. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  268. );
  269. const wechatData = {
  270. type: 'plan',
  271. wap_url: shenpiUrl,
  272. status: wxConst.status.check,
  273. tips: wxConst.tips.check,
  274. code: this.ctx.session.sessionProject.code,
  275. c_name: this.ctx.change.name,
  276. };
  277. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  278. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  279. pid: this.ctx.session.sessionProject.id,
  280. tid: this.ctx.tender.id,
  281. uid: audit.aid,
  282. sp_type: 'change',
  283. sp_id: audit.id,
  284. table_name: this.tableName,
  285. template: wxConst.template.change,
  286. wx_data: wechatData,
  287. });
  288. // 检查三方特殊推送
  289. await this.ctx.service.specMsg.addChangePlanMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_plan.flow);
  290. await transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: cpId });
  291. // todo 更新标段tender状态 ?
  292. await transaction.commit();
  293. } catch (err) {
  294. await transaction.rollback();
  295. throw err;
  296. }
  297. return true;
  298. }
  299. /**
  300. * 获取审核人需要审核的期列表
  301. *
  302. * @param auditorId
  303. * @return {Promise<*>}
  304. */
  305. async getAuditChangePlan(auditorId) {
  306. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  307. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  308. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  309. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  310. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  311. ' and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  312. const sqlParam = [this.tableName, this.ctx.service.changePlan.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  313. return await this.db.query(sql, sqlParam);
  314. }
  315. /**
  316. * 获取审核人审核的次数
  317. *
  318. * @param auditorId
  319. * @return {Promise<*>}
  320. */
  321. async getCountByChecked(auditorId) {
  322. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  323. }
  324. /**
  325. * 获取最近一次审批结束时间
  326. *
  327. * @param auditorId
  328. * @return {Promise<*>}
  329. */
  330. async getLastEndTimeByChecked(auditorId) {
  331. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  332. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  333. const sqlParam = [this.tableName, auditorId];
  334. const result = await this.db.queryOne(sql, sqlParam);
  335. return result ? result.end_time : null;
  336. }
  337. /**
  338. * 用于添加推送所需的content内容
  339. * @param {Number} pid 项目id
  340. * @param {Number} tid 台账id
  341. * @param {Number} cpId 方案id
  342. * @param {Number} uid 审批人id
  343. */
  344. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  345. const noticeSql = 'SELECT * FROM (SELECT ' +
  346. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  347. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  348. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  349. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  350. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  351. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  352. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changePlan.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  353. const content = await this.db.query(noticeSql, noticeSqlParam);
  354. if (content.length) {
  355. content[0].opinion = opinion;
  356. }
  357. return content.length ? JSON.stringify(content[0]) : '';
  358. }
  359. /**
  360. * 审批
  361. * @param {Number} cpId - 方案id
  362. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  363. * @param {Number} times - 第几次审批
  364. * @return {Promise<void>}
  365. */
  366. async check(cpId, checkData, times = 1) {
  367. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo) {
  368. throw '提交数据错误';
  369. }
  370. const pid = this.ctx.session.sessionProject.id;
  371. switch (checkData.checkType) {
  372. case auditConst.status.checked:
  373. await this._checked(pid, cpId, checkData, times);
  374. break;
  375. case auditConst.status.checkNo:
  376. await this._checkNo(pid, cpId, checkData, times);
  377. break;
  378. default:
  379. throw '无效审批操作';
  380. }
  381. }
  382. async _checked(pid, cpId, checkData, times) {
  383. const time = new Date();
  384. // 整理当前流程审核人状态更新
  385. const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking });
  386. if (!audit) {
  387. throw '审核数据错误';
  388. }
  389. // 获取审核人列表
  390. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  391. const sqlParam = [this.tableName, cpId, times];
  392. const auditors = await this.db.query(sql, sqlParam);
  393. const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
  394. const transaction = await this.db.beginTransaction();
  395. try {
  396. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  397. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  398. // 获取推送必要信息
  399. const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion);
  400. // 添加推送
  401. const records = [{ pid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  402. auditors.forEach(audit => {
  403. records.push({ pid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  404. });
  405. await transaction.insert('zh_notice', records);
  406. // 清单审批流程修改的数据插入到audit_amount中,审批完成则最后一位并插入到审批后变更值中
  407. await this.ctx.service.changePlanList.insertAuditAmount(transaction, cpId, !nextAudit);
  408. // 无下一审核人表示,审核结束
  409. if (nextAudit) {
  410. // 流程至下一审批人
  411. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  412. // 同步 期信息
  413. await transaction.update(this.ctx.service.changePlan.tableName, {
  414. id: cpId, status: auditConst.status.checking,
  415. });
  416. // 微信模板通知
  417. const shenpiUrl = await this.ctx.helper.urlToShort(
  418. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  419. );
  420. const wechatData = {
  421. type: 'plan',
  422. wap_url: shenpiUrl,
  423. status: wxConst.status.check,
  424. tips: wxConst.tips.check,
  425. code: this.ctx.session.sessionProject.code,
  426. c_name: this.ctx.change.name,
  427. };
  428. await this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  429. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  430. pid: this.ctx.session.sessionProject.id,
  431. tid: this.ctx.tender.id,
  432. uid: nextAudit.aid,
  433. sp_type: 'change',
  434. sp_id: nextAudit.id,
  435. table_name: this.tableName,
  436. template: wxConst.template.change,
  437. wx_data: wechatData,
  438. });
  439. // 检查三方特殊推送
  440. await this.ctx.service.specMsg.addChangePlanMsg(transaction, pid, this.ctx.change, pushOperate.change_plan.flow);
  441. } else {
  442. // 本期结束
  443. // 生成截止本期数据 final数据
  444. // 同步 期信息
  445. await transaction.update(this.ctx.service.changePlan.tableName, {
  446. id: cpId, status: checkData.checkType,
  447. decimal: JSON.stringify(this.ctx.change.decimal),
  448. });
  449. // 微信模板通知
  450. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  451. const shenpiUrl = await this.ctx.helper.urlToShort(
  452. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  453. );
  454. const wechatData = {
  455. type: 'plan',
  456. wap_url: shenpiUrl,
  457. status: wxConst.status.success,
  458. tips: wxConst.tips.success,
  459. code: this.ctx.session.sessionProject.code,
  460. c_name: this.ctx.change.name,
  461. };
  462. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  463. // 检查三方特殊推送
  464. await this.ctx.service.specMsg.addChangePlanMsg(transaction, pid, this.ctx.change, pushOperate.change_plan.flow);
  465. }
  466. await transaction.commit();
  467. } catch (err) {
  468. await transaction.rollback();
  469. throw err;
  470. }
  471. }
  472. async _checkNo(pid, cpId, checkData, times) {
  473. const time = new Date();
  474. const changeData = await this.ctx.service.changePlan.getDataById(cpId);
  475. // 整理当前流程审核人状态更新
  476. const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking });
  477. if (!audit) {
  478. throw '审核数据错误';
  479. }
  480. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  481. // const sqlParam = [this.tableName, cpId, times];
  482. // const auditors = await this.db.query(sql, sqlParam);
  483. const auditors = await this.getAuditGroupByList(cpId, times);
  484. let order = 1;
  485. const newAuditors = [];
  486. for (const a of auditors) {
  487. a.times = times + 1;
  488. a.order = order;
  489. a.status = auditConst.status.uncheck;
  490. order++;
  491. newAuditors.push({
  492. tid: this.ctx.tender.id,
  493. cpid: cpId,
  494. times: a.times,
  495. order: a.order,
  496. status: a.status,
  497. aid: a.aid,
  498. });
  499. }
  500. const transaction = await this.db.beginTransaction();
  501. try {
  502. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  503. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  504. // 添加到消息推送表
  505. const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion);
  506. const records = [{ pid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  507. auditors.forEach(audit => {
  508. records.push({ pid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  509. });
  510. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  511. // 同步期信息
  512. await transaction.update(this.ctx.service.changePlan.tableName, {
  513. id: cpId, status: checkData.checkType,
  514. times: times + 1,
  515. });
  516. // 拷贝新一次审核流程列表
  517. await transaction.insert(this.tableName, newAuditors);
  518. // 清单审批值删除并重算变更金额
  519. await this.ctx.service.changePlanList.delAuditAmount(transaction, cpId);
  520. // 微信模板通知
  521. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  522. const shenpiUrl = await this.ctx.helper.urlToShort(
  523. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  524. );
  525. const wechatData = {
  526. type: 'plan',
  527. wap_url: shenpiUrl,
  528. status: wxConst.status.back,
  529. tips: wxConst.tips.back,
  530. code: this.ctx.session.sessionProject.code,
  531. c_name: this.ctx.change.name,
  532. };
  533. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  534. // 检查三方特殊推送
  535. await this.ctx.service.specMsg.addChangePlanMsg(transaction, pid, this.ctx.change, pushOperate.change_plan.flow);
  536. // 生成内容保存表至zh_change_project_history中,用于撤回
  537. // 回退spamount值数据
  538. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  539. where: { cpid: cpId },
  540. });
  541. await this.ctx.service.changePlanHistory.saveHistory(transaction, changeData, changeList);
  542. await transaction.commit();
  543. } catch (err) {
  544. await transaction.rollback();
  545. throw err;
  546. }
  547. }
  548. /**
  549. * 复制上一期的审批人列表给最新一期
  550. *
  551. * @param transaction - 新增一期的事务
  552. * @param {Object} preMaterial - 上一期
  553. * @param {Object} newaMaterial - 最新一期
  554. * @return {Promise<*>}
  555. */
  556. async copyPreChangePlanAuditors(transaction, preChange, newChange) {
  557. const auditors = await this.getAuditGroupByList(preChange.id, preChange.times);
  558. const newAuditors = [];
  559. for (const a of auditors) {
  560. const na = {
  561. tid: preChange.tid,
  562. cpid: newChange.id,
  563. aid: a.aid,
  564. times: newChange.times,
  565. order: newAuditors.length + 1,
  566. status: auditConst.status.uncheck,
  567. };
  568. newAuditors.push(na);
  569. }
  570. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  571. return result ? result.affectedRows === auditors.length : true;
  572. }
  573. async getAllAuditors(tenderId) {
  574. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  575. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  576. ' WHERE t.id = ?' +
  577. ' GROUP BY ma.aid';
  578. const sqlParam = [tenderId];
  579. return this.db.query(sql, sqlParam);
  580. }
  581. /**
  582. * 取待审批期列表(wap用)
  583. *
  584. * @param auditorId
  585. * @return {Promise<*>}
  586. */
  587. async getAuditChangePlanByWap(auditorId) {
  588. const sql =
  589. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`cpid`,' +
  590. ' s.*,' +
  591. ' t.`name` as `t_name`, t.`project_id`, t.`type`, t.`user_id`,' +
  592. ' ti.`deal_info`, ti.`decimal` ' +
  593. ' FROM ?? AS sa' +
  594. ' Left Join ?? AS s On sa.`cpid` = s.`id`' +
  595. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  596. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  597. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  598. const sqlParam = [
  599. this.tableName,
  600. this.ctx.service.changePlan.tableName,
  601. this.ctx.service.tender.tableName,
  602. this.ctx.service.tenderInfo.tableName,
  603. auditorId,
  604. auditConst.status.checking,
  605. ];
  606. return await this.db.query(sql, sqlParam);
  607. }
  608. /**
  609. * 审批撤回
  610. * @param {Number} stageId - 标段id
  611. * @param {Number} times - 第几次审批
  612. * @return {Promise<void>}
  613. */
  614. async checkCancel(change) {
  615. // 分3种情况,根据ctx.cancancel值判断:
  616. // 1.原报发起撤回,当前流程删除,并回到待上报
  617. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  618. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  619. switch (change.cancancel) {
  620. case 1: await this._userCheckCancel(change); break;
  621. case 2: await this._auditCheckCancel(change); break;
  622. case 4: await this._auditCheckCancelNo(change); break;
  623. default: throw '不可撤回,请刷新页面重试';
  624. }
  625. }
  626. /**
  627. * 原报撤回,直接改动审批人状态
  628. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  629. *
  630. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  631. * ...
  632. *
  633. * @param stage
  634. * @returns {Promise<void>}
  635. * @private
  636. */
  637. async _userCheckCancel(change) {
  638. const transaction = await this.db.beginTransaction();
  639. try {
  640. // 整理当前流程审核人状态更新
  641. const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times, status: auditConst.status.checking });
  642. // 审批人变成待审批状态
  643. await transaction.update(this.tableName, {
  644. id: curAudit.id,
  645. status: auditConst.status.uncheck,
  646. begin_time: null,
  647. opinion: null,
  648. });
  649. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  650. // 清单审批值删除并重算变更金额
  651. await this.ctx.service.changePlanList.delAuditAmount(transaction, change.id);
  652. // 变成待上报状态
  653. await transaction.update(this.ctx.service.changePlan.tableName, {
  654. id: change.id,
  655. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  656. });
  657. await transaction.commit();
  658. } catch (err) {
  659. await transaction.rollback();
  660. throw err;
  661. }
  662. }
  663. /**
  664. * 审批人撤回审批通过,插入两条数据
  665. *
  666. * 一审 1 A checked 一审 1 A checked
  667. * 二审 2 B checked pre -> 二审 2 B checked
  668. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  669. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  670. * 三审 5 C uncheck order、status改
  671. * 四审 6 D uncheck order改
  672. *
  673. * @param stage
  674. * @returns {Promise<void>}
  675. * @private
  676. */
  677. async _auditCheckCancel(change) {
  678. const time = new Date();
  679. const transaction = await this.db.beginTransaction();
  680. try {
  681. // 整理当前流程审核人状态更新
  682. const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times, status: auditConst.status.checking });
  683. const preAudit = change.preAudit;
  684. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  685. throw '撤回用户数据错误';
  686. }
  687. // 顺移其后审核人流程顺序
  688. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  689. await transaction.query(sql, [change.id, change.times, curAudit.order]);
  690. // 当前审批人2次添加至流程中
  691. const newAuditors = [];
  692. // 先入撤回记录
  693. newAuditors.push({
  694. tid: change.tid,
  695. cpid: change.id,
  696. aid: preAudit.aid,
  697. times: change.times,
  698. order: curAudit.order,
  699. status: auditConst.status.checkCancel,
  700. begin_time: time,
  701. end_time: time,
  702. opinion: '',
  703. });
  704. newAuditors.push({
  705. tid: change.tid,
  706. cpid: change.id,
  707. aid: preAudit.aid,
  708. times: change.times,
  709. order: curAudit.order + 1,
  710. status: auditConst.status.checking,
  711. begin_time: time,
  712. });
  713. await transaction.insert(this.tableName, newAuditors);
  714. // 当前审批人变成待审批
  715. await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
  716. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  717. // 清除上一人的值并调整spamount值
  718. const updateList = [];
  719. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  720. where: { cpid: change.id },
  721. });
  722. for (const cl of changeList) {
  723. const audit_amount = cl.audit_amount.split(',');
  724. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  725. audit_amount.splice(-1, 1);
  726. const list_update = {
  727. id: cl.id,
  728. audit_amount: audit_amount.join(','),
  729. spamount: parseFloat(last_amount),
  730. };
  731. updateList.push(list_update);
  732. }
  733. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateList);
  734. // 更新total_price
  735. await this.ctx.service.changePlanList.calcCamountSum(transaction);
  736. await transaction.commit();
  737. } catch (err) {
  738. await transaction.rollback();
  739. throw err;
  740. }
  741. }
  742. /**
  743. * 审批人撤回审批退回原报
  744. *
  745. * 1# 一审 1 A checked 1# 一审 1 A checked
  746. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  747. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  748. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  749. * 三审 5 C uncheck order改
  750. *
  751. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  752. * 二审 2 B uncheck
  753. * 三审 3 C uncheck
  754. *
  755. * @param stage
  756. * @returns {Promise<void>}
  757. * @private
  758. */
  759. async _auditCheckCancelNo(change) {
  760. const time = new Date();
  761. const transaction = await this.db.beginTransaction();
  762. try {
  763. // const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times - 1, status: auditConst.status.back });
  764. const curAudit = await this.getAuditorByStatus(change.id, auditConst.status.checkNo, change.times);
  765. // 整理上一个流程审核人状态更新
  766. // 顺移其后审核人流程顺序
  767. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  768. await transaction.query(sql, [change.id, change.times - 1, curAudit.order]);
  769. // 当前审批人2次添加至流程中
  770. const newAuditors = [];
  771. newAuditors.push({
  772. tid: change.tid,
  773. cpid: change.id,
  774. aid: curAudit.aid,
  775. times: curAudit.times,
  776. order: curAudit.order + 1,
  777. status: auditConst.status.checkCancel,
  778. begin_time: time,
  779. end_time: time,
  780. opinion: '',
  781. });
  782. newAuditors.push({
  783. tid: change.tid,
  784. cpid: change.id,
  785. aid: curAudit.aid,
  786. times: curAudit.times,
  787. order: curAudit.order + 2,
  788. status: auditConst.status.checking,
  789. begin_time: time,
  790. });
  791. await transaction.insert(this.tableName, newAuditors);
  792. // 删除当前次审批流
  793. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  794. // 回退数据
  795. await this.ctx.service.changePlanHistory.returnHistory(transaction, change.id);
  796. await transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: change.id });
  797. // // 设置变更立项为审批中
  798. // await transaction.update(this.ctx.service.changeProject.tableName, {
  799. // id: change.id,
  800. // time: change.times - 1,
  801. // status: auditConst.status.checking,
  802. // });
  803. await transaction.commit();
  804. } catch (err) {
  805. await transaction.rollback();
  806. throw err;
  807. }
  808. }
  809. /**
  810. * 重新审批变更令
  811. * @param { string } cid - 查询的清单
  812. * @return {Promise<*>} - 可用的变更令列表
  813. */
  814. async checkRevise(change) {
  815. const time = new Date();
  816. // 初始化事务
  817. const transaction = await this.db.beginTransaction();
  818. let result = false;
  819. try {
  820. const pid = this.ctx.session.sessionProject.id;
  821. // 获取审核人列表
  822. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  823. const sqlParam = [this.tableName, change.id, change.times];
  824. const auditors = await this.db.query(sql, sqlParam);
  825. // 添加到消息推送表
  826. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  827. const records = [];
  828. auditors.forEach(auditor => {
  829. records.push({
  830. pid,
  831. type: pushType.changePlan,
  832. uid: auditor.aid,
  833. status: auditConst.status.revise,
  834. content: noticeContent,
  835. });
  836. });
  837. await transaction.insert('zh_notice', records);
  838. // 获取当前次数审批人列表
  839. const auditList = await this.getAuditGroupByList(change.id, change.times);
  840. const lastAudit = await this.getLastAudit(change.id, change.times);
  841. const insert_audit_array = [];
  842. // 新增一个发起修订状态到审批流程中
  843. const revise_audit = {
  844. tid: change.tid,
  845. cpid: change.id,
  846. aid: change.uid,
  847. times: change.times,
  848. order: lastAudit.order + 1,
  849. status: auditConst.status.revise,
  850. begin_time: time,
  851. end_time: time,
  852. opinion: '',
  853. };
  854. insert_audit_array.push(revise_audit);
  855. // 新增新一次的审批人列表
  856. let order = 1;
  857. for (const al of auditList) {
  858. const insert_audit = {
  859. tid: change.tid,
  860. cpid: change.id,
  861. aid: al.aid,
  862. times: change.times + 1,
  863. order,
  864. status: auditConst.status.uncheck,
  865. };
  866. insert_audit_array.push(insert_audit);
  867. order++;
  868. }
  869. await transaction.insert(this.tableName, insert_audit_array);
  870. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  871. const changeData = await transaction.get(this.ctx.service.changePlan.tableName, { id: change.id });
  872. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  873. where: { cpid: change.id },
  874. });
  875. await this.ctx.service.changePlanHistory.saveHistory(transaction, changeData, changeList);
  876. // 清单审批值删除并重算变更金额
  877. await this.ctx.service.changePlanList.delAuditAmount(transaction, change.id);
  878. // 设置变更立项修订状态
  879. await transaction.update(this.ctx.service.changePlan.tableName, {
  880. id: change.id,
  881. decimal: null,
  882. status: auditConst.status.revise,
  883. times: change.times + 1,
  884. });
  885. await transaction.commit();
  886. result = true;
  887. } catch (error) {
  888. console.log(error);
  889. await transaction.rollback();
  890. result = false;
  891. }
  892. return result;
  893. }
  894. /**
  895. * 撤销修订变更令
  896. * @param { string } cid - 查询的清单
  897. * @return {Promise<*>} - 可用的变更令列表
  898. */
  899. async cancelRevise(change) {
  900. const time = new Date();
  901. // 初始化事务
  902. const transaction = await this.db.beginTransaction();
  903. let result = false;
  904. try {
  905. const pid = this.ctx.session.sessionProject.id;
  906. // 获取审核人列表
  907. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  908. const sqlParam = [this.tableName, change.id, change.times - 1];
  909. const auditors = await this.db.query(sql, sqlParam);
  910. // 添加到消息推送表
  911. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  912. const records = [];
  913. auditors.forEach(auditor => {
  914. records.push({
  915. pid,
  916. type: pushType.changePlan,
  917. uid: auditor.aid,
  918. status: auditConst.status.cancelRevise,
  919. content: noticeContent,
  920. });
  921. });
  922. await transaction.insert('zh_notice', records);
  923. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  924. // 新增一个撤销修订状态到审批流程中
  925. const revise_audit = {
  926. tid: change.tid,
  927. cpid: change.id,
  928. aid: this.ctx.session.sessionUser.accountId,
  929. times: change.times - 1,
  930. order: lastAudit.order + 1,
  931. status: auditConst.status.cancelRevise,
  932. begin_time: time,
  933. end_time: time,
  934. opinion: '',
  935. };
  936. await transaction.insert(this.ctx.service.changePlanAudit.tableName, revise_audit);
  937. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { cpid: change.id, times: change.times });
  938. await this.ctx.service.changePlanHistory.returnHistory(transaction, change.id);
  939. await transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: change.id });
  940. await transaction.commit();
  941. result = true;
  942. } catch (error) {
  943. console.log(error);
  944. await transaction.rollback();
  945. result = false;
  946. }
  947. return result;
  948. }
  949. /**
  950. * 重新审批变更方案
  951. * @param { string } cid - 查询的清单
  952. * @return {Promise<*>} - 可用的变更令列表
  953. */
  954. async checkAgain(change) {
  955. // 初始化事务
  956. const time = new Date();
  957. const transaction = await this.db.beginTransaction();
  958. let result = false;
  959. try {
  960. // 获取终审
  961. const zsAudit = await this.getAuditorByStatus(change.id, auditConst.status.checked);
  962. const lastAudit = await this.getLastAudit(change.id, change.times);
  963. const insert_audit_array = [];
  964. // 新增2个审批状态到审批列表中
  965. insert_audit_array.push({
  966. tid: change.tid,
  967. cpid: change.id,
  968. aid: zsAudit.aid,
  969. times: zsAudit.times,
  970. order: lastAudit.order + 1,
  971. status: auditConst.status.checkAgain,
  972. begin_time: time,
  973. end_time: time,
  974. opinion: '',
  975. });
  976. // 新增2个审批人到审批列表中
  977. insert_audit_array.push({
  978. tid: change.tid,
  979. cpid: change.id,
  980. aid: zsAudit.aid,
  981. times: zsAudit.times,
  982. order: lastAudit.order + 2,
  983. status: auditConst.status.checking,
  984. begin_time: time,
  985. });
  986. await transaction.insert(this.tableName, insert_audit_array);
  987. // 设置变更令审批中
  988. await transaction.update(this.ctx.service.changePlan.tableName, {
  989. id: change.id,
  990. status: auditConst.status.checking,
  991. });
  992. // 清除上一人的值并调整spamount值
  993. const updateList = [];
  994. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  995. where: { cpid: change.id },
  996. });
  997. for (const cl of changeList) {
  998. const audit_amount = cl.audit_amount.split(',');
  999. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  1000. audit_amount.splice(-1, 1);
  1001. const list_update = {
  1002. id: cl.id,
  1003. audit_amount: audit_amount.join(','),
  1004. spamount: parseFloat(last_amount),
  1005. };
  1006. updateList.push(list_update);
  1007. }
  1008. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateList);
  1009. // 更新total_price
  1010. await this.ctx.service.changePlanList.calcCamountSum(transaction);
  1011. await transaction.commit();
  1012. result = true;
  1013. } catch (error) {
  1014. await transaction.rollback();
  1015. result = false;
  1016. }
  1017. return result;
  1018. }
  1019. }
  1020. return ChangePlanAudit;
  1021. };