gongchunyi
4 天以前 965cd9cc87ee705eb8d6ce259ee33d26291fe0d3
src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
@@ -11,6 +11,7 @@
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.config.AliDingConfig;
import com.ruoyi.productionPlan.dto.ProductionPlanDto;
import com.ruoyi.productionPlan.dto.ProductionPlanSummaryDto;
import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
import com.ruoyi.productionPlan.pojo.ProductionPlan;
import com.ruoyi.productionPlan.service.ProductionPlanService;
@@ -26,9 +27,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>
@@ -133,8 +134,8 @@
                List<ProductionPlan> list = parseProductionPlans(dataArr, dataSyncType, totalCount);
                if (!list.isEmpty()) {
                    //  处理更新或新增
                    processSaveOrUpdate(list);
                    totalSynced += list.size();
                    int affected = processSaveOrUpdate(list);
                    totalSynced += affected;
                }
                //  判断是否还有下一页
@@ -277,19 +278,48 @@
        return list;
    }
    private void processSaveOrUpdate(List<ProductionPlan> list) {
    private int processSaveOrUpdate(List<ProductionPlan> list) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        int affected = 0;
        //  去重 formInstanceId
        Set<String> formIds = list.stream()
                .map(ProductionPlan::getFormInstanceId)
                .collect(Collectors.toSet());
        //  查询数据库已有数据
        List<ProductionPlan> existList = this.list(new LambdaQueryWrapper<ProductionPlan>().in(ProductionPlan::getFormInstanceId, formIds));
        //  Map (formInstanceId + materialCode)
        Map<String, ProductionPlan> existMap = new HashMap<>();
        for (ProductionPlan p : existList) {
            String key = p.getFormInstanceId() + "_" + p.getMaterialCode();
            existMap.put(key, p);
        }
        //  遍历同步数据
        for (ProductionPlan plan : list) {
            LambdaQueryWrapper<ProductionPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductionPlan::getFormInstanceId, plan.getFormInstanceId())
                    .eq(ProductionPlan::getMaterialCode, plan.getMaterialCode());
            ProductionPlan existing = this.getOne(queryWrapper);
            if (existing != null) {
                plan.setId(existing.getId());
                this.updateById(plan);
            } else {
            String key = plan.getFormInstanceId() + "_" + plan.getMaterialCode();
            ProductionPlan exist = existMap.get(key);
            if (exist == null) {
                // 新增
                this.save(plan);
                affected++;
                log.info("新增数据 formInstanceId={}, materialCode={}", plan.getFormInstanceId(), plan.getMaterialCode());
            } else {
                // 判断是否需要更新
                if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(plan.getFormModifiedTime())) {
                    plan.setId(exist.getId());
                    plan.setCreateTime(exist.getCreateTime());
                    this.updateById(plan);
                    affected++;
                    log.info("更新数据 formInstanceId={}, materialCode={}", plan.getFormInstanceId(), plan.getMaterialCode());
                }
            }
        }
        return affected;
    }
    private LocalDateTime parseUtcTime(String utcString) {
@@ -304,4 +334,9 @@
            return null;
        }
    }
    @Override
    public List<ProductionPlanSummaryDto> summaryByProductType(ProductionPlanSummaryDto query) {
        return baseMapper.selectSummaryByProductType(query);
    }
}