change_apply_audit.js 44 KB

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