'use strict'; /** * * * @author Mai * @date * @version */ module.exports = app => { class RevisePrice extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'revise_price'; } async _addDatas(data) { const datas = data instanceof Array ? data : [data]; const insertData = []; const count = await this.db.count(this.tableName, { rid: this.ctx.revise.id }); for (const [i, d] of datas.entries()) { if (!d.b_code) throw '新增单价调整,提交的数据错误'; const nd = { pid: this.ctx.session.sessionProject.id, tid: this.ctx.tender.id, rid: this.ctx.revise.id, order: count + i + 1, b_code: d.b_code, name: d.name, unit: d.unit, org_price: d.unit_price, new_price: d.unit_price, valid: 1, memo: d.memo || '', }; insertData.push(nd); } const result = await this.db.insert(this.tableName, insertData); for (const [i, d] of insertData.entries()) { d.id = result.insertId + i; } return insertData; } async _delDatas(data) { const datas = data instanceof Array ? data : [data]; await this.db.delete(this.tableName, { id: datas }); return datas; } async _updateDatas(data) { const datas = data instanceof Array ? data : [data]; const uDatas = []; for (const d of datas) { const nd = { id: d.id }; if (d.order !== undefined) nd.order = d.order; if (d.new_price !== undefined) { nd.new_price = this.ctx.helper.round(d.new_price, this.ctx.tender.info.decimal.up); nd.valid = !this.ctx.helper.checkZero(this.ctx.helper.sub(nd.new_price, d.org_price)); } if (d.memo !== undefined) nd.memo = d.memo; if (d.rela_lid !== undefined) nd.rela_lid = d.rela_lid; if (d.rela_cid !== undefined) nd.rela_cid = d.rela_cid; uDatas.push(nd); } if (uDatas.length > 0) { await this.db.updateRows(this.tableName, uDatas); return uDatas; } else { return []; } } async updateDatas(data) { const result = { add: [], del: [], update: [] }; try { if (data.add) { result.add = await this._addDatas(data.add); } if (data.update) { result.update = await this._updateDatas(data.update); } if (data.del) { result.del = await this._delDatas(data.del); } return result; } catch (err) { throw err; } } async doPriceUsed(stage, transaction) { const sql = `Update ${this.tableName} Set use_stage = ${stage.id}, use_stage_order = ${stage.order}` + ' Where tid = ? and use_stage = 0'; await transaction.query(sql, [stage.tid]); } async cancelPriceUsed(stage, transaction) { const sql = `Update ${this.tableName} Set use_stage = 0, use_stage_order = 0` + ' Where tid = ? and use_stage = ?'; await transaction.query(sql, [stage.tid, stage.id]); } } return RevisePrice; };