123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- module.exports = app => {
- class FinancialPayContract extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'financial_pay_contract';
- this.dataId = 'id';
- }
- async getContractList(fpid, needCheck = false) {
- const list = await this.getAllDataByCondition({ where: { fpid }, orders: [['id', 'asc']] });
- const updateData = [];
- const needCheckKeys = ['c_code', 'name', 'total_price', 'entity', 'bank', 'bank_account'];
- for (const l of list) {
- const allList = l.cid ? await this.getAllDataByCondition({ where: { spid: l.spid, cid: l.cid } }) : [];
- l.accumulate_pay_price = this._.sumBy(allList, 'pay_price');
- l.accumulate_settle_price = this._.sumBy(allList, 'settle_price');
- l.files = await this.ctx.service.financialPayAtt.getAtt(l.fpid, l.id);
- if (l.cid && needCheck) {
- const contractInfo = await this.ctx.service.contract.getDataById(l.cid);
- if (contractInfo) {
- const condition = {};
- for (const key of needCheckKeys) {
- if (l[key] !== contractInfo[key]) {
- condition[key] = contractInfo[key];
- l[key] = contractInfo[key];
- }
- }
- if (Object.keys(condition).length > 0) {
- updateData.push({ id: l.id, ...condition });
- }
- }
- }
- }
- if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
- return list;
- }
- async addWhiteContract(fp) {
- const insertData = {
- spid: fp.spid,
- tid: fp.tid,
- fpid: fp.id,
- };
- const result = await this.db.insert(this.tableName, insertData);
- const result2 = await this.getDataById(result.insertId);
- result2.files = [];
- return result2;
- }
- async addContracts(fp, cids) {
- const transaction = await this.db.beginTransaction();
- try {
- const list = await this.getAllDataByCondition({ where: { fpid: fp.id, cid: cids } });
- if (list.length > 0) {
- throw '合同已存在, 无法重复添加';
- }
- const contracts = await this.ctx.service.contract.getAllDataByCondition({ where: { id: cids } });
- const insertDatas = [];
- for (const c of contracts) {
- insertDatas.push({
- spid: fp.spid,
- tid: fp.tid,
- fpid: fp.id,
- cid: c.id,
- c_code: c.c_code,
- name: c.name,
- total_price: c.total_price,
- entity: c.entity,
- bank: c.bank,
- bank_account: c.bank_account,
- });
- }
- if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
- // 重新算资金支付总额
- // const tp = await this.calcCamountSum(fpid, transaction);
- await transaction.commit();
- return { contractList: await this.getContractList(fp.id) };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async delContract(fpid, ids) {
- const transaction = await this.db.beginTransaction();
- try {
- // 还要删除附件
- const attList = await this.ctx.service.financialPayAtt.getAllDataByCondition({ where: { fpid, fpcid: ids } });
- await this.ctx.helper.delFiles(attList);
- await transaction.delete(this.ctx.service.financialPayAtt.tableName, { fpcid: ids });
- // 判断是否可删
- await transaction.delete(this.tableName, { id: ids });
- // 重新算资金支付总额
- const tp = await this.calcCamountSum(fpid, transaction);
- await transaction.commit();
- return { tp, contractList: await this.getContractList(fpid) };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateContract(fpid, data) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, data);
- const tp = await this.calcCamountSum(fpid, transaction);
- await transaction.commit();
- return { tp };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- /**
- * 修改变更清单 复制粘贴
- * @param {Object} datas 修改内容
- * @return {void}
- */
- async updateContracts(fpid, datas) {
- const transaction = await this.db.beginTransaction();
- try {
- const updateArray = [];
- for (const d of datas) {
- if (d.id) {
- updateArray.push(d);
- }
- }
- if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
- const tp = await this.calcCamountSum(fpid, transaction);
- await transaction.commit();
- return { tp, contractList: await this.getContractList(fpid) };
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async calcCamountSum(fpid, transaction) {
- // 防止小数位不精确,采用取值计算
- const sql = 'SELECT `small_expenses`, `pay_price` FROM ?? WHERE fpid = ?';
- const sqlParam = [this.tableName, fpid];
- const list = await transaction.query(sql, sqlParam);
- let total_price = 0;
- let small_expenses_tp = 0;
- // const tp_decimal = this.ctx.change.decimal.tp;
- for (const l of list) {
- total_price = this.ctx.helper.accAdd(total_price, l.pay_price || 0);
- small_expenses_tp = this.ctx.helper.accAdd(small_expenses_tp, l.small_expenses ? l.pay_price || 0 : 0);
- }
- const updateData = {
- id: fpid,
- total_price,
- small_expenses_tp,
- };
- await transaction.update(this.ctx.service.financialPay.tableName, updateData);
- return total_price;
- }
- async getEntities(fpid) {
- const entities = [];
- const contracts = await this.getAllDataByCondition({ columns: ['entity'], where: { fpid } });
- for (const c of contracts) {
- if (c.entity && !this._.includes(entities, c.entity)) {
- entities.push(c.entity);
- }
- }
- return entities.join(',');
- }
- }
- return FinancialPayContract;
- };
|