excel批量导入 --renpc

This commit is contained in:
Renpc-kilig 2023-11-16 17:26:04 +08:00
parent 6c17114a5a
commit 01d006b60c
9 changed files with 316 additions and 60 deletions

View File

@ -0,0 +1,41 @@
package com.cm.population.controller.api.residential;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cm.population.PopulationApplication;
import com.cm.population.pojo.vos.residential.ReadExcelResidentialModel;
import com.cm.population.service.building.IBuildingService;
import com.cm.population.service.building.impl.BuildingServiceImpl;
import com.cm.population.service.residential.IResidentialService;
import com.cm.population.service.residential.impl.ResidentialServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.List;
public class ReadExcelController {
public static void main(String[] args) {
String fileName = "C:\\Users\\29492\\Desktop\\稀土路街道小区楼宇批量导入模板.xlsx";
List<Object> list = EasyExcel.read(fileName, ReadExcelResidentialModel.class,
new AnalysisEventListener<ReadExcelResidentialModel>() {
@Override
public void invoke(ReadExcelResidentialModel data, AnalysisContext context) {
if (!data.getCommunity().equals("社区")) {
System.out.println(data.toString());
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 这里可以在所有数据都被读取完毕后执行一些操作
}
}).sheet().doReadSync();
}
}

View File

@ -1,5 +1,8 @@
package com.cm.population.controller.api.residential;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONArray;
import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
@ -16,21 +19,22 @@ import com.cm.common.result.SuccessResultList;
import com.cm.population.config.properties.ProjectProperties;
import com.cm.population.pojo.dtos.community.CommunityDTO;
import com.cm.population.pojo.dtos.residential.ResidentialDTO;
import com.cm.population.pojo.vos.building.BuildingVO;
import com.cm.population.pojo.vos.residential.ReadExcelResidentialModel;
import com.cm.population.pojo.vos.residential.ResidentialVO;
import com.cm.population.service.building.IBuildingService;
import com.cm.population.service.residential.IResidentialService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @ClassName: ResidentialController
@ -48,6 +52,8 @@ public class ResidentialController extends AbstractController {
private IResidentialService residentialService;
@Autowired
private ProjectProperties projectProperties;
@Autowired
private IBuildingService buildingService;
@ApiOperation(value = "新增小区管理", notes = "新增小区管理接口")
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@ -60,7 +66,7 @@ public class ResidentialController extends AbstractController {
@ApiOperation(value = "删除小区管理", notes = "删除小区管理接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "ids", value = "ID列表用下划线分隔", paramType = "path", example = "1_2_3")
@ApiImplicitParam(name = "ids", value = "ID列表用下划线分隔", paramType = "path", example = "1_2_3")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@DeleteMapping("remove/{ids}")
@ -71,7 +77,7 @@ public class ResidentialController extends AbstractController {
@ApiOperation(value = "修改小区管理", notes = "修改小区管理接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "residentialId", value = "小区管理ID", paramType = "path")
@ApiImplicitParam(name = "residentialId", value = "小区管理ID", paramType = "path")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@PutMapping("update/{residentialId}")
@ -83,11 +89,11 @@ public class ResidentialController extends AbstractController {
@ApiOperation(value = "小区管理详情", notes = "小区管理详情接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "residentialId", value = "小区管理ID", paramType = "path")
@ApiImplicitParam(name = "residentialId", value = "小区管理ID", paramType = "path")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("get/{residentialId}")
public ResidentialDTO get(@PathVariable("residentialId") String residentialId) {
public ResidentialDTO get(@PathVariable("residentialId") String residentialId) {
return residentialService.get(residentialId);
}
@ -101,11 +107,11 @@ public class ResidentialController extends AbstractController {
@ApiOperation(value = "小区管理分页列表", notes = "小区管理分页列表接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
@ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
@ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
@ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
@ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
@ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
})
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
@GetMapping("listpage")
@ -140,12 +146,12 @@ public class ResidentialController extends AbstractController {
ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, null, String.class);
String str = responseEntity.getBody();
if(StringUtils.isEmpty(str)) {
if (StringUtils.isEmpty(str)) {
return new ArrayList<>();
}
List<ZTreeDTO> zTreeDTOList = JSONArray.parseArray(str, ZTreeDTO.class);
if(null == zTreeDTOList || zTreeDTOList.size() <= 0) {
if (null == zTreeDTOList || zTreeDTOList.size() <= 0) {
return new ArrayList<>();
}
@ -169,16 +175,67 @@ public class ResidentialController extends AbstractController {
ResponseEntity<String> responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, null, String.class);
String str = responseEntity.getBody();
if(StringUtils.isEmpty(str)) {
if (StringUtils.isEmpty(str)) {
return new ArrayList<>();
}
List<CommunityDTO> communityDTOList = JSONArray.parseArray(str, CommunityDTO.class);
if(null == communityDTOList || communityDTOList.size() <= 0) {
if (null == communityDTOList || communityDTOList.size() <= 0) {
return new ArrayList<>();
}
return communityDTOList;
}
@ApiOperation(value = "新增小区管理", notes = "新增小区管理接口")
@GetMapping("excel")
public SuccessResult excel() {
String fileName = "C:\\Users\\29492\\Desktop\\稀土路街道小区楼宇批量导入模板.xlsx";
List<Object> list = EasyExcel.read(fileName, ReadExcelResidentialModel.class,
new AnalysisEventListener<ReadExcelResidentialModel>() {
@Override
public void invoke(ReadExcelResidentialModel data, AnalysisContext context) {
System.out.println(data.toString());
Map<String, Object> params = new HashMap<>();
if (!data.getCommunity().equals("社区")) {
// 先查找当前小区是否存在不存在则新增
params.put("streetName", data.getStreet());
params.put("communityName", data.getCommunity());
params.put("name", data.getResidential());
List<ResidentialDTO> residentialDTOList = residentialService.list(params);
if(CollectionUtils.isEmpty(residentialDTOList)) {
ResidentialVO residentialVO = new ResidentialVO();
residentialVO.setStreetName(data.getStreet());
residentialVO.setCommunityName(data.getCommunity());
residentialVO.setName(data.getResidential());
residentialService.save(residentialVO);
}
// 插入楼栋信息获取小区信息
List<ResidentialDTO> residentialDTOList1 = residentialService.list(params);
if(!CollectionUtils.isEmpty(residentialDTOList)) {
BuildingVO buildingVO = new BuildingVO();
buildingVO.setStreetName(data.getStreet());
buildingVO.setCommunityName(data.getCommunity());
buildingVO.setResidentialId(residentialDTOList1.get(0).getResidentialId());
buildingVO.setResidentialName(residentialDTOList1.get(0).getName());
buildingVO.setName(data.getName());
buildingVO.setBuildNum(data.getBuildingNum());
buildingVO.setUnitNum(data.getUnit());
buildingVO.setFloorsNum(data.getFloor());
buildingService.save(buildingVO);
}
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 这里可以在所有数据都被读取完毕后执行一些操作
}
}).sheet().doReadSync();
return new SuccessResult();
}
}

View File

@ -27,11 +27,11 @@ public class BuildingDTO {
@ApiModelProperty(name = "name", value = "楼排名称")
private String name;
@ApiModelProperty(name = "buildNum", value = "楼/排号")
private Integer buildNum;
private String buildNum;
@ApiModelProperty(name = "floorsNum", value = "楼层数")
private Integer floorsNum;
private String floorsNum;
@ApiModelProperty(name = "unitNum", value = "单元/排数")
private Integer unitNum;
private String unitNum;
@ApiModelProperty(name = "address", value = "详细地址")
private String address;
@ApiModelProperty(name = "longitude", value = "经度")
@ -103,27 +103,27 @@ public class BuildingDTO {
this.name = name;
}
public Integer getBuildNum() {
return buildNum == null ? 0 : buildNum;
public String getBuildNum() {
return buildNum;
}
public void setBuildNum(Integer buildNum) {
public void setBuildNum(String buildNum) {
this.buildNum = buildNum;
}
public Integer getFloorsNum() {
return floorsNum == null ? 0 : floorsNum;
public String getFloorsNum() {
return floorsNum;
}
public void setFloorsNum(Integer floorsNum) {
public void setFloorsNum(String floorsNum) {
this.floorsNum = floorsNum;
}
public Integer getUnitNum() {
public String getUnitNum() {
return unitNum;
}
public void setUnitNum(Integer unitNum) {
public void setUnitNum(String unitNum) {
this.unitNum = unitNum;
}

View File

@ -34,11 +34,11 @@ public class HouseDTO {
@ApiModelProperty(name = "affiliationUnit", value = "所在单元/列")
private Integer affiliationUnit;
@ApiModelProperty(name = "unitNum", value = "单元/排数")
private Integer unitNum;
private String unitNum;
@ApiModelProperty(name = "affiliationFloors", value = "所在层/院")
private Integer affiliationFloors;
@ApiModelProperty(name = "floorsNum", value = "楼层数")
private Integer floorsNum;
private String floorsNum;
@ApiModelProperty(name = "houseNum", value = "门牌号")
private Integer houseNum;
@ApiModelProperty(name = "categoryId", value = "房屋类别")
@ -194,13 +194,6 @@ public class HouseDTO {
this.affiliationUnit = affiliationUnit;
}
public Integer getUnitNum() {
return unitNum;
}
public void setUnitNum(Integer unitNum) {
this.unitNum = unitNum;
}
public Integer getAffiliationFloors() {
return affiliationFloors;
@ -210,11 +203,19 @@ public class HouseDTO {
this.affiliationFloors = affiliationFloors;
}
public Integer getFloorsNum() {
public String getUnitNum() {
return unitNum;
}
public void setUnitNum(String unitNum) {
this.unitNum = unitNum;
}
public String getFloorsNum() {
return floorsNum;
}
public void setFloorsNum(Integer floorsNum) {
public void setFloorsNum(String floorsNum) {
this.floorsNum = floorsNum;
}

View File

@ -27,13 +27,13 @@ public class BuildingVO {
private String name;
@ApiModelProperty(name = "buildNum", value = "楼/排号")
@CheckNumberAnnotation(name = "楼/排号")
private Integer buildNum;
private String buildNum;
@ApiModelProperty(name = "floorsNum", value = "楼层数")
@CheckNumberAnnotation(name = "楼层数")
private Integer floorsNum;
private String floorsNum;
@ApiModelProperty(name = "unitNum", value = "单元/排数")
@CheckNumberAnnotation(name = "单元/排数")
private Integer unitNum;
private String unitNum;
@ApiModelProperty(name = "address", value = "详细地址")
private String address;
@ApiModelProperty(name = "longitude", value = "经度")
@ -87,27 +87,27 @@ public class BuildingVO {
this.name = name;
}
public Integer getBuildNum() {
return buildNum == null ? 0 : buildNum;
public String getBuildNum() {
return buildNum;
}
public void setBuildNum(Integer buildNum) {
public void setBuildNum(String buildNum) {
this.buildNum = buildNum;
}
public Integer getFloorsNum() {
return floorsNum == null ? 0 : floorsNum;
public String getFloorsNum() {
return floorsNum;
}
public void setFloorsNum(Integer floorsNum) {
public void setFloorsNum(String floorsNum) {
this.floorsNum = floorsNum;
}
public Integer getUnitNum() {
public String getUnitNum() {
return unitNum;
}
public void setUnitNum(Integer unitNum) {
public void setUnitNum(String unitNum) {
this.unitNum = unitNum;
}

View File

@ -32,7 +32,7 @@ public class HouseVO {
private String buildingName;
@ApiModelProperty(name = "affiliationUnit", value = "所在单元/列")
@CheckNumberAnnotation(name = "所在单元/列")
private Integer affiliationUnit;
private String affiliationUnit;
@ApiModelProperty(name = "affiliationFloors", value = "所在层/院")
@CheckNumberAnnotation(name = "所在层/院")
private String affiliationFloors;
@ -167,11 +167,11 @@ public class HouseVO {
this.buildingName = buildingName;
}
public Integer getAffiliationUnit() {
return affiliationUnit == null ? 0 : affiliationUnit;
public String getAffiliationUnit() {
return affiliationUnit;
}
public void setAffiliationUnit(Integer affiliationUnit) {
public void setAffiliationUnit(String affiliationUnit) {
this.affiliationUnit = affiliationUnit;
}

View File

@ -0,0 +1,57 @@
package com.cm.population.pojo.vos.residential;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
/**
*
* @ClassName: ResidentialVO
* @Description: 小区管理
* @Author: CodeFactory
* @Date: 2023-10-25 14:52:32
* @Version: 3.0
**/
@ApiModel
public class ReadExcelBuildingModel {
@ExcelProperty(value = "街道", index = 3)
private String name;
@ExcelProperty(value = "社区",index = 4)
private String buildingNum;
@ExcelProperty(value = "小区",index = 5)
private String unit;
@ExcelProperty(value = "小区",index = 6)
private String floor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBuildingNum() {
return buildingNum;
}
public void setBuildingNum(String buildingNum) {
this.buildingNum = buildingNum;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
}

View File

@ -0,0 +1,100 @@
package com.cm.population.pojo.vos.residential;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
/**
*
* @ClassName: ResidentialVO
* @Description: 小区管理
* @Author: CodeFactory
* @Date: 2023-10-25 14:52:32
* @Version: 3.0
**/
@ApiModel
public class ReadExcelResidentialModel {
@ExcelProperty(value = "街道", index = 0)
private String street;
@ExcelProperty(value = "社区",index = 1)
private String community;
@ExcelProperty(value = "小区",index = 2)
private String residential;
@ExcelProperty(value = "街道", index = 3)
private String name;
@ExcelProperty(value = "社区",index = 4)
private String buildingNum;
@ExcelProperty(value = "小区",index = 5)
private String unit;
@ExcelProperty(value = "小区",index = 6)
private String floor;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCommunity() {
return community;
}
public void setCommunity(String community) {
this.community = community;
}
public String getResidential() {
return residential;
}
public void setResidential(String residential) {
this.residential = residential;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBuildingNum() {
return buildingNum;
}
public void setBuildingNum(String buildingNum) {
this.buildingNum = buildingNum;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
@Override
public String toString() {
return "ReadExcelResidentialModel{" +
"street='" + street + '\'' +
", community='" + community + '\'' +
", residential='" + residential + '\'' +
", name='" + name + '\'' +
", buildingNum='" + buildingNum + '\'' +
", unit='" + unit + '\'' +
", floor='" + floor + '\'' +
'}';
}
}

View File

@ -263,8 +263,8 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
// 获取楼栋信息
BuildingDTO buildingDTO = buildingService.get(params.get("buildingId").toString());
if (null != buildingDTO) {
Integer floorsNum = buildingDTO.getFloorsNum();
Integer unitNum = buildingDTO.getUnitNum();
Integer floorsNum = Integer.valueOf(buildingDTO.getFloorsNum());
Integer unitNum = Integer.valueOf(buildingDTO.getUnitNum());
batchHouseDTO.setFloorCount(floorsNum.toString());
batchHouseDTO.setUnitCount(unitNum.toString());
@ -370,7 +370,7 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
insertHouseVO.setBuildingId(buildingId);
insertHouseVO.setBuildingName(buildingDTO.getName());
insertHouseVO.setAffiliationUnit(Integer.valueOf(batchVO.getUnitValue()));
insertHouseVO.setAffiliationUnit(batchVO.getUnitValue());
insertHouseVO.setAffiliationFloors(floorVO.getFloorValue());
insertHouseVO.setHouseNum(houseVO.getHouseValue());
saveReturnId(token, insertHouseVO);
@ -389,9 +389,9 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
BuildingDTO buildingDTO = buildingService.get(buildingId);
if (null != buildingDTO) {
// 楼层数
int floorsNum = buildingDTO.getFloorsNum();
int floorsNum = Integer.valueOf(buildingDTO.getFloorsNum());
// 单元数
int unitNum = buildingDTO.getUnitNum();
int unitNum = Integer.valueOf(buildingDTO.getUnitNum());
int houseCount = autoHouseDTO.getFloorUnitHouseCount();
@ -409,7 +409,7 @@ public class HouseServiceImpl extends AbstractService implements IHouseService {
houseVO.setBuildingName(buildingDTO.getName());
// 房屋信息单元楼层房号
houseVO.setAffiliationUnit(unit);
houseVO.setAffiliationUnit("" + unit);
houseVO.setAffiliationFloors(String.valueOf(floor));
for (int house = 1; house <= houseCount; house++) {
houseVO.setHouseNum(floor + "0" + house);