| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 | '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 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,                gcl_id: '0',            };            // 新增工料            const result = await this.db.insert(this.tableName, insertData);            if (result.affectedRows === 0) {                throw '新增空白清单数据失败';            }            return await this.getDataById(result.insertId);        }        /**         * 删除变更清单         * @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.calcQuantityByML(transaction, mb_id);                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 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);                }                console.log(insertDatas);                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;            for (const cl of changeList) {                total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, this.ctx.tender.info.decimal.tp));            }            const updateData = {                total_price,            };            const options = {                where: {                    cid: this.ctx.change.cid,                },            };            await transaction.update(this.ctx.service.change.tableName, updateData, options);        }        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;};
 |