change_plan_audit.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  73. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times];
  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. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  92. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  93. ' WHERE la.`cpid` = ? and la.`status` = ? ' +
  94. ' ORDER BY la.`times` desc, la.`order` desc';
  95. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, status];
  96. auditor = await this.db.queryOne(sql, sqlParam);
  97. break;
  98. case auditConst.status.checkNo :
  99. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status` ' +
  100. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  101. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?' +
  102. ' ORDER BY la.`times` desc, la.`order` desc';
  103. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checkNo, parseInt(times) - 1];
  104. auditor = await this.db.queryOne(sql, sqlParam);
  105. break;
  106. case auditConst.status.uncheck :
  107. break;
  108. default:break;
  109. }
  110. return auditor;
  111. }
  112. /**
  113. * 获取审核人流程列表(包括原报)
  114. * @param {Number} materialId 调差id
  115. * @param {Number} times 审核次数
  116. * @return {Promise<Array>} 查询结果集(包括原报)
  117. */
  118. async getAuditorsWithOwner(cpId, times = 1) {
  119. const result = await this.getAuditGroupByList(cpId, times);
  120. const sql =
  121. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
  122. ' FROM ' +
  123. this.ctx.service.changePlan.tableName +
  124. ' As s' +
  125. ' LEFT JOIN ' +
  126. this.ctx.service.projectAccount.tableName +
  127. ' As pa' +
  128. ' ON s.uid = pa.id' +
  129. ' WHERE s.id = ?';
  130. const sqlParam = [times, cpId, cpId];
  131. const user = await this.db.queryOne(sql, sqlParam);
  132. result.unshift(user);
  133. return result;
  134. }
  135. /**
  136. * 新增审核人
  137. *
  138. * @param {Number} cpId - 方案id
  139. * @param {Number} auditorId - 审核人id
  140. * @param {Number} times - 第几次审批
  141. * @return {Promise<number>}
  142. */
  143. async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) {
  144. const transaction = await this.db.beginTransaction();
  145. let flag = false;
  146. try {
  147. let newOrder = await this.getNewOrder(cpId, times);
  148. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  149. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  150. if (is_gdzs) await this._syncOrderByDelete(transaction, cpId, newOrder, times, '+');
  151. const data = {
  152. tid: this.ctx.tender.id,
  153. cpid: cpId,
  154. aid: auditorId,
  155. times,
  156. order: newOrder,
  157. status: auditConst.status.uncheck,
  158. };
  159. const result = await transaction.insert(this.tableName, data);
  160. await transaction.commit();
  161. flag = result.effectRows = 1;
  162. } catch (err) {
  163. await transaction.rollback();
  164. throw err;
  165. }
  166. return flag;
  167. }
  168. /**
  169. * 获取 最新审核顺序
  170. *
  171. * @param {Number} cpId - 方案id
  172. * @param {Number} times - 第几次审批
  173. * @return {Promise<number>}
  174. */
  175. async getNewOrder(cpId, times = 1) {
  176. const sql = 'SELECT Max(??) As max_order FROM ?? Where `cpid` = ? and `times` = ?';
  177. const sqlParam = ['order', this.tableName, cpId, times];
  178. const result = await this.db.queryOne(sql, sqlParam);
  179. return result && result.max_order ? result.max_order + 1 : 1;
  180. }
  181. /**
  182. * 移除审核人
  183. *
  184. * @param {Number} cpId - 变更方案id
  185. * @param {Number} auditorId - 审核人id
  186. * @param {Number} times - 第几次审批
  187. * @return {Promise<boolean>}
  188. */
  189. async deleteAuditor(cpId, auditorId, times = 1) {
  190. const transaction = await this.db.beginTransaction();
  191. try {
  192. const condition = { cpid: cpId, aid: auditorId, times };
  193. const auditor = await this.getDataByCondition(condition);
  194. if (!auditor) {
  195. throw '该审核人不存在';
  196. }
  197. await this._syncOrderByDelete(transaction, cpId, auditor.order, times);
  198. await transaction.delete(this.tableName, condition);
  199. await transaction.commit();
  200. } catch (err) {
  201. await transaction.rollback();
  202. throw err;
  203. }
  204. return true;
  205. }
  206. /**
  207. * 移除审核人时,同步其后审核人order
  208. * @param transaction - 事务
  209. * @param {Number} cpId - 变更方案id
  210. * @param {Number} auditorId - 审核人id
  211. * @param {Number} times - 第几次审批
  212. * @return {Promise<*>}
  213. * @private
  214. */
  215. async _syncOrderByDelete(transaction, cpId, order, times, selfOperate = '-') {
  216. this.initSqlBuilder();
  217. this.sqlBuilder.setAndWhere('cpid', {
  218. value: cpId,
  219. operate: '=',
  220. });
  221. this.sqlBuilder.setAndWhere('order', {
  222. value: order,
  223. operate: '>=',
  224. });
  225. this.sqlBuilder.setAndWhere('times', {
  226. value: times,
  227. operate: '=',
  228. });
  229. this.sqlBuilder.setUpdateData('order', {
  230. value: 1,
  231. selfOperate,
  232. });
  233. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  234. const data = await transaction.query(sql, sqlParam);
  235. return data;
  236. }
  237. /**
  238. * 开始审批
  239. * @param {Number} cpId - 方案id
  240. * @param {Number} times - 第几次审批
  241. * @return {Promise<boolean>}
  242. */
  243. async start(cpId, times = 1) {
  244. const audit = await this.getDataByCondition({ cpid: cpId, times, order: 1 });
  245. if (!audit) {
  246. // if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  247. // throw '请联系管理员添加审批人';
  248. // } else {
  249. throw '请先选择审批人,再上报数据';
  250. // }
  251. }
  252. const transaction = await this.db.beginTransaction();
  253. try {
  254. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  255. await transaction.update(this.ctx.service.changePlan.tableName, {
  256. id: cpId, status: auditConst.status.checking,
  257. });
  258. // 微信模板通知
  259. const shenpiUrl = await this.ctx.helper.urlToShort(
  260. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  261. );
  262. const wechatData = {
  263. type: 'plan',
  264. wap_url: shenpiUrl,
  265. status: wxConst.status.check,
  266. tips: wxConst.tips.check,
  267. code: this.ctx.session.sessionProject.code,
  268. c_name: this.ctx.change.name,
  269. };
  270. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  271. // todo 更新标段tender状态 ?
  272. await transaction.commit();
  273. } catch (err) {
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return true;
  278. }
  279. /**
  280. * 获取审核人需要审核的期列表
  281. *
  282. * @param auditorId
  283. * @return {Promise<*>}
  284. */
  285. async getAuditChangePlan(auditorId) {
  286. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  287. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  288. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  289. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  290. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  291. ' and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  292. 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];
  293. return await this.db.query(sql, sqlParam);
  294. }
  295. /**
  296. * 获取审核人审核的次数
  297. *
  298. * @param auditorId
  299. * @return {Promise<*>}
  300. */
  301. async getCountByChecked(auditorId) {
  302. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  303. }
  304. /**
  305. * 获取最近一次审批结束时间
  306. *
  307. * @param auditorId
  308. * @return {Promise<*>}
  309. */
  310. async getLastEndTimeByChecked(auditorId) {
  311. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  312. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  313. const sqlParam = [this.tableName, auditorId];
  314. const result = await this.db.queryOne(sql, sqlParam);
  315. return result ? result.end_time : null;
  316. }
  317. /**
  318. * 用于添加推送所需的content内容
  319. * @param {Number} pid 项目id
  320. * @param {Number} tid 台账id
  321. * @param {Number} cpId 方案id
  322. * @param {Number} uid 审批人id
  323. */
  324. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  325. const noticeSql = 'SELECT * FROM (SELECT ' +
  326. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  327. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  328. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  329. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  330. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  331. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  332. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changePlan.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  333. const content = await this.db.query(noticeSql, noticeSqlParam);
  334. if (content.length) {
  335. content[0].opinion = opinion;
  336. }
  337. return content.length ? JSON.stringify(content[0]) : '';
  338. }
  339. /**
  340. * 审批
  341. * @param {Number} cpId - 方案id
  342. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  343. * @param {Number} times - 第几次审批
  344. * @return {Promise<void>}
  345. */
  346. async check(cpId, checkData, times = 1) {
  347. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo) {
  348. throw '提交数据错误';
  349. }
  350. const pid = this.ctx.session.sessionProject.id;
  351. switch (checkData.checkType) {
  352. case auditConst.status.checked:
  353. await this._checked(pid, cpId, checkData, times);
  354. break;
  355. case auditConst.status.checkNo:
  356. await this._checkNo(pid, cpId, checkData, times);
  357. break;
  358. default:
  359. throw '无效审批操作';
  360. }
  361. }
  362. async _checked(pid, cpId, checkData, times) {
  363. const time = new Date();
  364. // 整理当前流程审核人状态更新
  365. const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking });
  366. if (!audit) {
  367. throw '审核数据错误';
  368. }
  369. // 获取审核人列表
  370. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  371. const sqlParam = [this.tableName, cpId, times];
  372. const auditors = await this.db.query(sql, sqlParam);
  373. const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
  374. const transaction = await this.db.beginTransaction();
  375. try {
  376. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  377. // 获取推送必要信息
  378. const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion);
  379. // 添加推送
  380. const records = [{ pid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  381. auditors.forEach(audit => {
  382. records.push({ pid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  383. });
  384. await transaction.insert('zh_notice', records);
  385. // 清单审批流程修改的数据插入到audit_amount中,审批完成则最后一位并插入到审批后变更值中
  386. await this.ctx.service.changePlanList.insertAuditAmount(transaction, cpId, !nextAudit);
  387. // 无下一审核人表示,审核结束
  388. if (nextAudit) {
  389. // 流程至下一审批人
  390. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  391. // 同步 期信息
  392. await transaction.update(this.ctx.service.changePlan.tableName, {
  393. id: cpId, status: auditConst.status.checking,
  394. });
  395. // 微信模板通知
  396. const shenpiUrl = await this.ctx.helper.urlToShort(
  397. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  398. );
  399. const wechatData = {
  400. type: 'plan',
  401. wap_url: shenpiUrl,
  402. status: wxConst.status.check,
  403. tips: wxConst.tips.check,
  404. code: this.ctx.session.sessionProject.code,
  405. c_name: this.ctx.change.name,
  406. };
  407. await this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  408. } else {
  409. // 本期结束
  410. // 生成截止本期数据 final数据
  411. // 同步 期信息
  412. await transaction.update(this.ctx.service.changePlan.tableName, {
  413. id: cpId, status: checkData.checkType,
  414. decimal: JSON.stringify(this.ctx.change.decimal),
  415. });
  416. // 微信模板通知
  417. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  418. const shenpiUrl = await this.ctx.helper.urlToShort(
  419. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  420. );
  421. const wechatData = {
  422. type: 'plan',
  423. wap_url: shenpiUrl,
  424. status: wxConst.status.success,
  425. tips: wxConst.tips.success,
  426. code: this.ctx.session.sessionProject.code,
  427. c_name: this.ctx.change.name,
  428. };
  429. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  430. }
  431. await transaction.commit();
  432. } catch (err) {
  433. await transaction.rollback();
  434. throw err;
  435. }
  436. }
  437. async _checkNo(pid, cpId, checkData, times) {
  438. const time = new Date();
  439. // 整理当前流程审核人状态更新
  440. const audit = await this.getDataByCondition({ cpid: cpId, times, status: auditConst.status.checking });
  441. if (!audit) {
  442. throw '审核数据错误';
  443. }
  444. const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  445. const sqlParam = [this.tableName, cpId, times];
  446. const auditors = await this.db.query(sql, sqlParam);
  447. let order = 1;
  448. for (const a of auditors) {
  449. a.times = times + 1;
  450. a.order = order;
  451. a.status = auditConst.status.uncheck;
  452. order++;
  453. }
  454. const transaction = await this.db.beginTransaction();
  455. try {
  456. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  457. // 添加到消息推送表
  458. const noticeContent = await this.getNoticeContent(pid, audit.tid, cpId, audit.aid, checkData.opinion);
  459. const records = [{ pid, type: pushType.changePlan, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  460. auditors.forEach(audit => {
  461. records.push({ pid, type: pushType.changePlan, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  462. });
  463. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  464. // 同步期信息
  465. await transaction.update(this.ctx.service.changePlan.tableName, {
  466. id: cpId, status: checkData.checkType,
  467. times: times + 1,
  468. });
  469. // 拷贝新一次审核流程列表
  470. await transaction.insert(this.tableName, auditors);
  471. // 清单审批值删除并重算变更金额
  472. await this.ctx.service.changePlanList.delAuditAmount(transaction, cpId);
  473. // 微信模板通知
  474. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  475. const shenpiUrl = await this.ctx.helper.urlToShort(
  476. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/plan/' + cpId + '/information#shenpi'
  477. );
  478. const wechatData = {
  479. type: 'plan',
  480. wap_url: shenpiUrl,
  481. status: wxConst.status.back,
  482. tips: wxConst.tips.back,
  483. code: this.ctx.session.sessionProject.code,
  484. c_name: this.ctx.change.name,
  485. };
  486. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  487. await transaction.commit();
  488. } catch (err) {
  489. await transaction.rollback();
  490. throw err;
  491. }
  492. }
  493. /**
  494. * 复制上一期的审批人列表给最新一期
  495. *
  496. * @param transaction - 新增一期的事务
  497. * @param {Object} preMaterial - 上一期
  498. * @param {Object} newaMaterial - 最新一期
  499. * @return {Promise<*>}
  500. */
  501. async copyPreChangePlanAuditors(transaction, preChange, newChange) {
  502. const auditors = await this.getAuditGroupByList(preChange.id, preChange.times);
  503. const newAuditors = [];
  504. for (const a of auditors) {
  505. const na = {
  506. tid: preChange.tid,
  507. cpid: newChange.id,
  508. aid: a.aid,
  509. times: newChange.times,
  510. order: newAuditors.length + 1,
  511. status: auditConst.status.uncheck,
  512. };
  513. newAuditors.push(na);
  514. }
  515. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  516. return result ? result.affectedRows === auditors.length : true;
  517. }
  518. async getAllAuditors(tenderId) {
  519. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  520. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  521. ' WHERE t.id = ?' +
  522. ' GROUP BY ma.aid';
  523. const sqlParam = [tenderId];
  524. return this.db.query(sql, sqlParam);
  525. }
  526. /**
  527. * 取待审批期列表(wap用)
  528. *
  529. * @param auditorId
  530. * @return {Promise<*>}
  531. */
  532. async getAuditChangePlanByWap(auditorId) {
  533. const sql =
  534. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`cpid`,' +
  535. ' s.*,' +
  536. ' t.`name` as `t_name`, t.`project_id`, t.`type`, t.`user_id`,' +
  537. ' ti.`deal_info`, ti.`decimal` ' +
  538. ' FROM ?? AS sa' +
  539. ' Left Join ?? AS s On sa.`cpid` = s.`id`' +
  540. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  541. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  542. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  543. const sqlParam = [
  544. this.tableName,
  545. this.ctx.service.changePlan.tableName,
  546. this.ctx.service.tender.tableName,
  547. this.ctx.service.tenderInfo.tableName,
  548. auditorId,
  549. auditConst.status.checking,
  550. ];
  551. return await this.db.query(sql, sqlParam);
  552. }
  553. }
  554. return ChangePlanAudit;
  555. };