'use strict'; /** * 调差清单关联工料表 数据模型 * * @author Mai * @date 2018/8/13 * @version */ const auditConst = require('../const/audit').material; module.exports = app => { class MaterialList extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'material_list'; } /** * 添加工料清单关联 * @return {void} */ async add(data) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const list = []; for (const mb of data.mb_id) { const newLists = { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: mb, gcl_id: data.gcl_id, xmj_id: data.xmj_id, mx_id: data.mx_id, in_time: new Date(), }; list.push(newLists); } // 新增工料 const result = await this.db.insert(this.tableName, list); if (result.affectedRows === 0) { throw '新增工料数据失败'; } return await this.getMaterialData(this.ctx.tender.id); } /** * 删除工料清单关联 * @param {int} id 工料id * @return {void} */ async del(id) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } // 判断是否可删 return await this.deleteById(id); } /** * 修改工料清单关联信息 * @param {Object} data 工料内容 * @return {void} */ async save(data) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } // 判断是否可修改 return await this.db.update(this.tableName, data); } /** * 应用工料清单到其它清单中 * @return {void} */ async addOther(data) { if (!this.ctx.tender || !this.ctx.material) { throw '数据错误'; } const list = []; const select = data.select; for (const mb of data.mx_id) { const newLists = { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: select.mb_id, gcl_id: select.gcl_id, xmj_id: select.xmj_id, mx_id: mb, quantity: select.quantity, in_time: new Date(), }; list.push(newLists); } // 新增工料 const result = await this.db.insert(this.tableName, list); if (result.affectedRows === 0) { throw '新增工料数据失败'; } return await this.getMaterialData(this.ctx.tender.id); } /** * 获取工料清单关联表 * @param {int} tid 标段id * @return {void} */ async getMaterialData(tid) { const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`quantity`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' + ' FROM ' + this.tableName + ' as ml' + ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' + ' ON ml.`mb_id` = mb.`id`' + ' WHERE ml.`tid` = ?'; const sqlParam = [tid]; return await this.db.query(sql, sqlParam); } } return MaterialList; };