| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | '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 });                    }                }                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;                let total_gcl = 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_gcl = this.ctx.helper.add(total_gcl, u.gcl);                    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,                        total_gcl,                    };                    await transaction.insert(this.ctx.service.schedule.tableName, newSchedule);                } else {                    await transaction.update(this.ctx.service.schedule.tableName, { id: scheduleInfo.id, total_gcl, total_tp });                }                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }    }    return ScheduleLedger;};
 |