| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | 'use strict';/** * 不参与调差-清单关联表 数据模型 * * @author Mai * @date 2018/8/13 * @version */module.exports = app => {    class MaterialListNotJoin extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'material_list_notjoin';        }        /**         * 添加不参与调差的清单         * @return {void}         */        async add(data) {            if (!this.ctx.tender || !this.ctx.material) {                throw '数据错误';            }            const transaction = await this.db.beginTransaction();            try {                const newListNotJoin = {                    tid: this.ctx.tender.id,                    mid: this.ctx.material.id,                    gcl_id: data.gcl_id,                    xmj_id: data.id,                    mx_id: data.mx_id !== undefined ? data.mx_id : null,                    in_time: new Date(),                };                data.xmj_id = data.id;                data.mx_id = data.mx_id !== undefined ? data.mx_id : null;                await this.updateAllMaterials(transaction, data, 'add');                // 新增不参与调差清单                const result = await transaction.insert(this.tableName, newListNotJoin);                if (result.affectedRows === 0) {                    throw '新增不参与调差清单数据失败';                }                await transaction.commit();                return await this.getDataById(result.insertId);            } catch (err) {                await transaction.rollback();                throw err;            }        }        /**         * 删除不参与调差的清单         * @param {int} id 工料id         * @return {void}         */        async del(id) {            if (!this.ctx.tender || !this.ctx.material) {                throw '数据错误';            }            const transaction = await this.db.beginTransaction();            try {                const notJoinInfo = await this.getDataById(id);                await this.updateAllMaterials(transaction, notJoinInfo, 'del');                // 判断是否可删                const result = await transaction.delete(this.tableName, { id });                await transaction.commit();                return result;            } catch (err) {                await transaction.rollback();                throw err;            }        }        /**         * 修改调差list和bill和material对应值         * @param transaction         * @returns {Promise<void>}         */        async updateAllMaterials(transaction, data, type) {            // 先判断material_list是否存在值并quantity不为null            const searchSql = {                mid: this.ctx.material.id,                gcl_id: data.gcl_id,                xmj_id: data.xmj_id,            };            if (data.mx_id !== null) {                searchSql.mx_id = data.mx_id;            }            const materialListData = await this.ctx.service.materialList.getAllDataByCondition({                where: searchSql,            });            console.log(data);            console.log(materialListData);            if (materialListData && materialListData.length !== 0) {                // 对应修改更新本期应耗数量值和总的本期金额计算                const mbIdList = [];                for (const ml of materialListData) {                    const updateData = {                        id: ml.id,                        is_join: type === 'add' ? 0 : 1,                    };                    if (mbIdList.indexOf(ml.mb_id) === -1) {                        mbIdList.push(ml.mb_id);                    }                    await transaction.update(this.ctx.service.materialList.tableName, updateData);                }                // 重新计算金额                for (const mb_id of mbIdList) {                    await this.service.materialList.calcQuantityByML(transaction, mb_id);                }            }        }        /**         * 复制上一期不参与调差的清单到下一期中         * @param {Object} transaction - 新增一期的事务         * @param {Object} list 上期清单         * @param {int} mid 工料id         * @return {void}         */        async copyNewStageNotJoinList(transaction, list, mid) {            if (!this.ctx.tender) {                throw '数据错误';            }            const notJoinlist = [];            for (const mb of list) {                const newLists = {                    tid: mb.tid,                    mid,                    gcl_id: mb.gcl_id,                    xmj_id: mb.xmj_id,                    mx_id: mb.mx_id,                    in_time: new Date(),                };                notJoinlist.push(newLists);            }            // 复制上一期不参与调差的清单            return notJoinlist.length > 0 ? await transaction.insert(this.tableName, notJoinlist) : true;        }    }    return MaterialListNotJoin;};
 |