phase_pay_audit.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. 'use strict';
  2. /**
  3. * 与期不同,含原报
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const auditType = auditConst.auditType;
  11. const pushType = auditConst.pushType;
  12. const smsTypeConst = require('../const/sms_type');
  13. const wxConst = require('../const/wechat_template');
  14. module.exports = app => {
  15. class PhasePayAudit extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'phase_pay_audit';
  25. }
  26. // ***** 查询审批人相关
  27. // 获取全部参与人
  28. async getAuditors(phaseId, auditTimes) {
  29. return await this.getAllDataByCondition({ where: { phase_id: phaseId, audit_times: auditTimes }, orders: [['active_order', 'asc']] }); // 全部参与的审批人
  30. }
  31. // 获取全部参与人 分组
  32. async getAuditorGroup(phaseId, auditTimes) {
  33. const auditors = await this.getAuditors(phaseId, auditTimes); // 全部参与的审批人
  34. return this.ctx.helper.groupAuditors(auditors, 'active_order');
  35. }
  36. // 获取全部参与人 去重
  37. async getUniqAuditors(phasePay) {
  38. const auditors = await this.getAuditors(phasePay.id, phasePay.audit_times); // 全部参与的审批人
  39. const result = [];
  40. auditors.forEach(x => {
  41. if (result.findIndex(r => { return x.audit_id === r.audit_id && x.audit_order === x.audit_order; }) < 0) {
  42. result.push(x);
  43. }
  44. });
  45. return result;
  46. }
  47. // 获取全部参与人 分组 去重
  48. async getUniqAuditorsGroup(phaseId, auditTimes) {
  49. const group = await this.getAuditorGroup(phaseId, auditTimes);
  50. return this.ctx.helper.groupAuditorsUniq(group);
  51. }
  52. // 获取某个状态的审批人
  53. async getAuditorsByStatus(phaseId, auditStatus, auditTimes) {
  54. const cur = await this.db.queryOne(`SELECT * From ${this.tableName} where phase_id = ? AND audit_times = ? AND audit_status = ? ORDER By audit_times DESC, active_order DESC `, [phaseId, auditTimes, auditStatus]);
  55. if (!cur) return [];
  56. return await this.getAllDataByCondition({ where: { phase_id: phaseId, audit_times: auditTimes, audit_order: cur.audit_order, audit_status: auditStatus}});
  57. }
  58. // 获取全部审批历史
  59. async getAuditorHistory(phaseId, auditTimes, reverse = false) {
  60. const history = [];
  61. if (auditTimes < 1) return history;
  62. for (let i = 1; i <= auditTimes; i++) {
  63. const auditors = await this.getAuditors(phaseId, i);
  64. const group = this.ctx.helper.groupAuditors(auditors, 'active_order');
  65. const historyGroup = [];
  66. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  67. for (const g of group) {
  68. const his = {
  69. auditYear: '', auditDate: '', auditTime: '', audit_time: null,
  70. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  71. auditors: g
  72. };
  73. his.is_final = his.audit_order === max_order;
  74. his.auditName = his.audit_order === 0 ? '原报' : (his.is_final ? '终审' : his.audit_order + '审');
  75. his.auditCnName = his.audit_order === 0 ? '原报' : (his.is_final ? '终审' : this.ctx.helper.transFormToChinese(his.audit_order) + '审');
  76. his.name = his.audit_type === auditType.key.common ? g[0].name : his.auditName;
  77. let audit_time;
  78. g.forEach(x => {
  79. if (x.audit_status === auditConst.phasePay.status.checkSkip) return;
  80. if (!his.audit_status || x.audit_status === auditConst.phasePay.status.checking) his.audit_status = x.audit_status;
  81. if (x.audit_time && (!audit_time || x.audit_time > audit_time)) {
  82. audit_time = x.audit_time;
  83. if (his.audit_status !== auditConst.phasePay.status.checking) his.audit_status = x.audit_status;
  84. }
  85. });
  86. if (audit_time) {
  87. his.audit_time = audit_time;
  88. const auditTime = this.ctx.moment(audit_time);
  89. his.auditYear = auditTime.format('YYYY');
  90. his.auditDate = auditTime.format('MM-DD');
  91. his.auditTime = auditTime.format('HH:mm:ss');
  92. }
  93. historyGroup.push(his);
  94. }
  95. if (reverse) {
  96. history.push(historyGroup.reverse());
  97. } else {
  98. history.push(historyGroup);
  99. }
  100. }
  101. return history;
  102. }
  103. async getAllAuditors(tenderId) {
  104. const sql =
  105. 'SELECT audit_id, tid FROM ' + this.tableName +
  106. ' WHERE tid = ?' +
  107. ' GROUP BY audit_id';
  108. const sqlParam = [tenderId];
  109. return this.db.query(sql, sqlParam);
  110. }
  111. // ***** 查询审批人相关
  112. // ***** 修改审批人相关
  113. /**
  114. * 获取 最新审核顺序
  115. *
  116. * @param {Number} phaseId - 期id
  117. * @param {Number} auditTimes - 第几次审批
  118. * @return {Promise<number>}
  119. */
  120. async getNewOrder(phaseId, auditTimes = 1) {
  121. const sql = `SELECT Max(active_order) As max_order, Max(audit_order) As max_audit_order FROM ${this.tableName} Where phase_id = ? and audit_times = ?`;
  122. const result = await this.db.queryOne(sql, [phaseId, auditTimes]);
  123. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  124. }
  125. /**
  126. * 同步审核人order
  127. * @param transaction - 事务
  128. * @param {Number} phaseId - 结算期id
  129. * @param {Number} auditOrder - 审核顺序(实际顺序)
  130. * @param {Number} auditTimes - 第几次审批
  131. * @param {String} selfOperate - 操作符(+/-)
  132. * @return {Promise<*>}
  133. * @private
  134. */
  135. async _syncAuditOrder(transaction, phaseId, auditOrder, auditTimes, selfOperate = '-') {
  136. const sql = `Update ${this.tableName} SET audit_order = audit_order${selfOperate}1, active_order = active_order${selfOperate}1
  137. WHERE phase_id = ? and audit_times = ? and active_order >= ?`;
  138. return await transaction.query(sql, [phaseId, auditTimes, auditOrder]);
  139. }
  140. /**
  141. * 新增审核人
  142. *
  143. * @param {Number} phaseId - 期id
  144. * @param {Number} auditor - 审核人
  145. * @param {Number} auditTimes - 第几次审批
  146. * @param {Number} is_gdzs - 是否固定终审
  147. * @return {Promise<number>}
  148. */
  149. async addAuditor(phaseId, auditor, auditTimes = 1, is_gdzs = 0) {
  150. let [newOrder, newAuditOrder] = await this.getNewOrder(phaseId, auditTimes);
  151. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  152. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  153. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  154. const transaction = await this.db.beginTransaction();
  155. try {
  156. if (is_gdzs) await this._syncAuditOrder(transaction, phaseId, newOrder, auditTimes, '+');
  157. const data = {
  158. tid: this.ctx.tender.id, phase_id: phaseId, audit_id: auditor.id,
  159. name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile,
  160. audit_times: auditTimes, active_order: newOrder, audit_status: auditConst.phasePay.status.uncheck,
  161. audit_order: newAuditOrder, audit_type: auditType.key.common,
  162. };
  163. const result = await transaction.insert(this.tableName, data);
  164. await transaction.commit();
  165. return result.effectRows = 1;
  166. } catch (err) {
  167. await transaction.rollback();
  168. throw err;
  169. }
  170. return false;
  171. }
  172. /**
  173. * 移除审核人
  174. *
  175. * @param {Number} phaseId - 期id
  176. * @param {Number} auditorId - 审核人id
  177. * @param {Number} auditTimes - 第几次审批
  178. * @return {Promise<boolean>}
  179. */
  180. async deleteAuditor(phaseId, auditorId, auditTimes = 1) {
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. const condition = { phase_id: phaseId, audit_id: auditorId, audit_times: auditTimes };
  184. const auditor = await this.getDataByCondition(condition);
  185. if (!auditor) throw '审批人不存在';
  186. // 移除整个流程的人
  187. await transaction.delete(this.tableName, { phase_id: phaseId, active_order: auditor.active_order, audit_times: auditTimes});
  188. await this._syncAuditOrder(transaction, phaseId, auditor.active_order, auditTimes);
  189. await transaction.commit();
  190. } catch (err) {
  191. await transaction.rollback();
  192. throw err;
  193. }
  194. return true;
  195. }
  196. // 拷贝上一期审批流程
  197. async copyPreAuditors(transaction, prePhasePay, newPhasePay) {
  198. const auditors = prePhasePay ? await this.getUniqAuditors(prePhasePay) : [];
  199. const newAuditors = [];
  200. // 添加原报
  201. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  202. newAuditors.push({
  203. tid: newPhasePay.tid, phase_id: newPhasePay.id,
  204. audit_id: this.ctx.session.sessionUser.accountId,
  205. audit_times: 1, audit_order: 0, audit_type: auditConst.auditType.key.common,
  206. active_order: 0, audit_status: auditConst.phasePay.status.uncheck,
  207. name: user.name, company: user.company, role: user.role, mobile: user.mobile,
  208. });
  209. // 添加其他参与人
  210. for (const a of auditors) {
  211. if (a.audit_order === 0) continue;
  212. newAuditors.push({
  213. tid: newPhasePay.tid, phase_id: newPhasePay.id,
  214. audit_id: a.audit_id,
  215. audit_times: 1, audit_order: a.audit_order, audit_type: a.audit_type,
  216. active_order: a.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  217. name: a.name, company: a.company, role: a.role, mobile: a.mobile,
  218. });
  219. }
  220. const result = await transaction.insert(this.tableName, newAuditors);
  221. if (result.affectedRows !== newAuditors.length) throw '初始化审批流程错误';
  222. }
  223. // 固定审批流-更新
  224. async updateNewAuditList(phasePay, newList) {
  225. const newAuditsInfo = newList && newList.length > 0
  226. ? await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: newList.map(x => { return x.audit_id; })} })
  227. : [];
  228. const transaction = await this.db.beginTransaction();
  229. try {
  230. // 先删除旧的审批流,再添加新的
  231. await transaction.query(`DELETE FROM ${this.tableName} where phase_id = ? and audit_times = ? and audit_order > 0`, [phasePay.id, phasePay.audit_times]);
  232. const newAuditors = [];
  233. for (const auditor of newList) {
  234. const auditorInfo = newAuditsInfo.find(x => { return x.id === auditor.audit_id; });
  235. if (!auditorInfo) throw '配置的审批人不存在';
  236. newAuditors.push({
  237. tid: phasePay.tid, phase_id: phasePay.id, audit_id: auditor.audit_id,
  238. name: auditorInfo.name, company: auditorInfo.company, role: auditorInfo.role, mobile: auditorInfo.mobile,
  239. audit_times: phasePay.audit_times, active_order: auditor.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  240. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  241. });
  242. }
  243. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  244. await transaction.commit();
  245. } catch (err) {
  246. await transaction.rollback();
  247. throw err;
  248. }
  249. }
  250. // 固定终审-更新
  251. async updateLastAudit(phasePay, auditList, lastId) {
  252. const lastUser = lastId ? await this.ctx.service.projectAccount.getDataById(lastId) : null;
  253. const transaction = await this.db.beginTransaction();
  254. try {
  255. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  256. const existAudit = lastId ? auditList.find(x => { return x.audit_id === lastId }) : null;
  257. let auditOrder = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.active_order)}, 0) + 1 : 1; // 最大值 + 1
  258. if (existAudit) {
  259. await transaction.delete(this.tableName, { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_id: lastId });
  260. const sameOrder = auditList.filter(x => { return x.active_order === existAudit.active_order });
  261. if (sameOrder.length === 1) {
  262. const updateData = [];
  263. auditList.forEach(x => {
  264. if (x.active_order <= existAudit.active_order) return;
  265. updateData.push({id: x.id, active_order: x.active_order - 1, audit_order: x.audit_order - 1});
  266. });
  267. if (updateData.length > 0) {
  268. await transaction.updateRows(updateData);
  269. }
  270. auditOrder = auditOrder - 1;
  271. }
  272. }
  273. // 添加终审
  274. if (lastUser) {
  275. const newAuditor = {
  276. tid: phasePay.tid, phase_id: phasePay.id, audit_id: lastId,
  277. name: lastUser.name, company: lastUser.company, role: lastUser.role, mobile: lastUser.mobile,
  278. audit_times: phasePay.audit_times, active_order: auditOrder, audit_status: auditConst.phasePay.status.uncheck,
  279. audit_type: auditType.key.common, audit_order: auditOrder,
  280. };
  281. await transaction.insert(this.tableName, newAuditor);
  282. }
  283. await transaction.commit();
  284. } catch (err) {
  285. await transaction.rollback();
  286. throw err;
  287. }
  288. }
  289. // ***** 修改审批人相关
  290. // ***** 审批操作
  291. /**
  292. * 用于添加推送所需的content内容
  293. * @param {Number} pid 项目id
  294. * @param {Number} tid 台账id
  295. * @param {Number} sid 期id
  296. * @param {Number} uid 审核人id
  297. */
  298. async _getNoticeContent(pid, tid, sid, uid, opinion = '') {
  299. const noticeSql =
  300. 'SELECT * FROM (SELECT ' +
  301. ' t.`id` As `tid`, t.`name`, s.`phase_order` as `order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  302. ` FROM (SELECT * FROM ${this.ctx.service.tender.tableName} WHERE id = ? ) As t` +
  303. ` LEFT JOIN ${this.ctx.service.phasePay.tableName} As s On s.id = ?` +
  304. ` LEFT JOIN ${this.ctx.service.projectAccount.tableName} As pa ON pa.id = ?` +
  305. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  306. const noticeSqlParam = [tid, sid, uid, pid];
  307. const content = await this.db.query(noticeSql, noticeSqlParam);
  308. if (content.length) {
  309. content[0].opinion = opinion;
  310. }
  311. return content.length ? JSON.stringify(content[0]) : '';
  312. }
  313. async start(phasePay) {
  314. const audits = await this.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_order: 1 } });
  315. if (audits.length === 0) {
  316. if(this.ctx.tender.info.shenpi.phasePay === shenpiConst.sp_status.gdspl) {
  317. throw '请联系管理员添加审批人';
  318. } else {
  319. throw '请先选择审批人,再上报数据';
  320. }
  321. }
  322. const transaction = await this.db.beginTransaction();
  323. try {
  324. const audit_time = new Date();
  325. // 更新原报数据
  326. await transaction.update(this.tableName, { audit_status: auditConst.phasePay.status.checked, audit_time },
  327. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_order: 0 } });
  328. // 更新下一审批人数据
  329. const updateData = audits.map(x => { return { id: x.id, audit_status: auditConst.phasePay.status.checking } });
  330. await transaction.updateRows(this.tableName, updateData);
  331. // 计算合同支付
  332. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  333. await transaction.update(this.ctx.service.phasePay.tableName, {
  334. id: phasePay.id, audit_status: auditConst.phasePay.status.checking, audit_max_sort: 1, audit_begin_time: audit_time, ...paySum
  335. });
  336. // 拷贝新流程合同支付
  337. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, 1);
  338. // todo 添加短信通知-需要审批提醒功能
  339. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  340. const users = this._.map(audits, 'audit_id');
  341. // 微信模板通知
  342. const wechatData = {
  343. wap_url: shenpiUrl,
  344. qi: phasePay.phase_order,
  345. status: wxConst.status.check,
  346. tips: wxConst.tips.check,
  347. code: this.ctx.session.sessionProject.code,
  348. };
  349. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  350. for (const a of audits) {
  351. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  352. pid: this.ctx.session.sessionProject.id,
  353. tid: phasePay.tid,
  354. uid: a.audit_id,
  355. sp_type: 'phasePay',
  356. sp_id: a.id,
  357. table_name: this.tableName,
  358. template: wxConst.template.phasePay,
  359. wx_data: wechatData,
  360. });
  361. }
  362. // todo 上报/审批 - 检查三方特殊推送
  363. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  364. await transaction.commit();
  365. } catch (err) {
  366. await transaction.rollback();
  367. throw err;
  368. }
  369. }
  370. async _checked(phasePay, opinion) {
  371. const accountId = this.ctx.session.sessionUser.accountId;
  372. const time = new Date();
  373. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  374. if (!selfAudit) throw '当前标段您无权审批';
  375. const nextAudits = await this.getAllDataByCondition({ where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, active_order: selfAudit.active_order + 1 } });
  376. const transaction = await this.db.beginTransaction();
  377. try {
  378. // 添加通知
  379. const noticeContent = await this._getNoticeContent(this.ctx.session.sessionProject.id, phasePay.tid, phasePay.id, accountId, opinion);
  380. const defaultNoticeRecord = {
  381. pid: this.ctx.session.sessionProject.id,
  382. type: pushType.phasePay,
  383. status: auditConst.phasePay.status.checked,
  384. content: noticeContent,
  385. };
  386. const records = phasePay.userIds.map(x => {
  387. return { uid: x, ...defaultNoticeRecord };
  388. });
  389. await transaction.insert('zh_notice', records);
  390. // 更新本人审批状态
  391. await transaction.update(this.tableName, {
  392. id: selfAudit.id,
  393. audit_status: auditConst.phasePay.status.checked, opinion: opinion,
  394. audit_time: time,
  395. });
  396. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  397. // 计算合同支付
  398. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  399. if (phasePay.curAuditors.length === 1 || selfAudit.audit_type === auditType.key.or) {
  400. // 或签更新他人审批状态
  401. if (selfAudit.audit_type === auditType.key.or) {
  402. const updateOther = [];
  403. for (const audit of phasePay.curAuditors) {
  404. if (audit.audit_id === selfAudit.audit_id) continue;
  405. updateOther.push({
  406. id: audit.id,
  407. audit_status: auditConst.phasePay.status.checkSkip,
  408. opinion: '',
  409. audit_time: time,
  410. });
  411. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  412. }
  413. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  414. }
  415. // 无下一审核人表示,审核结束
  416. if (nextAudits.length > 0) {
  417. // 流程至下一审批人
  418. const updateData = nextAudits.map(x => { return { id: x.id, audit_status: auditConst.phasePay.status.checking }; });
  419. await transaction.updateRows(this.tableName, updateData);
  420. // 计算合同支付
  421. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  422. await transaction.update(this.ctx.service.phasePay.tableName, {
  423. id: phasePay.id, audit_max_sort: selfAudit.active_order + 1, audit_status: auditConst.phasePay.status.checking, ...paySum
  424. });
  425. // 拷贝新流程合同支付
  426. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, selfAudit.active_order + 1);
  427. // todo 添加短信通知-需要审批提醒功能
  428. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/phasePay/' + phasePay.phase_order);
  429. const users = this._.map(nextAudits, 'audit_id');
  430. // 微信模板通知
  431. const wechatData = {
  432. wap_url: shenpiUrl,
  433. qi: phasePay.phase_order,
  434. status: wxConst.status.check,
  435. tips: wxConst.tips.check,
  436. code: this.ctx.session.sessionProject.code,
  437. };
  438. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  439. for (const a of nextAudits) {
  440. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  441. pid: this.ctx.session.sessionProject.id,
  442. tid: this.ctx.tender.id,
  443. uid: a.audit_id,
  444. sp_type: 'phasePay',
  445. sp_id: a.id,
  446. table_name: this.tableName,
  447. template: wxConst.template.phasePay,
  448. wx_data: wechatData,
  449. });
  450. }
  451. } else {
  452. const final_auditor_str = (selfAudit.audit_type === auditType.key.common)
  453. ? `${selfAudit.name}${(selfAudit.role ? '-' + selfAudit.role : '')}`
  454. : ctx.helper.transFormToChinese(selfAudit.audit_order) + '审';
  455. await transaction.update(this.ctx.service.phasePay.tableName, {
  456. id: phasePay.id, audit_end_time: time, audit_status: auditConst.phasePay.status.checked, final_auditor_str, ...paySum
  457. });
  458. // 添加短信通知-审批通过提醒功能
  459. const users = this._.uniq(phasePay.userIds);
  460. // 微信模板通知
  461. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/phasePay/' + phasePay.phase_order);
  462. const wechatData = {
  463. wap_url: shenpiUrl,
  464. qi: phasePay.phase_order,
  465. status: wxConst.status.success,
  466. tips: wxConst.tips.success,
  467. code: this.ctx.session.sessionProject.code,
  468. };
  469. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.result.toString(), wxConst.template.phasePay, wechatData);
  470. // todo 审批通过 - 检查三方特殊推送
  471. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.checked);
  472. }
  473. // todo 上报/审批 - 检查三方特殊推送
  474. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  475. } else {
  476. // 同步 期信息
  477. await transaction.update(this.ctx.service.phasePay.tableName, { id: phasePay.id, audit_status: auditConst.phasePay.status.checking, ...paySum });
  478. }
  479. await transaction.commit();
  480. } catch (err) {
  481. await transaction.rollback();
  482. throw err;
  483. }
  484. }
  485. async _checkNo(phasePay, opinion) {
  486. const pid = this.ctx.session.sessionProject.id;
  487. const accountId = this.ctx.session.sessionUser.accountId;
  488. const time = new Date();
  489. // 整理当前流程审核人状态更新
  490. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  491. if (!selfAudit) throw '当前标段您无权审批';
  492. const auditors = await this.getUniqAuditors(phasePay); // 全部参与的审批人
  493. const newAuditors = auditors.map(x => {
  494. return {
  495. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  496. audit_times: phasePay.audit_times + 1, audit_order: x.audit_order, audit_type: x.audit_type,
  497. active_order: x.audit_order, audit_status: auditConst.phasePay.status.uncheck,
  498. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  499. }
  500. });
  501. const transaction = await this.db.beginTransaction();
  502. try {
  503. // 添加提醒
  504. const noticeContent = await this._getNoticeContent(pid, selfAudit.tid, phasePay.id, selfAudit.audit_id, opinion);
  505. const defaultNoticeRecord = { pid, type: pushType.phasePay, status: auditConst.phasePay.status.checkNo, content: noticeContent };
  506. const records = phasePay.userIds.map(x => {
  507. return { uid: x, ...defaultNoticeRecord };
  508. });
  509. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  510. // 更新审批人状态数据
  511. const updateData = [];
  512. phasePay.curAuditors.forEach(x => {
  513. if (x.audit_id === selfAudit.audit_id) {
  514. updateData.push({ id: x.id, audit_status: auditConst.phasePay.status.checkNo, opinion: opinion, audit_time: time});
  515. } else {
  516. updateData.push({ id: x.id, audit_status: auditConst.phasePay.status.checkSkip, opinion: '', audit_time: null});
  517. }
  518. });
  519. await transaction.updateRows(this.tableName, updateData);
  520. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  521. // 更新 期信息
  522. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  523. await transaction.update(this.ctx.service.phasePay.tableName, {
  524. id: phasePay.id, audit_status: auditConst.phasePay.status.checkNo, audit_times: phasePay.audit_times + 1, audit_max_sort: 0, ...paySum
  525. });
  526. // 拷贝新流程合同支付
  527. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times + 1, 0);
  528. // 拷贝新一次审核流程列表
  529. await transaction.insert(this.tableName, newAuditors);
  530. // todo 添加短信通知-审批退回提醒功能
  531. const users = this._.uniq(phasePay.userIds);
  532. // 微信模板通知
  533. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  534. const wechatData = {
  535. wap_url: shenpiUrl,
  536. qi: phasePay.phase_order,
  537. status: wxConst.status.back,
  538. tips: wxConst.tips.back,
  539. code: this.ctx.session.sessionProject.code,
  540. };
  541. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.result.toString(), wxConst.template.phasePay, wechatData);
  542. // todo 上报/审批 - 检查三方特殊推送
  543. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  544. await transaction.commit();
  545. } catch (err) {
  546. await transaction.rollback();
  547. throw err;
  548. }
  549. }
  550. async _checkNoPre(phasePay, opinion) {
  551. const pid = this.ctx.session.sessionProject.id;
  552. const accountId = this.ctx.session.sessionUser.accountId;
  553. const time = new Date();
  554. // 整理当前流程审核人状态更新
  555. const selfAudit = phasePay.curAuditors.find(x => { return x.audit_id === accountId; });
  556. if (!selfAudit) throw '当前标段您无权审批';
  557. const preAuditors = phasePay.userGroups.find(x => { return x[0].audit_order === selfAudit.audit_order - 1; });
  558. const transaction = await this.db.beginTransaction();
  559. try {
  560. // 添加通知
  561. const noticeContent = await this._getNoticeContent(pid, phasePay.tid, phasePay.id, selfAudit.audit_id, opinion);
  562. const defaultNoticeRecord = {
  563. pid, type: pushType.phasePay, status: auditConst.phasePay.status.checkNoPre, content: noticeContent,
  564. };
  565. const records = phasePay.userIds.map(x => {
  566. return { uid: x, ...defaultNoticeRecord };
  567. });
  568. await transaction.insert('zh_notice', records);
  569. // 更新同一流程所有审批人状态
  570. const updateData = [];
  571. for (const audit of phasePay.curAuditors) {
  572. if (audit.audit_id === selfAudit.audit_id) {
  573. updateData.push({
  574. id: audit.id, audit_status: auditConst.phasePay.status.checkNoPre, opinion, audit_time: time,
  575. });
  576. } else {
  577. updateData.push({
  578. id: audit.id, audit_status: auditConst.phasePay.status.checkSkip, opinion: '', audit_time: null,
  579. });
  580. }
  581. }
  582. await transaction.updateRows(this.tableName, updateData);
  583. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  584. // 顺移其后审核人流程顺序
  585. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  586. await transaction.query(sql, [phasePay.id, selfAudit.audit_times, selfAudit.active_order]);
  587. // 上一审批人,当前审批人 再次添加至流程
  588. const newAuditors = [], uncheckAuditors = [];
  589. preAuditors.forEach(x => {
  590. newAuditors.push({
  591. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  592. audit_times: x.audit_times, audit_order: selfAudit.audit_order - 1,
  593. audit_status: auditConst.phasePay.status.checking,
  594. audit_type: x.audit_type, active_order: selfAudit.active_order + 1,
  595. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  596. });
  597. });
  598. const checkingAuditors_result = await transaction.insert(this.tableName, newAuditors);
  599. // 获取刚批量添加的所有list
  600. for (let j = 0; j < newAuditors.length; j++) {
  601. newAuditors[j].id = checkingAuditors_result.insertId + j;
  602. }
  603. phasePay.flowAuditors.forEach(x => {
  604. uncheckAuditors.push({
  605. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  606. audit_times: x.audit_times, active_order: selfAudit.active_order + 2,
  607. audit_status: auditConst.phasePay.status.uncheck,
  608. audit_type: x.audit_type, audit_order: x.audit_order,
  609. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  610. });
  611. });
  612. await transaction.insert(this.tableName, uncheckAuditors);
  613. const preAuditorIds = preAuditors.map(x => { return x.audit_id; });
  614. // 同步 期信息
  615. const paySum = await this.ctx.service.phasePayDetail.calculateSave(phasePay, transaction);
  616. await transaction.update(this.ctx.service.phasePay.tableName, {
  617. id: phasePay.id, audit_max_sort: selfAudit.active_order + 1, ...paySum,
  618. });
  619. // 拷贝新流程合同支付
  620. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times, selfAudit.active_order + 1);
  621. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  622. const users = preAuditorIds;
  623. // 微信模板通知
  624. const wechatData = {
  625. wap_url: shenpiUrl,
  626. qi: phasePay.phase_order,
  627. status: wxConst.status.check,
  628. tips: wxConst.tips.check,
  629. code: this.ctx.session.sessionProject.code,
  630. };
  631. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  632. for (const a of newAuditors) {
  633. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  634. pid: this.ctx.session.sessionProject.id,
  635. tid: phasePay.tid,
  636. uid: a.audit_id,
  637. sp_type: 'phasePay',
  638. sp_id: a.id,
  639. table_name: this.tableName,
  640. template: wxConst.template.phasePay,
  641. wx_data: wechatData,
  642. });
  643. }
  644. // todo 上报/审批 - 检查三方特殊推送
  645. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  646. await transaction.commit();
  647. } catch (err) {
  648. await transaction.rollback();
  649. throw err;
  650. }
  651. }
  652. async check(phasePay, checkType, opinion = '') {
  653. switch (checkType) {
  654. case auditConst.phasePay.status.checked:
  655. await this._checked(phasePay, opinion);
  656. break;
  657. case auditConst.phasePay.status.checkNo:
  658. await this._checkNo(phasePay, opinion);
  659. break;
  660. case auditConst.phasePay.status.checkNoPre:
  661. await this._checkNoPre(phasePay, opinion);
  662. break;
  663. default:
  664. throw '无效审批操作';
  665. }
  666. }
  667. async checkAgain(phasePay, force = false) {
  668. const accountId = this.ctx.session.sessionUser.accountId;
  669. const time = new Date();
  670. // 整理当前流程审核人状态更新
  671. const finalAudits = phasePay.auditorGroups[phasePay.auditorGroups.length - 1];
  672. if (!finalAudits || finalAudits.length === 0 || finalAudits[0].audit_order < 1) throw '审核数据错误';
  673. const selfAudit = finalAudits.find(x => { return x.audit_id === accountId; });
  674. if (!selfAudit && !force) throw '当前标段您无权审批';
  675. const finalAudit = selfAudit || finalAudits[0];
  676. const transaction = await this.db.beginTransaction();
  677. try {
  678. // 当前审批人2次添加至流程中
  679. const checkAgainAuditors = [], checkingAuditors = [];
  680. finalAudits.forEach(x => {
  681. checkAgainAuditors.push({
  682. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  683. audit_type: x.audit_type, audit_order: x.audit_order,
  684. audit_times: x.audit_times, active_order: x.active_order + 1,
  685. audit_status: x.audit_id === finalAudit.audit_id ? auditConst.phasePay.status.checkAgain : auditConst.phasePay.status.checkSkip,
  686. audit_time: time, opinion: '',
  687. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  688. });
  689. });
  690. finalAudits.forEach(x => {
  691. checkingAuditors.push({
  692. tid: x.tid, phase_id: x.phase_id, audit_id: x.audit_id,
  693. audit_type: x.audit_type, audit_order: x.audit_order,
  694. audit_times: x.audit_times, active_order: x.active_order + 2,
  695. audit_status: auditConst.phasePay.status.checking, audit_time: time, opinion: '',
  696. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  697. });
  698. });
  699. await transaction.insert(this.tableName, checkAgainAuditors);
  700. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  701. // 获取刚批量添加的所有list
  702. for (let j = 0; j < checkingAuditors.length; j++) {
  703. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  704. }
  705. // 同步 期信息
  706. await transaction.update(this.ctx.service.phasePay.tableName, {
  707. id: phasePay.id, audit_status: auditConst.phasePay.status.checking, audit_end_time: null, final_auditor_str: '', audit_max_sort: finalAudit.active_order + 2,
  708. });
  709. // 拷贝新流程合同支付
  710. await this.ctx.service.phasePayDetail.initPhaseDataByAudit(transaction, phasePay, phasePay.audit_times + 1, finalAudit.active_order + 2);
  711. // todo 添加短信通知-需要审批提醒功能
  712. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + phasePay.tid + '/phasePay/' + phasePay.phase_order);
  713. const users = this._.map(phasePay.auditAssists.filter(x => { return phasePay.finalAuditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  714. users.push(...phasePay.finalAuditorIds);
  715. // 微信模板通知
  716. const wechatData = {
  717. wap_url: shenpiUrl,
  718. qi: phasePay.phase_order,
  719. status: wxConst.status.check,
  720. tips: wxConst.tips.check,
  721. code: this.ctx.session.sessionProject.code,
  722. };
  723. await this.ctx.helper.sendWechat(users, smsTypeConst.const.PAY, smsTypeConst.judge.approval.toString(), wxConst.template.phasePay, wechatData);
  724. for (const a of checkingAuditors) {
  725. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.PAY, {
  726. pid: this.ctx.session.sessionProject.id,
  727. tid: phasePay.tid,
  728. uid: a.audit_id,
  729. sp_type: 'phasePay',
  730. sp_id: a.id,
  731. table_name: this.tableName,
  732. template: wxConst.template.phasePay,
  733. wx_data: wechatData,
  734. });
  735. }
  736. // todo 上报/审批 - 检查三方特殊推送
  737. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  738. await transaction.commit();
  739. } catch (err) {
  740. await transaction.rollback();
  741. throw err;
  742. }
  743. }
  744. /**
  745. * 原报撤回,直接改动审批人状态
  746. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  747. *
  748. * 一审 1 A checking -> A uncheck status改 pay:1->0 删1
  749. * ...
  750. *
  751. * @param phasePay
  752. * @returns {Promise<void>}
  753. * @private
  754. */
  755. async _userCheckCancel(phasePay) {
  756. const transaction = await this.db.beginTransaction();
  757. try {
  758. // 整理当前流程审核人状态更新
  759. // 审批人变成待审批状态
  760. const updateData = phasePay.curAuditors.map(x => {
  761. return {
  762. id: x.id, audit_status: auditConst.phasePay.status.uncheck,
  763. audit_time: null, opinion: '',
  764. }
  765. });
  766. await transaction.updateRows(this.tableName, updateData);
  767. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  768. await transaction.update(this.ctx.service.phasePay.tableName, {
  769. id: phasePay.id, audit_times: phasePay.audit_times, audit_max_sort: 0,
  770. audit_status: phasePay.audit_times === 1 ? auditConst.phasePay.status.uncheck : auditConst.phasePay.status.checkNo,
  771. });
  772. await transaction.delete(this.ctx.service.phasePayDetail.tableName,
  773. { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 0});
  774. await transaction.update(this.ctx.service.phasePayDetail.tableName,
  775. { audit_sort: 0, master_id: phasePay.id + '-' + phasePay.audit_times + '-0' },
  776. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 1 } });
  777. // todo 上报/审批 - 检查三方特殊推送
  778. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  779. await transaction.commit();
  780. } catch(err) {
  781. await transaction.rollback();
  782. throw err;
  783. }
  784. }
  785. /**
  786. * 审批人撤回审批通过,插入两条数据
  787. *
  788. * @param phasePay
  789. * @returns {Promise<void>}
  790. * @private
  791. */
  792. async _auditCheckCancel(phasePay) {
  793. if (phasePay.curAuditors.length === 0 || phasePay.curAuditors[0].active_order <= 1) {
  794. throw '撤回用户数据错误';
  795. }
  796. const accountId = this.ctx.session.sessionUser.accountId;
  797. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId; });
  798. if (!selfAuditor) throw '撤回用户数据错误';
  799. const time = new Date();
  800. const transaction = await this.db.beginTransaction();
  801. try {
  802. // 整理当前流程审核人状态更新
  803. // 顺移其后审核人流程顺序
  804. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  805. await transaction.query(sql, [phasePay.id, selfAuditor.audit_times, phasePay.curAuditors[0].active_order]);
  806. // 当前审批人2次添加至流程中
  807. const checkCancelAuditors = [], checkingAuditors = [];
  808. phasePay.preAuditors.forEach(x => {
  809. checkCancelAuditors.push({
  810. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  811. audit_times: x.audit_times, active_order: x.active_order + 1,
  812. audit_type: x.audit_type, audit_order: x.audit_order,
  813. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  814. audit_time: time, opinion: '',
  815. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  816. });
  817. });
  818. phasePay.preAuditors.forEach(x => {
  819. checkingAuditors.push({
  820. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  821. audit_times: x.audit_times, active_order: x.active_order + 2,
  822. audit_type: x.audit_type, audit_order: x.audit_order,
  823. audit_status: auditConst.phasePay.status.checking,
  824. audit_time: time, opinion: '',
  825. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  826. });
  827. });
  828. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  829. // 当前审批人变成待审批
  830. await transaction.updateRows(this.tableName, phasePay.curAuditors.map(x => { return {
  831. id: x.id, audit_time: null, audit_status: auditConst.phasePay.status.uncheck, active_order: x.active_order + 2
  832. }}));
  833. await this.ctx.service.phasePayDetail.initPhaseDataByAuditCancel(transaction, phasePay, phasePay.audit_times, selfAuditor.active_order + 1, phasePay.audit_times, selfAuditor.active_order + 2);
  834. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(phasePay.curAuditors, 'id'));
  835. // 同步 期信息
  836. await transaction.update(this.ctx.service.phasePay.tableName, {
  837. id: phasePay.id, audit_times: phasePay.audit_times,
  838. });
  839. // todo 上报/审批 - 检查三方特殊推送
  840. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  841. await transaction.commit();
  842. } catch(err) {
  843. await transaction.rollback();
  844. throw err;
  845. }
  846. }
  847. /**
  848. * 审批人撤回审批退回上一人,插入两条数据
  849. *
  850. * @param phasePay
  851. * @returns {Promise<void>}
  852. * @private
  853. */
  854. async _auditCheckCancelNoPre(phasePay) {
  855. if (phasePay.curAuditors.length === 0 || phasePay.curAuditors[0].active_order <= 1) {
  856. throw '撤回用户数据错误';
  857. }
  858. const accountId = this.ctx.session.sessionUser.accountId;
  859. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId; });
  860. if (!selfAuditor) throw '撤回用户数据错误';
  861. const time = new Date();
  862. const transaction = await this.db.beginTransaction();
  863. try {
  864. // 整理当前流程审核人状态更新
  865. // 删除当前审批人
  866. await transaction.delete(this.tableName, { id: phasePay.curAuditors.map(x => { return x.id; }) });
  867. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(phasePay.curAuditors, 'id'));
  868. // 添加撤回人到审批流程中
  869. const newAuditors = [];
  870. phasePay.preAuditors.forEach(x => {
  871. newAuditors.push({
  872. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  873. audit_times: x.audit_times, active_order: x.active_order + 1,
  874. audit_type: x.audit_type, audit_order: x.audit_order,
  875. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  876. audit_time: time, opinion: '',
  877. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  878. });
  879. });
  880. await transaction.insert(this.tableName, newAuditors);
  881. // 更新上一个人,最新审批状态为审批中
  882. await transaction.update(this.tableName, { audit_time: null, opinion: '', audit_status: auditConst.phasePay.status.checking }, {
  883. where: { phase_id: phasePay.id, audit_times: selfAuditor.audit_times, active_order: selfAuditor.active_order + 2 }
  884. });
  885. // 同步 期信息
  886. await transaction.update(this.ctx.service.phasePay.tableName, {
  887. id: phasePay.id, audit_times: phasePay.audit_times, audit_status: auditConst.phasePay.status.checking,
  888. });
  889. await this.ctx.service.phasePayDetail.initPhaseDataByAuditCancel(transaction, phasePay, phasePay.audit_times, selfAuditor.active_order + 1, phasePay.audit_times, selfAuditor.active_order + 2);
  890. // todo 上报/审批 - 检查三方特殊推送
  891. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  892. await transaction.commit();
  893. } catch(err) {
  894. await transaction.rollback();
  895. throw err;
  896. }
  897. }
  898. /**
  899. * 审批人撤回审批退回原报
  900. *
  901. * @param phasePay
  902. * @returns {Promise<void>}
  903. * @private
  904. */
  905. async _auditCheckCancelNo(phasePay) {
  906. const accountId = this.ctx.session.sessionUser.accountId;
  907. const selfAuditor = phasePay.preAuditors.find(x => { return x.audit_id === accountId && x.audit_status === auditConst.phasePay.status.checkNo; });
  908. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  909. const time = new Date();
  910. const transaction = await this.db.beginTransaction();
  911. try {
  912. // 整理上一个流程审核人状态更新
  913. // 顺移其后审核人流程顺序
  914. const sql = 'UPDATE ' + this.tableName + ' SET `active_order` = `active_order` + 2 WHERE phase_id = ? AND audit_times = ? AND `active_order` > ?';
  915. await transaction.query(sql, [phasePay.id, selfAuditor.audit_times, selfAuditor.active_order]);
  916. // 当前审批人2次添加至流程中
  917. const checkCancelAuditors = [], checkingAuditors = [];
  918. phasePay.preAuditors.forEach(x => {
  919. checkCancelAuditors.push({
  920. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  921. audit_times: x.audit_times, active_order: x.active_order + 1,
  922. audit_type: x.audit_type, audit_order: x.audit_order,
  923. audit_status: x.audit_id === selfAuditor.audit_id ? auditConst.phasePay.status.checkCancel : auditConst.phasePay.status.checkSkip,
  924. audit_time: time, opinion: '',
  925. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  926. });
  927. });
  928. phasePay.preAuditors.forEach(x => {
  929. checkingAuditors.push({
  930. tid: phasePay.tid, phase_id: phasePay.id, audit_id: x.audit_id,
  931. audit_times: x.audit_times, active_order: x.active_order + 2,
  932. audit_type: x.audit_type, audit_order: x.audit_order,
  933. audit_status: auditConst.phasePay.status.checking,
  934. audit_time: time, opinion: '',
  935. name: x.name, company: x.company, role: x.role, mobile: x.mobile,
  936. });
  937. });
  938. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  939. // 删除当前次审批流
  940. await transaction.delete(this.tableName, { phase_id: phasePay.id, audit_times: phasePay.audit_times });
  941. // 计算并合同支付最终数据
  942. await transaction.update(this.ctx.service.phasePay.tableName, {
  943. id: phasePay.id, audit_times: phasePay.audit_times - 1, audit_status: auditConst.phasePay.status.checking,
  944. });
  945. await transaction.update(this.ctx.service.phasePayDetail.tableName,
  946. { audit_times: selfAuditor.audit_times, audit_sort: selfAuditor.active_order + 2, master_id: `${phasePay.id}-${selfAuditor.audit_times}-${selfAuditor.active_order + 2}` },
  947. { where: { phase_id: phasePay.id, audit_times: phasePay.audit_times, audit_sort: 0 } });
  948. // todo 上报/审批 - 检查三方特殊推送
  949. // await this.ctx.service.specMsg.addPhasePayMsg(transaction, this.ctx.session.sessionProject.id, phasePay, pushOperate.phasePay.flow);
  950. await transaction.commit();
  951. } catch(err) {
  952. await transaction.rollback();
  953. throw err;
  954. }
  955. }
  956. /**
  957. * 会签未全部审批通过时,撤回仅修改本人状态
  958. *
  959. * @param phasePay
  960. * @returns {Promise<void>}
  961. * @private
  962. */
  963. async _auditCheckCancelAnd(phasePay) {
  964. const accountId = this.ctx.session.sessionUser.accountId;
  965. const selfAuditor = phasePay.flowAuditors.find(x => { return x.audit_id === accountId; });
  966. if (!selfAuditor || selfAuditor.audit_status !== auditConst.phasePay.status.checked) throw '不可撤回';
  967. const transaction = await this.db.beginTransaction();
  968. try {
  969. await transaction.update(this.tableName, {
  970. id: selfAuditor.id, audit_status: auditConst.phasePay.status.checking, opinion: '', audit_time: null,
  971. });
  972. await transaction.commit();
  973. } catch(err) {
  974. await transaction.rollback();
  975. throw err;
  976. }
  977. }
  978. /**
  979. * 审批撤回
  980. * @param {Number} phasePay - 结算期
  981. * @return {Promise<void>}
  982. */
  983. async checkCancel(phasePay) {
  984. // 分5种情况,根据phasePay.cancancel值判断:
  985. // 1.原报发起撤回,当前流程删除,并回到待上报
  986. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  987. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  988. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  989. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  990. if (phasePay.cancancel === 5) {
  991. await this._auditCheckCancelAnd(phasePay);
  992. } else {
  993. switch (phasePay.cancancel) {
  994. case 1: await this._userCheckCancel(phasePay); break;
  995. case 2: await this._auditCheckCancel(phasePay); break;
  996. case 3: await this._auditCheckCancelNoPre(phasePay); break;
  997. case 4: await this._auditCheckCancelNo(phasePay); break;
  998. default: throw '不可撤回,请刷新页面重试';
  999. }
  1000. }
  1001. }
  1002. // ***** 审批操作
  1003. }
  1004. return PhasePayAudit;
  1005. };