瀏覽代碼

数据看板查询速度优化

hu 2 年之前
父節點
當前提交
6198e6faed

+ 2 - 1
src/custom/restcontroller/webmanage/sale/databoard/SQL/订货额类别统计分析.sql

@@ -1 +1,2 @@
-select cast(ifnull(sum(if(t2.isclose,t2.deliedqty*t2.price,t2.amount)),0) AS DECIMAL(16,6)) typestatistics  from sa_order t1 inner join sa_orderitems t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid where t1.siteid=$siteid$ and $where$ and t1.status not in('新建','提交') and exists(select * from sa_itemsaleclass s1 where s1.itemid=t2.itemid  and s1.itemclassid in $itemclassid$)
+select ifnull(t2.itemclassname,'其他') marketingcategory,t1.* from (
+select t3.marketingcategory,sum(cast(ifnull(if(t2.isclose,t2.deliedqty*t2.price,t2.amount),0) AS DECIMAL(16,6))) typestatistics  from sa_order t1 inner join sa_orderitems t2 on t1.sa_orderid=t2.sa_orderid and t1.siteid=t2.siteid left join plm_item t3 on t2.itemid=t3.itemid and t2.siteid=t3.siteid where t1.siteid=$siteid$ and $where$ and t1.status not in('新建','提交') group by t3.marketingcategory) t1 left join plm_itemclass t2 on t1.marketingcategory=t2.itemclassid

+ 27 - 23
src/custom/restcontroller/webmanage/sale/databoard/databoard.java

@@ -9,6 +9,7 @@ import common.annotation.API;
 import common.annotation.CACHEING;
 import common.data.Row;
 import common.data.Rows;
+import common.data.RowsMap;
 import common.data.SQLFactory;
 import org.apache.commons.lang.StringUtils;
 import restcontroller.R;
@@ -543,28 +544,31 @@ public class databoard extends Controller {
         }
 
 
-        Rows topItemclassRows = dbConnect.runSqlQuery("select itemclassid,itemclassname marketingcategory from plm_itemclass where classtype='营销' and siteid='" + siteid + "' and ifnull(parentid,0)=0");
-        for (Row row : topItemclassRows) {
-            ArrayList<Long> itemclassids = ItemClass.getSubItemClassIds(this, row.getLong("itemclassid"));
-            SQLFactory factory = new SQLFactory(this, "订货额类别统计分析");
-            factory.addParameter("siteid", siteid);
-            factory.addParameter_SQL("where", where);
-            factory.addParameter_in("itemclassid", itemclassids);
-            Rows rows = dbConnect.runSqlQuery(factory.getSQL(false));
-
-            SQLFactory factoryLastYear = new SQLFactory(this, "订货额类别统计分析");
-            factoryLastYear.addParameter("siteid", siteid);
-            factoryLastYear.addParameter_SQL("where", whereLastYear);
-            factoryLastYear.addParameter_in("itemclassid", itemclassids);
-            Rows rowsLastYear = dbConnect.runSqlQuery(factoryLastYear.getSQL(false));
+        //Rows topItemclassRows = dbConnect.runSqlQuery("select itemclassid,itemclassname marketingcategory from plm_itemclass where classtype='营销' and siteid='" + siteid + "' and ifnull(parentid,0)=0");
+        SQLFactory factory = new SQLFactory(this, "订货额类别统计分析");
+        factory.addParameter("siteid", siteid);
+        factory.addParameter_SQL("where", where);
+        Rows rows = dbConnect.runSqlQuery(factory.getSQL(false));
+
+        SQLFactory factoryLastYear = new SQLFactory(this, "订货额类别统计分析");
+        factoryLastYear.addParameter("siteid", siteid);
+        factoryLastYear.addParameter_SQL("where", whereLastYear);
+        Rows rowsLastYear = dbConnect.runSqlQuery(factoryLastYear.getSQL(false));
+        RowsMap rowsLastYearMap =rowsLastYear.toRowsMap("marketingcategory");
+
+        for (Row row : rows) {
             if (rows.isNotEmpty()) {
-                row.put("amount", rows.get(0).getBigDecimal("typestatistics"));
+                row.put("amount", row.getBigDecimal("typestatistics"));
             } else {
                 row.put("amount", BigDecimal.ZERO);
             }
-            if (rowsLastYear.isNotEmpty()) {
-                row.put("lastyearamount", rowsLastYear.get(0).getBigDecimal("typestatistics"));
-            } else {
+            if(rowsLastYearMap.containsKey(row.getString("marketingcategory"))){
+                if(rowsLastYearMap.get(row.getString("marketingcategory")).isNotEmpty()){
+                    row.put("lastyearamount", rowsLastYearMap.get(row.getString("marketingcategory")).get(0).getBigDecimal("typestatistics"));
+                }else {
+                    row.put("lastyearamount", BigDecimal.ZERO);
+                }
+            }else{
                 row.put("lastyearamount", BigDecimal.ZERO);
             }
             if (row.getBigDecimal("lastyearamount").compareTo(BigDecimal.ZERO) == 0) {
@@ -573,17 +577,17 @@ public class databoard extends Controller {
                 row.put("yearonyear", (row.getBigDecimal("amount").subtract(row.getBigDecimal("lastyearamount"))).divide(row.getBigDecimal("lastyearamount"), 4, BigDecimal.ROUND_HALF_UP));
             }
         }
-        BigDecimal sumamount = topItemclassRows.sum("amount");
-        for (Row row : topItemclassRows) {
+        BigDecimal sumamount = rows.sum("amount");
+        for (Row row : rows) {
             if (sumamount.compareTo(BigDecimal.ZERO) == 0) {
                 row.put("proportion", BigDecimal.ZERO);
             } else {
                 row.put("proportion", row.getBigDecimal("amount").divide(sumamount, 4, BigDecimal.ROUND_HALF_UP));
             }
         }
-        topItemclassRows.sort(Comparator.comparingDouble(o ->o.getDouble("amount")));
-        Collections.reverse(topItemclassRows);
-        return getSucReturnObject().setData(topItemclassRows).toString();
+        rows.sort(Comparator.comparingDouble(o ->o.getDouble("amount")));
+        Collections.reverse(rows);
+        return getSucReturnObject().setData(rows).toString();
     }
 
     /**