浏览代码

优化批量插入,部分计算

MaiXinRong 3 年之前
父节点
当前提交
f4cc15b869
共有 7 个文件被更改,包括 122 次插入376 次删除
  1. 0 100
      app/base/base_tree_service.js
  2. 108 129
      app/service/ledger.js
  3. 0 52
      app/service/pos.js
  4. 5 47
      app/service/revise_bills.js
  5. 0 18
      app/service/revise_pos.js
  6. 1 3
      app/service/stage_change.js
  7. 8 27
      app/service/stage_pos.js

+ 0 - 100
app/base/base_tree_service.js

@@ -1011,106 +1011,6 @@ class TreeService extends Service {
             return await this.getDataById(datas.id);
         }
     }
-
-    async pasteBlockRelaData(relaData) {
-        if (!this.transaction) throw '更新数据错误';
-        // for (const id of relaData) {
-        //     await this.ctx.service.pos.copyBillsPosData(id.org, id.new, this.transaction);
-        // }
-    }
-
-    async getPasteBlockResult(select, copyNodes) {
-        const createData = await this.getDataByIds(newIds);
-        const updateData = await this.getNextsData(selectData[this.setting.mid], selectData[this.setting.pid], selectData[this.setting.order] + copyNodes.length);
-        //const posData = await this.ctx.service.pos.getPosData({ lid: newIds });
-        return {
-            ledger: { create: createData, update: updateData },
-            //pos: posData,
-        };
-    }
-
-    /**
-     * 复制粘贴整块
-     * @param {Number} tenderId - 标段Id
-     * @param {Number} selectId - 选中几点Id
-     * @param {Array} block - 复制节点Id
-     * @return {Object} - 提价后的数据(其中新增粘贴数据,只返回第一层)
-     */
-    async pasteBlock(mid, kid, paste) {
-        if ((mid <= 0) || (kid <= 0)) return [];
-
-        const selectData = await this.getDataByKid(mid, kid);
-        if (!selectData) throw '数据错误';
-
-        const copyNodes = await this.getDataByNodeIds(paste.tid, paste.block);
-        if (!copyNodes || copyNodes.length <= 0)  throw '复制数据错误';
-        for (const node of copyNodes) {
-            if (node[this.setting.pid] !== copyNodes[0][this.setting.pid]) throw '复制数据错误:仅可操作同层节点';
-        }
-
-        const newParentPath = selectData[this.setting.fullPath].replace(selectData[this.setting.kid], '');
-        const orgParentPath = copyNodes[0][this.setting.fullPath].replace(copyNodes[0][this.setting.kid], '');
-        const newIds = [];
-        this.transaction = await this.db.beginTransaction();
-        try {
-            // 选中节点的所有后兄弟节点,order+粘贴节点个数
-            await this._updateChildrenOrder(mid, selectData[this.setting.pid], selectData[this.setting.order], copyNodes.length);
-            for (let iNode = 0; iNode < copyNodes.length; iNode++) {
-                const node = copyNodes[iNode];
-                let datas = await this.getDataByFullPath(mid, node[this.setting.fullPath] + '%');
-                datas = this._.sortBy(datas, 'level');
-
-                const maxId = await this._getMaxLid(mid);
-                this._cacheMaxLid(mid, maxId + datas.length);
-
-                const billsId = [];
-                // 计算粘贴数据中需更新部分
-                for (let index = 0; index < datas.length; index++) {
-                    const data = datas[index];
-                    const newId = maxId + index + 1;
-                    const idChange = {
-                        org: data.id,
-                    };
-                    if (this.setting.uuid) data.id = this.uuid.v4();
-                    idChange.new = data.id;
-                    data[this.setting.mid] = mid;
-                    if (!data[this.setting.isLeaf]) {
-                        for (const children of datas) {
-                            children[this.setting.fullPath] = children[this.setting.fullPath].replace('-' + data[this.setting.kid], '-' + newId);
-                            if (children[this.setting.pid] === data[this.setting.kid]) {
-                                children[this.setting.pid] = newId;
-                            }
-                        }
-                    } else {
-                        data[this.setting.fullPath] = data[this.setting.fullPath].replace('-' + data[this.setting.kid], '-' + newId);
-                    }
-                    data[this.setting.kid] = newId;
-                    data[this.setting.fullPath] = data[this.setting.fullPath].replace(orgParentPath, newParentPath);
-                    if (data[this.setting.pid] === node[this.setting.pid]) {
-                        data[this.setting.pid] = selectData[this.setting.pid];
-                        data[this.setting.order] = selectData[this.setting.order] + iNode + 1;
-                    }
-                    data[this.setting.level] = data[this.setting.level] + selectData[this.setting.level] - copyNodes[0][this.setting.level];
-                    idChange.isLeaf = data[this.setting.isLeaf];
-                    billsId.push(idChange);
-                    newIds.push(data.id);
-                }
-                const newData = await this.transaction.insert(this.tableName, datas);
-                await this.pasteBlockRelaData(billsId);
-            }
-            // 数据库创建新增节点数据
-            await this.transaction.commit();
-            this.transaction = null;
-        } catch (err) {
-            await this.transaction.rollback();
-            this.transaction = null;
-            throw err;
-        }
-
-        // 查询应返回的结果
-        return await this.getPasteBlockResult(selectData, copyNodes);
-    }
-
 }
 
 module.exports = TreeService;

