| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const auditConst = require('../const/audit').contract;
- const auditType = require('../const/audit').auditType;
- const BaseService = require('../base/base_service');
- module.exports = app => {
- class Contract extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- const setting = {
- spid: 'spid',
- mid: 'tid',
- type: 'contract_type',
- kid: 'contract_id',
- pid: 'contract_pid',
- order: 'order',
- level: 'level',
- isLeaf: 'is_leaf',
- fullPath: 'full_path',
- keyPre: 'contract_maxLid:', // 换个名称,防止缓存导致旧数据出问题
- uuid: true,
- };
- super(ctx);
- this.setting = setting;
- this.tableName = 'contract';
- this.dataId = 'id';
- }
- async add(options, node, data) {
- if (!options[this.setting.type] || !node || !data) throw '参数有误';
- const insertId = data.cid || this.uuid.v4();
- const transaction = await this.db.beginTransaction();
- try {
- const add_key = ['c_code', 'name', 'tax', 'price', 'party_a', 'party_a_user', 'party_b', 'party_b_user', 'sign_date', 'address', 'entity', 'bank', 'bank_account', 'type', 'rela_tender', 'remark'];
- const maxId = await this.ctx.service.contractTree._getMaxLid(options);
- const insertData = {
- id: insertId,
- spid: options.spid || null,
- tid: options.tid || null,
- contract_type: options[this.setting.type],
- uid: this.ctx.session.sessionUser.accountId,
- contract_id: maxId + 1,
- contract_pid: !node.c_code ? node.contract_id : node.contract_pid,
- level: !node.c_code ? node.level + 1 : node.level,
- is_leaf: 1,
- // c_code: data.c_code,
- // name: data.name,
- // total_price: data.price ? parseFloat(data.price) : 0,
- // price: data.price ? parseFloat(data.price) : 0,
- // party_a: data.party_a,
- // party_b: data.party_b,
- // sign_date: data.sign_date,
- // remark: data.remark,
- // type: data.type,
- create_time: new Date(),
- need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractSubProjectShenpi : this.ctx.subProject.page_show.openContractTenderShenpi,
- };
- for (const key of add_key) {
- if (key === 'price') {
- insertData[key] = data[key] ? parseFloat(data[key]) : 0;
- insertData.total_price = data[key] ? parseFloat(data[key]) : 0;
- continue;
- } else if (key === 'tax') {
- insertData[key] = data[key] ? parseFloat(data[key]) : null;
- continue;
- } else if (key === 'sign_date') {
- insertData[key] = data[key] ? new Date(data[key]) : null;
- continue;
- }
- insertData[key] = data[key] || '';
- }
- if (data.attribute_json) insertData.attribute_json = data.attribute_json;
- // 删除attachments
- if (!data.attach && data.fid) {
- const fileInfo = await this.ctx.service.contractAtt.getDataById(data.fid);
- if (fileInfo || Object.keys(fileInfo).length) {
- await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + fileInfo.filepath);
- await transaction.delete(this.ctx.service.contractAtt.tableName, { id: data.fid });
- }
- }
- insertData[this.setting.fullPath] = !node.c_code
- ? node[this.setting.fullPath] + '-' + insertData[this.setting.kid]
- : node[this.setting.fullPath].replace('-' + node[this.setting.kid], '-' + insertData[this.setting.kid]);
- const order = !node.c_code ? (!node.is_leaf ? await this.getMaxOrder(options, node.contract_id, transaction) : 1) : node.order + 1;
- insertData[this.setting.order] = order;
- await this.ctx.service.contractTree._updateChildrenOrder(options, insertData[this.setting.pid], insertData[this.setting.order], 1, transaction);
- await transaction.insert(this.tableName, insertData);
- if (!node.c_code && node.is_leaf) {
- await transaction.update(this.ctx.service.contractTree.tableName, { id: node.id, is_leaf: 0 });
- }
- this.ctx.service.contractTree._cacheMaxLid(options, maxId + 1);
- await this.ctx.service.contractSpAudit.makeAudits(transaction, options, insertData, 'contract', this.ctx.session.sessionUser.accountId);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- const createData = await this.ctx.service.contract.getDataById(insertId);
- createData.username = this.ctx.session.sessionUser.name;
- const updateData = await this.ctx.service.contractTree.getNextsData(options, !node.c_code ? node.contract_id : node[this.setting.pid], node[this.setting.order] + 1);
- if (!node.c_code) {
- const parent = await this.ctx.service.contractTree.getDataById(node.id);
- updateData.push(parent);
- }
- return { create: createData, update: updateData };
- }
- /**
- * 提交数据 - 响应计算(增量方式计算)
- * @param {Number} tenderId
- * @param {Object} data
- * @return {Promise<*>}
- */
- async updateCalc(options, data) {
- const helper = this.ctx.helper;
- if (!data) {
- throw '提交数据错误';
- }
- const datas = data instanceof Array ? data : [data];
- const ids = [];
- for (const row of datas) {
- ids.push(row.id);
- }
- const transaction = await this.db.beginTransaction();
- try {
- const updateDatas = [];
- for (const row of datas) {
- const updateNode = await this.getDataById(row.id);
- if (!updateNode) {
- throw '提交数据错误';
- }
- updateDatas.push(row);
- }
- if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { contract: await this.getDataById(ids), files: await this.ctx.service.contractAtt.getAtt(ids[0]) };
- }
- async getMaxOrder(options, pid, transaction) {
- const sql = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
- const params = [this.tableName, pid];
- const result = await transaction.query(sql, params);
- const maxOrder = result[0].max_order || 0;
- const sql1 = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
- const params1 = [this.ctx.service.contractTree.tableName, pid];
- const result1 = await transaction.query(sql1, params1);
- const maxOrder1 = result1[0].max_order || 0;
- return Math.max(maxOrder, maxOrder1) + 1;
- }
- async getListByUsers(options, user) {
- const _ = this._;
- const list = await this.getAllDataByCondition({ where: options });
- const userList = list.length > 0 ? await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: this._.map(list, 'uid') } }) : [];
- const shenpi_status = this.ctx.contract_tender ? this.ctx.subProject.page_show.openContractTenderShenpi : this.ctx.subProject.page_show.openContractSubProjectShenpi;
- for (const l of list) {
- const userInfo = userList.find(item => item.id === l.uid);
- l.username = userInfo ? userInfo.name : '';
- if (shenpi_status) {
- // await this.ctx.service.contractSpAudit.loadUser(l);
- // await this.ctx.service.contractSpAudit.loadAuditViewData(l);
- // await this.ctx.service.contractSpAudit.checkShenpi(l);
- if (l.status !== auditConst.status.checked || !l.final_auditor_str) {
- l.curAuditors2 = await this.ctx.service.contractSpAudit.getAuditorsByStatus(l, 'contract', l.status, l.times);
- if (l.status === auditConst.status.checked && !l.final_auditor_str) {
- const final_auditor_str = l.curAuditors2[0].audit_type === auditType.key.common
- ? l.curAuditors2[0].name + (l.curAuditors2[0].role ? '-' + l.curAuditors2[0].role : '')
- : this.ctx.helper.transFormToChinese(l.curAuditors2[0].audit_order) + '审';
- await this.defaultUpdate({ final_auditor_str, id: l.id });
- l.final_auditor_str = final_auditor_str;
- }
- }
- }
- }
- if (user.is_admin) {
- return list;
- }
- const userPermission = options.tid ? await this.ctx.service.contractAudit.getDataByCondition({ spid: options.spid || null, tid: options.tid || null, uid: user.accountId }) : await this.ctx.service.subProjPermission.getContractPermission(this.ctx.subProject.permission.contract_permission);
- if (!userPermission) return [];
- const cloneOptions = this._.cloneDeep(options);
- cloneOptions.uid = user.accountId;
- const userTreePermission = await this.ctx.service.contractTreeAudit.getAllDataByCondition({ where: cloneOptions });
- if (userTreePermission.length === 0) return list;
- const newList = this._.filter(list, { uid: user.accountId });
- // 审批的列表也要可显示
- const checkList = await this.ctx.service.contractSpAudit.getAuditList(options, user.accountId);
- // 用 checkList里的数组和list的id对比,存在则合并到newList中
- newList.push(...this._.filter(list, function(item) {
- return checkList.some(checkItem => checkItem.id === item.id && item.uid !== user.accountId);
- }));
- const userInfo = await this.ctx.service.projectAccount.getDataById(user.accountId);
- // const unit = userInfo.company ? await this.ctx.service.constructionUnit.getDataByCondition({ pid: userInfo.project_id, name: userInfo.company }) : null;
- const uids = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id'], where: { project_id: userInfo.project_id, company: userInfo.company } });
- for (const ut of userTreePermission) {
- if (userPermission.permission_show_node) {
- const nodes = this._.filter(list, { contract_pid: ut.contract_id });
- newList.push(...this._.filter(nodes, function(item) {
- return item.uid !== user.accountId;
- }));
- } else if (userPermission.permission_show_unit) {
- const nodes = this._.filter(list, function(item) {
- return item.contract_pid === ut.contract_id && _.includes(_.map(uids, 'id'), item.uid);
- });
- newList.push(...this._.filter(nodes, function(item) {
- return item.uid !== user.accountId;
- }));
- // } else {
- // const nodes = this._.filter(list, { contract_pid: ut.contract_id, uid: user.accountId });
- // newList.push(...nodes);
- }
- }
- return newList;
- }
- async savePageShow(spid, page_show) {
- const transaction = await this.db.beginTransaction();
- try {
- await this.ctx.service.subProject.updatePageshow(spid, page_show, transaction);
- // 同步更新所有pay和contract状态
- await transaction.update(this.tableName, { need_shenpi: page_show.openContractSubProjectShenpi }, { where: { spid } });
- await transaction.update(this.ctx.service.contractPay.tableName, { need_shenpi: page_show.openContractPaySubProjectShenpi }, { where: { spid } });
- const tenders = await this.ctx.service.tender.getAllDataByCondition({ columns: ['id'], where: { spid } });
- if (tenders.length > 0) {
- await transaction.update(this.tableName, { need_shenpi: page_show.openContractTenderShenpi }, { where: { tid: this._.map(tenders, 'id') } });
- await transaction.update(this.ctx.service.contractPay.tableName, { need_shenpi: page_show.openContractPayTenderShenpi }, { where: { tid: this._.map(tenders, 'id') } });
- }
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return false;
- }
- // async getCountByUser(stid, type, user) {
- //
- // }
- }
- return Contract;
- };
|