liding
8 小时以前 94509204d25f7c0ad213ae2322be2bd5bfd17424
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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.*;
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;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
 
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
public class MyStartupRunner implements CommandLineRunner {
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private CoalFieldService coalFieldService;
    @Autowired
    private CoalPlanService coalPlanService;
 
    /**
     * 项目启动时需要执行的方法
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        // 初始化省市区
        initRegion();
        // 初始新增配煤计算器中的煤种字段和方案
        initCoalFields();
    }
 
    private void initRegion() {
        // 判断是是否有省市区信息
        List<Province> olds = provinceMapper.selectList(null);
        if (olds.isEmpty()) {
            // 读取地区json文件
            List<AreaDTO> areaDTOS = null;
            try {
                // 从 resources/doc/ 加载 JSON 文件
                ClassPathResource resource = new ClassPathResource("doc/area.json");
 
                ObjectMapper objectMapper = new ObjectMapper();
                areaDTOS = objectMapper.readValue(
                        resource.getInputStream(),
                        objectMapper.getTypeFactory().constructCollectionType(List.class, AreaDTO.class)
                );
            } catch (Exception e) {
                log.error("读取地区json文件失败:" + e);
            }
 
            List<Province> provinces = new ArrayList<>();
            List<City> cities = new ArrayList<>();
            List<District> districts = new ArrayList<>();
 
            if (!areaDTOS.isEmpty()) {
                log.info("地区信息初始化开始");
 
                // 省
                areaDTOS.forEach(areaDTO -> {
                    Province province = new Province();
                    province.setId(Long.parseLong(areaDTO.getNo()));
                    province.setName(areaDTO.getName());
                    provinces.add(province);
 
                    if (!areaDTO.getChildren().isEmpty()) {
                        // 市
                        areaDTO.getChildren().forEach(cityDTO -> {
                            City city = new City();
                            city.setId(Long.parseLong(cityDTO.getNo()));
                            city.setName(cityDTO.getName());
                            city.setProvinceId(Long.valueOf(areaDTO.getNo()));
                            cities.add(city);
 
                            if (!cityDTO.getChildren().isEmpty()) {
                                // 区
                                cityDTO.getChildren().forEach(districtDTO -> {
                                    District district = new District();
                                    district.setId(Long.parseLong(districtDTO.getNo()));
                                    district.setName(districtDTO.getName());
                                    district.setCityId(Long.valueOf(cityDTO.getNo()));
                                    districts.add(district);
                                });
                            }
                        });
                    }
                });
 
 
                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("初始化配煤方案失败,请稍后重试");
        }
    }
}