| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const audit = require('../const/audit').common;
- const shenpiConst = require('../const/shenpi');
- module.exports = app => {
- class SafeStage extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'safe_stage';
- }
- calculateStage(data) {
- const helper = this.ctx.helper;
- const datas = data instanceof Array ? data : [data];
- const formatNum = this.ctx.tender.info.display.thousandth ? this.ctx.helper.formatNum : function(num) { return num ? num + '' : ''; };
- datas.forEach(x => {
- x.end_tp = helper.add(x.tp, x.pre_tp);
- for (const prop in x) {
- if (prop.indexOf('_tp') > 0) {
- x['display_' + prop] = formatNum(x[prop]);
- }
- }
- });
- }
- /**
- * 获取全部修订
- * @param tid
- * @returns {Promise<*>}
- */
- async getAllStages (tid, sort = 'ASC') {
- const result = await this.getAllDataByCondition({
- where: { tid: tid },
- orders: [['stage_order', sort]],
- });
- return result;
- }
- async getStage(id) {
- const result = await this.getDataById(id);
- result.decimal = result.bills_decimal ? JSON.parse(result.bills_decimal) : { qty: 3, tp: 2, up: 2};
- return result;
- }
- async getStageByOrder(tid, stage_order) {
- const result = await this.getDataByCondition({ tid, stage_order });
- result.decimal = result.bills_decimal ? JSON.parse(result.bills_decimal) : { qty: 3, tp: 2, up: 2};
- return result;
- }
- async getMaxOrder(tid) {
- const sql = 'SELECT Max(`stage_order`) As max_order FROM ' + this.tableName + ' Where `tid` = ?';
- const sqlParam = [tid];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.max_order || 0;
- }
- async add(tid, stage_code, stage_date) {
- if (!tid) throw '数据错误';
- const user_id = this.ctx.session.sessionUser.accountId;
- const maxOrder = await this.getMaxOrder(tid);
- const data = {
- id: this.uuid.v4(), tid: tid, create_user_id: user_id, update_user_id: user_id,
- stage_order: maxOrder + 1, stage_code, stage_date,
- audit_times: 1, audit_status: audit.status.uncheck,
- bills_decimal: JSON.stringify({ up: 2, tp: 2, qty: 3 }),
- };
- const preStage = maxOrder > 0 ? await this.getStageByOrder(tid, maxOrder) : null;
- if (preStage) {
- data.pre_bills_tp = this.ctx.helper.add(preStage.bills_tp, preStage.pre_bills_tp);
- }
- const transaction = await this.db.beginTransaction();
- try {
- const result = await transaction.insert(this.tableName, data);
- if (result.affectedRows !== 1) throw '新增安全计量期失败';
- await this.ctx.service.safeStageBills.initStageBills(transaction, data, preStage);
- await this.ctx.service.safeStageAudit.copyPreAuditors(transaction, preStage, data);
- await transaction.commit();
- return data;
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- }
- async delete(id) {
- // const info = await this.getDataById(id);
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.tableName, { id });
- await conn.delete(this.ctx.service.safeStageBills.tableName, { stage_id: id });
- const files = await this.ctx.service.safeStageFile.getFiles({ where: { stage_id: id} });
- for (const f of files) {
- this.ctx.app.fujianOss.delete(f.filepath);
- }
- await conn.delete(this.ctx.service.safeStageFile.tableName, { stage_id: id });
- await conn.delete(this.ctx.service.safeStageAudit.tableName, { stage_id: id });
- // 记录删除日志
- // await this.ctx.service.projectLog.addProjectLog(conn, projectLogConst.type.safeStage, projectLogConst.status.delete, `第${info.phase_order}期`);
- await conn.commit();
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- }
- async save(stage, data) {
- await this.defaultUpdate({ id: stage.id, stage_date: data.stage_date, stage_code: data.stage_code });
- }
- async loadUser(safeStage) {
- safeStage.user = await this.ctx.service.projectAccount.getAccountInfoById(safeStage.create_user_id);
- safeStage.auditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.curTimes || safeStage.audit_times);
- safeStage.auditorIds = this._.map(safeStage.auditors, 'audit_id');
- safeStage.curAuditors = safeStage.auditors.filter(x => { return x.audit_status === audit.status.checking; });
- safeStage.curAuditorIds = safeStage.curAuditors.map(x => { return x.audit_id; });
- safeStage.flowAuditors = safeStage.curAuditors.length === 0 ? [] : safeStage.auditors.filter(x => { return x.active_order === safeStage.curAuditors[0].active_order; });
- safeStage.flowAuditorIds = safeStage.flowAuditors.map(x => { return x.audit_id; });
- safeStage.nextAuditors = safeStage.curAuditors.length > 0 ? safeStage.auditors.filter(x => { return x.active_order === safeStage.curAuditors[0].active_order + 1; }) : [];
- safeStage.nextAuditorIds = this._.map(safeStage.nextAuditors, 'audit_id');
- safeStage.auditorGroups = this.ctx.helper.groupAuditors(safeStage.auditors, 'active_order');
- safeStage.userGroups = this.ctx.helper.groupAuditorsUniq(safeStage.auditorGroups);
- safeStage.finalAuditorIds = safeStage.userGroups.length > 1 ? safeStage.userGroups[safeStage.userGroups.length - 1].map(x => { return x.audit_id; }) : [];
- safeStage.userIds = safeStage.audit_status === audit.status.uncheck // 当前流程下全部参与人id
- ? [safeStage.user_id]
- : safeStage.auditorIds;
- }
- async loadAuditViewData(safeStage) {
- if (!safeStage.user) safeStage.user = await this.ctx.service.projectAccount.getAccountInfoById(safeStage.user_id);
- const auditTimes = safeStage.audit_status === audit.status.checkNo ? safeStage.audit_times - 1 : safeStage.audit_times;
- safeStage.auditHistory = await this.ctx.service.safeStageAudit.getAuditorHistory(safeStage.id, auditTimes);
- // 获取审批流程中左边列表
- if (safeStage.audit_status === audit.status.checkNo && safeStage.create_user_id !== this.ctx.session.sessionUser.accountId) {
- const auditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.audit_times - 1); // 全部参与的审批人
- const auditorGroups = this.ctx.helper.groupAuditors(auditors);
- safeStage.hisUserGroup = this.ctx.helper.groupAuditorsUniq(auditorGroups);
- } else {
- safeStage.hisUserGroup = safeStage.userGroups;
- }
- }
- /**
- * cancancel = 0 不可撤回
- * cancancel = 1 原报撤回
- * cancancel = 2 审批人撤回 审批通过
- * cancancel = 3 审批人撤回 审批退回上一人
- * cancancel = 4 审批人撤回 退回原报
- * cancancel = 5 会签未全部审批通过时,审批人撤回 审批通过
- *
- * @param safeStage
- * @returns {Promise<void>}
- */
- async doCheckCanCancel(safeStage) {
- // 默认不可撤回
- safeStage.cancancel = 0;
- // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
- const status = audit.status;
- if (safeStage.audit_status === status.checked || safeStage.audit_status === status.uncheck) return;
- const accountId = this.ctx.session.sessionUser.accountId;
- if (safeStage.audit_status !== status.checkNo) {
- // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
- if (safeStage.flowAuditors.find(x => { return x.audit_status !== status.checking}) && safeStage.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
- if (safeStage.curAuditorIds.indexOf(accountId) < 0 && safeStage.flowAuditorIds.indexOf(accountId) >= 0) {
- safeStage.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
- return;
- }
- 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; }) : [];
- const preAuditorCheckAgain = preAuditors.find(pa => { return pa.audit_status === status.checkAgain; });
- const preAuditorCheckCancel = preAuditors.find(pa => { return pa.audit_status === status.checkCancel; });
- const preAuditorHasOld = preAuditors.find(pa => { return pa.is_old === 1; });
- const preAuditorIds = (preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.audit_id })); // 重审不可撤回
- if ((this._.isEqual(safeStage.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel) || preAuditorHasOld) {
- return; // 不可以多次撤回
- }
- const preAuditChecked = preAuditors.find(pa => { return pa.audit_status === status.checked && pa.audit_id === accountId; });
- const preAuditCheckNoPre = preAuditors.find(pa => { return pa.audit_status === status.checkNoPre && pa.audit_id === accountId; });
- if (preAuditorIds.indexOf(accountId) >= 0) {
- if (preAuditChecked) {
- safeStage.cancancel = 2;// 审批人撤回审批通过
- } else if (preAuditCheckNoPre) {
- safeStage.cancancel = 3;// 审批人撤回审批退回上一人
- }
- safeStage.preAuditors = preAuditors;
- } else if (preAuditors.length === 0 && accountId === safeStage.create_user_id) {
- safeStage.cancancel = 1;// 原报撤回
- }
- } else {
- const lastAuditors = await this.ctx.service.safeStageAudit.getAuditors(safeStage.id, safeStage.audit_times - 1);
- const onAuditor = this._.findLast(lastAuditors, { audit_status: status.checkNo });
- if (onAuditor.audit_id === accountId) {
- safeStage.cancancel = 4;// 审批人撤回退回原报
- safeStage.preAuditors = lastAuditors.filter(x => { return x.active_order === onAuditor.active_order });
- }
- }
- }
- async doCheckStage(safeStage) {
- const accountId = this.ctx.session.sessionUser.accountId;
- // 审批退回时,原报读取本轮流程,其他人读取上一轮流程
- if (safeStage.audit_status === audit.status.checkNo) {
- safeStage.curTimes = safeStage.create_user_id === accountId ? safeStage.audit_times : safeStage.audit_times - 1;
- } else {
- safeStage.curTimes = safeStage.audit_times;
- }
- // 加载参与人
- await this.loadUser(safeStage);
- if (safeStage.audit_status === audit.status.uncheck) {
- safeStage.readOnly = accountId !== safeStage.create_user_id;
- safeStage.curSort = 0;
- } else if (safeStage.audit_status === audit.status.checkNo) {
- safeStage.readOnly = accountId !== safeStage.create_user_id;
- if (!safeStage.readOnly) {
- safeStage.curSort = 0;
- } else {
- const checkNoAudit = await this.service.safeStageAudit.getDataByCondition({
- phase_id: safeStage.id, audit_times: safeStage.audit_times - 1, audit_status: audit.status.checkNo,
- });
- safeStage.curSort = checkNoAudit.active_order;
- }
- } else if (safeStage.audit_status === audit.status.checked) {
- safeStage.readOnly = true;
- safeStage.curSort = safeStage.audit_max_sort;
- } else {
- // 会签,会签人部分审批通过时,只读,但是curSort需按原来的取值
- safeStage.curSort = safeStage.flowAuditorIds.indexOf(accountId) >= 0 ? safeStage.curAuditors[0].active_order : safeStage.curAuditors[0].active_order - 1;
- safeStage.readOnly = safeStage.curAuditorIds.indexOf(accountId) < 0;
- safeStage.canCheck = safeStage.readOnly && safeStage.curAuditorIds.indexOf(accountId) > 0;
- }
- await this.doCheckCanCancel(safeStage);
- }
- async checkShenpi(safeStage) {
- const status = audit.status;
- const info = this.ctx.tender.info;
- const shenpi_status = info.shenpi.safe_stage;
- if ((safeStage.audit_status === status.uncheck || safeStage.audit_status === status.checkNo) && shenpi_status !== shenpiConst.sp_status.sqspr) {
- // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
- const auditList = await this.ctx.service.safeStageAudit.getAllDataByCondition({ where: { phase_id: safeStage.id, audit_times: safeStage.audit_times }, orders: [['audit_order', 'asc']] });
- auditList.shift();
- if (shenpi_status === shenpiConst.sp_status.gdspl) {
- const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: safeStage.tid, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpi_status } });
- // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
- let sameAudit = auditList.length === shenpiList.length;
- if (sameAudit) {
- for (const audit of auditList) {
- const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
- if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
- sameAudit = false;
- break;
- }
- }
- }
- if (!sameAudit) {
- await this.ctx.service.safeStageAudit.updateNewAuditList(safeStage, shenpiList);
- await this.loadUser(safeStage);
- }
- } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
- const shenpiInfo = await this.ctx.service.shenpiAudit.getDataByCondition({ tid: safeStage.tid, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpi_status });
- // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
- const lastAuditors = auditList.filter(x => { x.active_order === auditList.active_order; });
- if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
- await this.ctx.service.safeStageAudit.updateLastAudit(safeStage, auditList, shenpiInfo.audit_id);
- await this.loadUser(safeStage);
- } else if (!shenpiInfo) {
- // 不存在终审人的状态下这里恢复为授权审批人
- this.ctx.tender.info.shenpi.safe_stage = shenpiConst.sp_status.sqspr;
- }
- }
- }
- }
- }
- return SafeStage;
- };
|