‘linchangsheng’ 1 month ago
parent
commit
0d2d8bde83

+ 90 - 63
mall-server-api/src/main/java/com/gree/mall/manager/logic/workorder/OrderBaseCountLogic.java

@@ -17,6 +17,7 @@ import com.gree.mall.manager.plus.service.PgOrderBaseService;
 import io.swagger.models.auth.In;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import lombok.var;
 import org.apache.commons.collections4.CollectionUtils;
 import org.elasticsearch.index.query.BoolQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilder;
@@ -26,15 +27,22 @@ import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.Aggregations;
 import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
 import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter;
+import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
+import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
+import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
+import org.elasticsearch.search.aggregations.bucket.histogram.ParsedDateHistogram;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
 import org.springframework.data.elasticsearch.core.SearchHits;
+import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
 import org.springframework.stereotype.Service;
 import org.springframework.web.bind.annotation.RequestParam;
 
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -194,74 +202,108 @@ public class OrderBaseCountLogic {
         orderStatusQx.add("YCGB");
         orderStatusQx.add("FWQX");
         orderStatusQx.add("FL");
+        orderStatusQx.add("YZP");
 
-        for (int i = 6; i >= 0; i--) {
-            Integer day = i*-1;
+        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
 
-            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
+        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds())) {
+            queryBuilder.must(
+                    QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("create_websit_id", adminUser.getAdminWebsitIds()))
+                            .should(QueryBuilders.termsQuery("websit_id", adminUser.getAdminWebsitIds()))
+            );
+        }
+        queryBuilder.must(
+                QueryBuilders.rangeQuery("create_time")
+                        .gte(DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (-7))),"yyyy-MM-dd HH:mm:ss"))
+                        .lte(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")));
+
+        NativeSearchQuery searchQueryDay = new NativeSearchQueryBuilder()
+                .withQuery(queryBuilder)
+                .withSourceFilter(new FetchSourceFilter(new String[]{"create_time"}, null))
+                .withPageable(PageRequest.of(1, 1))
+                .addAggregation(AggregationBuilders.dateHistogram("xzgd")
+                        .field("create_time")
+                        .calendarInterval(DateHistogramInterval.DAY)
+                        .format("yyyy-MM-dd"))
+                .build();
 
-            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds())) {
-                queryBuilder.must(
-                        QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("create_websit_id", adminUser.getAdminWebsitIds()))
-                                .should(QueryBuilders.termsQuery("websit_id", adminUser.getAdminWebsitIds()))
-                );
-            }
 
+        // 执行查询并获取聚合结果
+        SearchHits<OrderBaseEs> searchHitsXZGD = elasticsearchRestTemplate.search(searchQueryDay, OrderBaseEs.class);
 
