فهرست منبع

导入Excel,优化导入数据库速度

MaiXinRong 6 سال پیش
والد
کامیت
857881e559
4فایلهای تغییر یافته به همراه124 افزوده شده و 53 حذف شده
  1. 50 21
      app/lib/analysis_excel.js
  2. 6 3
      app/public/js/zh_calc.js
  3. 60 19
      app/service/ledger.js
  4. 8 10
      app/service/stage_bills.js

+ 50 - 21
app/lib/analysis_excel.js

@@ -59,6 +59,7 @@ class ImportBaseTree {
             this.tempData.push(node);
         }
         for (const node of this.items) {
+            node.tender_id = this.ctx.tender.id;
             node.children = this.items.filter(function (i) {
                 return i.ledger_pid === node.ledger_id;
             });
@@ -131,6 +132,7 @@ class ImportBaseTree {
      * @returns {*}
      */
     addXmjNode(node) {
+        node.tender_id = this.ctx.tender.id;
         node.children = [];
         if (node.code.split(this.splitChar).length > 1) {
             const temp = this.findTempData(node);
@@ -157,6 +159,7 @@ class ImportBaseTree {
      * @param {Object} node - 工程量清单
      */
     addGclNode(node) {
+        node.tender_id = this.ctx.tender.id;
         node.pos = [];
         if (this.finalXmjNode) {
             return this.addNodeWithParent(node, this.finalXmjNode);
@@ -170,6 +173,10 @@ class ImportBaseTree {
      */
     addPos (pos){
         if (this.finalNode && this.finalNode.pos) {
+            pos.tid = this.ctx.tender.id;
+            pos.add_stage = 0;
+            pos.add_times = 0;
+            pos.add_user = this.ctx.session.sessionUser.accountId;
             this.finalNode.pos.push(pos);
             this.pos.push(pos);
             pos.sgfh_qty = this.ctx.helper.round(pos.sgfh_qty, this.finalPrecision.value);
@@ -202,20 +209,28 @@ class ImportBaseTree {
         for (const node of this.items) {
             if (node.children && node.children.length > 0) { continue; }
             if (!node.pos || node.pos.length === 0) { continue; }
+            // node.sgfh_qty = this.ctx.helper.sum(_.map(node.pos, 'sgfh_qty'));
+            // if (node.sgfh_qty && node.unit_price) {
+            //     node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price),
+            //         this.ctx.tender.info.decimal.tp);
+            // } else {
+            //     node.sgfh_tp = null;
+            // }
+            // node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
+            // if (node.quantity && node.unit_price) {
+            //     node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price),
+            //         this.ctx.tender.info.decimal.tp);
+            // } else {
+            //     node.total_price = null;
+            // }
             node.sgfh_qty = this.ctx.helper.sum(_.map(node.pos, 'sgfh_qty'));
             if (node.sgfh_qty && node.unit_price) {
-                node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price),
-                    this.ctx.tender.info.decimal.tp);
+                node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
             } else {
                 node.sgfh_tp = null;
             }
-            node.quantity = this.ctx.helper.sum(_.map(node.pos, 'quantity'));
-            if (node.quantity && node.unit_price) {
-                node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price),
-                    this.ctx.tender.info.decimal.tp);
-            } else {
-                node.total_price = null;
-            }
+            node.quantity = node.sgfh_qty;
+            node.total_price = node.sgfh_tp;
         }
     }
 }
@@ -265,17 +280,24 @@ class AnalysisExcelTree {
         node.unit_price = this.toNumber(row[this.colsDef.unit_price]);
         node.drawing_code = row[this.colsDef.drawing_code];
         node.memo = row[this.colsDef.memo];
+        // if (node.sgfh_qty && node.unit_price) {
+        //     node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
+        // } else {
+        //     node.sgfh_tp = null;
+        // }
+        // node.quantity = node.sgfh_qty;
+        // if (node.quantity && node.unit_price) {
+        //     node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
+        // } else {
+        //     node.total_price = null;
+        // }
         if (node.sgfh_qty && node.unit_price) {
-            node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
+            node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
         } else {
             node.sgfh_tp = null;
         }
         node.quantity = node.sgfh_qty;
-        if (node.quantity && node.unit_price) {
-            node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
-        } else {
-            node.total_price = null;
-        }
+        node.total_price = node.sgfh_tp;
         return this.cacheTree.addXmjNode(node);
     }
     /**
@@ -294,17 +316,24 @@ class AnalysisExcelTree {
         node.unit_price = this.toNumber(row[this.colsDef.unit_price]);
         node.drawing_code = row[this.colsDef.drawing_code];
         node.memo = row[this.colsDef.memo];
+        // if (node.sgfh_qty && node.unit_price) {
+        //     node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
+        // } else {
+        //     node.sgfh_tp = null;
+        // }
+        // node.quantity = node.sgfh_qty;
+        // if (node.quantity && node.unit_price) {
+        //     node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
+        // } else {
+        //     node.total_price = null;
+        // }
         if (node.sgfh_qty && node.unit_price) {
-            node.sgfh_tp = this.ctx.helper.round(this.ctx.helper.mul(node.sgfh_qty, node.unit_price), this.ctx.tender.info.decimal.tp);
+            node.sgfh_tp = this.ctx.helper.mul(node.sgfh_qty, node.unit_price, this.ctx.tender.info.decimal.tp);
         } else {
             node.sgfh_tp = null;
         }
         node.quantity = node.sgfh_qty;
-        if (node.quantity && node.unit_price) {
-            node.total_price = this.ctx.helper.round(this.ctx.helper.mul(node.quantity, node.unit_price), this.ctx.tender.info.decimal.tp);
-        } else {
-            node.total_price = null;
-        }
+        node.total_price = node.sgfh_tp;
         return this.cacheTree.addGclNode(node);
     }
     /**

+ 6 - 3
app/public/js/zh_calc.js

@@ -65,7 +65,8 @@ const ZhCalc = (function () {
      * @returns {number}
      */
     function add(num1, num2) {
-        return zhBaseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
+        //return zhBaseCalc.add(num1 ? num1 : 0, num2 ? num2: 0);
+        return num1 ? (num2 ? zhBaseCalc.add(num1, num2) : num1) : num2;
     };
     /**
      * 减法 num1 - num2
@@ -83,7 +84,8 @@ const ZhCalc = (function () {
      * @returns {*}
      */
     function mul(num1, num2, digit = 6) {
-        return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
+        //return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
+        return (num1 && num2) ? (Decimal.mul(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
     };
     /**
      * 除法 num1 / num2
@@ -93,7 +95,8 @@ const ZhCalc = (function () {
      */
     function div(num1, num2, digit = 6) {
         if (num2 && !checkZero(num2)) {
-            return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
+            //return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
+            return num1 ? (Decimal.div(num1, num2).toDecimalPlaces(digit).toNumber()) : 0;
         } else {
             return null;
         }

+ 60 - 19
app/service/ledger.js

@@ -1674,16 +1674,11 @@ module.exports = app => {
                             if (row.qtcl_qty === undefined) calcData.qtcl_qty = updateNode.qtcl_qty;
                             if (row.deal_qty === undefined) calcData.deal_qty = updateNode.deal_qty;
                             calcData.quantity = this.ctx.helper.sum([calcData.sgfh_qty, calcData.sjcl_qty, calcData.qtcl_qty]);
-                            calcData.sgfh_tp = this.ctx.helper.round(
-                                this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price), info.decimal.tp);
-                            calcData.sjcl_tp = this.ctx.helper.round(
-                                this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price), info.decimal.tp);
-                            calcData.qtcl_tp = this.ctx.helper.round(
-                                this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price), info.decimal.tp);
-                            calcData.total_price = this.ctx.helper.round(
-                                this.ctx.helper.mul(calcData.quantity, calcData.unit_price), info.decimal.tp);
-                            calcData.deal_tp = this.ctx.helper.round(
-                                this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price), info.decimal.tp);
+                            calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
+                            calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
+                            calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
+                            calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
+                            calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
                         } else if (row.sgfh_tp !== undefined || row.sjcl_tp !== undefined || row.qtcl_tp !== undefined || row.deal_tp !== undefined) {
                             calcData.sgfh_qty = null;
                             calcData.sjcl_qty = null;
@@ -1758,8 +1753,7 @@ module.exports = app => {
                     quantity: gcl.quantity,
                 };
                 child.full_path = parent.full_path + '.' + child.ledger_id;
-                child.total_price = this.ctx.helper.round(
-                    this.ctx.helper.mul(child.unit_price, child.quantity), this.ctx.tender.info.decimal.tp);
+                child.total_price = this.ctx.helper.mul(child.unit_price, child.quantity, this.ctx.tender.info.decimal.tp);
                 tp = this.ctx.helper.add(tp, child.total_price);
                 result.push(child);
                 newIds.push(child.ledger_id);
@@ -1940,10 +1934,10 @@ module.exports = app => {
             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.round(this.ctx.helper.mul(data.sgfh_qty, node.unit_price), info.decimal.tp);
-            data.sjcl_tp = this.round(this.ctx.helper.mul(data.sjcl_qty, node.unit_price), info.decimal.tp);
-            data.qtcl_tp = this.round(this.ctx.helper.mul(data.qtcl_qty, node.unit_price), info.decimal.tp);
-            data.total_price = this.round(this.ctx.helper.mul(data.quantity, node.unit_price), info.decimal.tp);
+            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);
         }
 
@@ -2011,6 +2005,51 @@ module.exports = app => {
             } else if (node.pos && node.pos.length > 0) {
                 await this.ctx.service.pos.insertLedgerPosData(transaction, this.ctx.tender.id, data, node.pos);
             }
+            if (node.pos && node.pos.length > 0) {
+                for (const p of node.pos) {
+                    p.lid = result.insertId;
+                }
+            }
+        }
+
+        async _importCacheTreeNodes(transaction, nodes) {
+            const datas = [];
+            for (const node of nodes) {
+                datas.push({
+                    tender_id: this.ctx.tender.id,
+                    ledger_id: node.ledger_id,
+                    ledger_pid: node.ledger_pid,
+                    level: node.level,
+                    order: node.order,
+                    is_leaf: !node.children || node.children.length === 0,
+                    full_path: node.full_path,
+                    code: node.code,
+                    b_code: node.b_code,
+                    name: node.name,
+                    unit: node.unit,
+                    sgfh_qty: node.sgfh_qty,
+                    sgfh_tp: node.sgfh_tp,
+                    quantity: node.quantity,
+                    unit_price: node.unit_price,
+                    total_price: node.total_price,
+                    dgn_qty1: node.dgn_qty1,
+                    dgn_qty2: node.dgn_qty2,
+                    memo: node.memo,
+                    drawing_code: node.drawing_code,
+                });
+            }
+            await transaction.insert(this.tableName, datas);
+            const sql = 'SELECT id, ledger_id FROM ' + this.tableName + ' WHERE tender_id = ?';
+            const sqlParam = [this.ctx.tender.id];
+            const insertDatas = await transaction.query(sql, sqlParam);
+            for (const iD of insertDatas) {
+                const node = this.ctx.helper._.find(nodes, {ledger_id: iD.ledger_id});
+                if (node.pos && node.pos.length > 0) {
+                    for (const p of node.pos) {
+                        p.lid = iD.id;
+                    }
+                }
+            }
         }
 
         /**
@@ -2029,9 +2068,11 @@ module.exports = app => {
             try {
                 await transaction.delete(this.tableName, {tender_id: this.ctx.tender.id});
                 await transaction.delete(this.ctx.service.pos.tableName, {tid: this.ctx.tender.id});
-                for (const node of cacheTree.roots) {
-                    await this._importCacheTreeNode(transaction, node);
-                }
+                // for (const node of cacheTree.roots) {
+                //     await this._importCacheTreeNode(transaction, node);
+                // }
+                await this._importCacheTreeNodes(transaction, cacheTree.items);
+                await transaction.insert(this.ctx.service.pos.tableName, cacheTree.pos);
                 await transaction.commit();
                 this.cache.set(cacheKey, cacheTree.items.length + 1, 'EX', this.ctx.app.config.cacheTime);
             } catch (err) {

+ 8 - 10
app/service/stage_bills.js

@@ -113,13 +113,11 @@ module.exports = app => {
             const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerData.unit);
             if (insertData.contract_qty) {
                 d.contract_qty = this.round(insertData.contract_qty, precision.value);
-                d.contract_tp = this.round(
-                    this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price), info.decimal.tp);
+                d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerData.unit_price, info.decimal.tp);
             }
             if (insertData.qc_qty) {
                 d.qc_qty = this.round(insertData.qc_qty, precision.value);
-                d.qc_tp = this.round(
-                    this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price), info.decimal.tp);
+                d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerData.unit_price, info.decimal.tp);
             }
             if (insertData.postil) {
                 d.postil = insertData.postil;
@@ -143,11 +141,11 @@ module.exports = app => {
                     const precision = this.ctx.helper.findPrecision(info.precision, ledgerBills.unit);
                     if (d.contract_qty !== undefined) {
                         d.contract_qty = this.round(d.contract_qty, precision.value);
-                        d.contract_tp = this.round(this.ctx.helper.mul(d.contract_qty, ledgerBills.unit_price), info.decimal.tp);
+                        d.contract_tp = this.ctx.helper.mul(d.contract_qty, ledgerBills.unit_price, info.decimal.tp);
                     }
                     if (d.qc_qty !== undefined) {
                         d.qc_qty = this.round(d.qc_qty, precision.value);
-                        d.qc_tp = this.round(this.ctx.helper.mul(d.qc_qty, ledgerBills.unit_price), info.decimal.tp);
+                        d.qc_tp = this.ctx.helper.mul(d.qc_qty, ledgerBills.unit_price, info.decimal.tp);
                     }
                     if (!stageBills) {
                         d.tid = this.ctx.tender.id;
@@ -184,11 +182,11 @@ module.exports = app => {
             const updateData = {};
             if (data.contract_qty) {
                 updateData.contract_qty = this.round(data.contract_qty, precision.value);
-                updateData.contract_tp = this.round(this.ctx.helper.mul(updateData.contract_qty, ledgerBills.unit_price), info.decimal.tp);
+                updateData.contract_tp = this.ctx.helper.mul(updateData.contract_qty, ledgerBills.unit_price, info.decimal.tp);
             }
             if (data.qc_qty) {
                 updateData.qc_qty = this.round(data.qc_qty, precision.value);
-                updateData.qc_tp = this.round(this.ctx.helper.mul(updateData.qc_qty, ledgerBills.unit_price), info.decimal.tp);
+                updateData.qc_tp = this.ctx.helper.mul(updateData.qc_qty, ledgerBills.unit_price, info.decimal.tp);
             }
             if (stageBills) {
                 if ((updateData.contract_qty === undefined || stageBills.contract_qty !== updateData.contract_qty) ||
@@ -226,11 +224,11 @@ module.exports = app => {
             // 计算
             if (posGather.contract_qty !== undefined) {
                 posGather.contract_qty = this.round(posGather.contract_qty, precision.value);
-                posGather.contract_tp = this.round(this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price), info.decimal.tp);
+                posGather.contract_tp = this.ctx.helper.mul(posGather.contract_qty, ledgerBills.unit_price, info.decimal.tp);
             }
             if (posGather.qc_qty !== undefined) {
                 posGather.qc_qty = this.round(posGather.qc_qty, precision.value);
-                posGather.qc_tp = this.round(this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price), info.decimal.tp);
+                posGather.qc_tp = this.ctx.helper.mul(posGather.qc_qty, ledgerBills.unit_price, info.decimal.tp);
             }
             if (stageBills) {
                 if (stageBills.contract_qty === posGather.contract_qty && stageBills.qc_qty === posGather.qc_qty) {