change_plan_audit.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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.changeProjectHistory.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. let order = 1;
  456. for (const a of auditors) {
  457. a.times = times + 1;
  458. a.order = order;
  459. a.status = auditConst.status.uncheck;
  460. order++;
  461. }
  462. const transaction = await this.db.beginTransaction();
  463. try {
  464. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  465. // 添加到消息推送表
  466. const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion);
  467. const records = [{ pid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  468. auditors.forEach(audit => {
  469. records.push({ pid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  470. });
  471. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  472. // 同步期信息
  473. await transaction.update(this.ctx.service.changePlan.tableName, {
  474. id: cpId, status: checkData.checkType,
  475. times: times + 1,
  476. });
  477. // 拷贝新一次审核流程列表
  478. await transaction.insert(this.tableName, auditors);
  479. // 清单审批值删除并重算变更金额
  480. await this.ctx.service.changePlanList.delAuditAmount(transaction, cpId);
  481. // 微信模板通知
  482. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  483. const shenpiUrl = await this.ctx.helper.urlToShort(
  484. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  485. );
  486. const wechatData = {
  487. type: 'plan',
  488. wap_url: shenpiUrl,
  489. status: wxConst.status.back,
  490. tips: wxConst.tips.back,
  491. code: this.ctx.session.sessionProject.code,
  492. c_name: this.ctx.change.name,
  493. };
  494. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  495. // 生成内容保存表至zh_change_project_history中,用于撤回
  496. // 回退spamount值数据
  497. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  498. where: { cpid: cpId },
  499. });
  500. await this.ctx.service.changePlanHistory.saveHistory(transaction, changeData, changeList);
  501. await transaction.commit();
  502. } catch (err) {
  503. await transaction.rollback();
  504. throw err;
  505. }
  506. }
  507. /**
  508. * 复制上一期的审批人列表给最新一期
  509. *
  510. * @param transaction - 新增一期的事务
  511. * @param {Object} preMaterial - 上一期
  512. * @param {Object} newaMaterial - 最新一期
  513. * @return {Promise<*>}
  514. */
  515. async copyPreChangePlanAuditors(transaction, preChange, newChange) {
  516. const auditors = await this.getAuditGroupByList(preChange.id, preChange.times);
  517. const newAuditors = [];
  518. for (const a of auditors) {
  519. const na = {
  520. tid: preChange.tid,
  521. cpid: newChange.id,
  522. aid: a.aid,
  523. times: newChange.times,
  524. order: newAuditors.length + 1,
  525. status: auditConst.status.uncheck,
  526. };
  527. newAuditors.push(na);
  528. }
  529. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  530. return result ? result.affectedRows === auditors.length : true;
  531. }
  532. async getAllAuditors(tenderId) {
  533. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  534. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  535. ' WHERE t.id = ?' +
  536. ' GROUP BY ma.aid';
  537. const sqlParam = [tenderId];
  538. return this.db.query(sql, sqlParam);
  539. }
  540. /**
  541. * 取待审批期列表(wap用)
  542. *
  543. * @param auditorId
  544. * @return {Promise<*>}
  545. */
  546. async getAuditChangePlanByWap(auditorId) {
  547. const sql =
  548. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`cpid`,' +
  549. ' s.*,' +
  550. ' t.`name` as `t_name`, t.`project_id`, t.`type`, t.`user_id`,' +
  551. ' ti.`deal_info`, ti.`decimal` ' +
  552. ' FROM ?? AS sa' +
  553. ' Left Join ?? AS s On sa.`cpid` = s.`id`' +
  554. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  555. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  556. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  557. const sqlParam = [
  558. this.tableName,
  559. this.ctx.service.changePlan.tableName,
  560. this.ctx.service.tender.tableName,
  561. this.ctx.service.tenderInfo.tableName,
  562. auditorId,
  563. auditConst.status.checking,
  564. ];
  565. return await this.db.query(sql, sqlParam);
  566. }
  567. /**
  568. * 审批撤回
  569. * @param {Number} stageId - 标段id
  570. * @param {Number} times - 第几次审批
  571. * @return {Promise<void>}
  572. */
  573. async checkCancel(change) {
  574. // 分3种情况,根据ctx.cancancel值判断:
  575. // 1.原报发起撤回,当前流程删除,并回到待上报
  576. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  577. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  578. switch (change.cancancel) {
  579. case 1: await this._userCheckCancel(change); break;
  580. case 2: await this._auditCheckCancel(change); break;
  581. case 4: await this._auditCheckCancelNo(change); break;
  582. default: throw '不可撤回,请刷新页面重试';
  583. }
  584. }
  585. /**
  586. * 原报撤回,直接改动审批人状态
  587. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  588. *
  589. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  590. * ...
  591. *
  592. * @param stage
  593. * @returns {Promise<void>}
  594. * @private
  595. */
  596. async _userCheckCancel(change) {
  597. const transaction = await this.db.beginTransaction();
  598. try {
  599. // 整理当前流程审核人状态更新
  600. const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times, status: auditConst.status.checking });
  601. // 审批人变成待审批状态
  602. await transaction.update(this.tableName, {
  603. id: curAudit.id,
  604. status: auditConst.status.uncheck,
  605. begin_time: null,
  606. opinion: null,
  607. });
  608. // 清单审批值删除并重算变更金额
  609. await this.ctx.service.changePlanList.delAuditAmount(transaction, change.id);
  610. // 变成待上报状态
  611. await transaction.update(this.ctx.service.changePlan.tableName, {
  612. id: change.id,
  613. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  614. });
  615. await transaction.commit();
  616. } catch (err) {
  617. await transaction.rollback();
  618. throw err;
  619. }
  620. }
  621. /**
  622. * 审批人撤回审批通过,插入两条数据
  623. *
  624. * 一审 1 A checked 一审 1 A checked
  625. * 二审 2 B checked pre -> 二审 2 B checked
  626. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  627. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  628. * 三审 5 C uncheck order、status改
  629. * 四审 6 D uncheck order改
  630. *
  631. * @param stage
  632. * @returns {Promise<void>}
  633. * @private
  634. */
  635. async _auditCheckCancel(change) {
  636. const time = new Date();
  637. const transaction = await this.db.beginTransaction();
  638. try {
  639. // 整理当前流程审核人状态更新
  640. const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times, status: auditConst.status.checking });
  641. const preAudit = change.preAudit;
  642. if (!curAudit || curAudit.order <= 1 || !preAudit) {
  643. throw '撤回用户数据错误';
  644. }
  645. // 顺移其后审核人流程顺序
  646. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  647. await transaction.query(sql, [change.id, change.times, curAudit.order]);
  648. // 当前审批人2次添加至流程中
  649. const newAuditors = [];
  650. // 先入撤回记录
  651. newAuditors.push({
  652. tid: change.tid,
  653. cpid: change.id,
  654. aid: preAudit.aid,
  655. times: change.times,
  656. order: curAudit.order,
  657. status: auditConst.status.checkCancel,
  658. begin_time: time,
  659. end_time: time,
  660. opinion: '',
  661. });
  662. newAuditors.push({
  663. tid: change.tid,
  664. cpid: change.id,
  665. aid: preAudit.aid,
  666. times: change.times,
  667. order: curAudit.order + 1,
  668. status: auditConst.status.checking,
  669. begin_time: time,
  670. });
  671. await transaction.insert(this.tableName, newAuditors);
  672. // 当前审批人变成待审批
  673. await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
  674. // 清除上一人的值并调整spamount值
  675. const updateList = [];
  676. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  677. where: { cpid: change.id },
  678. });
  679. for (const cl of changeList) {
  680. const audit_amount = cl.audit_amount.split(',');
  681. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  682. audit_amount.splice(-1, 1);
  683. const list_update = {
  684. id: cl.id,
  685. audit_amount: audit_amount.join(','),
  686. spamount: parseFloat(last_amount),
  687. };
  688. updateList.push(list_update);
  689. }
  690. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateList);
  691. // 更新total_price
  692. await this.ctx.service.changePlanList.calcCamountSum(transaction);
  693. await transaction.commit();
  694. } catch (err) {
  695. await transaction.rollback();
  696. throw err;
  697. }
  698. }
  699. /**
  700. * 审批人撤回审批退回原报
  701. *
  702. * 1# 一审 1 A checked 1# 一审 1 A checked
  703. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  704. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  705. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  706. * 三审 5 C uncheck order改
  707. *
  708. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  709. * 二审 2 B uncheck
  710. * 三审 3 C uncheck
  711. *
  712. * @param stage
  713. * @returns {Promise<void>}
  714. * @private
  715. */
  716. async _auditCheckCancelNo(change) {
  717. const time = new Date();
  718. const transaction = await this.db.beginTransaction();
  719. try {
  720. // const curAudit = await this.getDataByCondition({ cpid: change.id, times: change.times - 1, status: auditConst.status.back });
  721. const curAudit = await this.getAuditorByStatus(change.id, auditConst.status.checkNo, change.times);
  722. // 整理上一个流程审核人状态更新
  723. // 顺移其后审核人流程顺序
  724. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  725. await transaction.query(sql, [change.id, change.times - 1, curAudit.order]);
  726. // 当前审批人2次添加至流程中
  727. const newAuditors = [];
  728. newAuditors.push({
  729. tid: change.tid,
  730. cpid: change.id,
  731. aid: curAudit.aid,
  732. times: curAudit.times,
  733. order: curAudit.order + 1,
  734. status: auditConst.status.checkCancel,
  735. begin_time: time,
  736. end_time: time,
  737. opinion: '',
  738. });
  739. newAuditors.push({
  740. tid: change.tid,
  741. cpid: change.id,
  742. aid: curAudit.aid,
  743. times: curAudit.times,
  744. order: curAudit.order + 2,
  745. status: auditConst.status.checking,
  746. begin_time: time,
  747. });
  748. await transaction.insert(this.tableName, newAuditors);
  749. // 删除当前次审批流
  750. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  751. // 回退数据
  752. await this.ctx.service.changePlanHistory.returnHistory(transaction, change.id);
  753. await transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: change.id });
  754. // // 设置变更立项为审批中
  755. // await transaction.update(this.ctx.service.changeProject.tableName, {
  756. // id: change.id,
  757. // time: change.times - 1,
  758. // status: auditConst.status.checking,
  759. // });
  760. await transaction.commit();
  761. } catch (err) {
  762. await transaction.rollback();
  763. throw err;
  764. }
  765. }
  766. /**
  767. * 重新审批变更令
  768. * @param { string } cid - 查询的清单
  769. * @return {Promise<*>} - 可用的变更令列表
  770. */
  771. async checkRevise(change) {
  772. const time = new Date();
  773. // 初始化事务
  774. const transaction = await this.db.beginTransaction();
  775. let result = false;
  776. try {
  777. const pid = this.ctx.session.sessionProject.id;
  778. // 获取审核人列表
  779. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  780. const sqlParam = [this.tableName, change.id, change.times];
  781. const auditors = await this.db.query(sql, sqlParam);
  782. // 添加到消息推送表
  783. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  784. const records = [];
  785. auditors.forEach(auditor => {
  786. records.push({
  787. pid,
  788. type: pushType.changePlan,
  789. uid: auditor.aid,
  790. status: auditConst.status.revise,
  791. content: noticeContent,
  792. });
  793. });
  794. await transaction.insert('zh_notice', records);
  795. // 获取当前次数审批人列表
  796. const auditList = await this.getAuditGroupByList(change.id, change.times);
  797. const lastAudit = await this.getLastAudit(change.id, change.times);
  798. const insert_audit_array = [];
  799. // 新增一个发起修订状态到审批流程中
  800. const revise_audit = {
  801. tid: change.tid,
  802. cpid: change.id,
  803. aid: change.uid,
  804. times: change.times,
  805. order: lastAudit.order + 1,
  806. status: auditConst.status.revise,
  807. begin_time: time,
  808. end_time: time,
  809. opinion: '',
  810. };
  811. insert_audit_array.push(revise_audit);
  812. // 新增新一次的审批人列表
  813. let order = 1;
  814. for (const al of auditList) {
  815. const insert_audit = {
  816. tid: change.tid,
  817. cpid: change.id,
  818. aid: al.aid,
  819. times: change.times + 1,
  820. order,
  821. status: auditConst.status.uncheck,
  822. };
  823. insert_audit_array.push(insert_audit);
  824. order++;
  825. }
  826. await transaction.insert(this.tableName, insert_audit_array);
  827. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  828. const changeData = await transaction.get(this.ctx.service.changePlan.tableName, { id: change.id });
  829. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  830. where: { cpid: change.id },
  831. });
  832. await this.ctx.service.changePlanHistory.saveHistory(transaction, changeData, changeList);
  833. // 清单审批值删除并重算变更金额
  834. await this.ctx.service.changePlanList.delAuditAmount(transaction, change.id);
  835. // 设置变更立项修订状态
  836. await transaction.update(this.ctx.service.changePlan.tableName, {
  837. id: change.id,
  838. decimal: null,
  839. status: auditConst.status.revise,
  840. times: change.times + 1,
  841. });
  842. await transaction.commit();
  843. result = true;
  844. } catch (error) {
  845. console.log(error);
  846. await transaction.rollback();
  847. result = false;
  848. }
  849. return result;
  850. }
  851. /**
  852. * 撤销修订变更令
  853. * @param { string } cid - 查询的清单
  854. * @return {Promise<*>} - 可用的变更令列表
  855. */
  856. async cancelRevise(change) {
  857. const time = new Date();
  858. // 初始化事务
  859. const transaction = await this.db.beginTransaction();
  860. let result = false;
  861. try {
  862. const pid = this.ctx.session.sessionProject.id;
  863. // 获取审核人列表
  864. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  865. const sqlParam = [this.tableName, change.id, change.times - 1];
  866. const auditors = await this.db.query(sql, sqlParam);
  867. // 添加到消息推送表
  868. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  869. const records = [];
  870. auditors.forEach(auditor => {
  871. records.push({
  872. pid,
  873. type: pushType.changePlan,
  874. uid: auditor.aid,
  875. status: auditConst.status.cancelRevise,
  876. content: noticeContent,
  877. });
  878. });
  879. await transaction.insert('zh_notice', records);
  880. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  881. // 新增一个撤销修订状态到审批流程中
  882. const revise_audit = {
  883. tid: change.tid,
  884. cpid: change.id,
  885. aid: this.ctx.session.sessionUser.accountId,
  886. times: change.times - 1,
  887. order: lastAudit.order + 1,
  888. status: auditConst.status.cancelRevise,
  889. begin_time: time,
  890. end_time: time,
  891. opinion: '',
  892. };
  893. await transaction.insert(this.ctx.service.changePlanAudit.tableName, revise_audit);
  894. await transaction.delete(this.ctx.service.changePlanAudit.tableName, { cpid: change.id, times: change.times });
  895. await this.ctx.service.changePlanHistory.returnHistory(transaction, change.id);
  896. await transaction.delete(this.ctx.service.changePlanHistory.tableName, { cpid: change.id });
  897. await transaction.commit();
  898. result = true;
  899. } catch (error) {
  900. console.log(error);
  901. await transaction.rollback();
  902. result = false;
  903. }
  904. return result;
  905. }
  906. /**
  907. * 重新审批变更方案
  908. * @param { string } cid - 查询的清单
  909. * @return {Promise<*>} - 可用的变更令列表
  910. */
  911. async checkAgain(change) {
  912. // 初始化事务
  913. const time = new Date();
  914. const transaction = await this.db.beginTransaction();
  915. let result = false;
  916. try {
  917. // 获取终审
  918. const zsAudit = await this.getAuditorByStatus(change.id, auditConst.status.checked);
  919. const lastAudit = await this.getLastAudit(change.id, change.times);
  920. const insert_audit_array = [];
  921. // 新增2个审批状态到审批列表中
  922. insert_audit_array.push({
  923. tid: change.tid,
  924. cpid: change.id,
  925. aid: zsAudit.aid,
  926. times: zsAudit.times,
  927. order: lastAudit.order + 1,
  928. status: auditConst.status.checkAgain,
  929. begin_time: time,
  930. end_time: time,
  931. opinion: '',
  932. });
  933. // 新增2个审批人到审批列表中
  934. insert_audit_array.push({
  935. tid: change.tid,
  936. cpid: change.id,
  937. aid: zsAudit.aid,
  938. times: zsAudit.times,
  939. order: lastAudit.order + 2,
  940. status: auditConst.status.checking,
  941. begin_time: time,
  942. });
  943. await transaction.insert(this.tableName, insert_audit_array);
  944. // 设置变更令审批中
  945. await transaction.update(this.ctx.service.changePlan.tableName, {
  946. id: change.id,
  947. status: auditConst.status.checking,
  948. });
  949. // 清除上一人的值并调整spamount值
  950. const updateList = [];
  951. const changeList = await this.ctx.service.changePlanList.getAllDataByCondition({
  952. where: { cpid: change.id },
  953. });
  954. for (const cl of changeList) {
  955. const audit_amount = cl.audit_amount.split(',');
  956. const last_amount = audit_amount[audit_amount.length - 1] ? audit_amount[audit_amount.length - 1] : 0;
  957. audit_amount.splice(-1, 1);
  958. const list_update = {
  959. id: cl.id,
  960. audit_amount: audit_amount.join(','),
  961. spamount: parseFloat(last_amount),
  962. };
  963. updateList.push(list_update);
  964. }
  965. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.changePlanList.tableName, updateList);
  966. // 更新total_price
  967. await this.ctx.service.changePlanList.calcCamountSum(transaction);
  968. await transaction.commit();
  969. result = true;
  970. } catch (error) {
  971. await transaction.rollback();
  972. result = false;
  973. }
  974. return result;
  975. }
  976. }
  977. return ChangePlanAudit;
  978. };