+ 108 - 129
app/service/ledger.js

@@ -487,17 +487,56 @@ module.exports = app => {
          */
         async batchInsertChild(tenderId, selectId, data) {
             const result = { ledger: {}, pos: null };
-            if ((tenderId <= 0) || (selectId <= 0)) {
-                return result;
-            }
+            if ((tenderId <= 0) || (selectId <= 0)) return result;
             const selectData = await this.getDataByNodeId(tenderId, selectId);
-            if (!selectData) {
-                throw '位置数据错误';
-            }
+            if (!selectData) throw '位置数据错误';
 
+            const info = this.ctx.tender.info;
             this.transaction = await this.db.beginTransaction();
-            const newIds = [];
             const lastChild = await this.getLastChildData(tenderId, selectId);
+            // 计算id
+            const maxId = await this._getMaxLid(tenderId);
+            const insertBillsData = [], insertPosData = [], in_time = new Date();
+            const order = lastChild ? lastChild.order : 0;
+            for (let i = 0, iLen = data.length; i < iLen; i++) {
+                // 合并新增数据
+                const qd = {
+                    id: this.uuid.v4(),
+                    tender_id: tenderId,
+                    ledger_id: maxId + i + 1,
+                    ledger_pid: selectData.ledger_id,
+                    is_leaf: true,
+                    order: order + i + 1,
+                    level: selectData.level + 1,
+                    b_code: data[i].b_code,
+                    name: data[i].name,
+                    unit: data[i].unit,
+                    unit_price: this.ctx.helper.round(data[i].price, info.decimal.up),
+                };
+                qd.full_path = selectData.full_path + '-' + qd.ledger_id;
+                insertBillsData.push(qd);
+                const precision = this.ctx.helper.findPrecision(info.precision, qd.unit);
+                if (data[i].pos.length > 0) {
+                    for (const [i, p] of data[i].pos.entries()) {
+                        const inD = {
+                            id: this.uuid.v4(), tid: tid, lid: bills.id,
+                            add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
+                            in_time, porder: i + 1,
+                            name: p.name, drawing_code: p.drawing_code,
+                        };
+                        if (d.quantity) {
+                            inD.sgfh_qty = this.round(p.quantity, precision.value);
+                            inD.quantity = inD.sgfh_qty;
+                            qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inD.sgfh_qty);
+                        }
+                        insertPosData.push(inD);
+                        await transaction.insert(this.tableName, insertDatas);
+                    }
+                    qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, info.decimal.tp);
+                    qd.quantity = qd.sgfh_qty;
+                    qd.total_price = qd.sgfh_tp;
+                }
+            }
             try {
                 // 更新父项isLeaf
                 if (!lastChild) {
@@ -505,46 +544,15 @@ module.exports = app => {
                         id: selectData.id,
                         is_leaf: false,
                         unit_price: null,
-                        sgfh_qty: null,
-                        sgfh_tp: null,
-                        sjcl_qty: null,
-                        sjcl_tp: null,
-                        qtcl_qty: null,
-                        qtcl_tp: null,
-                        quantity: null,
-                        total_price: null,
-                        deal_qty: null,
-                        deal_tp: null,
+                        sgfh_qty: null, sgfh_tp: null,
+                        sjcl_qty: null, sjcl_tp: null,
+                        qtcl_qty: null, qtcl_tp: null,
+                        quantity: null, total_price: null,
+                        deal_qty: null, deal_tp: null,
                     });
                 }
