change_project_audit.js 28 KB

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