From 4c33acfe648e9b008f91c5e2cf446550e6b0174d Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期二, 22 七月 2025 17:35:57 +0800
Subject: [PATCH] 1.正式库导出优化 2.销售导出 3.设备使用状态优化 4.定时任务优化

---
 main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java |  196 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 188 insertions(+), 8 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java
index aa53535..be1daf3 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/OfficialInventoryServiceImpl.java
@@ -15,16 +15,24 @@
 import com.ruoyi.basic.mapper.CoalValueMapper;
 import com.ruoyi.basic.mapper.SupplyMapper;
 import com.ruoyi.business.dto.OfficialInventoryDto;
+import com.ruoyi.business.dto.SalesRecordDto;
 import com.ruoyi.business.entity.OfficialInventory;
+import com.ruoyi.business.entity.SalesRecord;
 import com.ruoyi.business.mapper.OfficialInventoryMapper;
 import com.ruoyi.business.service.OfficialInventoryService;
+import com.ruoyi.business.utils.DynamicExcelUtil;
+import com.ruoyi.business.vo.OfficialInventoryExportVo;
 import com.ruoyi.business.vo.OfficialInventoryVo;
+import com.ruoyi.business.vo.SalesRecordExportVo;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.system.mapper.SysUserMapper;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -43,6 +51,7 @@
  * @since 2025-06-04
  */
 @Service
+@Slf4j
 @RequiredArgsConstructor
 public class OfficialInventoryServiceImpl extends ServiceImpl<OfficialInventoryMapper, OfficialInventory> implements OfficialInventoryService {
 
@@ -355,6 +364,160 @@
     }
 
     @Override
