'use strict'; module.exports = app => { class ScheduleLedgerMonth extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'schedule_ledger_month'; } async save(data) { // 判断是添加,删除,还是修改 const transaction = await this.db.beginTransaction(); try { const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth }); if (info) { if (data.plan_gcl === null && data.plan_tp === null && info.sj_gcl === null && info.sj_tp === null) { await transaction.delete(this.tableName, { id: info.id }); } else { const updateData = { id: info.id, plan_gcl: data.plan_gcl, plan_tp: data.plan_tp, }; await transaction.update(this.tableName, updateData); } } else { const insertData = { tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth, plan_gcl: data.plan_gcl, plan_tp: data.plan_tp, }; await transaction.insert(this.tableName, insertData); } // 重新计算本月、总 计划金额和计划工程量 await this.calcMonthPlan(transaction, this.ctx.tender.id, data.yearmonth); await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async saveDatas(datas) { // 判断是添加,删除,还是修改 const transaction = await this.db.beginTransaction(); try { const ymArray = []; for (const data of datas) { const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth }); if (!this._.includes(ymArray, data.yearmonth)) { ymArray.push(data.yearmonth); } if (info) { if (data.plan_gcl === null && data.plan_tp === null && info.sj_gcl === null && info.sj_tp === null) { await transaction.delete(this.tableName, { id: info.id }); } else { const updateData = { id: info.id, plan_gcl: data.plan_gcl, plan_tp: data.plan_tp, }; await transaction.update(this.tableName, updateData); } } else { const insertData = { tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth, plan_gcl: data.plan_gcl, plan_tp: data.plan_tp, }; await transaction.insert(this.tableName, insertData); } } // 重新计算本月、总 计划金额和计划工程量 for (const ym of ymArray) { await this.calcMonthPlan(transaction, this.ctx.tender.id, ym); } await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async saveSj(data) { // 判断是添加,删除,还是修改 const transaction = await this.db.beginTransaction(); try { const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth }); if (info) { if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) { await transaction.delete(this.tableName, { id: info.id }); } else { const updateData = { id: info.id, sj_gcl: data.sj_gcl, sj_tp: data.sj_tp, }; await transaction.update(this.tableName, updateData); } } else { const insertData = { tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth, sj_gcl: data.sj_gcl, sj_tp: data.sj_tp, }; await transaction.insert(this.tableName, insertData); } // 重新计算本月、总 计划金额和计划工程量 await this.calcMonthSj(transaction, this.ctx.tender.id, data.yearmonth); await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async saveSjDatas(datas) { // 判断是添加,删除,还是修改 const transaction = await this.db.beginTransaction(); try { const ymArray = []; for (const data of datas) { const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth }); if (!this._.includes(ymArray, data.yearmonth)) { ymArray.push(data.yearmonth); } if (info) { if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) { await transaction.delete(this.tableName, { id: info.id }); } else { const updateData = { id: info.id, sj_gcl: data.sj_gcl, sj_tp: data.sj_tp, }; await transaction.update(this.tableName, updateData); } } else { const insertData = { tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth, sj_gcl: data.sj_gcl, sj_tp: data.sj_tp, }; await transaction.insert(this.tableName, insertData); } } // 重新计算本月、总 计划金额和计划工程量 for (const ym of ymArray) { await this.calcMonthSj(transaction, this.ctx.tender.id, ym); } await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async calcMonthSj(transaction, tid, yearmonth) { const sql = 'SELECT SUM(`sj_gcl`) as total_sj_gcl, SUM(`sj_tp`) as total_sj_tp FROM ?? WHERE tid = ? and yearmonth = ?'; const sqlParam = [this.tableName, tid, yearmonth]; const result = await transaction.queryOne(sql, sqlParam); const updateData = { sj_gcl: result.total_sj_gcl, sj_tp: result.total_sj_tp, }; const option = { where: { tid, yearmonth, }, }; return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option); } async calcMonthPlan(transaction, tid, yearmonth) { const sql = 'SELECT SUM(`plan_gcl`) as total_plan_gcl, SUM(`plan_tp`) as total_plan_tp FROM ?? WHERE tid = ? and yearmonth = ?'; const sqlParam = [this.tableName, tid, yearmonth]; const result = await transaction.queryOne(sql, sqlParam); const updateData = { plan_gcl: result.total_plan_gcl, plan_tp: result.total_plan_tp, }; const option = { where: { tid, yearmonth, }, }; return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option); } async getConllectionList(tid, yearmonthArray) { const sql = 'SELECT tid, lid, SUM(`plan_gcl`) as plan_gcl, SUM(`plan_tp`) as plan_tp, SUM(`sj_gcl`) as sj_gcl, SUM(`sj_tp`) as sj_tp' + ' FROM ?? WHERE `tid` = ? AND `yearmonth` in (?) GROUP BY `lid`'; const sqlParam = [this.tableName, tid, yearmonthArray]; const result = await this.db.query(sql, sqlParam); return result; } // async getHadDataList(tid) { // const sql = 'SELECT lid FROM ?? WHERE tid = ? ' // } } return ScheduleLedgerMonth; };