'use strict'; /** * 部位明细 * * @author Mai * @date * @version */ module.exports = app => { class Pos extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'pos'; } async getPosData(condition) { return await this.db.select(this.tableName, { where: condition, columns: ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code'], }); } async savePosData(data, tid) { const transaction = await this.db.beginTransaction(); try { const result = { ledger: {}, pos: null }; if (data.updateType === 'add') { const tender = await this.ctx.service.tender.getTender(tid); data.updateData.tid = tid; // todo 新增期 data.updateData.add_stage = 0; data.updateData.add_times = 0; data.updateData.add_user = this.ctx.session.sessionUser.accountId; const addRst = await transaction.insert(this.tableName, data.updateData); data.updateData.id = addRst.insertId; } else if (data.updateType === 'update') { const datas = data.updateData instanceof Array ? data.updateData : [data.updateData]; result.ledger.update = []; const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')}); console.log(datas); for (const d of datas) { const op = this._.find(orgPos, function (p) { return p.id = d.id; }); await transaction.update(this.tableName, d, {tid: tid, id: d.id}); if (d.quantity && op && (result.ledger.update.indexOf(op.lid) === -1)) { result.ledger.update.push(op.lid); } } console.log(result.ledger.update); for (const lid of result.ledger.update) { await this.ctx.service.ledger.calc(tid, lid, transaction); } } else if (data.updateType === 'delete') { if (!data.updateData || data.updateData.length === 0) { throw '提交数据错误'; } const pos = await this.getPosData({tid: tid, id: data.updateData}); const ledgerIds = this._.map(pos, 'lid'); await transaction.delete(this.tableName, {tid: tid, id: data.updateData}); for (const lid of ledgerIds) { await this.ctx.service.ledger.calc(tid, lid, transaction); } result.ledger.update = ledgerIds; } else { throw '提交数据错误'; } await transaction.commit(); result.pos = data.updateData; result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update); return result; } catch (err) { await transaction.rollback(); throw err; } } async deletePosData(transaction, tid, lid) { await transaction.delete(this.tableName, {tid: tid, lid: lid}); } async copyBillsPosData(orgLid, newLid, transaction) { const posData = await this.getAllDataByCondition({ where: { lid: orgLid } }); if (posData.length > 0) { for (const pd of posData) { delete pd.id; pd.lid = newLid; } await transaction.insert(this.tableName, posData); } } /** * 批量插入部位数据 - 仅供批量插入清单部位调用 * @param transaction - 所属事务 * @param {Number} tid - 标段id * @param {Number} lid - 台账id * @param {Array} data - 新增数据 * @returns {Promise} */ async insertLedgerPosData(transaction, tid, lid, data) { for (const d of data) { d.tid = tid; d.lid = lid; // todo 新增期 d.add_stage = 0; d.add_times = 0; d.add_user = this.ctx.session.sessionUser.accountId; } await transaction.insert(this.tableName, data); } } return Pos; };