+    public void officialInventoryExport(HttpServletResponse response, OfficialInventoryDto officialInventoryDto) {
+        try {
+            // 鑾峰彇鐓よ川瀛楁閰嶇疆
+            List<CoalField> allCoalFields = coalFieldMapper.selectList(null);
+            List<String> dynamicHeaders = allCoalFields.stream()
+                    .map(CoalField::getFieldName)
+                    .collect(Collectors.toList());
+
+            // 鑾峰彇鏁版嵁
+            List<OfficialInventory> list = officialInventoryDto.getExportIds() != null && !officialInventoryDto.getExportIds().isEmpty()
+                    ? officialInventoryMapper.selectByIds(officialInventoryDto.getExportIds())
+                    : officialInventoryMapper.selectList(null);
+
+            // 杞崲涓哄鍑篤O
+            List<OfficialInventoryExportVo> exportData = convertToExportVo(list, allCoalFields);
+
+            // 浣跨敤澧炲己鐨凟xcel宸ュ叿瀵煎嚭
+            DynamicExcelUtil<OfficialInventoryExportVo> util =
+                    new DynamicExcelUtil<>(OfficialInventoryExportVo.class, dynamicHeaders);
+
+            // 瀵煎嚭Excel
+            util.exportExcel(response, exportData, "搴撳瓨鏁版嵁");
+
+        } catch (Exception e) {
+            log.error("搴撳瓨瀵煎嚭澶辫触", e);
+            // 鍙互鑰冭檻杩斿洖鏇村弸濂界殑閿欒淇℃伅缁欏墠绔�
+            throw new RuntimeException("瀵煎嚭澶辫触: " + e.getMessage());
+        }
+    }
+
+    private List<OfficialInventoryExportVo> convertToExportVo(List<OfficialInventory> list, List<CoalField> coalFields) {
+        if (CollectionUtils.isEmpty(list)) {
+            return Collections.emptyList();
+        }
+
+        // 鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑ID
+        Set<Long> coalIds = list.stream()
+                .map(OfficialInventory::getCoalId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        Set<Long> supplierIds = list.stream()
+                .map(OfficialInventory::getSupplierId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        Set<Long> registrantIds = list.stream()
+                .map(OfficialInventory::getRegistrantId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        Set<Long> planIds = list.stream()
+                .map(OfficialInventory::getCoalPlanId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 鎵归噺鏌ヨ鍏宠仈鏁版嵁
+        Map<Long, CoalInfo> coalInfoMap = getCoalInfoMap(coalIds);
+        Map<Long, Supply> supplyMap = getSupplyMap(supplierIds);
+        Map<Long, SysUser> userMap = getUserMap(registrantIds);
+        Map<Long, List<CoalValue>> coalValuesMap = getCoalValuesMap(planIds);
+
+        // 鏋勫缓瀛楁ID鍒板瓧娈靛悕绉扮殑鏄犲皠锛屾彁楂樻煡鎵炬晥鐜�
+        Map<String, String> fieldIdToNameMap = coalFields.stream()
+                .collect(Collectors.toMap(CoalField::getFields, CoalField::getFieldName));
+
+        // 杞崲鏁版嵁
+        return list.stream().map(record -> {
+            OfficialInventoryExportVo vo = new OfficialInventoryExportVo();
+            vo.initDynamicFields(); // 鍒濆鍖�
+
+            // 璁剧疆鍩虹灞炴��
+            BeanUtils.copyProperties(record, vo);
+
+            // 璁剧疆鍏宠仈淇℃伅
+            setCoalInfo(vo, record.getCoalId(), coalInfoMap);
+            setSupplierInfo(vo, record.getSupplierId(), supplyMap);
+            setUserMapInfo(vo, record.getRegistrantId(), userMap);
+
+            // 鍔ㄦ�佸瓧娈靛鐞�
+            if (record.getCoalPlanId() != null) {
+                List<CoalValue> values = coalValuesMap.getOrDefault(record.getCoalPlanId(), Collections.emptyList());
+                setDynamicFields(vo, values, fieldIdToNameMap);
+            }
+
+            return vo;
+        }).collect(Collectors.toList());
+    }
+
+    private void setCoalInfo(OfficialInventoryExportVo vo, Long coalId, Map<Long, CoalInfo> coalInfoMap) {
+        if (coalId != null && coalInfoMap.containsKey(coalId)) {
+            vo.setCoal(coalInfoMap.get(coalId).getCoal());
+        }
+    }
+
+    private void setSupplierInfo(OfficialInventoryExportVo vo, Long supplierId, Map<Long, Supply> supplyMap) {
+        if (supplierId != null && supplyMap.containsKey(supplierId)) {
+            vo.setSupplierName(supplyMap.get(supplierId).getSupplierName());
+        }
+    }
+
+    private void setUserMapInfo(OfficialInventoryExportVo vo, Long registrantId, Map<Long, SysUser> userMap) {
+        if (registrantId != null && userMap.containsKey(registrantId)) {
+            vo.setRegistrant(userMap.get(registrantId).getNickName());
+        }
+    }
+
+    private void setDynamicFields(OfficialInventoryExportVo vo, List<CoalValue> values, Map<String, String> fieldIdToNameMap) {
+        for (CoalValue value : values) {
+            String fieldName = fieldIdToNameMap.get(value.getFields());
+            if (fieldName != null) {
+                vo.getCoalQualityProperties().put(fieldName, value.getCoalValue());
+            }
+        }
+    }
+
+    private Map<Long, List<CoalValue>> getCoalValuesMap(Set<Long> planIds) {
+        if (CollectionUtils.isEmpty(planIds)) {
+            return Collections.emptyMap();
+        }
+
+        List<CoalValue> allValues = coalValueMapper.selectList(
+                new LambdaQueryWrapper<CoalValue>()
+                        .in(CoalValue::getPlanId, planIds));
+
+        return allValues.stream()
+                .collect(Collectors.groupingBy(CoalValue::getPlanId));
+    }
+
+    private Map<Long, CoalInfo> getCoalInfoMap(Set<Long> coalIds) {
+        if (CollectionUtils.isEmpty(coalIds)) {
+            return Collections.emptyMap();
+        }
+        return coalInfoMapper.selectByIds(coalIds).stream()
+                .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+    }
+
+    private Map<Long, Supply> getSupplyMap(Set<Long> supplierIds) {
+        if (CollectionUtils.isEmpty(supplierIds)) {
+            return Collections.emptyMap();
+        }
+        return supplyMapper.selectByIds(supplierIds).stream()
+                .collect(Collectors.toMap(Supply::getId, Function.identity()));
+    }
+
+    private Map<Long, SysUser> getUserMap(Set<Long> registrantIds) {
+        if (CollectionUtils.isEmpty(registrantIds)) {
+            return Collections.emptyMap();
+        }
+        return sysUserMapper.selectBatchIds(registrantIds).stream()
+                .collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
+    }
+
+    @Override
     public List<OfficialInventoryDto> coalBlendingList() {
         // 1. 鏌ヨ鍩虹搴撳瓨鏁版嵁
         List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(null);
@@ -369,16 +532,33 @@
             planIds.add(inventory.getCoalPlanId());
         });
         // 3. 鎵归噺鏌ヨ鍏宠仈鏁版嵁
-        Map<Long, CoalInfo> coalInfoMap = coalInfoMapper.selectByIds(coalIds).stream()
-                .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+        Map<Long, CoalInfo> coalInfoMap;
+        if (!coalIds.isEmpty()) {
+            coalInfoMap = coalInfoMapper.selectByIds(coalIds).stream()
+                    .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+        } else {
+            coalInfoMap = new HashMap<>();
+        }
 
-        Map<Long, Supply> supplyMap = supplyMapper.selectByIds(supplierIds).stream()
-                .collect(Collectors.toMap(Supply::getId, Function.identity()));
+        Map<Long, Supply> supplyMap;
+        if (!supplierIds.isEmpty()) {
+            supplyMap = supplyMapper.selectByIds(supplierIds).stream()
+                    .collect(Collectors.toMap(Supply::getId, Function.identity()));
+        } else {
+            supplyMap = new HashMap<>();
+            log.warn("supplierIds 涓虹┖锛岃烦杩囨煡璇� Supply");
+        }
 
-        Map<Long, List<CoalValue>> coalValuesMap = coalValueMapper.selectList(
-                        new LambdaQueryWrapper<CoalValue>().in(CoalValue::getPlanId, planIds))
-                .stream()
-                .collect(Collectors.groupingBy(CoalValue::getPlanId));
+        Map<Long, List<CoalValue>> coalValuesMap;
+        if (!planIds.isEmpty()) {
+            coalValuesMap = coalValueMapper.selectList(
+                            new LambdaQueryWrapper<CoalValue>().in(CoalValue::getPlanId, planIds))
+                    .stream()
+                    .collect(Collectors.groupingBy(CoalValue::getPlanId));
+        } else {
+            coalValuesMap = new HashMap<>();
+            log.warn("planIds 涓虹┖锛岃烦杩囨煡璇� CoalValue");
+        }
         // 4. 缁勮DTO
         return officialInventories.stream()
                 .map(inventory -> {

--
Gitblit v1.9.3