'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) { 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); // 判断是否已创建了形象进度表 const scheduleInfo = await this.ctx.service.schedule.getDataByCondition({ tid: this.ctx.tender.id }); if (!scheduleInfo) { const newSchedule = { tid: this.ctx.tender.id, }; await transaction.insert(this.ctx.service.schedule.tableName, newSchedule); } await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } } return ScheduleLedger; };