-                const order = lastChild ? lastChild.order : 0;
-                // 计算id
-                const maxId = await this._getMaxLid(tenderId);
-
-                // 数据库创建新增节点数据
-                for (let i = 0, iLen = data.length; i < iLen; i++) {
-                    // 合并新增数据
-                    const qd = {
-                        id: this.uuid.v4(),
-                        tender_id: tenderId,
-                        ledger_id: maxId + i + 1,
-                        ledger_pid: selectData.ledger_id,
-                        is_leaf: true,
-                        order: order + i + 1,
-                        level: selectData.level + 1,
-                        b_code: data[i].b_code,
-                        name: data[i].name,
-                        unit: data[i].unit,
-                        unit_price: data[i].price,
-                    };
-                    qd.full_path = selectData.full_path + '-' + qd.ledger_id;
-                    const insertResult = await this.transaction.insert(this.tableName, qd);
-                    newIds.push(qd.id);
-                    if (data[i].pos.length > 0) {
-                        await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
-                        await this.calcNode(qd, this.transaction);
-                    }
-                }
+                await this.transaction.insert(this.tableName, insertBillsData);
+                if (insertPosData.length > 0) await this.transaction.insert(this.ctx.service.pos.tableName, insertPosData);
                 this._cacheMaxLid(tenderId, maxId + data.length);
                 await this.transaction.commit();
             } catch (err) {
@@ -553,11 +561,9 @@ module.exports = app => {
             }
 
             // 查询应返回的结果
-            result.ledger.create = await this.getDataByIds(newIds);
-            if (!lastChild) {
-                result.ledger.update = await this.getDataByIds([selectData.id]);
-            }
-            result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
+            result.ledger.create = insertBillsData;
+            if (!lastChild) result.ledger.update = await this.getDataByIds([selectData.id]);
+            result.pos = insertPosData;
             return result;
         }
 
