change_apply_audit.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').changeApply;
  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 ChangeApplyAudit 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_apply_audit';
  28. }
  29. /**
  30. * 获取 审核列表信息
  31. *
  32. * @param {Number} caId - 变更立项id
  33. * @param {Number} times - 第几次审批
  34. * @return {Promise<*>}
  35. */
  36. async getAuditors(caId, 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 `caid` = ? 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.`caid` = ? 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, caId, times, caId, times];
  41. const result = await this.db.query(sql, sqlParam);
  42. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `caid` = ? AND `times` = ? GROUP BY `aid`) as a';
  43. const sqlParam2 = [this.tableName, caId, 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} caId - 变更立项id
  54. * @param {Number} times - 第几次审批
  55. * @return {Promise<*>}
  56. */
  57. async getCurAuditor(caId, 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.`caid` = ? and la.`status` = ? and la.`times` = ?';
  61. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, caId, 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.`caid`, la.`aid`, la.`order` ' +
  72. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  73. ' WHERE la.`caid` = ? 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(caId, 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.`caid`, la.`aid`, la.`order` ' +
  94. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  95. ' WHERE la.`caid` = ? and la.`status` = ? ' +
  96. ' ORDER BY la.`times` desc, la.`order` desc';
  97. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, caId, 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.`caid`, la.`aid`, la.`order` ' +
  102. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  103. ' WHERE la.`caid` = ? and la.`status` = ? and la.`times` = ?' +
  104. ' ORDER BY la.`times` desc, la.`order` desc';
  105. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, caId, 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(caid, times, transaction = null) {
  115. const sql = 'SELECT * FROM ?? WHERE `caid` = ? AND `times` = ? ORDER BY `order` DESC';
  116. const sqlParam = [this.tableName, caid, 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(caId, times = 1) {
  126. const result = await this.getAuditGroupByList(caId, times);
  127. const sql =
  128. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As caid, 0 As `order`' +
  129. ' FROM ' +
  130. this.ctx.service.changeApply.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, caId, caId];
  138. const user = await this.db.queryOne(sql, sqlParam);
  139. result.unshift(user);
  140. return result;
  141. }
  142. /**
  143. * 新增审核人
  144. *
  145. * @param {Number} caId - 立项书id
  146. * @param {Number} auditorId - 审核人id
  147. * @param {Number} times - 第几次审批
  148. * @return {Promise<number>}
  149. */
  150. async addAuditor(caId, 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(caId, times);
  155. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  156. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  157. if (is_gdzs) await this._syncOrderByDelete(transaction, caId, newOrder, times, '+');
  158. const data = {
  159. tid: this.ctx.tender.id,
  160. caid: caId,
  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} caId - 立项书id
  179. * @param {Number} times - 第几次审批
  180. * @return {Promise<number>}
  181. */
  182. async getNewOrder(caId, times = 1) {
  183. const sql = 'SELECT Max(??) As max_order FROM ?? Where `caid` = ? and `times` = ?';
  184. const sqlParam = ['order', this.tableName, caId, 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} caId - 变更立项书id
  192. * @param {Number} auditorId - 审核人id
  193. * @param {Number} times - 第几次审批
  194. * @return {Promise<boolean>}
  195. */
  196. async deleteAuditor(caId, auditorId, times = 1) {
  197. const transaction = await this.db.beginTransaction();
  198. try {
  199. const condition = { caid: caId, aid: auditorId, times };
  200. const auditor = await this.getDataByCondition(condition);
  201. if (!auditor) {
  202. throw '该审核人不存在';
  203. }
  204. await this._syncOrderByDelete(transaction, caId, 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} caId - 变更立项书id
  217. * @param {Number} auditorId - 审核人id
  218. * @param {Number} times - 第几次审批
  219. * @return {Promise<*>}
  220. * @private
  221. */
  222. async _syncOrderByDelete(transaction, caId, order, times, selfOperate = '-') {
  223. this.initSqlBuilder();
  224. this.sqlBuilder.setAndWhere('caid', {
  225. value: caId,
  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} caId - 立项书id
  247. * @param {Number} times - 第几次审批
  248. * @return {Promise<boolean>}
  249. */
  250. async start(caId, times = 1) {
  251. const audit = await this.getDataByCondition({ caid: caId, 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.changeApply.tableName, {
  263. id: caId, status: auditConst.status.checking,
  264. });
  265. // 微信模板通知
  266. const wechatData = {
  267. type: 'apply',
  268. status: wxConst.status.check,
  269. tips: wxConst.tips.check,
  270. code: this.ctx.session.sessionProject.code,
  271. c_name: this.ctx.change.name,
  272. };
  273. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  274. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  275. pid: this.ctx.session.sessionProject.id,
  276. tid: this.ctx.tender.id,
  277. uid: audit.aid,
  278. sp_type: 'change',
  279. sp_id: audit.id,
  280. table_name: this.tableName,
  281. template: wxConst.template.change,
  282. wx_data: wechatData,
  283. });
  284. await transaction.delete(this.ctx.service.changeApplyHistory.tableName, { caid: caId });
  285. // todo 更新标段tender状态 ?
  286. // 检查三方特殊推送
  287. await this.ctx.service.specMsg.addChangeApplyMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_apply.flow);
  288. await transaction.commit();
  289. } catch (err) {
  290. await transaction.rollback();
  291. throw err;
  292. }
  293. return true;
  294. }
  295. /**
  296. * 获取审核人需要审核的期列表
  297. *
  298. * @param auditorId
  299. * @return {Promise<*>}
  300. */
  301. async getAuditChangeApply(auditorId) {
  302. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`caid`,' +
  303. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  304. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  305. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  306. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  307. ' and ma.`caid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  308. const sqlParam = [this.tableName, this.ctx.service.changeApply.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  309. const result = await this.db.query(sql, sqlParam);
  310. // 过滤result中存在重复sid的值, 保留最新的一条
  311. const filterResult = [];
  312. const caidArr = [];
  313. for (const r of result) {
  314. if (caidArr.indexOf(r.caid) === -1) {
  315. filterResult.push(r);
  316. caidArr.push(r.caid);
  317. }
  318. }
  319. return filterResult;
  320. }
  321. /**
  322. * 获取审核人审核的次数
  323. *
  324. * @param auditorId
  325. * @return {Promise<*>}
  326. */
  327. async getCountByChecked(auditorId) {
  328. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  329. }
  330. /**
  331. * 获取最近一次审批结束时间
  332. *
  333. * @param auditorId
  334. * @return {Promise<*>}
  335. */
  336. async getLastEndTimeByChecked(auditorId) {
  337. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  338. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  339. const sqlParam = [this.tableName, auditorId];
  340. const result = await this.db.queryOne(sql, sqlParam);
  341. return result ? result.end_time : null;
  342. }
  343. /**
  344. * 用于添加推送所需的content内容
  345. * @param {Number} pid 项目id
  346. * @param {Number} tid 台账id
  347. * @param {Number} caId 立项书id
  348. * @param {Number} uid 审批人id
  349. */
  350. async getNoticeContent(pid, tid, caId, uid, opinion = '') {
  351. const noticeSql = 'SELECT * FROM (SELECT ' +
  352. ' t.`id` As `tid`, ma.`caid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  353. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  354. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  355. ' LEFT JOIN ?? As ma ON m.`id` = ma.`caid`' +
  356. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  357. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  358. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changeApply.tableName, caId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  359. const content = await this.db.query(noticeSql, noticeSqlParam);
  360. if (content.length) {
  361. content[0].opinion = opinion;
  362. }
  363. return content.length ? JSON.stringify(content[0]) : '';
  364. }
  365. /**
  366. * 审批
  367. * @param {Number} caId - 立项书id
  368. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  369. * @param {Number} times - 第几次审批
  370. * @return {Promise<void>}
  371. */
  372. async check(caId, checkData, times = 1) {
  373. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo) {
  374. throw '提交数据错误';
  375. }
  376. const pid = this.ctx.session.sessionProject.id;
  377. switch (checkData.checkType) {
  378. case auditConst.status.checked:
  379. await this._checked(pid, caId, checkData, times);
  380. break;
  381. case auditConst.status.checkNo:
  382. await this._checkNo(pid, caId, checkData, times);
  383. break;
  384. default:
  385. throw '无效审批操作';
  386. }
  387. }
  388. async _checked(pid, caId, checkData, times) {
  389. const time = new Date();
  390. // 整理当前流程审核人状态更新
  391. const audit = await this.getDataByCondition({ caid: caId, times, status: auditConst.status.checking });
  392. if (!audit) {
  393. throw '审核数据错误';
  394. }
  395. // 获取审核人列表
  396. const sql = 'SELECT `tid`, `caid`, `aid`, `order` FROM ?? WHERE `caid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  397. const sqlParam = [this.tableName, caId, times];
  398. const auditors = await this.db.query(sql, sqlParam);
  399. const nextAudit = await this.getDataByCondition({ caid: caId, times, order: audit.order + 1 });
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  403. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  404. // 获取推送必要信息
  405. const noticeContent = await this.getNoticeContent(pid, audit.tid, caId, audit.aid, checkData.opinion);
  406. // 添加推送
  407. const records = [{ pid, type: pushType.changeApply, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  408. auditors.forEach(audit => {
  409. records.push({ pid, type: pushType.changeApply, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  410. });
  411. await transaction.insert('zh_notice', records);
  412. // 无下一审核人表示,审核结束
  413. if (nextAudit) {
  414. // 流程至下一审批人
  415. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  416. // 同步 期信息
  417. await transaction.update(this.ctx.service.changeApply.tableName, {
  418. id: caId, status: auditConst.status.checking,
  419. });
  420. // 微信模板通知
  421. const wechatData = {
  422. type: 'apply',
  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.addChangeApplyMsg(transaction, pid, this.ctx.change, pushOperate.change_apply.flow);
  441. } else {
  442. // 本期结束
  443. // 生成截止本期数据 final数据
  444. // 同步 期信息
  445. await transaction.update(this.ctx.service.changeApply.tableName, {
  446. id: caId, status: checkData.checkType,
  447. notice_code: checkData.notice_code,
  448. notice_uid: checkData.notice_uid,
  449. decimal: JSON.stringify(this.ctx.change.decimal),
  450. });
  451. // 微信模板通知
  452. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  453. const wechatData = {
  454. type: 'apply',
  455. status: wxConst.status.success,
  456. tips: wxConst.tips.success,
  457. code: this.ctx.session.sessionProject.code,
  458. c_name: this.ctx.change.name,
  459. };
  460. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  461. // 检查三方特殊推送
  462. await this.ctx.service.specMsg.addChangeApplyMsg(transaction, pid, this.ctx.change, pushOperate.change_apply.flow);
  463. await this.ctx.service.specMsg.addChangeApplyMsg(transaction, pid, this.ctx.change, pushOperate.change_apply.checked);
  464. }
  465. await transaction.commit();
  466. } catch (err) {
  467. await transaction.rollback();
  468. throw err;
  469. }
  470. }
  471. async _checkNo(pid, caId, checkData, times) {
  472. const time = new Date();
  473. const changeData = await this.ctx.service.changeApply.getDataById(caId);
  474. // 整理当前流程审核人状态更新
  475. const audit = await this.getDataByCondition({ caid: caId, times, status: auditConst.status.checking });
  476. if (!audit) {
  477. throw '审核数据错误';
  478. }
  479. // const sql = 'SELECT `tid`, `caid`, `aid`, `order` FROM ?? WHERE `caid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  480. // const sqlParam = [this.tableName, caId, times];
  481. // const auditors = await this.db.query(sql, sqlParam);
  482. const auditors = await this.getAuditGroupByList(caId, times);
  483. let order = 1;
  484. const newAuditors = [];
  485. for (const a of auditors) {
  486. a.times = times + 1;
  487. a.order = order;
  488. a.status = auditConst.status.uncheck;
  489. order++;
  490. newAuditors.push({
  491. tid: this.ctx.tender.id,
  492. caid: caId,
  493. times: a.times,
  494. order: a.order,
  495. status: a.status,
  496. aid: a.aid,
  497. });
  498. }
  499. const transaction = await this.db.beginTransaction();
  500. try {
  501. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  502. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  503. // 添加到消息推送表
  504. const noticeContent = await this.getNoticeContent(pid, audit.tid, caId, audit.aid, checkData.opinion);
  505. const records = [{ pid, type: pushType.changeApply, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  506. auditors.forEach(audit => {
  507. records.push({ pid, type: pushType.changeApply, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  508. });
  509. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  510. // 同步期信息
  511. await transaction.update(this.ctx.service.changeApply.tableName, {
  512. id: caId, status: checkData.checkType,
  513. times: times + 1,
  514. });
  515. // 拷贝新一次审核流程列表
  516. await transaction.insert(this.tableName, newAuditors);
  517. // 微信模板通知
  518. // const begin_audit = await this.getDataByCondition({
  519. // mid: materialId,
  520. // order: 1,
  521. // });
  522. // 微信模板通知
  523. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  524. const wechatData = {
  525. type: 'apply',
  526. status: wxConst.status.back,
  527. tips: wxConst.tips.back,
  528. code: this.ctx.session.sessionProject.code,
  529. c_name: this.ctx.change.name,
  530. };
  531. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  532. // 生成内容保存表至zh_change_project_history中,用于撤回
  533. // 回退spamount值数据
  534. const changeList = await this.ctx.service.changeApplyList.getAllDataByCondition({
  535. where: { caid: caId },
  536. });
  537. await this.ctx.service.changeApplyHistory.saveHistory(transaction, changeData, changeList);
  538. // 检查三方特殊推送
  539. await this.ctx.service.specMsg.addChangeApplyMsg(transaction, pid, this.ctx.change, pushOperate.change_apply.flow);
  540. await transaction.commit();
  541. } catch (err) {
  542. await transaction.rollback();
  543. throw err;
  544. }
  545. }
  546. /**
  547. * 复制上一期的审批人列表给最新一期
  548. *
  549. * @param transaction - 新增一期的事务
  550. * @param {Object} preMaterial - 上一期
  551. * @param {Object} newaMaterial - 最新一期
  552. * @return {Promise<*>}
  553. */
  554. async copyPreChangeApplyAuditors(transaction, preChange, newChange) {
  555. const auditors = await this.getAuditGroupByList(preChange.id, preChange.times);
  556. const newAuditors = [];
  557. for (const a of auditors) {
  558. const na = {
  559. tid: preChange.tid,
  560. caid: newChange.id,
  561. aid: a.aid,
  562. times: newChange.times,
  563. order: newAuditors.length + 1,
  564. status: auditConst.status.uncheck,
  565. };
  566. newAuditors.push(na);
  567. }
  568. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  569. return result ? result.affectedRows === auditors.length : true;
  570. }
  571. async getAllAuditors(tenderId) {
  572. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  573. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  574. ' WHERE t.id = ?' +
  575. ' GROUP BY ma.aid';
  576. const sqlParam = [tenderId];
  577. return this.db.query(sql, sqlParam);
  578. }
  579. /**
  580. * 审批撤回
  581. * @param {Number} stageId - 标段id
  582. * @param {Number} times - 第几次审批
  583. * @return {Promise<void>}
  584. */
  585. async checkCancel(change) {
  586. // 分3种情况,根据ctx.cancancel值判断:
  587. // 1.原报发起撤回,当前流程删除,并回到待上报
  588. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  589. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  590. switch (change.cancancel) {
  591. case 1: await this._userCheckCancel(change); break;
  592. case 2: await this._auditCheckCancel(change); break;
  593. case 4: await this._auditCheckCancelNo(change); break;
  594. default: throw '不可撤回,请刷新页面重试';
  595. }
  596. }
  597. /**
  598. * 原报撤回,直接改动审批人状态
  599. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  600. *
  601. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  602. * ...
  603. *
  604. * @param stage
  605. * @returns {Promise<void>}
  606. * @private
  607. */
  608. async _userCheckCancel(change) {
  609. const transaction = await this.db.beginTransaction();
  610. try {
  611. // 整理当前流程审核人状态更新
  612. const curAudit = await this.getDataByCondition({ caid: change.id, times: change.times, status: auditConst.status.checking });
  613. // 审批人变成待审批状态
  614. await transaction.update(this.tableName, {
  615. id: curAudit.id,
  616. status: auditConst.status.uncheck,
  617. begin_time: null,
  618. opinion: null,
  619. });
  620. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  621. // 变成待上报状态
  622. await transaction.update(this.ctx.service.changeApply.tableName, {
  623. id: change.id,
  624. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  625. });
  626. await transaction.commit();
  627. } catch (err) {
  628. await transaction.rollback();
  629. throw err;
  630. }
  631. }
  632. /**
  633. * 审批人撤回审批通过,插入两条数据
  634. *
  635. * 一审 1 A checked 一审 1 A checked
  636. * 二审 2 B checked pre -> 二审 2 B checked
  637. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  638. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  639. * 三审 5 C uncheck order、status改
  640. * 四审 6 D uncheck order改
  641. *
  642. * @param stage
  643. * @returns {Promise<void>}
  644. * @private
  645. */
  646. async _auditCheckCancel(change) {
  647. const time = new Date();
  648. const transaction = await this.db.beginTransaction();
  649. try {
  650. // 整理当前流程审核人状态更新
  651. const curAudit = await this.getDataByCondition({ caid: change.id, times: change.times, status: auditConst.status.checking });
  652. const preAudit = change.preAudit;
  653. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  654. throw '撤回用户数据错误';
  655. }
  656. // 顺移其后审核人流程顺序
  657. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE caid = ? AND times = ? AND `order` > ?';
  658. await transaction.query(sql, [change.id, change.times, curAudit.order]);
  659. // 当前审批人2次添加至流程中
  660. const newAuditors = [];
  661. // 先入撤回记录
  662. newAuditors.push({
  663. tid: change.tid,
  664. caid: change.id,
  665. aid: preAudit.aid,
  666. times: change.times,
  667. order: curAudit.order,
  668. status: auditConst.status.checkCancel,
  669. begin_time: time,
  670. end_time: time,
  671. opinion: '',
  672. });
  673. newAuditors.push({
  674. tid: change.tid,
  675. caid: change.id,
  676. aid: preAudit.aid,
  677. times: change.times,
  678. order: curAudit.order + 1,
  679. status: auditConst.status.checking,
  680. begin_time: time,
  681. });
  682. await transaction.insert(this.tableName, newAuditors);
  683. // 当前审批人变成待审批
  684. await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
  685. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, curAudit.id);
  686. await transaction.commit();
  687. } catch (err) {
  688. await transaction.rollback();
  689. throw err;
  690. }
  691. }
  692. /**
  693. * 审批人撤回审批退回原报
  694. *
  695. * 1# 一审 1 A checked 1# 一审 1 A checked
  696. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  697. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  698. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  699. * 三审 5 C uncheck order改
  700. *
  701. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  702. * 二审 2 B uncheck
  703. * 三审 3 C uncheck
  704. *
  705. * @param stage
  706. * @returns {Promise<void>}
  707. * @private
  708. */
  709. async _auditCheckCancelNo(change) {
  710. const time = new Date();
  711. const transaction = await this.db.beginTransaction();
  712. try {
  713. // const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times - 1, status: auditConst.status.back });
  714. const curAudit = await this.getAuditorByStatus(change.id, auditConst.status.checkNo, change.times);
  715. // 整理上一个流程审核人状态更新
  716. // 顺移其后审核人流程顺序
  717. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE caid = ? AND times = ? AND `order` > ?';
  718. await transaction.query(sql, [change.id, change.times - 1, curAudit.order]);
  719. // 当前审批人2次添加至流程中
  720. const newAuditors = [];
  721. newAuditors.push({
  722. tid: change.tid,
  723. caid: change.id,
  724. aid: curAudit.aid,
  725. times: curAudit.times,
  726. order: curAudit.order + 1,
  727. status: auditConst.status.checkCancel,
  728. begin_time: time,
  729. end_time: time,
  730. opinion: '',
  731. });
  732. newAuditors.push({
  733. tid: change.tid,
  734. caid: change.id,
  735. aid: curAudit.aid,
  736. times: curAudit.times,
  737. order: curAudit.order + 2,
  738. status: auditConst.status.checking,
  739. begin_time: time,
  740. });
  741. await transaction.insert(this.tableName, newAuditors);
  742. // 删除当前次审批流
  743. await transaction.delete(this.tableName, { caid: change.id, times: change.times });
  744. // 回退数据
  745. await this.ctx.service.changeApplyHistory.returnHistory(transaction, change.id);
  746. await transaction.delete(this.ctx.service.changeApplyHistory.tableName, { caid: change.id });
  747. // // 设置变更立项为审批中
  748. // await transaction.update(this.ctx.service.changeProject.tableName, {
  749. // id: change.id,
  750. // time: change.times - 1,
  751. // status: auditConst.status.checking,
  752. // });
  753. await transaction.commit();
  754. } catch (err) {
  755. await transaction.rollback();
  756. throw err;
  757. }
  758. }
  759. /**
  760. * 重新审批变更令
  761. * @param { string } cid - 查询的清单
  762. * @return {Promise<*>} - 可用的变更令列表
  763. */
  764. async checkRevise(change) {
  765. const time = new Date();
  766. // 初始化事务
  767. const transaction = await this.db.beginTransaction();
  768. let result = false;
  769. try {
  770. const pid = this.ctx.session.sessionProject.id;
  771. // 获取审核人列表
  772. const sql = 'SELECT `tid`, `caid`, `aid`, `order` FROM ?? WHERE `caid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  773. const sqlParam = [this.tableName, change.id, change.times];
  774. const auditors = await this.db.query(sql, sqlParam);
  775. // 添加到消息推送表
  776. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  777. const records = [];
  778. auditors.forEach(auditor => {
  779. records.push({
  780. pid,
  781. type: pushType.changeApply,
  782. uid: auditor.aid,
  783. status: auditConst.status.revise,
  784. content: noticeContent,
  785. });
  786. });
  787. await transaction.insert('zh_notice', records);
  788. // 获取当前次数审批人列表
  789. const auditList = await this.getAuditGroupByList(change.id, change.times);
  790. const lastAudit = await this.getLastAudit(change.id, change.times);
  791. const insert_audit_array = [];
  792. // 新增一个发起修订状态到审批流程中
  793. const revise_audit = {
  794. tid: change.tid,
  795. caid: change.id,
  796. aid: change.uid,
  797. times: change.times,
  798. order: lastAudit.order + 1,
  799. status: auditConst.status.revise,
  800. begin_time: time,
  801. end_time: time,
  802. opinion: '',
  803. };
  804. insert_audit_array.push(revise_audit);
  805. // 新增新一次的审批人列表
  806. let order = 1;
  807. for (const al of auditList) {
  808. const insert_audit = {
  809. tid: change.tid,
  810. caid: change.id,
  811. aid: al.aid,
  812. times: change.times + 1,
  813. order,
  814. status: auditConst.status.uncheck,
  815. };
  816. insert_audit_array.push(insert_audit);
  817. order++;
  818. }
  819. await transaction.insert(this.tableName, insert_audit_array);
  820. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  821. const changeData = await transaction.get(this.ctx.service.changeApply.tableName, { id: change.id });
  822. const changeList = await this.ctx.service.changeApplyList.getAllDataByCondition({
  823. where: { caid: change.id },
  824. });
  825. await this.ctx.service.changeApplyHistory.saveHistory(transaction, changeData, changeList);
  826. // 设置变更立项修订状态
  827. await transaction.update(this.ctx.service.changeApply.tableName, {
  828. id: change.id,
  829. notice_code: null,
  830. notice_uid: null,
  831. decimal: null,
  832. status: auditConst.status.revise,
  833. times: change.times + 1,
  834. });
  835. await transaction.commit();
  836. result = true;
  837. } catch (error) {
  838. console.log(error);
  839. await transaction.rollback();
  840. result = false;
  841. }
  842. return result;
  843. }
  844. /**
  845. * 撤销修订变更令
  846. * @param { string } cid - 查询的清单
  847. * @return {Promise<*>} - 可用的变更令列表
  848. */
  849. async cancelRevise(change) {
  850. const time = new Date();
  851. // 初始化事务
  852. const transaction = await this.db.beginTransaction();
  853. let result = false;
  854. try {
  855. const pid = this.ctx.session.sessionProject.id;
  856. // 获取审核人列表
  857. const sql = 'SELECT `tid`, `caid`, `aid`, `order` FROM ?? WHERE `caid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  858. const sqlParam = [this.tableName, change.id, change.times - 1];
  859. const auditors = await this.db.query(sql, sqlParam);
  860. // 添加到消息推送表
  861. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  862. const records = [];
  863. auditors.forEach(auditor => {
  864. records.push({
  865. pid,
  866. type: pushType.changeApply,
  867. uid: auditor.aid,
  868. status: auditConst.status.cancelRevise,
  869. content: noticeContent,
  870. });
  871. });
  872. await transaction.insert('zh_notice', records);
  873. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  874. // 新增一个撤销修订状态到审批流程中
  875. const revise_audit = {
  876. tid: change.tid,
  877. caid: change.id,
  878. aid: this.ctx.session.sessionUser.accountId,
  879. times: change.times - 1,
  880. order: lastAudit.order + 1,
  881. status: auditConst.status.cancelRevise,
  882. begin_time: time,
  883. end_time: time,
  884. opinion: '',
  885. };
  886. await transaction.insert(this.ctx.service.changeApplyAudit.tableName, revise_audit);
  887. await transaction.delete(this.ctx.service.changeApplyAudit.tableName, { caid: change.id, times: change.times });
  888. await this.ctx.service.changeApplyHistory.returnHistory(transaction, change.id);
  889. await transaction.delete(this.ctx.service.changeApplyHistory.tableName, { caid: change.id });
  890. await transaction.commit();
  891. result = true;
  892. } catch (error) {
  893. console.log(error);
  894. await transaction.rollback();
  895. result = false;
  896. }
  897. return result;
  898. }
  899. /**
  900. * 重新审批变更申请
  901. * @param { string } cid - 查询的清单
  902. * @return {Promise<*>} - 可用的变更令列表
  903. */
  904. async checkAgain(change) {
  905. // 初始化事务
  906. const time = new Date();
  907. const transaction = await this.db.beginTransaction();
  908. let result = false;
  909. try {
  910. // 获取终审
  911. const zsAudit = await this.getAuditorByStatus(change.id, auditConst.status.checked);
  912. const lastAudit = await this.getLastAudit(change.id, change.times);
  913. const insert_audit_array = [];
  914. // 新增2个审批状态到审批列表中
  915. insert_audit_array.push({
  916. tid: change.tid,
  917. caid: change.id,
  918. aid: zsAudit.aid,
  919. times: zsAudit.times,
  920. order: lastAudit.order + 1,
  921. status: auditConst.status.checkAgain,
  922. begin_time: time,
  923. end_time: time,
  924. opinion: '',
  925. });
  926. // 新增2个审批人到审批列表中
  927. insert_audit_array.push({
  928. tid: change.tid,
  929. caid: change.id,
  930. aid: zsAudit.aid,
  931. times: zsAudit.times,
  932. order: lastAudit.order + 2,
  933. status: auditConst.status.checking,
  934. begin_time: time,
  935. });
  936. await transaction.insert(this.tableName, insert_audit_array);
  937. // 设置变更令审批中
  938. await transaction.update(this.ctx.service.changeApply.tableName, {
  939. id: change.id,
  940. status: auditConst.status.checking,
  941. notice_code: null,
  942. notice_uid: null,
  943. decimal: null,
  944. });
  945. // 检查三方特殊推送
  946. await this.ctx.service.specMsg.addChangeApplyMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_apply.flow);
  947. await transaction.commit();
  948. result = true;
  949. } catch (error) {
  950. await transaction.rollback();
  951. result = false;
  952. }
  953. return result;
  954. }
  955. }
  956. return ChangeApplyAudit;
  957. };