Browse Source

社会组织导出

dev_shibei_match
sunyuchao 4 years ago
parent
commit
7cfe2cba90
  1. 276
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelPoiUtils.java
  2. 31
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ExcelVerifyInfo.java
  3. 66
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java
  4. 34
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java
  5. 8
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcSocietyOrgService.java
  6. 6
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcSocietyOrgServiceImpl.java

276
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<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException {
defaultExport(list, fileName, response);
}
/**
* 默认的 excel 导出
*
* @param list 数据列表
* @param fileName 导出时的excel名称
* @param response
*/
private static void defaultExport(List<Map<String, Object>> 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<String, Object> 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 <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
return importExcel(file, 1, 1, pojoClass);
}
/**
* excel 导入
*
* @param filePath excel文件路径
* @param titleRows 表格内数据标题行
* @param headerRows 表头行
* @param pojoClass pojo类型
* @param <T>
* @return
*/
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> 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 <T>
* @return
*/
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> 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 <T>
* @return
*/
public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows, Class<T> 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 <T>
* @return
*/
public static <T> ExcelImportResult<T> importExcelMore(MultipartFile file,Integer titleRows, Integer headerRows, Class<T> 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 <T>
* @return
*/
private static <T> ExcelImportResult<T> importExcelMore(InputStream inputStream,Integer titleRows, Integer headerRows, Class<T> 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());
}
}
}

31
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;
}
}

66
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcSocietyOrgController.java

@ -17,24 +17,36 @@
package com.epmet.controller; package com.epmet.controller;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.epmet.commons.tools.annotation.LoginUser; 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.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.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.dto.form.AddSocietyOrgFormDTO; import com.epmet.dto.form.AddSocietyOrgFormDTO;
import com.epmet.dto.form.EditSocietyOrgFormDTO; import com.epmet.dto.form.EditSocietyOrgFormDTO;
import com.epmet.dto.form.GetListPlaceOrgFormDTO;
import com.epmet.dto.form.GetListSocietyOrgFormDTO; import com.epmet.dto.form.GetListSocietyOrgFormDTO;
import com.epmet.dto.form.demand.ServiceQueryFormDTO; import com.epmet.dto.form.demand.ServiceQueryFormDTO;
import com.epmet.dto.result.GetListPlaceOrgResultDTO;
import com.epmet.dto.result.GetListSocietyOrgResultDTO; import com.epmet.dto.result.GetListSocietyOrgResultDTO;
import com.epmet.dto.result.demand.OptionDTO; import com.epmet.dto.result.demand.OptionDTO;
import com.epmet.excel.IcSocietyOrgExcel;
import com.epmet.service.IcSocietyOrgService; import com.epmet.service.IcSocietyOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
@ -43,6 +55,7 @@ import java.util.List;
* @author generator generator@elink-cn.com * @author generator generator@elink-cn.com
* @since v1.0.0 2021-11-18 * @since v1.0.0 2021-11-18
*/ */
@Slf4j
@RestController @RestController
@RequestMapping("societyorg") @RequestMapping("societyorg")
public class IcSocietyOrgController { public class IcSocietyOrgController {
@ -109,4 +122,49 @@ public class IcSocietyOrgController {
ValidatorUtils.validateEntity(formDTO,ServiceQueryFormDTO.AddUserInternalGroup.class,ServiceQueryFormDTO.SocietyOrgInternalGroup.class); ValidatorUtils.validateEntity(formDTO,ServiceQueryFormDTO.AddUserInternalGroup.class,ServiceQueryFormDTO.SocietyOrgInternalGroup.class);
return new Result<List<OptionDTO>>().ok(societyOrgService.queryServiceList(formDTO)); return new Result<List<OptionDTO>>().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<IcSocietyOrgExcel> importResult = ExcelPoiUtils.importExcelMore(file, 0, 1, IcSocietyOrgExcel.class);
List<IcSocietyOrgExcel> failList = importResult.getFailList();
//存放错误数据行号
List<Integer> 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<IcSocietyOrgExcel> result = importResult.getList();
List<Integer> 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);
}
} }

