'use strict'; /** * * * @author Mai * @date 2018/8/14 * @version */ const audit = require('../const/audit'); module.exports = app => { class ChangeAuditList extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'change_audit_list'; } /** * 取出变更令清单列表,并按台账清单在前,空白清单在后排序 * @return {void} */ async getList(cid) { const sql = 'SELECT * FROM ?? WHERE `cid` = ? ORDER BY `lid` = "0", `id` asc'; const sqlParam = [this.tableName, cid]; return await this.db.query(sql, sqlParam); } /** * 添加空白变更清单 * @return {void} */ async add(data) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } const insertData = { tid: this.ctx.tender.id, cid: this.ctx.change.cid, lid: '0', code: '', name: '', bwmx: '', unit: '', unit_price: null, oamount: 0, camount: 0, samount: '', detail: '', spamount: 0, xmj_code: null, xmj_jldy: null, xmj_dwgc: null, xmj_fbgc: null, xmj_fxgc: null, gcl_id: '', }; // 新增工料 const result = await this.db.insert(this.tableName, insertData); if (result.affectedRows === 0) { throw '新增空白清单数据失败'; } return await this.getDataById(result.insertId); } /** * 批量添加空白变更清单 * @return {void} */ async batchAdd(data) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } const num = data.num ? parseInt(data.num) : 0; if (num < 1 || num > 100) { throw '批量添加的空白清单数目不能小于1或大于100'; } const insertArray = []; for (let i = 0; i < num; i++) { const insertData = { tid: this.ctx.tender.id, cid: this.ctx.change.cid, lid: '0', code: '', name: '', bwmx: '', unit: '', unit_price: null, oamount: 0, camount: 0, samount: '', detail: '', spamount: 0, xmj_code: null, xmj_jldy: null, xmj_dwgc: null, xmj_fbgc: null, xmj_fxgc: null, gcl_id: '', }; insertArray.push(insertData); } // 新增工料 const result = await this.db.insert(this.tableName, insertArray); if (result.affectedRows !== num) { throw '批量添加空白清单数据失败'; } // 获取刚批量添加的所有list for (let j = 0; j < num; j++) { insertArray[j].id = result.insertId + j; } return insertArray; } /** * 删除变更清单 * @param {int} id 清单id * @return {void} */ async del(id) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { // 判断是否可删 await transaction.delete(this.tableName, { id }); // 重新算变更令总额 await this.calcCamountSum(transaction); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } /** * 修改变更清单 * @param {Object} data 工料内容 * @param {int} order 期数 * @return {void} */ async save(data, order) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } const transaction = await this.db.beginTransaction(); try { // const mb_id = data.mb_id; // delete data.mb_id; await transaction.update(this.tableName, data); // await this.calcQuantityByML(transaction, mb_id); await this.calcCamountSum(transaction); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } /** * 修改变更清单 复制粘贴 * @param {Object} datas 修改内容 * @return {void} */ async saveDatas(datas) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } // 判断是否可修改 // 判断t_type是否为费用 const transaction = await this.db.beginTransaction(); try { // for (const data of datas) { // const mb_id = data.mb_id; // delete data.mb_id; // await transaction.update(this.tableName, data); // await this.calcQuantityByML(transaction, mb_id); // } await transaction.updateRows(this.tableName, datas); await this.calcCamountSum(transaction); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } /** * 台账数据清单 重新选择 * @param {Object} datas 内容 * @return {void} */ async saveLedgerListDatas(datas) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } // 判断是否可修改 // 判断t_type是否为费用 const transaction = await this.db.beginTransaction(); try { const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? GROUP BY b.cbid'; const sqlParam1 = [this.ctx.service.stageChange.tableName, this.tableName, this.ctx.change.cid]; const usedList = await transaction.query(sql1, sqlParam1); // 先删除原本的台账清单数据 const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"'; const sqlParam = [this.tableName, this.ctx.change.cid]; await transaction.query(sql, sqlParam); const insertDatas = []; for (const data of datas) { data.tid = this.ctx.tender.id; data.cid = this.ctx.change.cid; data.spamount = data.camount; data.samount = ''; insertDatas.push(data); } if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas); await this.calcCamountSum(transaction); // 更新stage_change和stage_change_final的cbid if (usedList.length > 0) { const updateList = []; const newList = await transaction.select(this.tableName, { where: { cid: this.ctx.change.cid } }); for (const used of usedList) { const newone = this._.find(newList, { code: used.code, lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx }); if (newone) { updateList.push({ row: { cbid: newone.id, }, where: { cid: this.ctx.change.cid, cbid: used.id, }, }); } } if (updateList.length > 0) { await transaction.updateRows(this.ctx.service.stageChange.tableName, updateList); await transaction.updateRows(this.ctx.service.stageChangeFinal.tableName, updateList); } } await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } /** * 台账数据清单 清除部分并重新算原设计总金额 * @param {Object} datas 内容 * @return {void} */ async removeLedgerListDatas(datas) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } // 判断是否可修改 // 判断t_type是否为费用 const transaction = await this.db.beginTransaction(); try { // 先删除原本的台账清单数据 // const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"'; // const sqlParam = [this.tableName, this.ctx.change.cid]; // await transaction.query(sql, sqlParam); // const insertDatas = []; for (const data of datas) { // data.tid = this.ctx.tender.id; // data.cid = this.ctx.change.cid; // data.spamount = data.camount; // data.samount = ''; // insertDatas.push(data); await transaction.delete(this.tableName, { id: data.id }); } // if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas); await this.calcCamountSum(transaction); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async calcCamountSum(transaction) { // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?'; // const sqlParam = [this.tableName, this.change.cid]; // const tp = await transaction.queryOne(sql, sqlParam); // 防止小数位不精确,采用取值计算 const sql = 'SELECT unit_price, spamount FROM ?? WHERE cid = ?'; const sqlParam = [this.tableName, this.ctx.change.cid]; const changeList = await transaction.query(sql, sqlParam); let total_price = 0; const tp_decimal = this.ctx.change.tp_decimal ? this.ctx.change.tp_decimal : this.ctx.tender.info.decimal.tp; for (const cl of changeList) { total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, tp_decimal)); } const updateData = { total_price, }; const options = { where: { cid: this.ctx.change.cid, }, }; await transaction.update(this.ctx.service.change.tableName, updateData, options); } /** * 用户数据数量提交 * @param {Object} data 内容 * @return {void} */ async saveAmountData(data) { if (!this.ctx.tender || !this.ctx.change) { throw '数据错误'; } // 判断是否可修改 // 判断t_type是否为费用 const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, data); await this.calcCamountSum(transaction); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async gatherBgBills(tid) { const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' + ' FROM ' + this.tableName + ' cb' + ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' + ' WHERE cb.tid = ? and c.status = ?' + ' GROUP BY code, name, unit, unit_price'; const param = [tid, audit.flow.status.checked]; const result = await this.db.query(sql, param); for (const b of result) { b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp); } return result; } /** * 报表用 * Tony Kang * @param {tid} tid - 标段id * @return {void} */ async getChangeAuditBills(tid) { const sql = 'SELECT cb.*' + ' FROM ' + this.tableName + ' cb' + ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' + ' WHERE c.tid = ? and c.status = 3' + ' ORDER BY cb.cid, cb.code'; const param = [tid]; const result = await this.db.query(sql, param); return result; } } return ChangeAuditList; };