'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) { const sql = 'SELECT ftt.*, t.`name` as name FROM ?? as ftt LEFT JOIN ?? as t ON ftt.`tid` = t.`id` WHERE ftt.`trid` = ?'; 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 = []; for (const t of tenders) { const tender = await this.ctx.service.tender.getDataById(t.tid); 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.tid, sorder: t.sorder.join(','), uid: this.ctx.session.sessionUser.accountId, total_price: tender.total_price, contract_tp: this._.sumBy(stages, 'contract_tp'), qc_tp: this._.sumBy(stages, 'qc_tp'), pc_tp: this._.sumBy(stages, 'pc_tp'), yf_tp: this._.sumBy(stages, 'yf_tp'), sf_tp: this._.sumBy(stages, 'sf_tp'), hb_tp: this._.sumBy(stages, 'sf_tp'), }; insertDatas.push(insertData); } await transaction.insert(this.tableName, insertDatas); await this.calcHbTp(transferInfo.id, transaction); await transaction.commit(); return true; } catch (e) { console.log(e); await transaction.rollback(); return false; } } async delTenders(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 transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } async updateHbTp(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 transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } return true; } 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; };