| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- module.exports = app => {
- class FinancialTransferTender extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'financial_transfer_tender';
- }
- async getList(trid, pageLimit = 0) {
- const sql = 'SELECT ftt.*, t.`name` as name FROM ?? as ftt LEFT JOIN ?? as t ON ftt.`tid` = t.`id` WHERE ftt.`trid` = ?';
- // if (pageLimit) {
- // const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
- // const offset = limit * (this.ctx.page - 1);
- // const limitString = offset >= 0 ? offset + ',' + limit : limit;
- // sql += ' LIMIT ' + limitString;
- // }
- const sqlParams = [this.tableName, this.ctx.service.tender.tableName, trid];
- const list = await this.db.query(sql, sqlParams);
- for (const l of list) {
- l.files = await this.ctx.service.financialTransferTenderAtt.getAtt(l.id);
- }
- return list;
- }
- async addTenders(transferInfo, tenders) {
- const transaction = await this.db.beginTransaction();
- try {
- const insertDatas = [];
- // const needCalc = [];
- const updateDatas = [];
- const ftList = await this.ctx.service.financialTransfer.getAllDataByCondition({ where: { spid: transferInfo.spid } });
- const beforeFt = this._.filter(ftList, function(item) {
- return item.id < transferInfo.id;
- });
- const afterFt = this._.filter(ftList, function(item) {
- return item.id > transferInfo.id;
- });
- for (const t of tenders) {
- const tender = await this.ctx.service.tender.getDataById(t);
- if (!tender) {
- throw '标段不存在';
- }
- const ftt = await this.getDataByCondition({ spid: transferInfo.spid, trid: transferInfo.id, tid: t });
- if (ftt) {
- throw '资金划拨标段已存在';
- }
- // await this.ctx.service.tenderCache.loadTenderCache(tender, this.ctx.session.sessionUser.accountId);
- // if (tender.stage_tp) {
- // tender.end_sf_tp = this.ctx.helper.add(tender.stage_tp.pre_sf_tp, tender.stage_tp.sf_tp);
- // } else if (tender.lastStage) {
- // tender.end_sf_tp = this.ctx.helper.add(tender.lastStage.pre_sf_tp, tender.lastStage.sf_tp);
- // }
- // 找出已添加的划拨标段,得出pre_hb_tp
- let pre_hb_tp = 0;
- if (beforeFt.length > 0) {
- const beforeFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(beforeFt, 'id'), tid: t } });
- if (beforeFttList && beforeFttList.length > 0) {
- pre_hb_tp = this._.sumBy(beforeFttList, 'hb_tp');
- }
- }
- // const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: t.tid, order: t.sorder } });
- const insertData = {
- spid: transferInfo.spid,
- trid: transferInfo.id,
- tid: t,
- uid: this.ctx.session.sessionUser.accountId,
- hb_tp: 0,
- pre_hb_tp,
- };
- if (afterFt.length > 0) {
- const afterFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(afterFt, 'id'), tid: t } });
- if (afterFttList && afterFttList.length > 0) {
- for (const aftt of afterFttList) {
- aftt.pre_hb_tp = this.ctx.helper.add(aftt.pre_hb_tp, insertData.hb_tp);
- updateDatas.push({ id: aftt.id, pre_hb_tp: aftt.pre_hb_tp });
- // needCalc.push(aftt.trid);
- }
- }
- }
- insertDatas.push(insertData);
- }
- await transaction.insert(this.tableName, insertDatas);
- if (updateDatas.length > 0) {
- await transaction.updateRows(this.tableName, updateDatas);
- }
- // 计算划拨金额
- await this.calcHbTp(transferInfo.id, transaction);
- if (afterFt.length > 0) {
- const newFtList = await transaction.select(this.ctx.service.financialTransfer.tableName, { where: { spid: transferInfo.spid } });
- // 更新划拨金额
- const updatePreHbTp = [];
- for (const tr of afterFt) {
- // await this.calcHbTp(tr, transaction);
- const thisFt = this._.find(newFtList, { id: tr.id });
- let pre_hb_tp = 0;
- const beforeFtList = this._.filter(newFtList, function(item) {
- return item.id < tr.id;
- });
- if (beforeFtList && beforeFtList.length > 0) {
- pre_hb_tp = this.ctx.helper.sum(this._.map(beforeFtList, 'total_price'));
- }
- if (pre_hb_tp !== thisFt.pre_hb_tp) {
- updatePreHbTp.push({ id: tr.id, pre_hb_tp });
- }
- }
- if (updatePreHbTp.length > 0) {
- await transaction.updateRows(this.ctx.service.financialTransfer.tableName, updatePreHbTp);
- }
- }
- await transaction.commit();
- return true;
- } catch (e) {
- console.log(e);
- await transaction.rollback();
- return false;
- }
- }
- async delTenders(transferInfo, ftid) {
- if (!ftid) {
- throw '参数有误';
- }
- const node = await this.getDataById(ftid);
- if (!node) {
- throw '资金划拨标段不存在';
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id: node.id });
- // 删除合同附件
- const attList = await this.ctx.service.financialTransferTenderAtt.getAllDataByCondition({ where: { ftid } });
- await this.ctx.helper.delFiles(attList);
- await this.calcHbTp(node.trid, transaction);
- await this.calcPreHbTp(transaction, transferInfo, node);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async updateHbTp(transferInfo, ftid, hb_tp) {
- if (!ftid) {
- throw '参数有误';
- }
- const node = await this.getDataById(ftid);
- if (!node) {
- throw '资金划拨标段不存在';
- }
- if (node.is_lock) {
- throw '资金划拨标段已锁定,无法修改';
- }
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, { id: node.id, hb_tp });
- await this.calcHbTp(node.trid, transaction);
- await this.calcPreHbTp(transaction, transferInfo, node, hb_tp);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async updateTender(transferInfo, postData) {
- const keys = ['source'];
- if (!this._.includes(keys, postData.key)) {
- throw '参数有误';
- }
- const node = await this.getDataById(postData.node);
- if (!node) {
- throw '资金划拨标段不存在';
- }
- if (node.is_lock) {
- throw '资金划拨标段已锁定,无法修改';
- }
- const transaction = await this.db.beginTransaction();
- try {
- const updateData = {
- id: node.id,
- };
- updateData[postData.key] = postData.value;
- await transaction.update(this.tableName, updateData);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- return true;
- }
- async calcPreHbTp(transaction, transferInfo, ftt, newHbTp = 0) {
- const ftList = await transaction.select(this.ctx.service.financialTransfer.tableName, { where: { spid: transferInfo.spid } });
- const afterFt = this._.filter(ftList, function(item) {
- return item.id > transferInfo.id;
- });
- if (afterFt.length === 0) return;
- const updateDatas = [];
- const afterFttList = await this.getAllDataByCondition({ where: { spid: transferInfo.spid, trid: this._.map(afterFt, 'id'), tid: ftt.tid } });
- if (afterFttList && afterFttList.length > 0) {
- for (const aftt of afterFttList) {
- aftt.pre_hb_tp = this.ctx.helper.add(this.ctx.helper.sub(aftt.pre_hb_tp, ftt.hb_tp), newHbTp);
- updateDatas.push({ id: aftt.id, pre_hb_tp: aftt.pre_hb_tp });
- }
- }
- if (updateDatas.length > 0) {
- await transaction.updateRows(this.tableName, updateDatas);
- }
- if (afterFt.length > 0) {
- // 更新划拨金额
- const updatePreHbTp = [];
- for (const tr of afterFt) {
- // await this.calcHbTp(tr, transaction);
- const thisFt = this._.find(ftList, { id: tr.id });
- let pre_hb_tp = 0;
- const beforeFtList = this._.filter(ftList, function(item) {
- return item.id < tr.id;
- });
- if (beforeFtList && beforeFtList.length > 0) {
- pre_hb_tp = this.ctx.helper.sum(this._.map(beforeFtList, 'total_price'));
- }
- if (pre_hb_tp !== thisFt.pre_hb_tp) {
- updatePreHbTp.push({ id: tr.id, pre_hb_tp });
- }
- }
- if (updatePreHbTp.length > 0) {
- await transaction.updateRows(this.ctx.service.financialTransfer.tableName, updatePreHbTp);
- }
- }
- }
- async calcHbTp(trid, transaction = null) {
- const sql = 'SELECT SUM(hb_tp) as hb_tp FROM ?? WHERE `trid` = ?';
- const sqlParams = [this.tableName, trid];
- const result = transaction ? await transaction.queryOne(sql, sqlParams) : await this.db.queryOne(sql, sqlParams);
- transaction ? await transaction.update(this.ctx.service.financialTransfer.tableName, { id: trid, total_price: result.hb_tp }) : await this.db.update(this.ctx.service.financialTransfer.tableName, { id: trid, total_price: result.hb_tp });
- }
- }
- return FinancialTransferTender;
- };
|