safe_stage.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../const/audit').common;
  10. const auditType = require('../const/audit').auditType;
  11. const shenpiConst = require('../const/shenpi');
  12. module.exports = app => {
  13. class SafeStage extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'safe_stage';
  23. }
  24. calculateStage(data) {
  25. const helper = this.ctx.helper;
  26. const datas = data instanceof Array ? data : [data];
  27. const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
  28. datas.forEach(x => {
  29. x.end_tp = helper.add(x.tp, x.pre_tp);
  30. for (const prop in x) {
  31. if (prop.indexOf('_tp') > 0) {
  32. x['display_' + prop] = formatNum(x[prop]);
  33. }
  34. }
  35. });
  36. }
  37. /**
  38. * 获取全部修订
  39. * @param tid
  40. * @returns {Promise<*>}
  41. */
  42. async getAllStages (tid, sort = 'ASC') {
  43. const result = await this.getAllDataByCondition({
  44. where: { tid: tid },
  45. orders: [['stage_order', sort]],
  46. });
  47. return result;
  48. }
  49. async getStage(id) {
  50. const result = await this.getDataById(id);
  51. result.decimal = result.bills_decimal ? JSON.parse(result.bills_decimal) : { qty: 3, tp: 2, up: 2};
  52. return result;
  53. }
  54. async getStageByOrder(tid, stage_order) {
  55. const result = await this.getDataByCondition({ tid, stage_order });
  56. result.decimal = result.bills_decimal ? JSON.parse(result.bills_decimal) : { qty: 3, tp: 2, up: 2};
  57. return result;
  58. }
  59. async getMaxOrder(tid) {
  60. const sql = 'SELECT Max(`stage_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
  61. const sqlParam = [tid];
  62. const result = await this.db.queryOne(sql, sqlParam);
  63. return result.max_order || 0;
  64. }
  65. async add(tid, stage_code, stage_date) {
  66. if (!tid) throw '数据错误';
  67. const user_id = this.ctx.session.sessionUser.accountId;
  68. const maxOrder = await this.getMaxOrder(tid);
  69. const data = {
  70. id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
  71. stage_order: maxOrder + 1, stage_code, stage_date,
  72. audit_times: 1, audit_status: audit.status.uncheck,
  73. bills_decimal: JSON.stringify({ up: 2, tp: 2, qty: 3 }),
  74. };
  75. const preStage = maxOrder > 0 ? await this.getStageByOrder(tid, maxOrder) : null;
  76. if (preStage) {
  77. data.pre_bills_tp = this.ctx.helper.add(preStage.bills_tp, preStage.pre_bills_tp);
  78. }
  79. const transaction = await this.db.beginTransaction();
  80. try {
  81. const result = await transaction.insert(this.tableName, data);
  82. if (result.affectedRows !== 1) throw '新增安全计量期失败';
  83. await this.ctx.service.safeStageBills.initStageBills(transaction, data, preStage);
  84. await this.ctx.service.safeStageAudit.copyPreAuditors(transaction, preStage, data);
  85. await transaction.commit();
  86. return data;
  87. } catch(err) {
  88. await transaction.rollback();
  89. throw err;
  90. }
  91. }
  92. async delete(id) {
  93. // const info = await this.getDataById(id);
  94. const conn = await this.db.beginTransaction();
  95. try {
  96. await conn.delete(this.tableName, { id });
  97. await conn.delete(this.ctx.service.safeStageBills.tableName, { stage_id: id });
  98. const files = await this.ctx.service.safeStageFile.getFiles({ where: { stage_id: id} });
  99. for (const f of files) {
  100. this.ctx.app.fujianOss.delete(f.filepath);
  101. }
  102. await conn.delete(this.ctx.service.safeStageFile.tableName, { stage_id: id });
  103. await conn.delete(this.ctx.service.safeStageAudit.tableName, { stage_id: id });
  104. // 记录删除日志
  105. // await this.ctx.service.projectLog.addProjectLog(conn, projectLogConst.type.safeStage, projectLogConst.status.delete, `第${info.stage_order}期`);
  106. await conn.commit();
  107. } catch (err) {
  108. await conn.rollback();
  109. throw err;
  110. }
  111. }
  112. async save(stage, data) {
  113. await this.defaultUpdate({ id: stage.id, stage_date: data.stage_date, stage_code: data.stage_code });
  114. }
  115. async loadUser(safeStage) {
  116. safeStage.user = await this.ctx.service.projectAccount.getAccountInfoById(safeStage.create_user_id);
  117. safeStage.auditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.curTimes || safeStage.audit_times);
  118. safeStage.auditorIds = this._.map(safeStage.auditors, 'audit_id');
  119. safeStage.curAuditors = safeStage.auditors.filter(x => { return x.audit_status === audit.status.checking; });
  120. safeStage.curAuditorIds = safeStage.curAuditors.map(x => { return x.audit_id; });
  121. safeStage.flowAuditors = safeStage.curAuditors.length === 0 ? [] : safeStage.auditors.filter(x => { return x.active_order === safeStage.curAuditors[0].active_order; });
  122. safeStage.flowAuditorIds = safeStage.flowAuditors.map(x => { return x.audit_id; });
  123. safeStage.nextAuditors = safeStage.curAuditors.length > 0 ? safeStage.auditors.filter(x => { return x.active_order === safeStage.curAuditors[0].active_order + 1; }) : [];
  124. safeStage.nextAuditorIds = this._.map(safeStage.nextAuditors, 'audit_id');
  125. safeStage.auditorGroups = this.ctx.helper.groupAuditors(safeStage.auditors, 'active_order');
  126. safeStage.userGroups = this.ctx.helper.groupAuditorsUniq(safeStage.auditorGroups);
  127. safeStage.finalAuditorIds = safeStage.userGroups.length > 1 ? safeStage.userGroups[safeStage.userGroups.length - 1].map(x => { return x.audit_id; }) : [];
  128. safeStage.userIds = safeStage.audit_status === audit.status.uncheck // 当前流程下全部参与人id
  129. ? [safeStage.user_id]
  130. : safeStage.auditorIds;
  131. }
  132. async loadAuditViewData(safeStage) {
  133. if (!safeStage.user) safeStage.user = await this.ctx.service.projectAccount.getAccountInfoById(safeStage.user_id);
  134. const auditTimes = safeStage.audit_status === audit.status.checkNo ? safeStage.audit_times - 1 : safeStage.audit_times;
  135. safeStage.auditHistory = await this.ctx.service.safeStageAudit.getAuditorHistory(safeStage.id, auditTimes);
  136. // 获取审批流程中左边列表
  137. if (safeStage.audit_status === audit.status.checkNo && safeStage.create_user_id !== this.ctx.session.sessionUser.accountId) {
  138. const auditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.audit_times - 1); // 全部参与的审批人
  139. const auditorGroups = this.ctx.helper.groupAuditors(auditors);
  140. safeStage.hisUserGroup = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  141. } else {
  142. safeStage.hisUserGroup = safeStage.userGroups;
  143. }
  144. }
  145. /**
  146. * cancancel = 0 不可撤回
  147. * cancancel = 1 原报撤回
  148. * cancancel = 2 审批人撤回 审批通过
  149. * cancancel = 3 审批人撤回 审批退回上一人
  150. * cancancel = 4 审批人撤回 退回原报
  151. * cancancel = 5 会签未全部审批通过时,审批人撤回 审批通过
  152. *
  153. * @param safeStage
  154. * @returns {Promise<void>}
  155. */
  156. async doCheckCanCancel(safeStage) {
  157. // 默认不可撤回
  158. safeStage.cancancel = 0;
  159. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  160. const status = audit.status;
  161. if (safeStage.audit_status === status.checked || safeStage.audit_status === status.uncheck) return;
  162. const accountId = this.ctx.session.sessionUser.accountId;
  163. if (safeStage.audit_status !== status.checkNo) {
  164. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  165. if (safeStage.flowAuditors.find(x => { return x.audit_status !== status.checking}) && safeStage.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
  166. if (safeStage.curAuditorIds.indexOf(accountId) < 0 && safeStage.flowAuditorIds.indexOf(accountId) >= 0) {
  167. safeStage.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
  168. return;
  169. }
  170. const preAuditors = safeStage.curAuditors[0] && safeStage.curAuditors[0].active_order !== 1 ? safeStage.auditors.filter(x => { return x.active_order === safeStage.curAuditors[0].active_order - 1; }) : [];
  171. const preAuditorCheckAgain = preAuditors.find(pa => { return pa.audit_status === status.checkAgain; });
  172. const preAuditorCheckCancel = preAuditors.find(pa => { return pa.audit_status === status.checkCancel; });
  173. const preAuditorHasOld = preAuditors.find(pa => { return pa.is_old === 1; });
  174. const preAuditorIds = (preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.audit_id })); // 重审不可撤回
  175. if ((this._.isEqual(safeStage.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel) || preAuditorHasOld) {
  176. return; // 不可以多次撤回
  177. }
  178. const preAuditChecked = preAuditors.find(pa => { return pa.audit_status === status.checked && pa.audit_id === accountId; });
  179. const preAuditCheckNoPre = preAuditors.find(pa => { return pa.audit_status === status.checkNoPre && pa.audit_id === accountId; });
  180. if (preAuditorIds.indexOf(accountId) >= 0) {
  181. if (preAuditChecked) {
  182. safeStage.cancancel = 2;// 审批人撤回审批通过
  183. } else if (preAuditCheckNoPre) {
  184. safeStage.cancancel = 3;// 审批人撤回审批退回上一人
  185. }
  186. safeStage.preAuditors = preAuditors;
  187. } else if (preAuditors.length === 0 && accountId === safeStage.create_user_id) {
  188. safeStage.cancancel = 1;// 原报撤回
  189. }
  190. } else {
  191. const lastAuditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.audit_times - 1);
  192. const onAuditor = this._.findLast(lastAuditors, { audit_status: status.checkNo });
  193. if (onAuditor.audit_id === accountId) {
  194. safeStage.cancancel = 4;// 审批人撤回退回原报
  195. safeStage.preAuditors = lastAuditors.filter(x => { return x.active_order === onAuditor.active_order });
  196. }
  197. }
  198. }
  199. async doCheckStage(safeStage) {
  200. const accountId = this.ctx.session.sessionUser.accountId;
  201. // 审批退回时,原报读取本轮流程,其他人读取上一轮流程
  202. if (safeStage.audit_status === audit.status.checkNo) {
  203. safeStage.curTimes = safeStage.create_user_id === accountId ? safeStage.audit_times : safeStage.audit_times - 1;
  204. } else {
  205. safeStage.curTimes = safeStage.audit_times;
  206. }
  207. // 加载参与人
  208. await this.loadUser(safeStage);
  209. if (safeStage.audit_status === audit.status.uncheck) {
  210. safeStage.readOnly = accountId !== safeStage.create_user_id;
  211. safeStage.curSort = 0;
  212. } else if (safeStage.audit_status === audit.status.checkNo) {
  213. safeStage.readOnly = accountId !== safeStage.create_user_id;
  214. if (!safeStage.readOnly) {
  215. safeStage.curSort = 0;
  216. } else {
  217. const checkNoAudit = await this.service.safeStageAudit.getDataByCondition({
  218. stage_id: safeStage.id, audit_times: safeStage.audit_times - 1, audit_status: audit.status.checkNo,
  219. });
  220. safeStage.curSort = checkNoAudit.active_order;
  221. }
  222. } else if (safeStage.audit_status === audit.status.checked) {
  223. safeStage.readOnly = true;
  224. safeStage.curSort = safeStage.audit_max_sort;
  225. } else {
  226. // 会签,会签人部分审批通过时,只读,但是curSort需按原来的取值
  227. safeStage.curSort = safeStage.flowAuditorIds.indexOf(accountId) >= 0 ? safeStage.curAuditors[0].active_order : safeStage.curAuditors[0].active_order - 1;
  228. safeStage.readOnly = safeStage.curAuditorIds.indexOf(accountId) < 0;
  229. safeStage.canCheck = safeStage.readOnly && safeStage.curAuditorIds.indexOf(accountId) > 0;
  230. }
  231. await this.doCheckCanCancel(safeStage);
  232. }
  233. async checkShenpi(safeStage) {
  234. const status = audit.status;
  235. const info = this.ctx.tender.info;
  236. const shenpi_status = info.shenpi.safe_stage;
  237. if ((safeStage.audit_status === status.uncheck || safeStage.audit_status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
  238. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  239. const auditList = await this.ctx.service.safeStageAudit.getAllDataByCondition({ where: { stage_id: safeStage.id, audit_times: safeStage.audit_times }, orders: [['audit_order', 'asc']] });
  240. auditList.shift();
  241. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  242. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: safeStage.tid, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpi_status } });
  243. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  244. let sameAudit = auditList.length === shenpiList.length;
  245. if (sameAudit) {
  246. for (const audit of auditList) {
  247. const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
  248. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  249. sameAudit = false;
  250. break;
  251. }
  252. }
  253. }
  254. if (!sameAudit) {
  255. await this.ctx.service.safeStageAudit.updateNewAuditList(safeStage, shenpiList);
  256. await this.loadUser(safeStage);
  257. }
  258. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  259. const shenpiInfo = await this.ctx.service.shenpiAudit.getDataByCondition({ tid: safeStage.tid, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpi_status });
  260. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  261. const lastAuditors = auditList.filter(x => { x.active_order === auditList.active_order; });
  262. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
  263. await this.ctx.service.safeStageAudit.updateLastAudit(safeStage, auditList, shenpiInfo.audit_id);
  264. await this.loadUser(safeStage);
  265. } else if (!shenpiInfo) {
  266. // 不存在终审人的状态下这里恢复为授权审批人
  267. this.ctx.tender.info.shenpi.safe_stage = shenpiConst.sp_status.sqspr;
  268. }
  269. }
  270. }
  271. }
  272. async _getUserInfo(id) {
  273. if (!this.cacheUserInfo) this.cacheUserInfo = [];
  274. const cache = this.cacheUserInfo.find(x => { return x.id === id; });
  275. if (cache) return cache;
  276. const user = await this.ctx.service.projectAccount.getDataById(id);
  277. this.cacheUserInfo.push(user);
  278. return user;
  279. }
  280. async copyPaySafeData(payTenderId) {
  281. const details = await this.ctx.service.paymentDetail.getAllDataByCondition({ where: { tender_id: payTenderId, type: 1 } });
  282. const tid = this.ctx.tender.id;
  283. const conn = await this.db.beginTransaction();
  284. try {
  285. const insertStage = [], insertBills = [], insertAudit = [], insertFile = [];
  286. for (const detail of details) {
  287. const stage = {
  288. id: this.uuid.v4(), tid, create_user_id: detail.uid, update_user_id: this.ctx.session.sessionUser.accountId,
  289. stage_order: detail.order, stage_code: detail.code, stage_date: detail.s_time,
  290. audit_times: detail.times, audit_status: detail.status,
  291. bills_decimal: detail.bills_decimal || JSON.stringify({ up: 2, tp: 2, qty: 3 }),
  292. create_time: detail.in_time, final_auditor_str: '',
  293. };
  294. insertStage.push(stage);
  295. const safeBills = await this.ctx.service.paymentSafeBills.getAllDataByCondition({ where: { detail_id: detail.id } });
  296. for (const sb of safeBills) {
  297. sb.tender_id = tid;
  298. delete sb.detail_id;
  299. sb.stage_id = stage.id;
  300. const his = sb.cur_his ? JSON.parse(sb.cur_his) : [];
  301. for (const h of his) {
  302. h.audit_times = h.times;
  303. h.active_order = h.order;
  304. delete h.times;
  305. delete h.order;
  306. }
  307. sb.cur_his = JSON.stringify(his);
  308. insertBills.push(sb);
  309. }
  310. const user = await this._getUserInfo(detail.uid);
  311. const audits = await this.ctx.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id }, orders: [['times', 'asc'], ['order', 'asc']]});
  312. if (detail.status === audit.status.checked) {
  313. const fa = await this._getUserInfo(audits[audits.length - 1].aid);
  314. stage.final_auditor_str =`${fa.name}${(fa.role ? '-' + fa.role : '')}`;
  315. }
  316. if (audits.length === 0) {
  317. insertAudit.push({
  318. tid, stage_id: stage.id, audit_id: user.id,
  319. name: user.name, company: user.company, role: user.role, mobile: user.mobile,
  320. audit_times: a.times, audit_order: 0, active_order: 0, audit_type: auditType.key.common,
  321. });
  322. }
  323. for (const a of audits) {
  324. const auditor = await this._getUserInfo(a.aid);
  325. if (a.order === 1) {
  326. insertAudit.push({
  327. tid, stage_id: stage.id, audit_id: user.id,
  328. name: user.name, company: user.company, role: user.role, mobile: user.mobile,
  329. audit_times: a.times, audit_order: 0, active_order: 0, audit_type: auditType.key.common,
  330. audit_time: a.begin_time, audit_status: audit.status.checked,
  331. });
  332. }
  333. const same = audits.filter(x => { return a.aid === x.aid && a.times === x.times; });
  334. const audit_order = this.ctx.helper._.min(same.map(x => { return x.order}));
  335. insertAudit.push({
  336. tid, stage_id: stage.id, audit_id: auditor.id,
  337. name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile,
  338. audit_times: a.times, audit_order, active_order: a.order, audit_type: auditType.key.common,
  339. audit_status: a.status, audit_time: a.end_time, opinion: a.opinion || '',
  340. });
  341. }
  342. const files = await this.ctx.service.paymentDetailAtt.getAllDataByCondition({ where: { td_id: detail.id } });
  343. for (const f of files) {
  344. const fu = await this._getUserInfo(f.uid);
  345. insertFile.push({
  346. id: this.uuid.v4(), tid, stage_id: stage.id, type: 'bills', rela_id: f.safe_id,
  347. filename: f.filename, fileext: f.fileext, filesize: f.filesize, filepath: f.filepath,
  348. user_id: fu.id, user_name: fu.name, user_company: fu.company, user_role: fu.role,
  349. create_time: fu.upload_time, update_time: fu.upload_time,
  350. });
  351. }
  352. }
  353. await conn.insert(this.tableName, insertStage);
  354. await conn.insert(this.ctx.service.safeStageBills.tableName, insertBills);
  355. await conn.insert(this.ctx.service.safeStageAudit.tableName, insertAudit);
  356. if (insertFile.length > 0) await conn.insert(this.ctx.service.safeStageFile.tableName, insertFile);
  357. await conn.commit();
  358. } catch (err) {
  359. await conn.rollback();
  360. throw err;
  361. }
  362. }
  363. }
  364. return SafeStage;
  365. };