change_apply_audit.js 46 KB

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