change_apply_audit.js 41 KB

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