gongchunyi
2 天以前 2d94ddd240fca69ebbc4b0570abb30c803db7754
src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
@@ -4,16 +4,22 @@
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.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;
import com.ruoyi.production.pojo.ProductMaterialConfig;
import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.production.service.ProductMaterialConfigService;
import com.ruoyi.production.service.ProductMaterialService;
import com.ruoyi.production.service.ProductMaterialSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -25,9 +31,9 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
/**
 * <br>
@@ -48,209 +54,137 @@
    @Autowired
    private ProductMaterialConfigService productMaterialConfigService;
    @Autowired
    private ProductMaterialSkuService productMaterialSkuService;
    /**
     * 同步锁,防止手动和定时任务同时执行
     * 同步锁
     */
    private final ReentrantLock syncLock = new ReentrantLock();
    /**
     * config缓存
     */
    private final Map<String, Integer> configCache = new HashMap<>();
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void loadProductMaterialData() {
        syncProductMaterialData(1);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncProductMaterialJob() {
        syncProductMaterialData(2);
    }
    /**
     * 同步数据
     * 同步物料数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void syncProductMaterialData(Integer dataSyncType) {
    public void syncProductMaterialData(Integer syncType) {
        if (!syncLock.tryLock()) {
            log.warn("同步正在进行中,本次 {} 同步请求被跳过", dataSyncType == 1 ? "手动" : "定时任务");
            log.warn("同步任务正在执行,本次请求跳过");
            return;
        }
        try {
            // 获取 AccessToken
            String accessToken = getAccessToken();
            if (StringUtils.isEmpty(accessToken)) {
                return;
            }
            // 获取本地最后同步时间
            LocalDateTime lastSyncTime = getLastSyncTime();
            log.info("开始物料编码增量同步,本地最后修改时间: {}", lastSyncTime);
            log.info("开始同步物料,本地最后时间 {}", lastSyncTime);
            int pageNumber = 1;
            int pageSize = 50;
            int page = 1;
            int size = 50;
            boolean hasMore = true;
            int totalSynced = 0;
            int total = 0;
            while (hasMore) {
                // 查询参数
                JSONObject searchParam = buildSearchParam(lastSyncTime, pageNumber, pageSize);
                // 调用宜搭接口拉取数据
                String dataRes = HttpUtils.sendPostJson(
                JSONObject param = buildSearchParam(lastSyncTime, page, size);
                String res = HttpUtils.sendPostJson(
                        aliDingConfig.getSearchFormDataUrl(),
                        searchParam.toJSONString(),
                        param.toJSONString(),
                        StandardCharsets.UTF_8.name(),
                        null,
                        accessToken
                );
                if (StringUtils.isEmpty(dataRes)) {
                    log.warn("第 {} 页拉取数据为空", pageNumber);
                JSONObject result = JSON.parseObject(res);
                JSONArray dataArr = result.getJSONArray("data");
                Integer totalCount = result.getInteger("totalCount");
                if (CollectionUtils.isEmpty(dataArr)) {
                    break;
                }
                JSONObject resultObj = JSON.parseObject(dataRes);
                JSONArray dataArr = resultObj.getJSONArray("data");
                Integer totalCount = resultObj.getInteger("totalCount");
                if (dataArr == null || dataArr.isEmpty()) {
                    log.info("没有更多新数据需要同步");
                    break;
                }
                // 解析并保存数据
                List<ProductMaterial> list = parseProductMaterials(dataArr, totalCount);
                if (!list.isEmpty()) {
                    // 处理更新或新增
                    int affected = processSaveOrUpdate(list);
                    totalSynced += affected;
                }
                // 判断是否还有下一页
                hasMore = (pageNumber * pageSize) < totalCount;
                pageNumber++;
                log.info("正在同步第 {} 页,当前已同步 {}/{}", pageNumber - 1, totalSynced, totalCount);
                List<ProductMaterialSku> list = parseProductMaterials(dataArr);
                int affected = processSaveOrUpdate(list);
                total += affected;
                hasMore = page * size < totalCount;
                page++;
                log.info("同步进度 {} / {}", total, totalCount);
            }
            log.info("物料数据同步完成,共同步 {} 条数据", totalSynced);
            log.info("物料同步完成 共 {} 条", total);
        } catch (Exception e) {
            log.error("同步物料编码异常", 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<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ProductMaterial::getFormModifiedTime).last("LIMIT 1");
        ProductMaterial lastRecord = this.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<ProductMaterial> parseProductMaterials(JSONArray dataArr, Integer totalCount) {
        List<ProductMaterial> list = new ArrayList<>();
    private List<ProductMaterialSku> parseProductMaterials(JSONArray arr) {
        List<ProductMaterialSku> list = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (int i = 0; i < dataArr.size(); i++) {
            JSONObject item = dataArr.getJSONObject(i);
            String formInstanceId = item.getString("formInstanceId");
            JSONObject originator = item.getJSONObject("originator");
            String originatorName = originator != null && originator.containsKey("userName")
                    ? originator.getJSONObject("userName").getString("nameInChinese") : "未知";
        for (int i = 0; i < arr.size(); i++) {
            JSONObject item = arr.getJSONObject(i);
            JSONObject formData = item.getJSONObject("formData");
            String materialName = formData.getString("textField_l92f36f5");
            ProductMaterial material = new ProductMaterial();
            material.setFormInstanceId(formInstanceId);
            material.setIdentifierCode(formData.getString("textField_l92h77ju"));
            material.setMaterialCode(formData.getString("textField_l92f36f2"));
            material.setMaterialName(formData.getString("textField_l92f36f5"));
            material.setSpecification(formData.getString("textField_l92f36f6"));
            material.setMaterialName(materialName);
            material.setBaseUnit(formData.getString("textField_la147lnw"));
            material.setMaterialAttribute(formData.getString("selectField_la14k51j"));
            material.setFinishedProductName(formData.getString("radioField_lbkk2nn2"));
            material.setRemark(formData.getString("textareaField_l92f36f9"));
            // 处理物料类型和存货类别
            String materialType = formData.getString("selectField_l92f36fb");
            String inventoryCat = formData.getString("selectField_la154noy");
            material.setMaterialTypeId(getOrCreateConfigId(materialType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
            material.setInventoryCategoryId(getOrCreateConfigId(inventoryCat, MaterialConfigTypeEnum.INVENTORY_CAT.name()));
            material.setOriginatorName(originatorName);
            material.setOriginatorOrg("宁夏中创绿能实业集团有限公司");
            material.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT")));
            material.setCreateTime(now);
            material.setUpdateTime(now);
            list.add(material);
            String type = formData.getString("selectField_l92f36fb");
            String inventory = formData.getString("selectField_la154noy");
            material.setMaterialTypeId(getOrCreateConfigId(type, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
            material.setInventoryCategoryId(getOrCreateConfigId(inventory, MaterialConfigTypeEnum.INVENTORY_CAT.name()));
            Long materialId = getOrCreateMaterial(material);
            ProductMaterialSku sku = new ProductMaterialSku();
            sku.setMaterialId(materialId);
            sku.setMaterialCode(formData.getString("textField_l92f36f2"));
            sku.setSpecification(formData.getString("textField_l92f36f6"));
            sku.setIdentifierCode(formData.getString("textField_l92h77ju"));
            sku.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT")));
            sku.setCreateTime(now);
            sku.setUpdateTime(now);
            list.add(sku);
        }
        return list;
    }
    private Long getOrCreateMaterial(ProductMaterial material) {
        ProductMaterial exist = this.getOne(
                new LambdaQueryWrapper<ProductMaterial>()
                        .eq(ProductMaterial::getMaterialName, material.getMaterialName())
        );
        if (exist == null) {
            material.setCreateTime(LocalDateTime.now());
            material.setUpdateTime(LocalDateTime.now());
            this.save(material);
            return material.getId();
        }
        return exist.getId();
    }
    private Integer getOrCreateConfigId(String name, String type) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        String key = type + "_" + name;
        if (configCache.containsKey(key)) {
            return configCache.get(key);
        }
        ProductMaterialConfig config = productMaterialConfigService.getOne(new LambdaQueryWrapper<ProductMaterialConfig>()
                .eq(ProductMaterialConfig::getConfigName, name)
@@ -261,46 +195,235 @@
            config.setConfigType(type);
            productMaterialConfigService.save(config);
        }
        configCache.put(key, config.getId());
        return config.getId();
    }
    private int processSaveOrUpdate(List<ProductMaterial> list) {
        if (list == null || list.isEmpty()) {
    private int processSaveOrUpdate(List<ProductMaterialSku> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        int affected = 0;
        for (ProductMaterialSku sku : list) {
        for (ProductMaterial material : list) {
            ProductMaterial exist = this.getOne(new LambdaQueryWrapper<ProductMaterial>()
                    .eq(ProductMaterial::getFormInstanceId, material.getFormInstanceId()));
            ProductMaterialSku exist = productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
                    .eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
                    .eq(ProductMaterialSku::getSpecification, sku.getSpecification()));
            if (exist == null) {
                this.save(material);
                productMaterialSkuService.save(sku);
                affected++;
                log.info("新增物料数据 formInstanceId={}", material.getFormInstanceId());
            } else {
                if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(material.getFormModifiedTime())) {
                    material.setId(exist.getId());
                    material.setCreateTime(exist.getCreateTime());
                    this.updateById(material);
                    affected++;
                    log.info("更新物料数据 formInstanceId={}", material.getFormInstanceId());
                }
            } else if (!Objects.equals(exist.getFormModifiedTime(), sku.getFormModifiedTime())) {
                sku.setId(exist.getId());
                sku.setCreateTime(exist.getCreateTime());
                productMaterialSkuService.updateById(sku);
                affected++;
            }
        }
        return affected;
    }
    private LocalDateTime parseUtcTime(String utcString) {
        if (StringUtils.isEmpty(utcString)) {
    @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<>();
        }
        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 List<ProductMaterialGroupDto> productMaterialListByQuery(String materialName, Integer materialTypeId) {
        if (StringUtils.isEmpty(materialName) && materialTypeId == null) {
            return new ArrayList<>();
        }
        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));
        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 result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProductMaterial(ProductMaterial productMaterial) {
        validateProductMaterial(productMaterial, false);
        if (existsMaterialName(productMaterial.getMaterialName(), null)) {
            throw new ServiceException("物料名称已存在");
        }
        LocalDateTime now = LocalDateTime.now();
        if (productMaterial.getCreateTime() == null) {
            productMaterial.setCreateTime(now);
        }
        productMaterial.setUpdateTime(now);
        if (!this.save(productMaterial)) {
            throw new ServiceException("新增物料失败");
        }
        log.info("新增物料成功 materialName={}", productMaterial.getMaterialName());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductMaterial(ProductMaterial productMaterial) {
        validateProductMaterial(productMaterial, true);
        ProductMaterial exist = this.getById(productMaterial.getId());
        if (exist == null) {
            throw new ServiceException("物料不存在");
        }
        if (existsMaterialName(productMaterial.getMaterialName(), productMaterial.getId())) {
            throw new ServiceException("物料名称已存在");
        }
        productMaterial.setUpdateTime(LocalDateTime.now());
        if (!this.updateById(productMaterial)) {
            throw new ServiceException("修改物料失败");
        }
        log.info("修改物料成功 id={}", productMaterial.getId());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductMaterial(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择至少一条数据");
        }
        if (!this.removeByIds(ids)) {
            throw new ServiceException("删除物料失败");
        }
        log.info("删除物料成功 ids={}", ids);
    }
    private void validateProductMaterial(ProductMaterial productMaterial, boolean requireId) {
        if (productMaterial == null) {
            throw new ServiceException("参数不能为空");
        }
        if (requireId && productMaterial.getId() == null) {
            throw new ServiceException("主键ID不能为空");
        }
        if (StringUtils.isEmpty(productMaterial.getMaterialName())) {
            throw new ServiceException("物料名称不能为空");
        }
    }
    private boolean existsMaterialName(String materialName, Long excludeId) {
        if (StringUtils.isEmpty(materialName)) {
            return false;
        }
        LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductMaterial::getMaterialName, materialName);
        if (excludeId != null) {
            queryWrapper.ne(ProductMaterial::getId, excludeId);
        }
        return this.count(queryWrapper) > 0;
    }
    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;
    }
    private LocalDateTime parseUtcTime(String utc) {
        if (StringUtils.isEmpty(utc)) {
            return null;
        }
        try {
            OffsetDateTime odt = OffsetDateTime.parse(utcString);
            return odt.toLocalDateTime();
        } catch (DateTimeParseException ex) {
            log.warn("解析时间 {} 失败: {}", utcString, ex.getMessage());
            return OffsetDateTime.parse(utc).toLocalDateTime();
        } catch (DateTimeParseException e) {
            log.warn("时间解析失败 {}", utc);
            return null;
        }
    }
}
    private String getAccessToken() {
        String params = "appkey=" + aliDingConfig.getAppKey() + "&appsecret=" + aliDingConfig.getAppSecret();
        String res = HttpUtils.sendGet(aliDingConfig.getAccessTokenUrl(), params);
        JSONObject obj = JSON.parseObject(res);
        return obj.getString("access_token");
    }
    private LocalDateTime getLastSyncTime() {
        ProductMaterialSku last = productMaterialSkuService.getOne(new LambdaQueryWrapper<ProductMaterialSku>()
                .orderByDesc(ProductMaterialSku::getFormModifiedTime)
                .last("limit 1"));
        return last == null ? null : last.getFormModifiedTime();
    }
    private JSONObject buildSearchParam(LocalDateTime lastTime, int page, int size) {
        JSONObject obj = new JSONObject();
        obj.put("appType", aliDingConfig.getAppType());
        obj.put("systemToken", aliDingConfig.getSystemToken());
        obj.put("formUuid", aliDingConfig.getMaterialCodeFormUuid());
        obj.put("currentPage", page);
        obj.put("pageSize", size);
        return obj;
    }
}