liding
10 小时以前 94509204d25f7c0ad213ae2322be2bd5bfd17424
ruoyi-admin/src/main/java/com/ruoyi/web/controller/init/MyStartupRunner.java
@@ -1,12 +1,15 @@
package com.ruoyi.web.controller.init;
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.District;
import com.ruoyi.basic.entity.Province;
import com.ruoyi.basic.mapper.CityMapper;
import com.ruoyi.basic.mapper.DistrictMapper;
import com.ruoyi.basic.mapper.ProvinceMapper;
import com.ruoyi.basic.mapper.*;
import com.ruoyi.basic.service.CoalFieldService;
import com.ruoyi.basic.service.CoalPlanService;
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;
@@ -16,6 +19,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Slf4j
@Component
@@ -26,6 +33,10 @@
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private CoalFieldService coalFieldService;
    @Autowired
    private CoalPlanService coalPlanService;
    /**
     * 项目启动时需要执行的方法
@@ -37,6 +48,8 @@
    public void run(String... args) throws Exception {
        // 初始化省市区
        initRegion();
        // 初始新增配煤计算器中的煤种字段和方案
        initCoalFields();
    }
    private void initRegion() {
@@ -96,14 +109,74 @@
                });
                provinceMapper.insertOrUpdate(provinces);
                cityMapper.insertOrUpdate(cities);
                districtMapper.insertOrUpdate(districts);
                provinceMapper.insert(provinces);
                cityMapper.insert(cities);
                districtMapper.insert(districts);
                log.info("地区信息初始化结束");
            }
        }
    }
    private void initCoalFields() {
        try {
            // 1. 初始化字段逻辑
            Supplier<CoalFieldDto> dtoSupplier = CoalFieldDto::new; // 假设存在无参构造函数
            List<CoalFieldDto> fields = Stream.of("发热量", "硫分", "灰分", "水分")
                    .map(fieldName -> {
                        CoalFieldDto dto = dtoSupplier.get();
                        dto.setFieldName(fieldName); // 假设存在 setFieldName 方法
                        dto.setFieldDescription("配煤计算器媒质字段");
                        return dto;
                    })
                    .toList();
            // 2. 保存字段(如果不存在)
            Set<String> existingNames = coalFieldService.getFieldNamesByNames(
                    fields.stream().map(CoalFieldDto::getFieldName).collect(Collectors.toSet())
            );
            fields.forEach(field -> {
                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("初始化配煤方案失败,请稍后重试");
        }
    }
}