liding
12 小时以前 94509204d25f7c0ad213ae2322be2bd5bfd17424
ruoyi-admin/src/main/java/com/ruoyi/web/controller/init/MyStartupRunner.java
@@ -2,14 +2,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.basic.dto.CoalFieldDto;
import com.ruoyi.basic.dto.CoalPlanDto;
import com.ruoyi.basic.entity.City;
import com.ruoyi.basic.entity.CoalField;
import com.ruoyi.basic.entity.District;
import com.ruoyi.basic.entity.Province;
import com.ruoyi.basic.mapper.*;
import com.ruoyi.basic.service.CoalFieldService;
import com.ruoyi.basic.service.CoalPlanService;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.web.controller.init.dto.AreaDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -18,9 +18,9 @@
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -48,7 +48,7 @@
    public void run(String... args) throws Exception {
        // 初始化省市区
        initRegion();
        // 初始新增配煤计算器中的字段和方案
        // 初始新增配煤计算器中的煤种字段和方案
        initCoalFields();
    }
@@ -120,19 +120,63 @@
    }
    private void initCoalFields() {
        try {
            // 1. 初始化字段逻辑
            Supplier<CoalFieldDto> dtoSupplier = CoalFieldDto::new; // 假设存在无参构造函数
        List<CoalFieldDto> fields = Stream.of("发热量", "硫分", "灰分", "水分")
                .map(CoalFieldDto::from)
                    .map(fieldName -> {
                        CoalFieldDto dto = dtoSupplier.get();
                        dto.setFieldName(fieldName); // 假设存在 setFieldName 方法
                        dto.setFieldDescription("配煤计算器媒质字段");
                        return dto;
                    })
                .toList();
        // 批量获取已存在的 fieldNames
        Set<String> existingFieldNames = coalFieldService.getFieldNamesByNames(
            // 2. 保存字段(如果不存在)
            Set<String> existingNames = coalFieldService.getFieldNamesByNames(
                fields.stream().map(CoalFieldDto::getFieldName).collect(Collectors.toSet())
        );
        fields.forEach(field -> {
            if (!existingFieldNames.contains(field.getFieldName())) {
                if (!existingNames.contains(field.getFieldName())) {
                coalFieldService.addOrEditCoalField(field);
            }
        });
            // 3. 查询所有字段ID
            List<CoalFieldDto> allFields = coalFieldService.getFieldsByNames(
                    fields.stream().map(CoalFieldDto::getFieldName).collect(Collectors.toSet())
            );
            // 4. 创建方案
            String planName = "配煤计算器方案";
            boolean planExists = coalPlanService.checkPlanExists(allFields.stream()
                    .map(f -> f.getId().toString())
                    .collect(Collectors.joining(",")));
            if (!planExists) {
                // 创建新方案
                CoalPlanDto planDto = new CoalPlanDto();
                planDto.setPlan(planName);
                planDto.setCoalFields(allFields.stream()
                        .map(CoalFieldDto::getFields)
                        .collect(Collectors.joining(",")));
                planDto.setFieldIds(allFields.stream()
                        .map(f -> f.getId().toString())
                        .collect(Collectors.joining(",")));
                planDto.setSchemeDesc("系统生成配煤计算器所需字段方案");
                coalPlanService.addOrEditCoalPlan(planDto);
                log.info("成功创建配煤计算器方案");
            } else {
                log.info("配煤计算器方案已存在,跳过创建");
            }
        } catch (Exception e) {
            // 记录系统异常日志,并转换为业务异常抛出(触发回滚)
            log.error("配煤计算器初始化失败", e);
            throw new BaseException("初始化配煤方案失败,请稍后重试");
        }
    }
}