添加了读取excel读取全国省市区到数据库的接口

This commit is contained in:
DESKTOP-G8BCEP0\HP 2021-09-24 15:19:36 +08:00
parent aa3f2560e7
commit 9127969382
6 changed files with 204 additions and 4 deletions

View File

@ -58,6 +58,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers(HttpMethod.POST,"/uploadFileMore").permitAll()
.antMatchers(HttpMethod.POST,"/uploadFile").permitAll()
.antMatchers(HttpMethod.POST,"/insertFireTest").permitAll()
.antMatchers(HttpMethod.POST,"/importCityExcel").permitAll()
// 所有其它请求需要身份认证
.anyRequest().authenticated()
.and()

View File

@ -293,4 +293,18 @@ public class FirePointController {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"添加成功",locale);
}
}
/**
* 导入全国省市区
* @param file
* @return
* @throws Exception
*/
@PostMapping("/importCityExcel")
public String importCityExcel(MultipartFile file) throws Exception {
// 获取区域信息
Locale locale = LocaleContextHolder.getLocale();
firePointService.importCityExcel(file);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"导入成功",locale);
}
}

View File

@ -0,0 +1,13 @@
package com.xkrs.dao;
import com.xkrs.model.entity.Nationwide;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
/**
* @author XinYi Song
*/
@Component
public interface NationwideDao extends JpaRepository<Nationwide,Long> {
}

View File

@ -0,0 +1,137 @@
package com.xkrs.model.entity;
import javax.persistence.*;
/**
* @author XinYi Song
* 全国省市区
*/
@Entity
@Table(name = "nationwide")
public class Nationwide {
/**
* 主键id
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "nationwide_seq_gen")
@SequenceGenerator(name = "nationwide_seq_gen", sequenceName = "nationwide_id_seq",allocationSize = 1)
private Integer id;
/**
* 省名称
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String proName;
/**
* 省编码
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String proCode;
/**
* 市名称
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String cityName;
/**
* 市编码
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String cityCode;
/**
* 区县名称
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String countyName;
/**
* 区县编码
*/
@Column(length = 65, columnDefinition = "varchar(65)")
private String countyCode;
public Nationwide() {
}
public Nationwide(Integer id, String proName, String proCode, String cityName, String cityCode, String countyName, String countyCode) {
this.id = id;
this.proName = proName;
this.proCode = proCode;
this.cityName = cityName;
this.cityCode = cityCode;
this.countyName = countyName;
this.countyCode = countyCode;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getCountyName() {
return countyName;
}
public void setCountyName(String countyName) {
this.countyName = countyName;
}
public String getCountyCode() {
return countyCode;
}
public void setCountyCode(String countyCode) {
this.countyCode = countyCode;
}
@Override
public String toString() {
return "Nationwide{" +
"id=" + id +
", proName='" + proName + '\'' +
", proCode='" + proCode + '\'' +
", cityName='" + cityName + '\'' +
", cityCode='" + cityCode + '\'' +
", countyName='" + countyName + '\'' +
", countyCode='" + countyCode + '\'' +
'}';
}
}

View File

@ -143,4 +143,11 @@ public interface FirePointService {
* @return
*/
FirePoint insertFireTest(FirePointQo firePointQo);
/**
* 导入全国省市区
* @param file
* @throws Exception
*/
void importCityExcel(MultipartFile file) throws Exception;
}

View File

@ -21,10 +21,8 @@ import javax.transaction.Transactional;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@ -69,6 +67,9 @@ public class FirePointServiceImpl implements FirePointService {
@Resource
private FireDao fireDao;
@Resource
private NationwideDao nationwideDao;
/**
* 添加火点信息
* @param firePointQo
@ -457,4 +458,31 @@ public class FirePointServiceImpl implements FirePointService {
log.info("-------发现新火点");
return fireDao.save(firePoint);
}
/**
* 导入全国省市区
* @param file
* @throws Exception
*/
@Transactional(rollbackOn = Exception.class)
@Override
public void importCityExcel(MultipartFile file) throws Exception {
//调用封装好的工具
ExcelImportUtil importUtil = new ExcelImportUtil(file);
//调用导入的方法获取sheet表的内容
List<Map<String, String>> maps = importUtil.readExcelContent();
//导入订单详情表
List<Nationwide> orderDetails = maps.stream().filter(Objects::nonNull).map(map -> {
Nationwide nationwide = new Nationwide();
nationwide.setProName(map.get("pro_name"));
nationwide.setProCode(map.get("pro_code"));
nationwide.setCityName(map.get("city_name"));
nationwide.setCityCode(map.get("city_code"));
nationwide.setCountyName(map.get("town_name"));
nationwide.setCountyCode(map.get("town_code"));
return nationwide;
}).collect(Collectors.toList());
//批量添加到订单详情
nationwideDao.saveAll(orderDetails);
}
}