-            //新增工单
-            NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
-                    .withQuery(queryBuilder)
-                    .withPageable(PageRequest.of(1, 1))
-                    .addAggregation(this.getAggs(AggregationBuilders.filter("xzgd", QueryBuilders.boolQuery()
-                            .must(QueryBuilders.rangeQuery("create_time")
-                                    .gte(DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss"))
-                                    .lte(DateUtil.format(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss")))
-                    )))
-                    .build();
-            SearchHits response = elasticsearchRestTemplate.search(searchQuery, OrderBaseEs.class);
+        var aggregationsXZGD = searchHitsXZGD.getAggregations();
 
-            xzgdList.add(this.getWorkerOrderCountResult(response.getAggregations(),"xzgd"));
+        if (aggregationsXZGD != null) {
+            ParsedDateHistogram dateHistogram = aggregationsXZGD.get("xzgd");
+            List<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
 
+            // 遍历每天的统计结果
+            for (Histogram.Bucket bucket : buckets) {
+                String date = bucket.getKeyAsString();
+                long count = bucket.getDocCount();
+                xzgdList.add(count);
+            }
+        }
 
 
+        NativeSearchQuery searchQueryWGD = new NativeSearchQueryBuilder()
+                .withQuery(queryBuilder)
+                .withQuery(QueryBuilders.termsQuery("order_status",orderStatus))
+                .withSourceFilter(new FetchSourceFilter(new String[]{"create_time"}, null))
+                .withPageable(PageRequest.of(1, 1))
+                .addAggregation(AggregationBuilders.dateHistogram("WGD")
+                        .field("create_time")
+                        .calendarInterval(DateHistogramInterval.DAY)
+                        .format("yyyy-MM-dd"))
+                .build();
+
 
-            //完工单
-            NativeSearchQuery searchQueryWGD = new NativeSearchQueryBuilder()
-                    .withQuery(queryBuilder)
-                    .withPageable(PageRequest.of(1, 1))
-                    .addAggregation(this.getAggs(AggregationBuilders.filter("wgd", QueryBuilders.boolQuery()
-                            .must(QueryBuilders.termsQuery("order_status",orderStatus))
-                            .must(QueryBuilders.rangeQuery("over_time")
-                                    .gte(DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss"))
-                                    .lte(DateUtil.format(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss")))
-                    )))
-                    .build();
-            SearchHits responseWGD = elasticsearchRestTemplate.search(searchQueryWGD, OrderBaseEs.class);
-            wgdList.add(this.getWorkerOrderCountResult(responseWGD.getAggregations(),"wgd"));
+        // 执行查询并获取聚合结果
+        SearchHits<OrderBaseEs> searchHitsXWGD = elasticsearchRestTemplate.search(searchQueryWGD, OrderBaseEs.class);
 
+        var aggregationsWGD = searchHitsXWGD.getAggregations();
 
+        if (aggregationsWGD != null) {
+            ParsedDateHistogram dateHistogram = aggregationsWGD.get("WGD");
+            List<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
 
+            // 遍历每天的统计结果
+            for (Histogram.Bucket bucket : buckets) {
+                String date = bucket.getKeyAsString();
+                long count = bucket.getDocCount();
+                wgdList.add(count);
+            }
+        }
 
 
-            //待商户派工
-            NativeSearchQuery searchQueryJXZ= new NativeSearchQueryBuilder()
-                    .withQuery(queryBuilder)
-                    .withPageable(PageRequest.of(1, 1))
-                    .addAggregation(this.getAggs(AggregationBuilders.filter("jxz", QueryBuilders.boolQuery()
-                            .mustNot(QueryBuilders.termsQuery("order_status",orderStatus))
-                            .mustNot(QueryBuilders.termsQuery("order_status",orderStatusQx))
-                            .must(QueryBuilders.rangeQuery("create_time")
-                                    .gte(DateUtil.format(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss"))
-                                    .lte(DateUtil.format(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day))),"yyyy-MM-dd HH:mm:ss")))
-                    )))
-                    .build();
-            SearchHits responseJXZ = elasticsearchRestTemplate.search(searchQueryJXZ, OrderBaseEs.class);
+        NativeSearchQuery searchQueryJXZ= new NativeSearchQueryBuilder()
+                .withQuery(queryBuilder)
+                .withQuery(QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery("order_status",orderStatus))
+                        .mustNot(QueryBuilders.termsQuery("order_status",orderStatusQx)))
+                .withSourceFilter(new FetchSourceFilter(new String[]{"create_time"}, null))
+                .withPageable(PageRequest.of(1, 1))
+                .addAggregation(AggregationBuilders.dateHistogram("JXZ")
+                        .field("create_time")
+                        .calendarInterval(DateHistogramInterval.DAY)
+                        .format("yyyy-MM-dd"))
+                .build();
 
 
-            jxzList.add(this.getWorkerOrderCountResult(responseJXZ.getAggregations(),"jxz"));
+        // 执行查询并获取聚合结果
+        SearchHits<OrderBaseEs> searchHitsJXZ= elasticsearchRestTemplate.search(searchQueryJXZ, OrderBaseEs.class);
 
+        var aggregationsJXZ = searchHitsJXZ.getAggregations();
 
+        if (aggregationsJXZ != null) {
+            ParsedDateHistogram dateHistogram = aggregationsWGD.get("JXZ");
+            List<? extends Histogram.Bucket> buckets = dateHistogram.getBuckets();
 
+            // 遍历每天的统计结果
+            for (Histogram.Bucket bucket : buckets) {
+                String date = bucket.getKeyAsString();
+                long count = bucket.getDocCount();
+                jxzList.add(count);
+            }
         }
 
         qtddqs.put("新增工单",xzgdList);
@@ -273,14 +315,6 @@ public class OrderBaseCountLogic {
         for (int i = 0; i < 7; i++) {
             Integer day = i;
 
-            BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
-
-            if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds())) {
-                queryBuilder.must(
-                        QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("create_websit_id", adminUser.getAdminWebsitIds()))
-                                .should(QueryBuilders.termsQuery("websit_id", adminUser.getAdminWebsitIds()))
-                );
-            }
 
 /*            Long qtsm = pgOrderBaseList.stream().filter(item -> item.getAppointmentTime() != null && item.getAppointmentTime().before(DateUtil.endOfDay(DateUtil.offsetDay(new Date(), (day)))) &&
                     item.getAppointmentTime().after(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), (day))))
@@ -324,14 +358,7 @@ public class OrderBaseCountLogic {
                         && item.getOrderTypeText().equals(orderTypeText)
                 ).count();*/
 
-                BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
 
-                if (CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds())) {
-                    queryBuilder.must(
-                            QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("create_websit_id", adminUser.getAdminWebsitIds()))
-                                    .should(QueryBuilders.termsQuery("websit_id", adminUser.getAdminWebsitIds()))
-                    );
-                }
 
                 //工单类型
                 NativeSearchQuery searchQueryGDLX= new NativeSearchQueryBuilder()