通过行政区划下载excel新增

master
徐杰盟 2024-05-28 10:41:40 +08:00
parent 224000c229
commit 497a655ee8
9 changed files with 220 additions and 11 deletions

View File

@ -12,13 +12,17 @@ import com.whdc.model.enums.Role;
import com.whdc.model.enums.VersionsType;
import com.whdc.model.group.Insert;
import com.whdc.model.group.Update;
import com.whdc.model.vo.AdcdTree;
import com.whdc.model.vo.AddressBookVo;
import com.whdc.model.vo.ExcelABVo;
import com.whdc.service.*;
import com.whdc.utils.ExcelCommon;
import com.whdc.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
@ -26,7 +30,10 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.whdc.model.MyConstant.REDIS_KEY;
@ -49,6 +56,9 @@ public class AddressBookController {
@Autowired
private IUserService userService;
@Autowired
private IAdinfoService adinfoService;
@Autowired
private IVersionsService versionsService;
@ -418,4 +428,56 @@ public class AddressBookController {
return ResultJson.ok(byId1);
}
@ApiOperation(value = "通过行政区划下载excel")
@GetMapping(value = "getExcel/{adnm}")
public ResultJson getExcel(@PathVariable("adnm") String adnm, HttpServletResponse response) {
List<AdcdTree> tree = adinfoService.tree(adnm, adnm);
if (CollectionUtils.isEmpty(tree)){
return ResultJson.error("当前行政区划不存在");
}
List<AddressBook> addressBooks = service.lambdaQuery().isNotNull(AddressBook::getAdcd)
.list();
if (CollectionUtils.isEmpty(addressBooks)) {
return ResultJson.error("行政区划责任人为空");
}
Map<String, List<AddressBook>> abMap = addressBooks.stream().collect(Collectors.groupingBy(AddressBook::getAdcd, Collectors.toList()));
// 行政区划责任人数据
List<ExcelABVo> data = Lists.newArrayList();
tree.forEach(t ->{
String adcd = t.getAdcd();
String cityAdnm = t.getAdnm();
List<AddressBook> books = abMap.get(adcd);
books.forEach(b ->{
data.add(new ExcelABVo(cityAdnm,cityAdnm,b.getName(),b.getPosition(),b.getPhone()));
});
t.getChildren().forEach(tc ->{
String tcAdcd = tc.getAdcd();
String tcCityAdnm = tc.getAdnm();
List<AddressBook> tcBooks = abMap.get(tcAdcd);
books.forEach(b ->{
data.add(new ExcelABVo(cityAdnm,tcCityAdnm,b.getName(),b.getPosition(),b.getPhone()));
});
});
});
if (CollectionUtils.isEmpty(data)) {
return ResultJson.error("当前行政区划责任人为空");
}
ExcelCommon.exportExcel(data,
"防汛抗旱通讯录", "防汛抗旱通讯录", ExcelABVo.class, "防汛抗旱通讯录_"+ adnm +"_" + LocalDateTime.now() + ".xlsx",
response);
return ResultJson.ok("");
}
}

View File

@ -134,7 +134,7 @@ public class WarningController {
*/
@ApiOperation(value = "历史气象预警统计")
@PostMapping("/getHistoryGroupWarning")
public ResultJson<List<WarningListVo>> getHistoryGroupWarning(@RequestBody GroupWarningDto dto) {
public ResultJson<List<WarningHistoryListVo>> getHistoryGroupWarning(@RequestBody GroupWarningDto dto) {
ApiDto apiDto = new ApiDto();
apiDto.setStartTime(dto.getStartTime());
@ -344,9 +344,6 @@ public class WarningController {
WarningHistoryListVo vo = vos.get(0);
List<WarningHistoryListVo> childList = Lists.newArrayList();
childList.addAll(vo.getChild());
if (CollectionUtils.isNotEmpty(children)) {
// 第一个是市级单位
for (int i = 1; i < children.size(); i++) {
@ -360,7 +357,7 @@ public class WarningController {
}
vo.setChild(childList);
vo.setAreaChild(childList);
respList.add(vo);
}

View File

@ -5,9 +5,11 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whdc.model.dto.AddressBootDto;
import com.whdc.model.entity.AddressBook;
import com.whdc.model.vo.AddressBookVo;
import com.whdc.model.vo.ExcelABVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author xusan
* @date 2024-05-11
@ -20,4 +22,4 @@ public interface AddressBookMapper extends BaseMapper<AddressBook> {
List<AddressBook> getListByAdnm(@Param("adnm") String adnm);
}
}

View File

@ -0,0 +1,42 @@
package com.whdc.model.vo;
import cn.afterturn.easypoi.excel.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Description:
* Created by XuSan on 2024/5/28.
*
* @author XuSan
* @version 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExcelABVo {
@Excel(name = "市", width = 20)
@ApiModelProperty(value = "市")
private String city;
@Excel(name = "区县", width = 20)
@ApiModelProperty(value = "区县")
private String county;
@Excel(name = "姓名", width = 20)
@ApiModelProperty(value = "姓名")
private String name;
@Excel(name = "职务", width = 50)
@ApiModelProperty(value = "职务")
private String position;
@Excel(name = "手机号", width = 30)
@ApiModelProperty(value = "手机号")
private String phone;
}

View File

@ -46,5 +46,8 @@ public class WarningHistoryListVo {
@ApiModelProperty(value = "子统计")
private List<WarningHistoryListVo> child;
@ApiModelProperty(value = "地区子统计")
private List<WarningHistoryListVo> areaChild;
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.whdc.model.dto.AddressBootDto;
import com.whdc.model.entity.AddressBook;
import com.whdc.model.vo.AddressBookVo;
import com.whdc.model.vo.ExcelABVo;
import java.util.List;
@ -25,4 +26,5 @@ public interface IAddressBookService extends IService<AddressBook> {
List<AddressBook> getListByAdnm(String adnm);
}

View File

@ -6,6 +6,7 @@ import com.whdc.mapper.AddressBookMapper;
import com.whdc.model.dto.AddressBootDto;
import com.whdc.model.entity.AddressBook;
import com.whdc.model.vo.AddressBookVo;
import com.whdc.model.vo.ExcelABVo;
import com.whdc.service.IAddressBookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
@ -59,4 +60,5 @@ public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, Addre
}
}

View File

@ -0,0 +1,95 @@
package com.whdc.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
/**
* Description:
* Created by XuSan on 2024/3/18.
*
* @author XuSan
* @version 1.0
*/
public class ExcelCommon {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null) ;
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}

View File

@ -54,8 +54,12 @@
FROM
ADDRESS_BOOK AB
LEFT JOIN ADINFO A ON AB.ADCD = A.ADCD
WHERE
<where>
<if test="adnm != null and adnm != '' ">
A.ADNM = #{adnm}
</if>
</where>
</select>
</mapper>