123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- const contractConst = require('../const/contract');
- module.exports = app => {
- class ContractPay extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'contract_pay';
- this.dataId = 'id';
- }
- async getPays(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);
- 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.contractPayAtt.getAtt(l.id);
- }
- }
- 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,
- pay_time: data.pay_time,
- pay_price: data.pay_price,
- debit_price: data.debit_price,
- yf_price: data.yf_price,
- sf_price: data.sf_price,
- pay_type: data.pay_type,
- remark: data.remark,
- create_time: new Date(),
- };
- await transaction.insert(this.tableName, insertData);
- await this.calcContract(transaction, node);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { pays: await this.getPays(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 cpInfo = await this.getDataById(data.id);
- if (!cpInfo) {
- throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
- }
- 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 { pays: await this.getPays(options, cid), node: { update: node } };
- }
- async del(options, cid, cpid) {
- if (!cpid) {
- throw '参数有误';
- }
- const node = await this.ctx.service.contract.getDataById(cid);
- if (!node) {
- throw '合同不存在';
- }
- const cpInfo = await this.getDataById(cpid);
- if (!cpInfo) {
- throw '合同' + contractConst.typeName[cpInfo.contract_type] + '不存在';
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id: cpid });
- // 删除合同附件
- const attList = await this.ctx.service.contractPayAtt.getAllDataByCondition({ where: { cpid } });
- await this.ctx.helper.delFiles(attList);
- await transaction.delete(this.ctx.service.contractPayAtt.tableName, { cpid });
- await this.calcContract(transaction, node);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return { pays: await this.getPays(options, cid), node: { update: node } };
- }
- async calcContract(transaction, node) {
- const paysList = await transaction.query('SELECT * FROM ?? WHERE `cid` = ?', [this.tableName, node.id]);
- let pay_price = 0;
- let debit_price = 0;
- let yf_price = 0;
- let sf_price = 0;
- for (const l of paysList) {
- pay_price = this.ctx.helper.add(pay_price, l.pay_price);
- debit_price = this.ctx.helper.add(debit_price, l.debit_price);
- yf_price = this.ctx.helper.add(yf_price, l.yf_price);
- sf_price = this.ctx.helper.add(sf_price, l.sf_price);
- }
- node.pay_price = pay_price;
- node.debit_price = debit_price;
- node.yf_price = yf_price;
- node.sf_price = sf_price;
- node.exist_pay = paysList.length === 0 ? 0 : 1;
- await transaction.update(this.ctx.service.contract.tableName, node);
- }
- }
- return ContractPay;
- };
|