phase_pay_audit.js 64 KB

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