From 7cfe2cba90cbc36266d9a8616ac804c646e5d666 Mon Sep 17 00:00:00 2001 From: sunyuchao Date: Wed, 24 Nov 2021 11:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BE=E4=BC=9A=E7=BB=84=E7=BB=87=E5=AF=BC?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/ExcelPoiUtils.java | 276 ++++++++++++++++++ .../commons/tools/utils/ExcelVerifyInfo.java | 31 ++ .../controller/IcSocietyOrgController.java | 66 ++++- .../com/epmet/excel/IcSocietyOrgExcel.java | 34 +-- .../epmet/service/IcSocietyOrgService.java | 8 + .../service/impl/IcSocietyOrgServiceImpl.java | 6 + 6 files changed, 398 insertions(+), 23 deletions(-) create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelPoiUtils.java create mode 100644 epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelVerifyInfo.java diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelPoiUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelPoiUtils.java new file mode 100644 index 0000000000..3ea8472867 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelPoiUtils.java @@ -0,0 +1,276 @@ +package com.epmet.commons.tools.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.TemplateExportParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; +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.io.InputStream; +import java.net.URLEncoder; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +public class ExcelPoiUtils { + /** + * excel 导出 + * + * @param list 数据列表 + * @param fileName 导出时的excel名称 + * @param response + */ + public static void exportExcel(List> list, String fileName, HttpServletResponse response) throws IOException { + defaultExport(list, fileName, response); + } + + /** + * 默认的 excel 导出 + * + * @param list 数据列表 + * @param fileName 导出时的excel名称 + * @param response + */ + private static void defaultExport(List> list, String fileName, HttpServletResponse response) throws IOException { + //把数据添加到excel表格中 + Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); + downLoadExcel(fileName, response, workbook); + } + + /** + * excel 导出 + * + * @param list 数据列表 + * @param pojoClass pojo类型 + * @param fileName 导出时的excel名称 + * @param response + * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) + */ + private static void defaultExport(List list, Class pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException { + //把数据添加到excel表格中 + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list); + downLoadExcel(fileName, response, workbook); + } + + /** + * excel 导出 + * + * @param list 数据列表 + * @param pojoClass pojo类型 + * @param fileName 导出时的excel名称 + * @param exportParams 导出参数(标题、sheet名称、是否创建表头,表格类型) + * @param response + */ + public static void exportExcel(List list, Class pojoClass, String fileName, ExportParams exportParams, HttpServletResponse response) throws IOException { + defaultExport(list, pojoClass, fileName, response, exportParams); + } + + /** + * excel 导出 + * + * @param list 数据列表 + * @param title 表格内数据标题 + * @param sheetName sheet名称 + * @param pojoClass pojo类型 + * @param fileName 导出时的excel名称 + * @param response + */ + public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, HttpServletResponse response) throws IOException { + defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF)); + } + + + + /** + * excel 导出 + * + * @param list 数据列表 + * @param title 表格内数据标题 + * @param sheetName sheet名称 + * @param pojoClass pojo类型 + * @param fileName 导出时的excel名称 + * @param isCreateHeader 是否创建表头 + * @param response + */ + public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException { + ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF); + exportParams.setCreateHeadRows(isCreateHeader); + defaultExport(list, pojoClass, fileName, response, exportParams); + } + /** + * 根据模板生成excel后导出 + * @param templatePath 模板路径 + * @param map 数据集合 + * @param fileName 文件名 + * @param response + * @throws IOException + */ + public static void exportExcel(TemplateExportParams templatePath, Map map, String fileName, HttpServletResponse response) throws IOException { + Workbook workbook = ExcelExportUtil.exportExcel(templatePath, map); + downLoadExcel(fileName, response, workbook); + } + + + /** + * excel下载 + * + * @param fileName 下载时的文件名称 + * @param response + * @param workbook excel数据 + */ + private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("content-Type", "application/vnd.ms-excel"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8")); + workbook.write(response.getOutputStream()); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + + + /** + * excel 导入 + * + * @param file excel文件 + * @param pojoClass pojo类型 + * @param + * @return + */ + public static List importExcel(MultipartFile file, Class pojoClass) throws IOException { + return importExcel(file, 1, 1, pojoClass); + } + + /** + * excel 导入 + * + * @param filePath excel文件路径 + * @param titleRows 表格内数据标题行 + * @param headerRows 表头行 + * @param pojoClass pojo类型 + * @param + * @return + */ + public static List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass) throws IOException { + if (StringUtils.isBlank(filePath)) { + return null; + } + ImportParams params = new ImportParams(); + params.setTitleRows(titleRows); + params.setHeadRows(headerRows); + params.setNeedSave(true); + params.setSaveUrl("/tmp/excel/"); + try { + return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); + } catch (NoSuchElementException e) { + throw new IOException("模板不能为空"); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + + /** + * excel 导入 + * + * @param file 上传的文件 + * @param titleRows 表格内数据标题行 + * @param headerRows 表头行 + * @param pojoClass pojo类型 + * @param + * @return + */ + public static List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass) throws IOException { + if (file == null) { + return null; + } + try { + return importExcel(file.getInputStream(), titleRows, headerRows, pojoClass); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + /** + * excel 导入 + * + * @param inputStream 文件输入流 + * @param titleRows 表格内数据标题行 + * @param headerRows 表头行 + * @param pojoClass pojo类型 + * @param + * @return + */ + public static List importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class pojoClass) throws IOException { + if (inputStream == null) { + return null; + } + ImportParams params = new ImportParams(); + params.setTitleRows(titleRows); + params.setHeadRows(headerRows); + params.setSaveUrl("/tmp/excel/"); + params.setNeedSave(true); + try { + return ExcelImportUtil.importExcel(inputStream, pojoClass, params); + } catch (NoSuchElementException e) { + throw new IOException("excel文件不能为空"); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + /** + * excel 导入,有错误信息 + * + * @param file 上传的文件 + * @param pojoClass pojo类型 + * @param + * @return + */ + public static ExcelImportResult importExcelMore(MultipartFile file,Integer titleRows, Integer headerRows, Class pojoClass) throws IOException { + if (file == null) { + return null; + } + try { + return importExcelMore(file.getInputStream(), titleRows, headerRows, pojoClass); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + + /** + * excel 导入 + * + * @param inputStream 文件输入流 + * @param pojoClass pojo类型 + * @param + * @return + */ + private static ExcelImportResult importExcelMore(InputStream inputStream,Integer titleRows, Integer headerRows, Class pojoClass) throws IOException { + if (inputStream == null) { + return null; + } + ImportParams params = new ImportParams(); + params.setTitleRows(titleRows);//表格内数据标题行 + params.setHeadRows(headerRows);//表头行 + params.setSaveUrl("/tmp/excel/"); + params.setNeedSave(true); + params.setNeedVerify(true); + try { + return ExcelImportUtil.importExcelMore(inputStream, pojoClass, params); + } catch (NoSuchElementException e) { + throw new IOException("excel文件不能为空"); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } +} diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelVerifyInfo.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelVerifyInfo.java new file mode 100644 index 0000000000..a00bb8bbf4 --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelVerifyInfo.java @@ -0,0 +1,31 @@ +package com.epmet.commons.tools.utils; + +import cn.afterturn.easypoi.handler.inter.IExcelDataModel; +import cn.afterturn.easypoi.handler.inter.IExcelModel; + +public class ExcelVerifyInfo implements IExcelModel, IExcelDataModel { + + private String errorMsg; + + private int rowNum; + + @Override + public Integer getRowNum() { + return rowNum; + } + + @Override + public void setRowNum(Integer rowNum) { + this.rowNum = rowNum; + } + + @Override + public String getErrorMsg() { + return errorMsg; + } + + @Override + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } +} diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java index 1ee342120d..c514dd870e 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java @@ -17,24 +17,36 @@ package com.epmet.controller; +import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; import com.epmet.commons.tools.annotation.LoginUser; +import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.security.dto.TokenDto; +import com.epmet.commons.tools.utils.ExcelPoiUtils; +import com.epmet.commons.tools.utils.ExcelUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.AddSocietyOrgFormDTO; import com.epmet.dto.form.EditSocietyOrgFormDTO; +import com.epmet.dto.form.GetListPlaceOrgFormDTO; import com.epmet.dto.form.GetListSocietyOrgFormDTO; import com.epmet.dto.form.demand.ServiceQueryFormDTO; +import com.epmet.dto.result.GetListPlaceOrgResultDTO; import com.epmet.dto.result.GetListSocietyOrgResultDTO; import com.epmet.dto.result.demand.OptionDTO; +import com.epmet.excel.IcSocietyOrgExcel; import com.epmet.service.IcSocietyOrgService; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** @@ -43,6 +55,7 @@ import java.util.List; * @author generator generator@elink-cn.com * @since v1.0.0 2021-11-18 */ +@Slf4j @RestController @RequestMapping("societyorg") public class IcSocietyOrgController { @@ -109,4 +122,49 @@ public class IcSocietyOrgController { ValidatorUtils.validateEntity(formDTO,ServiceQueryFormDTO.AddUserInternalGroup.class,ServiceQueryFormDTO.SocietyOrgInternalGroup.class); return new Result>().ok(societyOrgService.queryServiceList(formDTO)); } + + /** + * @Author sun + * @Description 九小场所下组织列表导出 + **/ + @GetMapping("export") + public void export(@LoginUser TokenDto tokenDto, @RequestBody GetListSocietyOrgFormDTO formDTO, HttpServletResponse response) throws Exception { + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setStaffId(tokenDto.getUserId()); + GetListSocietyOrgResultDTO list = societyOrgService.getList(formDTO); + ExcelUtils.exportExcelToTarget(response, null, list.getList(), IcSocietyOrgExcel.class); + } + + /** + * @Author sun + * @Description 九小场所下组织列表导入---咱不能使用 程序未开发 + **/ + @PostMapping("import") + public Result importExcel(@LoginUser TokenDto tokenDTO, @RequestParam("file") MultipartFile file) throws IOException { + + ExcelImportResult importResult = ExcelPoiUtils.importExcelMore(file, 0, 1, IcSocietyOrgExcel.class); + List failList = importResult.getFailList(); + + //存放错误数据行号 + List numList = new ArrayList<>(); + if (!CollectionUtils.isEmpty(failList)) { + for (IcSocietyOrgExcel entity : failList) { + log.error("第{}行,{}", entity.getRowNum(), entity.getErrorMsg());//打印失败的行 和失败的信息 + numList.add(entity.getRowNum()); + } + //return new Result().error(8001,failList.get(0).getErrorMsg()); + } + List result = importResult.getList(); + + List resultList = societyOrgService.importExcel(tokenDTO.getCustomerId(), result, tokenDTO.getUserId(), numList); + String str = String.format("共%s条,成功导入%s条。", numList.size() + result.size(), numList.size() + result.size() - resultList.size()); + if (resultList.size() > NumConstant.ZERO) { + Collections.sort(resultList); + String subList = resultList.stream().map(String::valueOf).collect(Collectors.joining("、")); + log.warn(str + "第" + subList + "行未成功!"); + return new Result().error(9999, str + "第" + subList + "行未成功!"); + } + return new Result().ok(str); + } + } \ No newline at end of file diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java index 5f3d464dfb..bfcebe6e8d 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java @@ -18,8 +18,12 @@ package com.epmet.excel; import cn.afterturn.easypoi.excel.annotation.Excel; +import com.epmet.commons.tools.utils.ExcelVerifyInfo; import lombok.Data; +import org.hibernate.validator.constraints.Length; +import javax.validation.constraints.NotBlank; +import java.io.Serializable; import java.util.Date; /** @@ -29,7 +33,7 @@ import java.util.Date; * @since v1.0.0 2021-11-18 */ @Data -public class IcSocietyOrgExcel { +public class IcSocietyOrgExcel extends ExcelVerifyInfo implements Serializable { @Excel(name = "唯一标识") private String id; @@ -44,21 +48,31 @@ public class IcSocietyOrgExcel { private String pids; @Excel(name = "社会组织名称") + @NotBlank(message = "不能为空") + @Length(max=50,message = "不能超过50个字") private String societyName; @Excel(name = "服务事项") + @NotBlank(message = "不能为空") + @Length(max=1000,message = "不能超过1000个字") private String serviceMatters; @Excel(name = "负责人") + @NotBlank(message = "不能为空") + @Length(max=20,message = "不能超过20个字") private String personInCharge; @Excel(name = "负责人电话") + @NotBlank(message = "不能为空") + @Length(max=11,message = "不能超过11个字") private String mobile; @Excel(name = "起始服务时间") + @NotBlank(message = "不能为空") private Date serviceStartTime; @Excel(name = "终止服务时间") + @NotBlank(message = "不能为空") private Date serviceEndTime; @Excel(name = "绑定管理员[组织下录入的工作人员]") @@ -76,23 +90,5 @@ public class IcSocietyOrgExcel { @Excel(name = "备注") private String remarks; - @Excel(name = "删除标识:0.未删除 1.已删除") - private Integer delFlag; - - @Excel(name = "乐观锁") - private Integer revision; - - @Excel(name = "创建人") - private String createdBy; - - @Excel(name = "创建时间") - private Date createdTime; - - @Excel(name = "更新人") - private String updatedBy; - - @Excel(name = "更新时间") - private Date updatedTime; - } \ No newline at end of file diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcSocietyOrgService.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcSocietyOrgService.java index f61eec5b94..0c5841b04a 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcSocietyOrgService.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcSocietyOrgService.java @@ -25,6 +25,7 @@ import com.epmet.dto.form.demand.ServiceQueryFormDTO; import com.epmet.dto.result.GetListSocietyOrgResultDTO; import com.epmet.dto.result.demand.OptionDTO; import com.epmet.entity.IcSocietyOrgEntity; +import com.epmet.excel.IcSocietyOrgExcel; import java.util.List; @@ -66,4 +67,11 @@ public interface IcSocietyOrgService extends BaseService { * @return */ List queryServiceList(ServiceQueryFormDTO formDTO); + + /** + * @Author sun + * @Description 九小场所下组织列表导入 + **/ + List importExcel(String customerId, List result, String userId, List numList); + } \ No newline at end of file diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java index 2c18383a39..ff8f95e7dd 100644 --- a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java @@ -36,6 +36,7 @@ import com.epmet.dto.result.SocietyOrgListResultDTO; import com.epmet.dto.result.StaffSinGridResultDTO; import com.epmet.dto.result.demand.OptionDTO; import com.epmet.entity.IcSocietyOrgEntity; +import com.epmet.excel.IcSocietyOrgExcel; import com.epmet.feign.EpmetUserOpenFeignClient; import com.epmet.service.IcSocietyOrgService; import com.epmet.service.IcUserDemandRecService; @@ -166,4 +167,9 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl importExcel(String customerId, List result, String userId, List numList) { + return numList; + } + } \ No newline at end of file