123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 'use strict';
- /**
- * 与期不同,含原报
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit');
- module.exports = app => {
- class SettleAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'settle_audit';
- }
- async getAuditors(settleId, times) {
- return await this.getAllDataByCondition({ where: { settle_id: settleId, audit_times: times } }); // 全部参与的审批人
- }
- async getAuditorGroup(settleId, times) {
- const auditors = await this.getAuditors(settleId, times); // 全部参与的审批人
- return this.ctx.helper.groupAuditors(auditors, 'active_order');
- }
- async getUniqAuditors(settle) {
- const auditors = await this.getAuditors(settle.id, settle.audit_times); // 全部参与的审批人
- const result = [];
- auditors.forEach(x => {
- if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === x.audit_order; }) < 0) {
- result.push(x);
- }
- });
- return result;
- }
- async getUniqAuditorsGroup(settleId, times) {
- const group = await this.getAuditorGroup(settleId, times);
- return this.ctx.helper.groupAuditorsUniq(group);
- }
- async getAuditorHistory(settleId, times, reverse = false) {
- const history = [];
- if (times >= 1) {
- for (let i = 1; i <= times; i++) {
- const auditors = await this.getAuditors(settleId, i);
- const group = this.ctx.helper.groupAuditors(auditors);
- const historyGroup = [];
- const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
- for (const g of group) {
- const his = {
- auditYear: '', auditDate: '', auditTime: '', audit_time: null,
- audit_type: g[0].audit_type, audit_order: g[0].audit_order,
- auditors: g
- };
- if (his.audit_type === auditType.key.common) {
- his.name = g[0].name;
- } else {
- his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
- }
- his.is_final = his.audit_order === max_order;
- let audit_time;
- g.forEach(x => {
- if (x.status === auditConst.settle.status.checkSkip) return;
- if (!his.status || x.status === auditConst.settle.status.checking) his.audit_status = x.audit_status;
- if (x.audit_time && (!audit_time || x.audit_time > audit_time)) {
- audit_time = x.audit_time;
- if (his.status !== auditConst.settle.status.checking) his.audit_status = x.audit_status;
- }
- });
- if (audit_time) {
- his.audit_time = audit_time;
- const auditTime = this.ctx.moment(audit_time);
- his.auditYear = auditTime.format('YYYY');
- his.auditDate = auditTime.format('MM-DD');
- his.auditTime = auditTime.format('HH:mm:ss');
- }
- historyGroup.push(his);
- }
- if (reverse) {
- history.push(historyGroup.reverse());
- } else {
- history.push(historyGroup);
- }
- }
- }
- return history;
- }
- async copyPreAuditors(transaction, preSettle, newSettle) {
- const auditors = preSettle ? await this.getUniqAuditors(preSettle) : [];
- const newAuditors = [];
- // 添加原报
- const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
- newAuditors.push({
- tid: newSettle.tid, settle_id: newSettle.id,
- audit_id: this.ctx.session.sessionUser.accountId,
- audit_times: 1, audit_order: 0, audit_type: auditConst.auditType.key.common,
- active_order: 0, audit_status: auditConst.settle.status.uncheck,
- name: user.name, company: user.company, role: user.role, mobile: user.mobile,
- });
- // 添加其他参与人
- for (const a of auditors) {
- newAuditors.push({
- tid: newSettle.tid, settle_id: newSettle.id,
- audit_id: a.id,
- audit_times: 1, audit_order: a.audit_order, audit_type: a.audit_type,
- active_order: a.audit_order, audit_status: auditConst.auditConst.settle.status.uncheck,
- name: a.name, company: a.company, role: a.role, mobile: a.mobile,
- });
- }
- const result = await transaction.insert(this.tableName, newAuditors);
- if (result.affectedRows !== newAuditors.length) throw '初始化审批流程错误';
- }
- }
- return SettleAudit;
- };
|