| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- const contractConst = require('../const/contract');
- const auditConst = require('../const/audit').contract;
- const auditType = require('../const/audit').auditType;
- module.exports = app => {
- class ContractSupplement extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'contract_supplement';
- this.dataId = 'id';
- }
- async getSupplements(options, cid) {
- const sql = 'SELECT * FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND `cid` = ? ORDER BY `create_time` DESC';
- const sqlParams = [this.tableName, cid];
- const list = await this.db.query(sql, sqlParams);
- const shenpi_status = this.ctx.contract_tender ? this.ctx.subProject.page_show.openContractTenderShenpi : this.ctx.subProject.page_show.openContractSubProjectShenpi;
- if (list.length > 0) {
- const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: list.map(item => item.uid) } });
- for (const l of list) {
- const userInfo = userList.find(item => item.id === l.uid);
- l.username = userInfo ? userInfo.name : '';
- l.files = await this.ctx.service.contractSupplementAtt.getAtt(l.id);
- 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.cid, l.id, 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;
- }
- }
- }
- }
- }
- return list;
- }
- async add(options, cid, data) {
- const node = await this.ctx.service.contract.getDataById(cid);
- if (!node) {
- throw '合同不存在';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const insertData = {
- spid: options.spid || null,
- tid: options.tid || null,
- contract_type: options.contract_type,
- cid,
- uid: this.ctx.session.sessionUser.accountId,
- code: data.code,
- name: data.name,
- price: data.price || 0,
- party_a: data.party_a || 0,
- party_b: data.party_b || 0,
- sign_time: data.sign_time || null,
- address: data.address,
- content: data.content,
- create_time: new Date(),
- need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractSubProjectShenpi : this.ctx.subProject.page_show.openContractTenderShenpi,
- };
- const result = await transaction.insert(this.tableName, insertData);
- await this.calcContract(transaction, node);
- await this.ctx.service.contractSpAudit.makeAudits(transaction, options, cid, result.insertId, this.ctx.session.sessionUser.accountId);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { supplements: await this.getSupplements(options, cid), node: { update: node } };
- }
- async save(options, cid, data) {
- if (!data.id) {
- throw '参数有误';
- }
- const node = await this.ctx.service.contract.getDataById(cid);
- if (!node) {
- throw '合同不存在';
- }
- const csInfo = await this.getDataById(data.id);
- if (!csInfo) {
- throw '补充合同不存在';
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, data);
- await this.calcContract(transaction, node);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { supplements: await this.getSupplements(options, cid), node: { update: node } };
- }
- async del(options, cid, csid) {
- if (!csid) {
- throw '参数有误';
- }
- const node = await this.ctx.service.contract.getDataById(cid);
- if (!node) {
- throw '合同不存在';
- }
- const csInfo = await this.getDataById(csid);
- if (!csInfo) {
- throw '补充合同不存在';
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id: csid });
- // 删除合同附件
- const attList = await this.ctx.service.contractSupplementAtt.getAllDataByCondition({ where: { csid } });
- await this.ctx.helper.delFiles(attList);
- await transaction.delete(this.ctx.service.contractSupplementAtt.tableName, { csid });
- await transaction.delete(this.ctx.service.contractSpAudit.tableName, { csid });
- await this.calcContract(transaction, node);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { supplements: await this.getSupplements(options, cid), node: { update: node } };
- }
- async calcContract(transaction, node) {
- const supplementsList = await transaction.query('SELECT `price` FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
- let supplement_price = 0;
- for (const l of supplementsList) {
- supplement_price = this.ctx.helper.add(supplement_price, l.price);
- }
- node.supplement_price = supplement_price;
- node.exist_supplement = supplementsList.length === 0 ? 0 : 1;
- await transaction.update(this.ctx.service.contract.tableName, node);
- }
- }
- return ContractSupplement;
- };
|