1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- '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) {
- 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 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);
- }
- }
- return ScheduleLedgerMonth;
- };
|