'use strict'; /** * * * @author Mai * @date * @version */ const validField = ['lid', 'pos_id', 'share', 'color', 'comment']; module.exports = app => { class LedgerTag extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'ledger_tag'; } /** * 获取台账、期所有标段 * @param {Number} tid - 标段id * @param {Number} sid - 期id(-1时查询台账分解全部标签) * @returns {Promise} */ async getDatas(tid, sid = -1, settleId = -1) { const sql = 'SELECT la.id, la.uid, la.lid, la.pos_id, la.share, la.color, la.comment, pa.name as u_name FROM ' + this.tableName + ' la ' + ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' pa ON la.uid = pa.id' + ' WHERE la.tid = ? and la.sid = ? and la.settle_id = ? and (la.uid = ? or la.share) ORDER BY la.create_time DESC'; return await this.db.query(sql, [tid, sid, settleId, this.ctx.session.sessionUser.accountId]); } /** * 过滤无效字段,容错 * @param data * @private */ _filterInvalidField(data) { for (const prop in data) { if (validField.indexOf(prop) === -1) { delete data[prop]; } } } async _addTag(data) { this._filterInvalidField(data); data.create_time = new Date(); data.modify_time = data.create_time; data.uid = this.ctx.session.sessionUser.accountId; data.tid = this.ctx.tender.id; data.pid = this.ctx.tender.data.project_id; if (this.ctx.stage) { data.sid = this.ctx.stage.id; data.sorder = this.ctx.stage.order; } if (this.ctx.settle) { data.settle_id = this.ctx.settle.id; data.settle_order = this.ctx.settle.settle_order; } const result = await this.db.insert(this.tableName, data); data.id = result.insertId; data.u_name = this.ctx.session.sessionUser.name; return data; } async _delTag(id) { const tag = await this.getDataById(id); if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据'; await this.deleteById(id); return id; } async _updateTag(data) { const tag = await this.getDataById(data.id); if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权修改该数据'; this._filterInvalidField(data); data.modify_time = new Date(); data.id = tag.id; const result = await this.db.update(this.tableName, data); if (result.affectedRows === 1) return data; } async update(data) { const result = {}; if (data.add) result.add = await this._addTag(data.add); if (data.del) result.del = await this._delTag(data.del); if (data.update) result.update = await this._updateTag(data.update); return result; } } return LedgerTag; };