change_project_audit.js 48 KB

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