change_project_audit.js 49 KB

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