| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const audit = require('../const/audit').common;
- const auditType = require('../const/audit').auditType;
- 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.stage_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({
- stage_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: { stage_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;
- }
- }
- }
- }
- async _getUserInfo(id) {
- if (!this.cacheUserInfo) this.cacheUserInfo = [];
- const cache = this.cacheUserInfo.find(x => { return x.id === id; });
- if (cache) return cache;
- const user = await this.ctx.service.projectAccount.getDataById(id);
- this.cacheUserInfo.push(user);
- return user;
- }
- async copyPaySafeData(payTenderId) {
- const details = await this.ctx.service.paymentDetail.getAllDataByCondition({ where: { tender_id: payTenderId, type: 1 } });
- const tid = this.ctx.tender.id;
- const conn = await this.db.beginTransaction();
- try {
- const insertStage = [], insertBills = [], insertAudit = [], insertFile = [];
- for (const detail of details) {
- const stage = {
- id: this.uuid.v4(), tid, create_user_id: detail.uid, update_user_id: this.ctx.session.sessionUser.accountId,
- stage_order: detail.order, stage_code: detail.code, stage_date: detail.s_time,
- audit_times: detail.times, audit_status: detail.status,
- bills_decimal: detail.bills_decimal || JSON.stringify({ up: 2, tp: 2, qty: 3 }),
- create_time: detail.in_time, final_auditor_str: '',
- };
- insertStage.push(stage);
- const safeBills = await this.ctx.service.paymentSafeBills.getAllDataByCondition({ where: { detail_id: detail.id } });
- for (const sb of safeBills) {
- sb.tender_id = tid;
- delete sb.detail_id;
- sb.stage_id = stage.id;
- const his = sb.cur_his ? JSON.parse(sb.cur_his) : [];
- for (const h of his) {
- h.audit_times = h.times;
- h.active_order = h.order;
- delete h.times;
- delete h.order;
- }
- sb.cur_his = JSON.stringify(his);
- insertBills.push(sb);
- }
- const user = await this._getUserInfo(detail.uid);
- const audits = await this.ctx.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id }, orders: [['times', 'asc'], ['order', 'asc']]});
- if (detail.status === audit.status.checked) {
- const fa = await this._getUserInfo(audits[audits.length - 1].aid);
- stage.final_auditor_str =`${fa.name}${(fa.role ? '-' + fa.role : '')}`;
- }
- if (audits.length === 0) {
- insertAudit.push({
- tid, stage_id: stage.id, audit_id: user.id,
- name: user.name, company: user.company, role: user.role, mobile: user.mobile,
- audit_times: a.times, audit_order: 0, active_order: 0, audit_type: auditType.key.common,
- });
- }
- for (const a of audits) {
- const auditor = await this._getUserInfo(a.aid);
- if (a.order === 1) {
- insertAudit.push({
- tid, stage_id: stage.id, audit_id: user.id,
- name: user.name, company: user.company, role: user.role, mobile: user.mobile,
- audit_times: a.times, audit_order: 0, active_order: 0, audit_type: auditType.key.common,
- audit_time: a.begin_time, audit_status: audit.status.checked,
- });
- }
- const same = audits.filter(x => { return a.aid === x.aid && a.times === x.times; });
- const audit_order = this.ctx.helper._.min(same.map(x => { return x.order}));
- insertAudit.push({
- tid, stage_id: stage.id, audit_id: auditor.id,
- name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile,
- audit_times: a.times, audit_order, active_order: a.order, audit_type: auditType.key.common,
- audit_status: a.status, audit_time: a.end_time, opinion: a.opinion || '',
- });
- }
- const files = await this.ctx.service.paymentDetailAtt.getAllDataByCondition({ where: { td_id: detail.id } });
- for (const f of files) {
- const fu = await this._getUserInfo(f.uid);
- insertFile.push({
- id: this.uuid.v4(), tid, stage_id: stage.id, type: 'bills', rela_id: f.safe_id,
- filename: f.filename, fileext: f.fileext, filesize: f.filesize, filepath: f.filepath,
- user_id: fu.id, user_name: fu.name, user_company: fu.company, user_role: fu.role,
- create_time: fu.upload_time, update_time: fu.upload_time,
- });
- }
- }
- await conn.insert(this.tableName, insertStage);
- await conn.insert(this.ctx.service.safeStageBills.tableName, insertBills);
- await conn.insert(this.ctx.service.safeStageAudit.tableName, insertAudit);
- if (insertFile.length > 0) await conn.insert(this.ctx.service.safeStageFile.tableName, insertFile);
- await conn.commit();
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- }
- }
- return SafeStage;
- };
|