@@ -570,50 +576,63 @@ module.exports = app => {
          */
         async batchInsertNext(tenderId, selectId, data) {
             const result = { ledger: {}, pos: null };
-            if ((tenderId <= 0) || (selectId <= 0)) {
-                return result;
-            }
+            if ((tenderId <= 0) || (selectId <= 0)) return result;
             const selectData = await this.getDataByNodeId(tenderId, selectId);
-            if (!selectData) {
-                throw '位置数据错误';
-            }
+            if (!selectData) throw '位置数据错误';
             const parentData = await this.getDataByNodeId(tenderId, selectData.ledger_pid);
-            if (!parentData) {
-                throw '位置数据错误';
-            }
+            if (!parentData) throw '位置数据错误';
 
+            const info = this.ctx.tender.info;
             this.transaction = await this.db.beginTransaction();
-            const newIds = [];
+
+            const insertBillsData = [], insertPosData = [];
+            const maxId = await this._getMaxLid(tenderId);
+            const order = selectData.order;
+            for (let i = 0, iLen = data.length; i < iLen; i++) {
+                // 合并新增数据
+                const qd = {
+                    id: this.uuid.v4(),
+                    tender_id: tenderId,
+                    ledger_id: maxId + i + 1,
+                    ledger_pid: selectData.ledger_id,
+                    is_leaf: true,
+                    order: order + i + 1,
+                    level: selectData.level + 1,
+                    b_code: data[i].b_code,
+                    name: data[i].name,
+                    unit: data[i].unit,
+                    unit_price: this.ctx.helper.round(data[i].price, info.decimal.up),
+                };
+                qd.full_path = selectData.full_path + '-' + qd.ledger_id;
+                insertBillsData.push(qd);
+                const precision = this.ctx.helper.findPrecision(info.precision, qd.unit);
+                if (data[i].pos.length > 0) {
+                    for (const [i, p] of data[i].pos.entries()) {
+                        const inD = {
+                            id: this.uuid.v4(), tid: tid, lid: bills.id,
+                            add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
+                            in_time, porder: i + 1,
+                            name: p.name, drawing_code: p.drawing_code,
+                        };
+                        if (d.quantity) {
+                            inD.sgfh_qty = this.round(p.quantity, precision.value);
+                            inD.quantity = inD.sgfh_qty;
+                            qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inD.sgfh_qty);
+                        }
+                        insertPosData.push(inD);
+                        await transaction.insert(this.tableName, insertDatas);
+                    }
+                    qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, info.decimal.tp);
+                    qd.quantity = qd.sgfh_qty;
+                    qd.total_price = qd.sgfh_tp;
+                }
+            }
+
             try {
                 // 选中节点的所有后兄弟节点,order+粘贴节点个数
                 await this._updateChildrenOrder(tenderId, selectData.ledger_pid, selectData.order + 1, data.length);
-                // 计算id和order
-                const maxId = await this._getMaxLid(tenderId);
-                const order = selectData.order;
-                // 数据库创建新增节点数据
-                for (let i = 0, iLen = data.length; i < iLen; i++) {
-                    // 合并新增数据
-                    const qd = {
-                        id: this.uuid.v4(),
-                        tender_id: tenderId,
-                        ledger_id: maxId + i + 1,
-                        ledger_pid: parentData.ledger_id,
-                        is_leaf: true,
-                        order: order + i + 1,
-                        level: parentData.level + 1,
-                        b_code: data[i].b_code,
-                        name: data[i].name,
-                        unit: data[i].unit,
-                        unit_price: data[i].price,
-                    };
-                    qd.full_path = parentData.full_path + '-' + qd.ledger_id;
-                    const insertResult = await this.transaction.insert(this.tableName, qd);
-                    newIds.push(qd.id);
-                    if (data[i].pos.length > 0) {
-                        await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
-                        await this.calcNode(qd, this.transaction);
-                    }
-                }
+                await this.transaction.insert(this.tableName, insertBillsData);
+                if (insertPosData.length > 0) await this.transaction.insert(this.ctx.service.pos.tableName, insertPosData);
                 this._cacheMaxLid(tenderId, maxId + data.length);
                 await this.transaction.commit();
             } catch (err) {
@@ -622,52 +641,12 @@ module.exports = app => {
             }
 
             // 查询应返回的结果
-            result.ledger.create = await this.getDataByIds(newIds);
+            result.ledger.create = insertBillsData;
             result.ledger.update = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + data.length);
-            result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
+            result.pos = insertPosData;
             return result;
         }
 
