| | |
| | | package com.ruoyi.web.controller.init; |
| | | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | 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.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; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class MyStartupRunner implements CommandLineRunner{ |
| | | public class MyStartupRunner implements CommandLineRunner { |
| | | @Autowired |
| | | private ProvinceMapper provinceMapper; |
| | | @Autowired |
| | | private CityMapper cityMapper; |
| | | @Autowired |
| | | private DistrictMapper districtMapper; |
| | | |
| | | /** |
| | | * 项目启动时需要执行的方法 |
| | | * |
| | | * @param args |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | System.out.println("!!!!!!!!!!!!!!!!!!!!!"); |
| | | // 初始化省市区 |
| | | initRegion(); |
| | | } |
| | | |
| | | 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.insertOrUpdate(provinces); |
| | | cityMapper.insertOrUpdate(cities); |
| | | districtMapper.insertOrUpdate(districts); |
| | | |
| | | log.info("地区信息初始化结束"); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | } |