change_apply_audit.js 46 KB

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