|
@@ -6,6 +6,39 @@ module.exports = app => {
|
|
|
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);
|
|
|
+ await transaction.commit();
|
|
|
+ return true;
|
|
|
+ } catch (err) {
|
|
|
+ await transaction.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return ScheduleLedger;
|
|
|
};
|