123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 'use strict';
- /**
- * 奖罚金
- *
- * @author Mai
- * @date
- * @version
- */
- const auditConst = require('../const/audit');
- module.exports = app => {
- class SettleAuditAss extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'settle_audit_ass';
- }
- async getData(settle) {
- const settleData = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times } });
- if (settle.status === auditConst.settle.status.checked) return settleData;
- const result = [];
- const data = await this.ctx.service.auditAss.getData(settle.tid);
- for (const sd of settleData) {
- const index = data.findIndex(x => { return x.user_id === sd.user_id && x.ass_user_id === sd.ass_user_id; });
- if (index >= 0) {
- data.splice(index, 1);
- result.push(sd);
- }
- }
- result.push(...data);
- return result;
- }
- async getUserAssist(settle, user_id) {
- const settleData = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times, user_id } });
- if (settle.status === auditConst.settle.status.checked) return settleData;
- const result = [];
- const data = await this.ctx.service.auditAss.getUserData(settle.tid, user_id);
- for (const sd of settleData) {
- const index = data.findIndex(x => { return x.user_id === sd.user_id && x.ass_user_id === sd.ass_user_id; });
- if (index >= 0) {
- data.splice(index, 1);
- result.push(sd);
- }
- }
- result.push(...data);
- return result;
- }
- async updateData(data) {
- if (!data.user_id || !data.ass_user_id || data.confirm === undefined) throw '缺少参数';
- const settleData = await this.getDataByCondition({ settle_id: this.ctx.settle.id, audit_times: this.ctx.settle.audit_times, user_id: data.user_id, ass_user_id: data.ass_user_id });
- if (data.confirm) {
- if (settleData && settleData.confirm) throw '数据错误';
- const auditAss = await this.ctx.service.auditAss.getDataByCondition({ tid: this.ctx.tender.id, user_id: data.user_id, ass_user_id: data.ass_user_id });
- if (settleData) {
- await this.defaultUpdate({ id: settleData.id, confirm: 1, ass_ledger_id: auditAss.ass_ledger_id });
- settleData.confirm = 1;
- return settleData;
- } else {
- const assInfo = await this.ctx.service.projectAccount.getDataById(data.ass_user_id);
- const insertData = {
- pid: this.ctx.session.sessionProject.id, tid: this.ctx.tender.id,
- settle_id: this.ctx.settle.id, audit_times: this.ctx.settle.audit_times,
- user_id: data.user_id, ass_user_id: data.ass_user_id, ass_ledger_id: auditAss.ass_ledger_id,
- name: assInfo.name, company: assInfo.company, role: assInfo.role,
- confirm: 1,
- };
- const result = await this.db.insert(this.tableName, insertData);
- insertData.id = result.insertId;
- return insertData;
- }
- } else {
- if (!settleData && !settle.confirm) throw '数据错误';
- await this.defaultUpdate({ id: settleData.id, confirm: 0, locked: 0 });
- settleData.confirm = 0;
- settleData.locked = 0;
- return settleData;
- }
- }
- async lockConfirm4CheckNoPre(settle, uid, pre_uid, transaction) {
- if (!transaction) throw '缺少参数';
- const locked = await this.getAllDataByCondition({ where: { settle_id: settle.id, locked: 1, audit_times: settle.curTimes } });
- locked.forEach(x => {
- x.locked_ledger_id = x.ass_ledger_id.split(',');
- });
- // 审批人数据锁定
- const locking = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: uid, confirm: 1 } });
- locking.forEach(x => {
- x.locked_ledger_id = x.ass_ledger_id.split(',');
- });
- if (locking.length > 0) {
- const updateLock = locking.map(x => { return { id: x.id, locked: 1 }; });
- await transaction.updateRows(this.tableName, updateLock);
- }
- const preConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: pre_uid, confirm: 1 } });
- // 前审批人数据,与被锁定数据相关数据确认状态保留
- locked.push(...locking);
- if (preConfirm.length > 0) {
- const delConfirm = [];
- for (const pc of preConfirm) {
- const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: settle.tid, ledger_id: pc.ledger_id });
- const billsOwner = bills ? bills.full_path.split('-') : [];
- const exist = locked.find(x => {
- for (const id of x.locked_ledger_id) {
- if (billsOwner.indexOf(id) >= 0) return true;
- }
- return false;
- });
- if (!exist) delConfirm.push(pc.id);
- }
- if (delConfirm.length > 0) await transaction.delete(this.tableName, { id: delConfirm });
- }
- }
- async lockConfirm4CheckNo(settle, uid, auditors, transaction) {
- if (!transaction) throw '缺少参数';
- // 审批人数据锁定
- const lockConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: uid, confirm: 1 } });
- if (lockConfirm.length === 0) return;
- const insertData = [];
- lockConfirm.forEach(x => {
- delete x.id;
- x.locked = 1;
- x.audit_times = x.audit_times + 1;
- insertData.push(x);
- });
- // 审批人前所有角色,与锁定数据相关确认状态保留
- const keepUid = [ settle.user_id ];
- for (const a of auditors) {
- if (a.aid === uid) break;
- keepUid.push(a.aid);
- }
- const keepConfirm = await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.curTimes, user_id: keepUid, confirm: 1 } });
- for (const kc of keepConfirm) {
- const bills = await this.ctx.service.ledger.getDataByCondition({ tender_id: settle.tid, ledger_id: kc.ledger_id });
- const billsOwner = bills ? bills.full_path.split('-') : [];
- const exist = lockConfirm.find(x => {
- const lid = x.ass_ledger_id.split(',');
- for (const id of lid) {
- if (billsOwner.indexOf(id) >= 0) return true;
- }
- return false;
- });
- if (exist) {
- delete kc.id;
- kc.locked = 0;
- kc.audit_times = kc.audit_times + 1;
- insertData.push(kc);
- }
- }
- if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
- }
- async cancelLock(settle, uid, transaction) {
- await transaction.update(this.tableName, { locked: 0 }, { where: { settle_id: settle.id, audit_times: settle.audit_times, user_id: uid } });
- }
- async getLockedId(settle) {
- return await this.getAllDataByCondition({ where: { settle_id: settle.id, audit_times: settle.audit_times, locked: 1 } });
- }
- async getReportData(settle_id, audit_times) {
- const sql = 'SELECT saa.tid, saa.settle_id, saa.audit_times, saa.user_id, saa.ass_ledger_id, saa.ass_user_id, saa.name, saa.company, saa.role, pa.sign_path, pa.stamp_path' +
- ` FROM ${this.tableName} saa LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON saa.ass_user_id = pa.id` +
- ` WHERE saa.settle_id = ? AND saa.audit_times = ?`;
- const result = await this.db.query(sql, [settle_id, audit_times]);
- result.forEach(x => { x.ass_ledger_id = x.ass_ledger_id ? x.ass_ledger_id.split(',') : []});
- return result;
- }
- }
- return SettleAuditAss;
- };
|