‘linchangsheng’ 1 هفته پیش
والد
کامیت
31c8f2d05d

+ 4 - 43
mall-server-api/src/main/java/com/gree/mall/manager/controller/workerorder/ESOrderBaseController.java

@@ -43,11 +43,9 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.FileWriter;
 import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Lock;
 
@@ -80,48 +78,11 @@ public class ESOrderBaseController {
         //1.组装查询条件
         ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
         //2.查询要导出的内容
-        zfireParamBean.setPageNum(1);
-        zfireParamBean.setPageSize(10000);
-        IPage<OrderBaseVO> orderBaseVOIPage = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
-        List<OrderBaseVO> records = orderBaseVOIPage.getRecords();
-        if (records.size() == 10000){
-            zfireParamBean.setPageNum(2);
-            zfireParamBean.setPageSize(10000);
-            IPage<OrderBaseVO> orderBaseVOIPage2 = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
 
-            records.addAll(orderBaseVOIPage2.getRecords());
-
-        }
-
-        if (records.size() == 20000){
-            zfireParamBean.setPageNum(3);
-            zfireParamBean.setPageSize(10000);
-            IPage<OrderBaseVO> orderBaseVOIPage3 = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
-
-            records.addAll(orderBaseVOIPage3.getRecords());
-
-        }
-
-        if (records.size() == 30000){
-            zfireParamBean.setPageNum(4);
-            zfireParamBean.setPageSize(10000);
-            IPage<OrderBaseVO> orderBaseVOIPage3 = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
-
-            records.addAll(orderBaseVOIPage3.getRecords());
-
-        }
-
-        if (records.size() == 40000){
-            zfireParamBean.setPageNum(5);
-            zfireParamBean.setPageSize(10000);
-            IPage<OrderBaseVO> orderBaseVOIPage3 = orderBaseEsLogic.orderBaseEsList(zfireParamBean);
-
-            records.addAll(orderBaseVOIPage3.getRecords());
-
-        }
+        List<OrderBaseVO> orderBaseVOIPage = orderBaseEsLogic.listExport(zfireParamBean);
 
         //3.导出
-        FieldUtils.exportData(records, zfireParam.getExportFields(), request, response);
+        FieldUtils.exportData(orderBaseVOIPage, zfireParam.getExportFields(), request, response);
     }
 
     @PostMapping("/status/count")

+ 99 - 1
mall-server-api/src/main/java/com/gree/mall/manager/logic/workorder/OrderBaseEsLogic.java

@@ -27,9 +27,13 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.index.query.BoolQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.search.Scroll;
 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.Aggregations;
@@ -40,6 +44,7 @@ import org.elasticsearch.search.sort.SortOrder;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.elasticsearch.core.*;
 import org.springframework.data.elasticsearch.core.document.Document;
+import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
 import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
 import org.springframework.stereotype.Service;
@@ -130,7 +135,7 @@ public class OrderBaseEsLogic {
         page.setSize(zfireParamBean.getPageSize());
         page.setRecords(BeanUtil.copyToList(orderBaseEs,OrderBaseVO.class));
         //服务单标识
-        if (zfireParamBean.getPageSize() != -1 && page.getTotal() > 0) {
+        if (zfireParamBean.getPageSize() != -1 && page.getTotal() > 0 && page.getRecords().size() > 0) {
             List<String> orderBaseIds = page.getRecords()
                     .stream()
                     .map(OrderBaseVO::getId)
@@ -641,4 +646,97 @@ public class OrderBaseEsLogic {
 
         orderWorkerEsRepository.saveAll(pgOrderWorkerEs);
     }
+
+    public List<OrderBaseVO> listExport(WorkOrderZfireParam zfireParamBean) {
+
+        AdminUserCom adminUser = commonLogic.getAdminUser();
+
+        BoolQueryBuilder queryBuilder1 = QueryBuilders.boolQuery();
+        this.montage(queryBuilder1,zfireParamBean);
+
+        if (CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds())) {
+            queryBuilder1.must(
+                    QueryBuilders.boolQuery().should(QueryBuilders.termsQuery("create_websit_id", adminUser.getAdminWebsitIds()))
+                            .should(QueryBuilders.termsQuery("websit_id", adminUser.getAdminWebsitIds()))
+            );
+
+        }
+
+        List<String> orderBaseId = new ArrayList<>();
+
+        if (zfireParamBean.getIsWait() != null && zfireParamBean.getIsWait()){
+            List<String> collect = workerOrderService.lambdaQuery()
+                    .eq(WorkerOrder::getPayStatus, "WAIT")
+                    .in(CollectionUtils.isNotEmpty(adminUser.getAdminWebsitIds()),WorkerOrder::getWebsitId,  adminUser.getAdminWebsitIds())
+                    .ne(WorkerOrder::getWorkerOrderId, "").select(WorkerOrder::getWorkerOrderId)
+                    .list().stream().map(WorkerOrder::getWorkerOrderId).collect(Collectors.toList());
+
+            if (CollectionUtils.isEmpty(collect))
+                return new ArrayList<>();
+
+            orderBaseId.addAll(collect);
+
+
+        }
+
+        if (!CollectionUtils.isEmpty(orderBaseId)){
+            queryBuilder1.must(QueryBuilders.termsQuery("id",orderBaseId));
+        }
+
+
+
+        NativeSearchQuery query  = new NativeSearchQueryBuilder()
+                .withQuery(queryBuilder1)
+                .withSort(SortBuilders.fieldSort("create_time").order(SortOrder.DESC))
+                .withPageable(PageRequest.of(0, 3000))
+                .build();
+
+
+
+        // 2. 初始化 Scroll
+        SearchScrollHits<OrderBaseEs> scrollHits = elasticsearchRestTemplate.searchScrollStart(
+                60000, // Scroll 上下文保持时间(毫秒)
+                query,
+                OrderBaseEs.class,
+                IndexCoordinates.of("pg_order_base")
+        );
+
+        String scrollId = scrollHits.getScrollId();
+        int batch = 0;
+
+        List<OrderBaseEs> orderBaseVOS = new ArrayList<>();
+
+        try {
+            while (!scrollHits.isEmpty()) {
+                // 3. 处理当前批次数据
+                scrollHits.forEach(hit -> {
+                    OrderBaseEs entity = hit.getContent();
+                    orderBaseVOS.add(entity);
+                });
+
+                batch++;
+
+                // 4. 获取下一批次
+                scrollHits = elasticsearchRestTemplate.searchScrollContinue(
+                        scrollId,
+                        60000,
+                        OrderBaseEs.class,
+                        IndexCoordinates.of("pg_order_base")
+                );
+                scrollId = scrollHits.getScrollId(); // 更新 scrollId
+            }
+        } finally {
+            // 5. 清理 Scroll 上下文
+            if (scrollId != null) {
+                List<String> scroll = new ArrayList<>();
+                scroll.add(scrollId);
+                elasticsearchRestTemplate.searchScrollClear(scroll);
+            }
+        }
+
+        List<OrderBaseVO> orderBaseVOS1 = BeanUtil.copyToList(orderBaseVOS, OrderBaseVO.class);
+
+        return orderBaseVOS1;
+
+    }
 }