123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 'use strict';
- const scheduleConst = require('../const/schedule');
- module.exports = app => {
- class ScheduleLedger extends app.BaseService {
- constructor(ctx) {
- super(ctx);
- this.tableName = 'schedule_ledger';
- }
- async saveLedger(datas) {
- const transaction = await this.db.beginTransaction();
- try {
- const oldDatas = await this.getAllDataByCondition({
- where: { tid: this.ctx.tender.id },
- });
- const oldLids = this._.map(oldDatas, 'ledger_id');
- const insertDatas = [];
- for (const l of datas.select_ledger) {
- if (oldLids.indexOf(l) === -1) {
- const data = {
- tid: this.ctx.tender.id,
- ledger_id: l,
- };
- insertDatas.push(data);
- } else {
- this._.pull(oldLids, l);
- }
- }
- if (oldLids.length > 0) {
- for (const ol of oldLids) {
- await transaction.delete(this.tableName, { tid: this.ctx.tender.id, ledger_id: ol });
- // 更新已选标段值
- await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: this.ctx.tender.id, lid: ol });
- }
- }
- if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
- // 更新所有为null
- await transaction.update(this.tableName, { gcl: null, tp: null }, { where: { tid: this.ctx.tender.id } });
- const updateOptions = [];
- let total_tp = 0;
- // 更新最底层和总设计值并更新最底层已填的计划与实际(金额、工程量)
- for (const u of datas.under_ledger) {
- updateOptions.push({
- row: {
- gcl: u.gcl,
- tp: u.tp,
- },
- where: {
- ledger_id: u.ledger_id,
- tid: this.ctx.tender.id,
- },
- });
- total_tp = this.ctx.helper.add(total_tp, u.tp);
- }
- if (updateOptions.length > 0) await transaction.updateRows(this.tableName, updateOptions);
- // 判断是否已创建了投资进度表
- const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id });
- if (!scheduleInfo) {
- const newSchedule = {
- tid: this.ctx.tender.id,
- total_tp,
- };
- await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);
- const ledgerData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
- const insertDatas = [];
- for (const le of ledgerData) {
- insertDatas.push({ tid: this.ctx.tender.id, ledger_id: le.ledger_id });
- }
- await transaction.insert(this.ctx.service.scheduleLedgerHistory.tableName, insertDatas);
- } else {
- await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_tp, revising: 0 });
- }
- // 判断是否已存在计划月,并更新计划月统计数据,再更新总的统计数据
- const smList = await this.ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
- if (scheduleInfo && smList.length > 0) {
- // 删除所有父节点已填过的值
- for (const p of datas.parent_ledger) {
- await transaction.delete(this.ctx.service.scheduleLedgerMonth.tableName, { tid: this.ctx.tender.id, lid: p });
- }
- if (datas.type === 'xz') {
- const slmUpdateOption = [];
- const mode = scheduleInfo.mode;
- for (const u of datas.under_ledger) {
- const dgn_price = this.ctx.helper.round(this.ctx.helper.div(u.tp, u.gcl), 2);
- if (dgn_price && dgn_price !== 0) {
- const sql = 'SELECT * FROM ?? WHERE tid = ? and lid = ?';
- const sqlParam = [this.ctx.service.scheduleLedgerMonth.tableName, this.ctx.tender.id, u.ledger_id];
- const lidList = await transaction.query(sql, sqlParam);
- if (lidList.length > 0) {
- for (const l of lidList) {
- if (mode === scheduleConst.plan_mode.tp) {
- const new_plan_gcl = l.plan_tp ? this.ctx.helper.round(this.ctx.helper.div(l.plan_tp, dgn_price), 3) : null;
- const new_sj_gcl = l.sj_tp ? this.ctx.helper.round(this.ctx.helper.div(l.sj_tp, dgn_price), 3) : null;
- slmUpdateOption.push({ id: l.id, plan_gcl: new_plan_gcl, sj_gcl: new_sj_gcl });
- } else if (mode === scheduleConst.plan_mode.gcl) {
- const new_plan_tp = l.plan_gcl ? this.ctx.helper.round(this.ctx.helper.mul(l.plan_gcl, dgn_price), 0) : null;
- const new_sj_tp = l.sj_gcl ? this.ctx.helper.round(this.ctx.helper.mul(l.sj_gcl, dgn_price), 0) : null;
- slmUpdateOption.push({ id: l.id, plan_tp: new_plan_tp, sj_tp: new_sj_tp });
- }
- }
- }
- }
- }
- if (slmUpdateOption.length > 0) await transaction.updateRows(this.ctx.service.scheduleLedgerMonth.tableName, slmUpdateOption);
- // scheduleLedgerHistory更新到最新版台账
- const ledgerData = await this.ctx.service.ledger.getData(this.ctx.tender.id);
- const insertDatas = [];
- for (const le of ledgerData) {
- insertDatas.push({ tid: this.ctx.tender.id, ledger_id: le.ledger_id });
- }
- await transaction.delete(this.ctx.service.scheduleLedgerHistory.tableName, { tid: this.ctx.tender.id });
- await transaction.insert(this.ctx.service.scheduleLedgerHistory.tableName, insertDatas);
- }
- for (const sm of smList) {
- await this.ctx.service.scheduleLedgerMonth.calcMonthPlan(transaction, this.ctx.tender.id, sm.yearmonth);
- await this.ctx.service.scheduleLedgerMonth.calcMonthSj(transaction, this.ctx.tender.id, sm.yearmonth);
- }
- await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
- await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
- }
- // 判断是否已存在计量进度,并更新计量统计数据,再更新总的统计数据
- if (datas.stageTpList) {
- await transaction.updateRows(this.ctx.service.scheduleStage.tableName, datas.stageTpList);
- // await this.ctx.service.scheduleStage.calcStageSjTp(transaction, this.ctx.tender.id);
- }
- await this.ctx.service.scheduleStage.updateStageSjTp(transaction, this.ctx.tender.id, datas.stageSjTp);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return ScheduleLedger;
- };
|