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 2b07959e33..ca877fda19 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 @@ -18,13 +18,17 @@ package com.epmet.controller; import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.fastjson.JSON; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.exception.EpmetException; 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.*; +import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.AddSocietyOrgFormDTO; import com.epmet.dto.form.EditSocietyOrgFormDTO; @@ -33,18 +37,26 @@ import com.epmet.dto.form.demand.ServiceQueryFormDTO; import com.epmet.dto.result.GetListSocietyOrgResultDTO; import com.epmet.dto.result.demand.OptionDTO; import com.epmet.excel.IcSocietyOrgExcel; +import com.epmet.excel.IcSocietyOrgExportExcel; import com.epmet.service.IcSocietyOrgService; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.net.URLEncoder; import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -129,11 +141,46 @@ public class IcSocietyOrgController { **/ @PostMapping("export") public void export(@LoginUser TokenDto tokenDto, @RequestBody GetListSocietyOrgFormDTO formDTO, HttpServletResponse response) throws Exception { + //废弃原来的导出 + // formDTO.setCustomerId(tokenDto.getCustomerId()); + // formDTO.setStaffId(tokenDto.getUserId()); + // formDTO.setIsPage(false); + // GetListSocietyOrgResultDTO list = societyOrgService.getList(formDTO); + // ExcelUtils.exportExcelToTarget(response, null, list.getList(), IcSocietyOrgExportExcel.class); + formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setStaffId(tokenDto.getUserId()); formDTO.setIsPage(false); - GetListSocietyOrgResultDTO list = societyOrgService.getList(formDTO); - ExcelUtils.exportExcelToTarget(response, null, list.getList(), IcSocietyOrgExcel.class); + ExcelWriter excelWriter = null; + formDTO.setPageNo(NumConstant.ONE); + formDTO.setPageSize(NumConstant.TEN_THOUSAND); + try { + String fileName = "社会组织" + DateUtils.format(new Date()) + ".xlsx"; + excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel(fileName, response), IcSocietyOrgExportExcel.class).build(); + WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").registerWriteHandler(new FreezeAndFilter()).build(); + GetListSocietyOrgResultDTO data = null; + List list = null; + do { + data = societyOrgService.getList(formDTO); + list = ConvertUtils.sourceToTarget(data.getList(), IcSocietyOrgExportExcel.class); + formDTO.setPageNo(formDTO.getPageNo() + NumConstant.ONE); + excelWriter.write(list, writeSheet); + } while (org.apache.commons.collections4.CollectionUtils.isNotEmpty(list) && list.size() == formDTO.getPageSize()); + } catch (EpmetException e) { + response.reset(); + response.setCharacterEncoding("UTF-8"); + response.setHeader("content-type", "application/json; charset=UTF-8"); + PrintWriter printWriter = response.getWriter(); + Result result = new Result<>().error(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),e.getMsg()); + printWriter.write(JSON.toJSONString(result)); + printWriter.close(); + } catch (Exception e) { + log.error("社会组织export exception", e); + } finally { + if (excelWriter != null) { + excelWriter.finish(); + } + } } /** @@ -168,4 +215,29 @@ public class IcSocietyOrgController { return new Result().ok(str); } + + /** + * 下载社会组织导入模板 + * @param response + * @throws IOException + */ + @RequestMapping(value = "import-template-download", method = {RequestMethod.GET, RequestMethod.POST}) + public void downloadTemplate(HttpServletResponse response) throws IOException { + response.setCharacterEncoding("UTF-8"); + response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); + response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("社区组织导入模板", "UTF-8") + ".xlsx"); + + InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/societyorg_import_template.xlsx"); + try { + ServletOutputStream os = response.getOutputStream(); + IOUtils.copy(is, os); + } finally { + if (is != null) { + is.close(); + } + } + } + + } 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 c4392b5e5a..e48c6cb6d6 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 @@ -34,37 +34,28 @@ import java.io.Serializable; @Data public class IcSocietyOrgExcel extends ExcelVerifyInfo implements Serializable { - @Excel(name = "组织名称") + @Excel(name = "所属组织") @NotBlank(message = "不能为空") private String agencyName; - @Excel(name = "社会组织名称") + @Excel(name = "组织名称") @NotBlank(message = "不能为空") @Length(max=50,message = "不能超过50个字") private String societyName; - @Excel(name = "服务事项") + @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 = "负责人电话") + @Excel(name = "服务电话") @NotBlank(message = "不能为空") @Length(max=11,message = "不能超过11个字") private String mobile; - @Excel(name = "起始服务时间") + @Excel(name = "服务时间") @NotBlank(message = "不能为空") - private String serviceStartTime; - - @Excel(name = "终止服务时间") - @NotBlank(message = "不能为空") - private String serviceEndTime; + private String serviceTimeStr; @Excel(name = "管理员姓名") private String adminStaffName; @@ -72,10 +63,4 @@ public class IcSocietyOrgExcel extends ExcelVerifyInfo implements Serializable { @Excel(name = "地址") private String address; - @Excel(name = "经度") - private String longitude; - - @Excel(name = "维度") - private String latitude; - } diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExportExcel.java b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExportExcel.java new file mode 100644 index 0000000000..bf914d42d6 --- /dev/null +++ b/epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcSocietyOrgExportExcel.java @@ -0,0 +1,42 @@ +package com.epmet.excel; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import lombok.Data; + +/** + * @Description + * @Author yzm + * @Date 2022/8/24 8:41 + */ +@Data +public class IcSocietyOrgExportExcel { + @ExcelProperty(value = "所属组织") + @ColumnWidth(30) + private String agencyName; + + @ExcelProperty(value = "组织名称") + @ColumnWidth(30) + private String societyName; + + @ColumnWidth(50) + @ExcelProperty(value = "服务内容") + private String serviceMatters; + + @ColumnWidth(20) + @ExcelProperty(value = "服务电话") + private String mobile; + + @ColumnWidth(20) + @ExcelProperty(value = "服务时间") + private String serviceTimeStr; + + @ColumnWidth(20) + @ExcelProperty(value = "管理员姓名") + private String adminStaffName; + + @ColumnWidth(30) + @ExcelProperty(value = "地址") + private String address; +} + 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 800d2e447c..16e1cee8f4 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 @@ -43,6 +43,7 @@ import com.epmet.service.IcSocietyOrgService; import com.epmet.service.IcUserDemandRecService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,7 +53,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @@ -232,53 +232,59 @@ public class IcSocietyOrgServiceImpl extends BaseServiceImpl map=new HashMap<>(); //2.查询绑定的管理员信息 - Set staffNames = list.stream().map(item -> item.getAdminStaffName().trim()).collect(Collectors.toSet()); - GetByRealNamesFormDTO dto = new GetByRealNamesFormDTO(); - dto.setCustomerId(customerId); - dto.setRealNames(staffNames); - Result> staffResult = epmetUserOpenFeignClient.getByRealNames(dto); - if (!staffResult.success()) { - throw new RenException("获取工作人员基础信息失败......"); - } - if (null == staffResult.getData()) { - Iterator iter = list.iterator(); - while (iter.hasNext()) { - IcSocietyOrgExcel obj = iter.next(); - numList.add(obj.getRowNum()); - iterator.remove(); + Set staffNames = list.stream().filter(t -> StringUtils.isNotBlank(t.getAdminStaffName())).map(item -> item.getAdminStaffName().trim()).collect(Collectors.toSet()); + if(!CollectionUtils.isEmpty(staffNames)){ + GetByRealNamesFormDTO dto = new GetByRealNamesFormDTO(); + dto.setCustomerId(customerId); + dto.setRealNames(staffNames); + Result> staffResult = epmetUserOpenFeignClient.getByRealNames(dto); + if (!staffResult.success()) { + throw new RenException("获取工作人员基础信息失败......"); } - return numList; + if (null == staffResult.getData()) { + Iterator iter = list.iterator(); + while (iter.hasNext()) { + IcSocietyOrgExcel obj = iter.next(); + numList.add(obj.getRowNum()); + iterator.remove(); + } + return numList; + } + map= staffResult.getData().stream().collect(Collectors.toMap(CustomerStaffDTO::getRealName, CustomerStaffDTO::getUserId)); } - Map map = staffResult.getData().stream().collect(Collectors.toMap(CustomerStaffDTO::getRealName, CustomerStaffDTO::getUserId)); //3.遍历封装有效数据 List houseEntityList = new ArrayList<>(); Iterator iterator1 = list.iterator(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); while (iterator1.hasNext()) { - IcSocietyOrgExcel icHouseExcel = iterator1.next(); + IcSocietyOrgExcel icSocietyOrgExcel = iterator1.next(); IcSocietyOrgEntity entity = new IcSocietyOrgEntity(); entity.setCustomerId(customerId); entity.setAgencyId(staffInfoCache.getAgencyId()); entity.setPids(staffInfoCache.getAgencyPIds()); - entity.setSocietyName(icHouseExcel.getSocietyName()); - entity.setServiceMatters(icHouseExcel.getServiceMatters()); - entity.setPersonInCharge(icHouseExcel.getPersonInCharge()); - entity.setMobile(icHouseExcel.getMobile()); - entity.setServiceStartTime(sdf.parse(icHouseExcel.getServiceStartTime())); - entity.setServiceEndTime(sdf.parse(icHouseExcel.getServiceEndTime())); - entity.setAdminStaffId(map.containsKey(icHouseExcel.getAdminStaffName().trim()) ? map.get(icHouseExcel.getAdminStaffName().trim()) : ""); - if ("".equals(entity.getAdminStaffId())) { - numList.add(icHouseExcel.getRowNum()); - log.warn(String.format("绑定的管理员不存在,管理员名称->%s,行号->%s", icHouseExcel.getAdminStaffName(), icHouseExcel.getRowNum())); + entity.setSocietyName(icSocietyOrgExcel.getSocietyName()); + entity.setServiceMatters(icSocietyOrgExcel.getServiceMatters()); + //负责人去掉 + // entity.setPersonInCharge(icSocietyOrgExcel.getPersonInCharge()); + entity.setMobile(icSocietyOrgExcel.getMobile()); + //服务时间手动输入 + entity.setServiceTimeStr(icSocietyOrgExcel.getServiceTimeStr()); + // entity.setServiceStartTime(sdf.parse(icHouseExcel.getServiceStartTime())); + // entity.setServiceEndTime(sdf.parse(icHouseExcel.getServiceEndTime())); + entity.setAdminStaffId(MapUtils.isNotEmpty(map)&&map.containsKey(icSocietyOrgExcel.getAdminStaffName().trim()) ? map.get(icSocietyOrgExcel.getAdminStaffName().trim()) : ""); + if (StringUtils.isBlank(entity.getAdminStaffId())) { + numList.add(icSocietyOrgExcel.getRowNum()); + log.warn(String.format("绑定的管理员不存在,管理员名称->%s,行号->%s", icSocietyOrgExcel.getAdminStaffName(), icSocietyOrgExcel.getRowNum())); iterator1.remove(); continue; } - entity.setAddress(icHouseExcel.getAddress()); - entity.setLongitude(icHouseExcel.getLongitude()); - entity.setLatitude(icHouseExcel.getLatitude()); + entity.setAddress(icSocietyOrgExcel.getAddress()); + //经纬度不能导入 + // entity.setLongitude(icHouseExcel.getLongitude()); + // entity.setLatitude(icHouseExcel.getLatitude()); houseEntityList.add(entity); } diff --git a/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/societyorg_import_template.xlsx b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/societyorg_import_template.xlsx new file mode 100644 index 0000000000..5d02dae502 Binary files /dev/null and b/epmet-module/epmet-heart/epmet-heart-server/src/main/resources/templates/societyorg_import_template.xlsx differ