From f68d79d0ff6658795c19c2fd473fab9ff6f0640d Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期六, 14 三月 2026 13:53:12 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_宁夏_中盛建材' into dev_宁夏_中盛建材

---
 src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java |  284 +++++++++++++++++++++++++++-----------------------------
 1 files changed, 136 insertions(+), 148 deletions(-)

diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
index b1dd5b4..678cfd2 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
@@ -1,14 +1,16 @@
 package com.ruoyi.production.service.impl;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.http.HttpUtils;
 import com.ruoyi.framework.config.AliDingConfig;
+import com.ruoyi.framework.util.AliDingUtils;
+import com.ruoyi.production.dto.ProductMaterialDto;
+import com.ruoyi.production.dto.ProductMaterialGroupDto;
 import com.ruoyi.production.enums.MaterialConfigTypeEnum;
 import com.ruoyi.production.mapper.ProductMaterialMapper;
 import com.ruoyi.production.pojo.ProductMaterial;
@@ -22,14 +24,13 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
-import java.time.OffsetDateTime;
-import java.time.ZoneId;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
 
 /**
  * <br>
@@ -81,126 +82,52 @@
         }
 
         try {
-            // 鑾峰彇 AccessToken
-            String accessToken = getAccessToken();
-            if (StringUtils.isEmpty(accessToken)) {
+
+
+            JSONArray searchConditions = new JSONArray();
+            JSONObject statusCondition = new JSONObject();
+            statusCondition.put("key", "processInstanceStatus");
+            JSONArray statusValueArray = new JSONArray();
+            statusValueArray.add("COMPLETED");
+            statusCondition.put("value", statusValueArray);
+            statusCondition.put("type", "ARRAY");
+            statusCondition.put("operator", "in");
+            statusCondition.put("componentName", "SelectField");
+            searchConditions.add(statusCondition);
+
+            JSONObject resultCondition = new JSONObject();
+            resultCondition.put("key", "processApprovedResult");
+            JSONArray resultValueArray = new JSONArray();
+            resultValueArray.add("agree");
+            resultCondition.put("value", resultValueArray);
+            resultCondition.put("type", "ARRAY");
+            resultCondition.put("operator", "in");
+            resultCondition.put("componentName", "SelectField");
+            searchConditions.add(resultCondition);
+
+            String searchFieldJson = searchConditions.toJSONString();
+
+            JSONArray dataArr = AliDingUtils.getFormDataList(aliDingConfig, aliDingConfig.getMaterialCodeFormUuid(), searchFieldJson, productMaterialSkuService, ProductMaterialSku::getFormModifiedTime);
+
+            if (dataArr.isEmpty()) {
+                log.info("娌℃湁鏇村鏂版暟鎹渶瑕佸悓姝�");
                 return;
             }
 
-            // 鑾峰彇鏈湴鏈�鍚庡悓姝ユ椂闂�
-            LocalDateTime lastSyncTime = getLastSyncTime();
-            log.info("寮�濮嬬墿鏂欑紪鐮佸閲忓悓姝ワ紝鏈湴鏈�鍚庝慨鏀规椂闂�: {}", lastSyncTime);
-
-            int pageNumber = 1;
-            int pageSize = 50;
-            boolean hasMore = true;
-            int totalSynced = 0;
-
-            while (hasMore) {
-                // 鏌ヨ鍙傛暟
-                JSONObject searchParam = buildSearchParam(lastSyncTime, pageNumber, pageSize);
-
-                // 璋冪敤瀹滄惌鎺ュ彛鎷夊彇鏁版嵁
-                String dataRes = HttpUtils.sendPostJson(aliDingConfig.getSearchFormDataUrl(), searchParam.toJSONString(), StandardCharsets.UTF_8.name(), null, accessToken);
-
-                if (StringUtils.isEmpty(dataRes)) {
-                    log.warn("绗� {} 椤垫媺鍙栨暟鎹负绌�", pageNumber);
-                    break;
-                }
-
-                JSONObject resultObj = JSON.parseObject(dataRes);
-                JSONArray dataArr = resultObj.getJSONArray("data");
-                Integer totalCount = resultObj.getInteger("totalCount");
-
-                if (dataArr == null || dataArr.isEmpty()) {
-                    log.info("娌℃湁鏇村鏂版暟鎹渶瑕佸悓姝�");
-                    break;
-                }
-
-                // 瑙f瀽骞朵繚瀛樻暟鎹�
-                List<ProductMaterialSku> list = parseProductMaterials(dataArr, totalCount);
-                if (!list.isEmpty()) {
-                    // 澶勭悊鏇存柊鎴栨柊澧�
-                    int affected = processSaveOrUpdate(list);
-                    totalSynced += affected;
-                }
-
-                // 鍒ゆ柇鏄惁杩樻湁涓嬩竴椤�
-                hasMore = (pageNumber * pageSize) < totalCount;
-                pageNumber++;
-
-                log.info("姝e湪鍚屾绗� {} 椤碉紝褰撳墠宸插悓姝� {}/{}", pageNumber - 1, totalSynced, totalCount);
+            // 瑙f瀽骞朵繚瀛樻暟鎹�
+            List<ProductMaterialSku> list = parseProductMaterials(dataArr, dataArr.size());
+            if (!list.isEmpty()) {
+                // 澶勭悊鏇存柊鎴栨柊澧�
+                int affected = processSaveOrUpdate(list);
+                log.info("鐗╂枡鏁版嵁鍚屾瀹屾垚锛屽叡鍚屾 {} 鏉℃暟鎹�", affected);
             }
 
-            log.info("鐗╂枡鏁版嵁鍚屾瀹屾垚锛屽叡鍚屾 {} 鏉℃暟鎹�", totalSynced);
         } catch (Exception e) {
             log.error("鍚屾鐗╂枡缂栫爜寮傚父", e);
         } finally {
             // 閲婃斁閿�
             syncLock.unlock();
         }
-    }
-
-    private String getAccessToken() {
-        String params = "appkey=" + aliDingConfig.getAppKey() + "&appsecret=" + aliDingConfig.getAppSecret();
-        String tokenRes = HttpUtils.sendGet(aliDingConfig.getAccessTokenUrl(), params);
-        JSONObject tokenObj = JSON.parseObject(tokenRes);
-        String accessToken = tokenObj.getString("access_token");
-        if (StringUtils.isEmpty(accessToken)) {
-            log.error("鑾峰彇閽夐拤AccessToken澶辫触: {}", tokenRes);
-        }
-        return accessToken;
-    }
-
-    private LocalDateTime getLastSyncTime() {
-        LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.orderByDesc(ProductMaterialSku::getFormModifiedTime).last("LIMIT 1");
-        ProductMaterialSku lastRecord = productMaterialSkuService.getOne(queryWrapper);
-        return lastRecord != null ? lastRecord.getFormModifiedTime() : null;
-    }
-
-    private JSONObject buildSearchParam(LocalDateTime lastSyncTime, int pageNumber, int pageSize) {
-        JSONObject searchParam = new JSONObject();
-        searchParam.put("appType", aliDingConfig.getAppType());
-        searchParam.put("systemToken", aliDingConfig.getSystemToken());
-        searchParam.put("userId", aliDingConfig.getUserId());
-        searchParam.put("formUuid", aliDingConfig.getMaterialCodeFormUuid());
-        searchParam.put("currentPage", pageNumber);
-        searchParam.put("pageSize", pageSize);
-
-        JSONArray searchConditions = new JSONArray();
-        JSONObject statusCondition = new JSONObject();
-        statusCondition.put("key", "processInstanceStatus");
-        JSONArray statusValueArray = new JSONArray();
-        statusValueArray.add("COMPLETED");
-        statusCondition.put("value", statusValueArray);
-        statusCondition.put("type", "ARRAY");
-        statusCondition.put("operator", "in");
-        statusCondition.put("componentName", "SelectField");
-        searchConditions.add(statusCondition);
-
-        JSONObject resultCondition = new JSONObject();
-        resultCondition.put("key", "processApprovedResult");
-        JSONArray resultValueArray = new JSONArray();
-        resultValueArray.add("agree");
-        resultCondition.put("value", resultValueArray);
-        resultCondition.put("type", "ARRAY");
-        resultCondition.put("operator", "in");
-        resultCondition.put("componentName", "SelectField");
-        searchConditions.add(resultCondition);
-
-        searchParam.put("searchFieldJson", searchConditions.toJSONString());
-        searchParam.put("orderConfigJson", "{\"gmt_modified\":\"+\"}");
-
-        if (lastSyncTime != null) {
-            String startTime = lastSyncTime.plusSeconds(1).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-            searchParam.put("modifiedFromTimeGMT", startTime);
-        }
-
-        String endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-        searchParam.put("modifiedToTimeGMT", endTime);
-
-        return searchParam;
     }
 
     private List<ProductMaterialSku> parseProductMaterials(JSONArray dataArr, Integer totalCount) {
@@ -237,7 +164,7 @@
             sku.setSupplyType(formData.getString("selectField_la14k51j"));
             sku.setOriginatorName(originatorName);
             sku.setOriginatorOrg("瀹佸涓垱缁胯兘瀹炰笟闆嗗洟鏈夐檺鍏徃");
-            sku.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT")));
+            sku.setFormModifiedTime(AliDingUtils.parseUtcTime(item.getString("modifiedTimeGMT")));
             sku.setCreateTime(now);
             sku.setUpdateTime(now);
 
@@ -301,10 +228,17 @@
 
         for (ProductMaterialSku sku : list) {
 
-            ProductMaterialSku exist =
-                    productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
-                            .eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
-                            .eq(ProductMaterialSku::getSpecification, sku.getSpecification()));
+            LambdaQueryWrapper<ProductMaterialSku> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
+                    .eq(ProductMaterialSku::getSpecification, sku.getSpecification());
+
+            if (StringUtils.isNotEmpty(sku.getMaterialCode())) {
+                wrapper.eq(ProductMaterialSku::getMaterialCode, sku.getMaterialCode());
+            } else {
+                wrapper.isNull(ProductMaterialSku::getMaterialCode);
+            }
+
+            ProductMaterialSku exist = productMaterialSkuService.getOne(wrapper);
             if (exist == null) {
                 productMaterialSkuService.save(sku);
                 affected++;
@@ -323,44 +257,98 @@
         return affected;
     }
 
-    private LocalDateTime parseUtcTime(String utcString) {
-        if (StringUtils.isEmpty(utcString)) {
-            return null;
+    @Override
+    public List<ProductMaterialGroupDto> ProductMaterialList(Integer type) {
+        List<ProductMaterialConfig> configList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
+                .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name())
+        );
+        if (CollectionUtils.isEmpty(configList)) {
+            return new ArrayList<>();
         }
-        try {
-            OffsetDateTime odt = OffsetDateTime.parse(utcString);
-            return odt.toLocalDateTime();
-        } catch (DateTimeParseException ex) {
-            log.warn("瑙f瀽鏃堕棿 {} 澶辫触: {}", utcString, ex.getMessage());
-            return null;
+        List<ProductMaterialGroupDto> result = new ArrayList<>();
+        Map<Integer, List<ProductMaterialDto>> materialMap = new HashMap<>();
+        if (type != null && type == 2) {
+            List<ProductMaterial> materialList = this.list(new LambdaQueryWrapper<ProductMaterial>()
+                    .select(
+                            ProductMaterial::getId,
+                            ProductMaterial::getMaterialTypeId,
+                            ProductMaterial::getInventoryCategoryId,
+                            ProductMaterial::getMaterialName
+                    )
+            );
+            materialMap = materialList.stream()
+                    .map(this::convert)
+                    .collect(Collectors.groupingBy(ProductMaterialDto::getMaterialTypeId));
         }
+
+        for (ProductMaterialConfig config : configList) {
+            ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+            dto.setConfigId(config.getId());
+            dto.setConfigName(config.getConfigName());
+            if (type != null && type == 2) {
+                dto.setMaterialList(materialMap.getOrDefault(config.getId(), new ArrayList<>()));
+            }
+            result.add(dto);
+        }
+        return result;
     }
 
+
     @Override
-    public Map<String, List<ProductMaterial>> ProductMaterialList(String materialName) {
+    public List<ProductMaterialGroupDto> productMaterialListByQuery(String materialName, Integer materialTypeId) {
 
-        List<ProductMaterialConfig> materialConfigList =
-                productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
-                        .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
-
-        Map<String, List<ProductMaterial>> productMaterialMap = new HashMap<>();
-        if (materialConfigList == null || materialConfigList.isEmpty()) {
-            return productMaterialMap;
+        if (StringUtils.isEmpty(materialName) && materialTypeId == null) {
+            return new ArrayList<>();
         }
-        for (ProductMaterialConfig materialConfig : materialConfigList) {
-            LambdaQueryWrapper<ProductMaterial> wrapper = new LambdaQueryWrapper<>();
-            wrapper.eq(ProductMaterial::getMaterialTypeId, materialConfig.getId())
-                    .select(ProductMaterial::getId, ProductMaterial::getMaterialName)
-                    .like(materialName != null && !materialName.isEmpty(), ProductMaterial::getMaterialName, materialName);
 
-            List<ProductMaterial> productMaterialList = list(wrapper);
+        LambdaQueryWrapper<ProductMaterial> wrapper = new LambdaQueryWrapper<>();
+        //  鍙煡璇㈤渶瑕佺殑瀛楁鏁版嵁
+        wrapper.select(
+                ProductMaterial::getId,
+                ProductMaterial::getMaterialTypeId,
+                ProductMaterial::getInventoryCategoryId,
+                ProductMaterial::getMaterialName
+        );
+        if (StringUtils.isNotEmpty(materialName)) {
+            wrapper.like(ProductMaterial::getMaterialName, materialName);
+        }
+        if (materialTypeId != null) {
+            wrapper.eq(ProductMaterial::getMaterialTypeId, materialTypeId);
+        }
+        List<ProductMaterial> materials = this.list(wrapper);
+        if (CollectionUtils.isEmpty(materials)) {
+            return new ArrayList<>();
+        }
+        Map<Integer, List<ProductMaterialDto>> map = materials.stream()
+                .map(this::convert)
+                .collect(Collectors.groupingBy(ProductMaterialDto::getMaterialTypeId));
 
-            if (productMaterialList != null && !productMaterialList.isEmpty()) {
-                productMaterialMap.put(materialConfig.getConfigName(), productMaterialList);
+        List<ProductMaterialConfig> configList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
+                .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
+
+        List<ProductMaterialGroupDto> result = new ArrayList<>();
+        for (ProductMaterialConfig config : configList) {
+            List<ProductMaterialDto> dtoList = map.get(config.getId());
+            if (CollectionUtils.isEmpty(dtoList)) {
+                continue;
             }
+            ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+            dto.setConfigId(config.getId());
+            dto.setConfigName(config.getConfigName());
+            dto.setMaterialList(dtoList);
+            result.add(dto);
         }
 
-        return productMaterialMap;
+        return result;
+    }
+
+    private ProductMaterialDto convert(ProductMaterial m) {
+        ProductMaterialDto dto = new ProductMaterialDto();
+        dto.setId(m.getId());
+        dto.setMaterialName(m.getMaterialName());
+        dto.setMaterialTypeId(m.getMaterialTypeId());
+        dto.setInventoryCategoryId(m.getInventoryCategoryId());
+        return dto;
     }
 
     @Override

--
Gitblit v1.9.3