34
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExcel.java

@ -18,8 +18,12 @@
package com.epmet.excel; package com.epmet.excel;
import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.Excel;
import com.epmet.commons.tools.utils.ExcelVerifyInfo;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
/** /**
@ -29,7 +33,7 @@ import java.util.Date;
* @since v1.0.0 2021-11-18 * @since v1.0.0 2021-11-18
*/ */
@Data @Data
public class IcSocietyOrgExcel { public class IcSocietyOrgExcel extends ExcelVerifyInfo implements Serializable {
@Excel(name = "唯一标识") @Excel(name = "唯一标识")
private String id; private String id;
@ -44,21 +48,31 @@ public class IcSocietyOrgExcel {
private String pids; private String pids;
@Excel(name = "社会组织名称") @Excel(name = "社会组织名称")
@NotBlank(message = "不能为空")
@Length(max=50,message = "不能超过50个字")
private String societyName; private String societyName;
@Excel(name = "服务事项") @Excel(name = "服务事项")
@NotBlank(message = "不能为空")
@Length(max=1000,message = "不能超过1000个字")
private String serviceMatters; private String serviceMatters;
@Excel(name = "负责人") @Excel(name = "负责人")
@NotBlank(message = "不能为空")
@Length(max=20,message = "不能超过20个字")
private String personInCharge; private String personInCharge;
@Excel(name = "负责人电话") @Excel(name = "负责人电话")
@NotBlank(message = "不能为空")
@Length(max=11,message = "不能超过11个字")
private String mobile; private String mobile;
@Excel(name = "起始服务时间") @Excel(name = "起始服务时间")
@NotBlank(message = "不能为空")
private Date serviceStartTime; private Date serviceStartTime;
@Excel(name = "终止服务时间") @Excel(name = "终止服务时间")
@NotBlank(message = "不能为空")
private Date serviceEndTime; private Date serviceEndTime;
@Excel(name = "绑定管理员[组织下录入的工作人员]") @Excel(name = "绑定管理员[组织下录入的工作人员]")
@ -76,23 +90,5 @@ public class IcSocietyOrgExcel {
@Excel(name = "备注") @Excel(name = "备注")
private String remarks; 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;
} }

8
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.GetListSocietyOrgResultDTO;
import com.epmet.dto.result.demand.OptionDTO; import com.epmet.dto.result.demand.OptionDTO;
import com.epmet.entity.IcSocietyOrgEntity; import com.epmet.entity.IcSocietyOrgEntity;
import com.epmet.excel.IcSocietyOrgExcel;
import java.util.List; import java.util.List;
@ -66,4 +67,11 @@ public interface IcSocietyOrgService extends BaseService<IcSocietyOrgEntity> {
* @return * @return
*/ */
List<OptionDTO> queryServiceList(ServiceQueryFormDTO formDTO); List<OptionDTO> queryServiceList(ServiceQueryFormDTO formDTO);
/**
* @Author sun
* @Description 九小场所下组织列表导入
**/
List<Integer> importExcel(String customerId, List<IcSocietyOrgExcel> result, String userId, List<Integer> numList);
} }

6
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.StaffSinGridResultDTO;
import com.epmet.dto.result.demand.OptionDTO; import com.epmet.dto.result.demand.OptionDTO;
import com.epmet.entity.IcSocietyOrgEntity; import com.epmet.entity.IcSocietyOrgEntity;
import com.epmet.excel.IcSocietyOrgExcel;
import com.epmet.feign.EpmetUserOpenFeignClient; import com.epmet.feign.EpmetUserOpenFeignClient;
import com.epmet.service.IcSocietyOrgService; import com.epmet.service.IcSocietyOrgService;
import com.epmet.service.IcUserDemandRecService; import com.epmet.service.IcUserDemandRecService;
@ -166,4 +167,9 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl<IcSocietyOrgDao, Ic
return resultList; return resultList;
} }
@Override
public List<Integer> importExcel(String customerId, List<IcSocietyOrgExcel> result, String userId, List<Integer> numList) {
return numList;
}
} }
Loading…
Cancel
Save