-        /**
-         *
-         * @param node
-         * @param transaction
-         * @return {Promise<void>}
-         * @private
-         */
-        async calcNode(node, transaction) {
-            const info = this.ctx.tender.info;
-            const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
-
-            const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?';
-            const data = await transaction.queryOne(calcQtySql, [this.ctx.service.pos.tableName, node.id]);
-            data.id = node.id;
-            data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
-            data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
-            data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
-            data.quantity = this.round(data.quantity, precision.value);
-            data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
-            data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
-            data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
-            data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
-            const result = await transaction.update(this.tableName, data);
-        }
-
-        /**
-         *
-         * @param {Number} tid - 标段id
-         * @param {Number} id - 需要计算的节点的id
-         * @param {Object} transaction - 操作所属事务,没有则创建
-         * @return {Promise<void>}
-         */
-        async calc(tid, id, transaction) {
-            const node = await transaction.get(this.tableName, { id });
-            if (!node) {
-                throw '数据错误';
-            }
-            await this.calcNode(node, transaction);
-        }
-
         async _importCacheTreeNodes(transaction, nodes) {
             const datas = [];
             for (const node of nodes) {

+ 0 - 52
app/service/pos.js

@@ -454,58 +454,6 @@ module.exports = app => {
         async deletePosData(transaction, tid, lid) {
             await transaction.delete(this.tableName, {tid: tid, lid: lid});
         }
-
-        /**
-         * 复制整块 拷贝部位明细数据
-         * @param {Number} orgLid - 拷贝的部位明细所属台账id
-         * @param {Number} newLid - 新的台账id
-         * @param transaction - 复制整块事务
-         * @returns {Promise<void>}
-         */
-        async copyBillsPosData(lid, transaction) {
-            const lidArr = lid instanceof Array ? lid : [lid];
-            const pasteData = [];
-            for (const id of lidArr) {
-                const posData = await this.getAllDataByCondition({ where: { lid: id.org } });
-                if (posData.length > 0) {
-                    for (const pd of posData) {
-                        pd.id = this.uuid.v4();
-                        pd.lid = id.new;
-                        pd.tid = this.ctx.tender.id;
-                        pd.in_time = new Date();
-                        pasteData.push(pd);
-                    }
-                }
-            }
-            await transaction.insert(this.tableName, pasteData);
-        }
-
-        /**
-         * 批量插入部位数据 - 仅供批量插入清单部位调用
-         * @param transaction - 所属事务
-         * @param {Number} tid - 标段id
-         * @param {Number} lid - 台账id
-         * @param {Array} data - 新增数据
-         * @returns {Promise<void>}
-         */
-        async insertLedgerPosData(transaction, tid, bills, data) {
-            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
-            const insertDatas = [];
-            for (const d of data) {
-                const inD = {
-                    id: this.uuid.v4(), tid: tid, lid: bills.id,
-                    add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
-                    in_time: new Date(), porder: data.indexOf(d) + 1,
-                    name: d.name, drawing_code: d.drawing_code,
-                };
-                if (d.quantity) {
-                    inD.sgfh_qty = this.round(d.quantity, precision.value);
-                    inD.quantity = inD.sgfh_qty;
-                }
-                insertDatas.push(inD);
-            }
-            await transaction.insert(this.tableName, insertDatas);
-        }
     }
 
     return Pos;

+ 5 - 47
app/service/revise_bills.js

@@ -140,11 +140,9 @@ module.exports = app => {
             }
 
             // 查询应返回的结果
-            result.ledger.create = await this.getDataById(newIds);
-            if (!lastChild) {
-                result.ledger.update = await this.getDataById(select.id);
-            }
-            result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
+            result.ledger.create = bills;
+            if (!lastChild) result.ledger.update = await this.getDataById(select.id);
+            result.pos = pos;
             return result;
         }
 
@@ -232,52 +230,12 @@ module.exports = app => {
             }
 
             // 查询应返回的结果
-            result.ledger.create = await this.getDataById(newIds);
+            result.ledger.create = bills;
             result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
-            result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
+            result.pos = pos;
             return result;
         }
 
