| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | 'use strict';/** * * * @author Mai * @date * @version */const auditConst = require('../const/audit').stage;module.exports = app => {    class StageOther extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'stage_other';        }        async getStageData(stage) {            const data = await this.getAllDataByCondition({where: { sid: stage.id }});            if (stage && stage.readOnly && stage.status !== auditConst.status.checked) {                for (const d of data) {                    const his = d.shistory ? JSON.parse(d.shistory) : [];                    const h = this.ctx.helper._.find(his, {                        stimes: stage.curTimes, sorder: stage.curOrder                    });                    d.tp = h ? h.tp : null;                }            }            return data;        }        async getPreStageData(sorder) {            const sql = 'SELECT o.uuid, Sum(o.tp) as tp ' +                '  From ' + this.tableName + ' o ' +                '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = o.sid' +                '  WHERE s.order < ? And o.tid = ?' +                '  GROUP By uuid';            const sqlParam = [sorder, this.ctx.tender.id];            const data = await this.db.query(sql, sqlParam);            return data;        }        async getEndStageData(sorder) {            const sql = 'SELECT o.uuid, Sum(o.tp) as tp ' +                '  From ' + this.tableName + ' o ' +                '  LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = o.sid' +                '  WHERE s.order <= ? And o.tid = ?' +                '  GROUP By uuid';            const sqlParam = [sorder, this.ctx.tender.id];            const data = await this.db.query(sql, sqlParam);            return data;        }        async _addDatas(data) {            const datas = data instanceof Array ? data : [data];            const insertData = [];            for (const d of datas) {                if (!d.name || !d.order) throw '新增其他数据,提交的数据错误';                const nd = {                    uuid: this.uuid.v4(),                    add_sid: this.ctx.stage.id,                    add_uid: this.ctx.session.sessionUser.accountId,                    add_time: new Date(),                    sid: this.ctx.stage.id,                    sorder: this.ctx.stage.order,                    tid: this.ctx.tender.id,                };                nd.name = d.name;                nd.order = d.order;                if (d.o_type) nd.o_type = d.o_type;                if (d.total_price) nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);                if (d.tp) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);                if (d.real_time) nd.real_time = d.real_time;                if (d.memo) nd.memo = d.memo;                insertData.push(nd);            }            await this.db.insert(this.tableName, insertData);            return await this.getAllDataByCondition({                where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }            });        }        async _delDatas (data) {            const datas = data instanceof Array ? data : [data];            const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });            for (const od of orgDatas) {                if (od.pre_used) throw '往期已经计量,不可删除';            }            await this.db.delete(this.tableName, {id: datas});            return datas;        }        async _updateDatas (data) {            const datas = data instanceof Array ? data : [data];            const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});            const uDatas = [];            for (const d of datas) {                const od = this.ctx.helper._.find(orgDatas, {id: d.id});                if (!od) continue;                const nd = {id: od.id};                if (d.name !== undefined) {                    if (od.pre_used) throw '往期已使用,不可修改名称';                    nd.name = d.name;                }                if (d.order !== undefined) nd.order = d.order;                if (d.o_type !== undefined) nd.o_type = d.o_type;                if (d.total_price !== undefined) {                    if (od.pre_used) throw '往期已使用,不可修改金额';                    nd.total_price = this.ctx.helper.round(d.total_price, this.ctx.tender.info.decimal.tp);                }                if (d.tp !== undefined) nd.tp = this.ctx.helper.round(d.tp, this.ctx.tender.info.decimal.tp);                if (d.real_time !== undefined) nd.real_time = new Date(d.real_time);                if (d.memo !== undefined) nd.memo = d.memo;                uDatas.push(nd);            }            if (uDatas.length > 0) {                await this.db.updateRows(this.tableName, uDatas);                return uDatas;            } else {                return [];            }        }        async updateDatas(data) {            const result = {add: [], del: [], update: []};            try {                if (data.add) {                    result.add = await this._addDatas(data.add);                }                if (data.update) {                    result.update = await this._updateDatas(data.update);                }                if (data.del) {                    result.del = await this._delDatas(data.del);                }                return result;            } catch (err) {                if (err.stack) {                    throw err;                } else {                    result.err = err.toString();                    return result;                }            }        }        async updateHistory(stage, transaction) {            const datas = await this.getStageData(stage);            if (datas.length === 0) return;            const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};            const updateDatas = [];            for (const d of datas) {                const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];                const his = this.ctx.helper._.find(datas, filter);                if (his) {                    his.tp = d.tp;                    if (d.sid === d.add_sid) his.total_price = d.total_price;                } else {                    const nHis = { stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder, tp: d.tp };                    if (d.sid === d.add_sid) nHis.total_price = d.total_price;                    history.push(nHis);                }                updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });            }            await transaction.updateRows(this.tableName, updateDatas);        }        async addInitialStageData(stage, preStage, transaction) {            if (!stage || !preStage) {                throw '标段数据有误';            }            const preDatas = await this.getStageData(preStage);            if (preDatas.length > 0) {                for (const pd of preDatas) {                    delete pd.id;                    pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.tp);                    delete pd.tp;                    pd.sid = stage.id;                    pd.sorder = stage.order;                    delete pd.shistory;                }                const result = await transaction.insert(this.tableName, preDatas);                return result.affectedRows === preDatas.length;            } else {                return true;            }        }    }    return StageOther;};
 |