| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | 'use strict';module.exports = app => {    class ScheduleStage extends app.BaseService {        constructor(ctx) {            super(ctx);            this.tableName = 'schedule_stage';        }        async getLastPlanMonth() {            const sql = 'SELECT `yearmonth` FROM ?? WHERE `tid` = ? ORDER BY `yearmonth` DESC Limit 0,1';            const sqlParam = [this.tableName, this.ctx.tender.id];            return await this.db.query(sql, sqlParam);        }        async add(data) {            const transaction = await this.db.beginTransaction();            try {                const insertData = {                    tid: this.ctx.tender.id,                    yearmonth: data.yearmonth,                    order: data.order,                };                // 更新schedule_month stage_tp_used为1                const updateData = {                    stage_tp_used: 1,                };                const option = {                    where: {                        tid: this.ctx.tender.id,                        yearmonth: data.yearmonth,                    },                };                await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);                await transaction.insert(this.tableName, insertData);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async del(data) {            const transaction = await this.db.beginTransaction();            try {                const info = await this.getDataById(data.id);                await transaction.delete(this.tableName, { id: data.id });                // 更新schedule_month stage_tp_used为0                const updateData = {                    stage_tp_used: 0,                };                const option = {                    where: {                        tid: this.ctx.tender.id,                        yearmonth: info.yearmonth,                    },                };                await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);                // 重算计量总金额                await this.calcStageSjTp(transaction, this.ctx.tender.id);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async changeOrder(data) {            const transaction = await this.db.beginTransaction();            try {                const updateData = {                    id: data.id,                    order: data.order,                };                await transaction.update(this.tableName, updateData);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async updateOneTp(data) {            const transaction = await this.db.beginTransaction();            try {                const updateData = {                    tp: data.tp,                };                const option = {                    where: {                        tid: this.ctx.tender.id,                        order: data.order,                    },                };                await transaction.update(this.tableName, updateData, option);                await this.updateStageSjTp(transaction, this.ctx.tender.id, data.stage_sj_tp);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async calcStageSjTp(transaction, tid) {            const sql = 'SELECT SUM(`tp`) as stage_sj_tp FROM ?? WHERE tid = ?';            const sqlParam = [this.tableName, tid];            const result = await transaction.queryOne(sql, sqlParam);            const updateData = {                stage_sj_tp: result.stage_sj_tp,            };            const option = {                where: {                    tid,                },            };            return await transaction.update(this.ctx.service.schedule.tableName, updateData, option);        }        async updateStageSjTp(transaction, tid, sj_tp) {            // 更新计量总金额            const stageData = {                stage_sj_tp: sj_tp,            };            const stageOption = {                where: {                    tid,                },            };            await transaction.update(this.ctx.service.schedule.tableName, stageData, stageOption);        }    }    return ScheduleStage;};
 |