'use strict'; /** * * * @author Mai * @date * @version */ module.exports = app => { class SettleSelect extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'settle_select'; } async updateData(data) { const result = {}; const conn = await this.db.beginTransaction(); try { if (data.del && data.del.length > 0) { const lid = [], pid = []; for (const d of data.del) { if (d.lid) lid.push(d.lid); if (d.pid) pid.push(d.pid); } const delLibData = lid.length > 0 ? await this.getAllDataByCondition({ where: { settle_id: this.ctx.settle.id, lid } }) : []; const delPidData = pid.length > 0 ? await this.getAllDataByCondition({ where: { settle_id: this.ctx.settle.id, pid } }) : []; if (delLibData.length + delPidData.length !== data.del.length) throw '提交数据错误'; const deleteData = [...delLibData, ...delPidData]; await conn.delete(this.tableName, { id: deleteData.map(x => { return x.id }) }); result.del = deleteData; } if (data.add && data.add.length > 0) { const addData = []; for (const d of data.add) { addData.push({ tid: this.ctx.settle.tid, settle_id: this.ctx.settle.id, settle_order: this.ctx.settle.settle_order, lid: d.lid || '', pid: d.pid || '', user_id: this.ctx.session.sessionUser.accountId, }); } await conn.insert(this.tableName, addData); result.add = addData; } await conn.commit(); } catch (err) { await conn.rollback(); throw err; } return result; } } return SettleSelect; };