change_plan_audit.js 49 KB

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