-        /**
-         *
-         * @param node
-         * @param transaction
-         * @returns {Promise<void>}
-         * @private
-         */
-        async _calcNode(node, transaction) {
-            const info = this.ctx.tender.info;
-            const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
-
-            const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?';
-            const data = await transaction.queryOne(calcQtySql, [this.ctx.service.revisePos.tableName, node.id]);
-            data.id = node.id;
-            data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
-            data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
-            data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
-            data.quantity = this.round(data.quantity, precision.value);
-            data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
-            data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
-            data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
-            data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
-            const result = await transaction.update(this.tableName, data);
-        }
-
-        /**
-         *
-         * @param {Number} tid - 标段id
-         * @param {Number} id - 需要计算的节点的id
-         * @param {Object} transaction - 操作所属事务,没有则创建
-         * @return {Promise<void>}
-         */
-        async calc(tid, id, transaction) {
-            const node = await transaction.get(this.tableName, {id: id});
-            if (!node) {
-                throw '数据错误';
-            }
-            await this._calcNode(node, transaction);
-        }
-
         async sumLoad(lid, rid, tenders) {
             const conn = await this.db.beginTransaction();
             try {

+ 0 - 18
app/service/revise_pos.js

@@ -63,24 +63,6 @@ module.exports = app => {
             return await this.db.query(sql, sqlParam);
         }
 
-        async insertLedgerPosData(transaction, tid, rid, bills, data) {
-            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
-            const insertDatas = [];
-            for (const d of data) {
-                const inD = {
-                    id: this.uuid.v4(), tid: tid, lid: bills.id, crid: rid,
-                    add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
-                    name: d.name, drawing_code: d.drawing_code,
-                };
-                if (d.quantity) {
-                    inD.sgfh_qty = this.round(d.quantity, precision.value);
-                    inD.quantity = inD.sgfh_qty;
-                }
-                insertDatas.push(inD);
-            }
-            await transaction.insert(this.tableName, insertDatas);
-        }
-
         /**
          * 删除清单下部位明细数据(删除清单时调用)
          *

+ 1 - 3
app/service/stage_change.js

@@ -258,9 +258,7 @@ module.exports = app => {
             // 更新数据
             const transaction = await this.db.beginTransaction();
             try {
-                if (newChanges.length > 0) {
-                    await transaction.insert(this.tableName, newChanges);
-                }
+                if (newChanges.length > 0) await transaction.insert(this.tableName, newChanges);
                 for (const c of updateChanges) {
                     await transaction.update(this.tableName, c);
                 }

+ 8 - 27
app/service/stage_pos.js

@@ -360,21 +360,10 @@ module.exports = app => {
 
             const transaction = await this.db.beginTransaction();
             try {
-                if (updatePos.length > 0) {
-                    await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
-                }
-                if (updateBills) {
-                    await transaction.update(this.ctx.service.ledger.tableName, updateBills);
-                }
-                if (updatePosStage.length > 0) {
-                    await transaction.updateRows(this.tableName, updatePosStage);
-                }
-                if (insertPosStage.length > 0) {
-                    await transaction.insert(this.tableName, insertPosStage);
-                }
-                for (const lid of result.ledger) {
-                    await this.ctx.service.stageBills.calc(this.ctx.tender.id, this.ctx.stage.id, lid, transaction);
-                }
+                if (updatePos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, updatePos);
+                if (updateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
+                if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
+                if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
                 await transaction.commit();
                 return result;
             } catch (err) {
@@ -477,18 +466,10 @@ module.exports = app => {
 
             const transaction = await this.db.beginTransaction();
             try {
-                if (updatePosStage.length > 0) {
-                    await transaction.updateRows(this.tableName, updatePosStage);
-                }
-                if (insertPosStage.length > 0) {
-                    await transaction.insert(this.tableName, insertPosStage);
-                }
-                if (updateBillsStage.length > 0) {
-                    await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
-                }
-                if (insertBillsStage.length > 0) {
-                    await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
-                }
+                if (updatePosStage.length > 0) await transaction.updateRows(this.tableName, updatePosStage);
+                if (insertPosStage.length > 0) await transaction.insert(this.tableName, insertPosStage);
+                if (updateBillsStage.length > 0) await transaction.updateRows(this.ctx.service.stageBills.tableName, updateBillsStage);
+                if (insertBillsStage.length > 0) await transaction.insert(this.ctx.service.stageBills.tableName, insertBillsStage);
                 await transaction.commit();
                 return result;
             } catch (err) {