ellisran 1 неделя назад
Родитель
Сommit
d82249e7e2
1 измененных файлов с 11 добавлено и 5 удалено
  1. 11 5
      app/service/change_ledger.js

+ 11 - 5
app/service/change_ledger.js

@@ -1485,6 +1485,7 @@ module.exports = app => {
                 updateParent,
                 addResult;
             const expandIds = [];
+            this.newBills = false;
             this.transaction = await this.db.beginTransaction();
             try {
                 // 从最顶层节点依次查询是否存在,否则添加
@@ -1502,7 +1503,12 @@ module.exports = app => {
                             ledger_pid: parent ? parent.ledger_id : rootId,
                             code: stdNode.code,
                             name: stdNode.name,
-                        }) || await this.ctx.service.ledger.getDataByCondition({tender_id: tenderId, ledger_pid: parent ? parent.ledger_id : rootId, code: stdNode.code, name: stdNode.name });
+                        });
+                        if (!node) {
+                            node = await this.ctx.service.ledger.getDataByCondition({tender_id: tenderId, ledger_pid: parent ? parent.ledger_id : rootId, code: stdNode.code, name: stdNode.name });
+                        } else {
+                            this.newBills = true;
+                        }
                         if (!node) {
                             let children = await this.getChildrenByParentId(this.tableName, tenderId, parent.ledger_id);
                             if (!this.newBills) {
@@ -1515,7 +1521,7 @@ module.exports = app => {
                             isNew = true;
                             const newData = this._filterStdData(stdNode);
                             newData.is_leaf = (i === len - 1);
-                            [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData, changeId);
+                            [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData, changeId, this.newBills);
                             if (parent && parent.is_leaf && this.newBills) {
                                 await this.transaction.update(this.tableName, { id: parent.id, is_leaf: false,
                                     unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null});
@@ -1599,7 +1605,7 @@ module.exports = app => {
          * @return {Promise<void>}
          * @private
          */
-        async _addChildAutoOrder(tenderId, parentData, data, changeId) {
+        async _addChildAutoOrder(tenderId, parentData, data, changeId, newBills = this.newBills) {
             const self = this;
             const findPreData = function(list, a) {
                 if (!list || list.length === 0) { return null; }
@@ -1613,14 +1619,14 @@ module.exports = app => {
 
             const pid = parentData ? parentData.ledger_id : rootId;
             let children = await this.getChildrenByParentId(this.tableName, tenderId, pid);
-            if (!this.newBills) {
+            if (!newBills) {
                 const ledgerChildren = await this.getChildrenByParentId(this.ctx.service.ledger.tableName, tenderId, pid);
                 children = this._.orderBy(this._.concat(children, ledgerChildren), [this.setting.order], ['asc']);
             }
             const preData = findPreData(children, data);
             if (!preData || children.indexOf(preData) < children.length - 1) {
                 await this._updateChildrenOrder(this.tableName, tenderId, pid, preData ? preData.order + 1 : 1);
-                if (!this.newBills) {
+                if (!newBills) {
                     await this._updateChildrenOrder(this.ctx.service.ledger.tableName, tenderId, pid, preData ? preData.order + 1 : 1);
                 }
             }