'use strict'; 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); } else { await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_tp }); } // 判断是否已存在计划月,并更新计划月统计数据,再更新总的统计数据 const smList = await this.ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: this.ctx.tender.id } }); if (smList.length > 0) { 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); } await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } } return ScheduleLedger; };