123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- const shenpiConst = require('../const/shenpi');
- const auditType = require('../const/audit').auditType;
- module.exports = app => {
- class FinancialPayTenderAudit extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'financial_pay_tender_audit';
- this.dataId = 'id';
- }
- async getList(spid, tid, accountList = null) {
- if (accountList === null) {
- accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'account', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
- });
- }
- const list = await this.db.select(this.tableName, { where: { spid, tid }, orders: [['id', 'asc']] });
- const removeList = [];
- for (const l of list) {
- const accountInfo = this._.find(accountList, { id: l.uid });
- if (!accountInfo) {
- removeList.push(l.id);
- continue;
- }
- l.name = accountInfo.name;
- l.company = accountInfo.company;
- }
- if (removeList.length > 0) {
- for (const r of removeList) {
- list.splice(this._.findIndex(list, { id: r }), 1);
- }
- }
- return list;
- }
- async saveAudits(spid, tid, accountList) {
- const transaction = await this.db.beginTransaction();
- try {
- // 判断是否已存在该用户,存在则不插入
- const pauditList = await this.getAllDataByCondition({ where: { spid, tid } });
- const financialAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid } });
- const pushData = [];
- const pushSubProjPermissionData = [];
- const updateSubProjPermissionData = [];
- for (const a of this._.uniqBy(accountList, 'id')) {
- if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
- const data = {
- spid,
- tid,
- uid: a.id,
- create_time: new Date(),
- };
- pushData.push(data);
- }
- const subProjPermission = this._.find(financialAudits, { uid: a.id });
- if (!subProjPermission) {
- pushSubProjPermissionData.push({
- id: this.uuid.v4(),
- spid,
- pid: this.ctx.session.sessionProject.id,
- uid: a.id,
- fund_trans_permission: 1,
- fund_pay_permission: 1,
- });
- } else {
- subProjPermission.fund_trans_permission = subProjPermission.fund_trans_permission ? this._.map(subProjPermission.fund_trans_permission.split(','), this._.toInteger) : [];
- subProjPermission.fund_pay_permission = subProjPermission.fund_pay_permission ? this._.map(subProjPermission.fund_pay_permission.split(','), this._.toInteger) : [];
- const financialPermission = await this.ctx.service.subProjPermission.getFinancailPermission(subProjPermission.fund_trans_permission, subProjPermission.fund_pay_permission);
- if (!financialPermission.pay_show) {
- const upPermission = {
- id: subProjPermission.id,
- fund_pay_permission: subProjPermission.fund_pay_permission.push(1).join(','),
- };
- if (!financialPermission.transfer_show) {
- upPermission.fund_tran_permission = subProjPermission.fund_trans_permission.push(1).join(',');
- }
- updateSubProjPermissionData.push(upPermission);
- }
- }
- }
- if (pushData.length > 0) {
- await transaction.insert(this.tableName, pushData);
- }
- if (pushSubProjPermissionData.length > 0) {
- await transaction.insert(this.ctx.service.subProjPermission.tableName, pushSubProjPermissionData);
- }
- if (updateSubProjPermissionData.length > 0) {
- await transaction.updateRows(this.ctx.service.subProjPermission.tableName, updateSubProjPermissionData);
- }
- transaction.commit();
- } catch (error) {
- console.log(error);
- transaction.rollback();
- }
- return true;
- }
- async delAudits(spid, tid, datas) {
- // return await this.db.delete(this.tableName, { id });
- // 需要同时移除审批流程里的人
- const reponseData = {};
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id: this._.map(datas, 'id') });
- const shenpiAudits = await this.ctx.service.shenpiAudit.getAuditList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
- const uids = this._.map(datas, 'uid');
- const removeData = [];
- for (const sp of shenpiAudits) {
- if (this._.includes(uids, sp.audit_id)) {
- removeData.push(sp.id);
- }
- }
- if (removeData.length > 0) {
- await transaction.delete(this.ctx.service.shenpiAudit.tableName, { id: removeData });
- // 重新分配order值
- const newSpAudits = await transaction.select(this.ctx.service.shenpiAudit.tableName, { where: { tid, sp_type: shenpiConst.sp_other_type.financial, sp_status: shenpiConst.sp_status.gdspl }, orders: [['audit_order', 'asc'], ['id', 'asc']] });
- // groupby audit_order
- const newSpAuditsGroup = this._.groupBy(newSpAudits, 'audit_order');
- const updateData = [];
- let order = 1;
- for (const a in newSpAuditsGroup) {
- const sameOrderAudits = newSpAuditsGroup[a];
- for (const sa of sameOrderAudits) {
- if (sa.audit_order !== order) {
- updateData.push({
- id: sa.id,
- audit_order: order,
- });
- }
- }
- order++;
- }
- if (updateData.length > 0) {
- await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData);
- }
- }
- await transaction.commit();
- reponseData.permissionList = await this.getList(spid, tid);
- reponseData.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(tid, shenpiConst.sp_other_type.financial, shenpiConst.sp_status.gdspl);
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- }
- return reponseData;
- }
- async updatePermission(updateData) {
- if (!updateData.id) {
- return false;
- }
- return await this.db.update(this.tableName, updateData);
- }
- async addShenpiAudit(spid, data, tid) {
- const reponseData = {};
- const info = await this.ctx.service.shenpiAudit.addAudit(data, tid);
- reponseData.shenpi = info;
- if (info) {
- const transaction = await this.db.beginTransaction();
- try {
- // 判断是否存在并加入到成员表中
- const fptAudit = await this.getDataByCondition({ spid, tid, uid: data.audit_id });
- if (!fptAudit) {
- await transaction.insert(this.tableName, { spid, tid, uid: data.audit_id, create_time: new Date() });
- }
- // 判断是否存在并加入到成员表中
- const fAudit = await this.ctx.service.subProjPermission.getDataByCondition({ spid, uid: data.audit_id });
- if (!fAudit) {
- await transaction.insert(this.ctx.service.subProjPermission.tableName, {
- id: this.uuid.v4(),
- spid,
- pid: this.ctx.session.sessionProject.id,
- uid: data.audit_id,
- fund_trans_permission: 1,
- fund_pay_permission: 1,
- });
- } else {
- fAudit.fund_trans_permission = fAudit.fund_trans_permission ? this._.map(fAudit.fund_trans_permission.split(','), this._.toInteger) : [];
- fAudit.fund_pay_permission = fAudit.fund_pay_permission ? this._.map(fAudit.fund_pay_permission.split(','), this._.toInteger) : [];
- const financialPermission = await this.ctx.service.subProjPermission.getFinancailPermission(fAudit.fund_trans_permission, fAudit.fund_pay_permission);
- if (!financialPermission.pay_show) {
- fAudit.fund_pay_permission.push(1);
- const upPermission = {
- id: fAudit.id,
- fund_pay_permission: fAudit.fund_pay_permission.join(','),
- };
- if (!financialPermission.transfer_show) {
- fAudit.fund_trans_permission.push(1);
- upPermission.fund_tran_permission = fAudit.fund_trans_permission.join(',');
- }
- await transaction.update(this.ctx.service.subProjPermission.tableName, upPermission);
- }
- }
- await transaction.commit();
- reponseData.permissionList = await this.getList(spid, tid);
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- }
- }
- return reponseData;
- }
- async copyShenpi2otherTender(spid, data, tid) {
- const reponseData = {};
- const info = await this.ctx.service.shenpiAudit.copyAudit2otherTender(data, tid);
- if (info) {
- const transaction = await this.db.beginTransaction();
- try {
- // const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
- const uidList = this._.map(data.auditList, 'audit_id');
- const tidList = data.tidList.split(',');
- if (tidList.length === 0) {
- throw '没有选择要复制的标段';
- }
- if (uidList.length > 0) {
- const pushData = [];
- const times = new Date();
- for (const t of tidList) {
- const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
- const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
- for (const d of diffList) {
- pushData.push({
- spid, tid: t, uid: d,
- create_time: times,
- });
- }
- }
- if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
- }
- await transaction.commit();
- reponseData.otherPermissionList = await this.getList(spid, tidList);
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- }
- }
- return reponseData;
- }
- async copyAudit2otherTender(spid, data, tid) {
- const reponseData = {};
- const transaction = await this.db.beginTransaction();
- try {
- const permissionList = await this.getAllDataByCondition({ where: { spid, tid } });
- const uidList = this._.map(permissionList, 'uid');
- const tidList = data.tidList.split(',');
- if (tidList.length === 0) {
- throw '没有选择要复制的标段';
- }
- if (uidList.length > 0) {
- const pushData = [];
- const updateData = [];
- const times = new Date();
- for (const t of tidList) {
- const tenderAudits = await this.getAllDataByCondition({ where: { spid, tid: t } });
- const diffList = this._.difference(uidList, this._.map(tenderAudits, 'uid'));
- for (const d of diffList) {
- const p = permissionList.find(item => item.uid === d);
- pushData.push({ spid, tid: t, uid: p.uid, is_report: p.is_report, create_time: times });
- }
- const sameList = this._.intersection(this._.map(tenderAudits, 'uid'), uidList);
- for (const s of sameList) {
- const p = permissionList.find(item => item.uid === s);
- const t = tenderAudits.find(item => item.uid === s);
- if (p.is_report !== t.is_report) {
- updateData.push({ id: t.id, is_report: p.is_report });
- }
- }
- }
- if (pushData.length > 0) await transaction.insert(this.tableName, pushData);
- if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
- }
- await transaction.commit();
- reponseData.otherPermissionList = await this.getList(spid, tidList);
- } catch (error) {
- console.log(error);
- await transaction.rollback();
- }
- return reponseData;
- }
- }
- return FinancialPayTenderAudit;
- };
|