change_plan_audit.js 48 KB

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