123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- 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 = this.uuid.v4();
- const transaction = await this.db.beginTransaction();
- try {
- 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.code,
- name: data.name,
- total_price: data.total_price ? parseFloat(data.total_price) : 0,
- party_b: data.party_b,
- remark: data.remark,
- create_time: new Date(),
- };
- 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 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 { update: await this.getDataById(ids) };
- }
- 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 });
- for (const l of list) {
- l.username = (await this.ctx.service.projectAccount.getAccountInfoById(l.uid)).name;
- }
- 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 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 getCountByUser(stid, type, user) {
- //
- // }
- }
- return Contract;
- };
|