diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/form/JimuReportExportRequestDTO.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/form/JimuReportExportRequestDTO.java new file mode 100644 index 0000000000..7e66c588e9 --- /dev/null +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/form/JimuReportExportRequestDTO.java @@ -0,0 +1,27 @@ +package com.epmet.commons.feignclient.dtos.form; + +import lombok.Data; + +/** + * 批量导出用的requst dto + */ +@Data +public class JimuReportExportRequestDTO { + private String excelConfigId; + private ExportRequestQueryParam queryParam = new ExportRequestQueryParam(); + + /** + * 批量导出用的请求参数 + */ + @Data + public static class ExportRequestQueryParam { + private String id; + private String token; + private String paramKey; + private String pageNo; + private Integer pageSize; + private String currentPageNo; + private Integer currentPageSize; + } + +} \ No newline at end of file diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java new file mode 100644 index 0000000000..fe6bf6240b --- /dev/null +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportDbDataResultDTO.java @@ -0,0 +1,17 @@ +package com.epmet.commons.feignclient.dtos.result; + +import lombok.Data; + +@Data +public class JimuReportDbDataResultDTO { + + private ReportDB reportDb; + + @Data + public static class ReportDB { + private String apiUrl; + private String apiMethod; + private String isList; + } + +} diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportFieldTreeResultDTO.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportFieldTreeResultDTO.java new file mode 100644 index 0000000000..ef4037afe8 --- /dev/null +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/dtos/result/JimuReportFieldTreeResultDTO.java @@ -0,0 +1,26 @@ +package com.epmet.commons.feignclient.dtos.result; + +import lombok.Data; + +import java.util.List; + +/** + * 积木报表,报表字段列表 + */ +@Data +public class JimuReportFieldTreeResultDTO { + + private Boolean expand; + private String code; + private List children; + private String dbId; + private String type; + private String isList; + + @Data + public static class Child { + private Boolean expand; + private String title; + private String fieldText; + } +} diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/JiMuReportOpenFeignClient.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/JiMuReportOpenFeignClient.java index 957521eabb..08b07c0dab 100644 --- a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/JiMuReportOpenFeignClient.java +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/JiMuReportOpenFeignClient.java @@ -2,15 +2,18 @@ package com.epmet.commons.feignclient.feigns; import com.epmet.commons.feignclient.dtos.JiMuPage; import com.epmet.commons.feignclient.dtos.JiMuResult; +import com.epmet.commons.feignclient.dtos.form.JimuReportExportRequestDTO; import com.epmet.commons.feignclient.dtos.result.JiMuReportDetailResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportDbDataResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportFieldTreeResultDTO; import com.epmet.commons.feignclient.feigns.fallback.JiMuReportOpenFeignClientFallbackFactory; import com.epmet.commons.tools.constant.ServiceConstant; +import feign.Response; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.util.MultiValueMap; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; +import java.util.List; import java.util.Map; /** @@ -33,4 +36,12 @@ public interface JiMuReportOpenFeignClient { @GetMapping("jmreport/show") JiMuResult> getReport(@RequestParam("id") String id,@RequestParam("apiUrl") String apiUrl,@RequestParam("params") String params,@RequestHeader MultiValueMap headers); + @GetMapping("/jmreport/field/tree/{report-id}") + JiMuResult>> fieldTree(@PathVariable("report-id") String reportId); + + @GetMapping("jmreport/loadDbData/{report-id}") + JiMuResult loadDbData(@PathVariable("report-id") String reportId); + + @PostMapping("jmreport/exportAllExcelStream") + Response exportAllExcelStream(JimuReportExportRequestDTO param); } diff --git a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/fallback/JiMuReportOpenFeignClientFallback.java b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/fallback/JiMuReportOpenFeignClientFallback.java index 409ae4ce19..b1c793d560 100644 --- a/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/fallback/JiMuReportOpenFeignClientFallback.java +++ b/epmet-commons/epmet-commons-feignclient/src/main/java/com/epmet/commons/feignclient/feigns/fallback/JiMuReportOpenFeignClientFallback.java @@ -2,10 +2,15 @@ package com.epmet.commons.feignclient.feigns.fallback; import com.epmet.commons.feignclient.dtos.JiMuPage; import com.epmet.commons.feignclient.dtos.JiMuResult; +import com.epmet.commons.feignclient.dtos.form.JimuReportExportRequestDTO; import com.epmet.commons.feignclient.dtos.result.JiMuReportDetailResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportDbDataResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportFieldTreeResultDTO; import com.epmet.commons.feignclient.feigns.JiMuReportOpenFeignClient; +import feign.Response; import org.springframework.util.MultiValueMap; +import java.util.List; import java.util.Map; /** @@ -26,4 +31,21 @@ public class JiMuReportOpenFeignClientFallback implements JiMuReportOpenFeignCli JiMuResult> rst = new JiMuResult<>(false, "请求失败", 200, null,null); return rst; } + + @Override + public JiMuResult loadDbData(String reportId) { + JiMuResult rst = new JiMuResult<>(false, "请求失败", 200, null,null); + return rst; + } + + @Override + public Response exportAllExcelStream(JimuReportExportRequestDTO param) { + return null; + } + + @Override + public JiMuResult>> fieldTree(String reportId) { + JiMuResult rst = new JiMuResult<>(false, "请求失败", 200, null,null); + return rst; + } } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/EpmetBaseRequestInterceptor.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/EpmetBaseRequestInterceptor.java index f7eb3a778d..746de189e9 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/EpmetBaseRequestInterceptor.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/EpmetBaseRequestInterceptor.java @@ -25,8 +25,6 @@ public class EpmetBaseRequestInterceptor implements RequestInterceptor { // Map requestHeaders = getHeadersFromRequest(); - log.debug("EpmetBaseRequestInterceptor#apply#inheritableAdditionalHeaders:" + requestHeaders); - if (requestHeaders != null && requestHeaders.size() > 0) { for (Map.Entry kv : requestHeaders.entrySet()) { template.header(kv.getKey(), kv.getValue()); diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/EpmetRequestHolder.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/EpmetRequestHolder.java index c86ef93152..e24f420fa7 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/EpmetRequestHolder.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/EpmetRequestHolder.java @@ -2,6 +2,7 @@ package com.epmet.commons.tools.utils; import com.alibaba.ttl.TransmittableThreadLocal; import com.epmet.commons.tools.constant.AppClientConstant; +import com.epmet.commons.tools.constant.Constant; import org.apache.commons.lang3.StringUtils; import java.util.HashMap; @@ -86,6 +87,14 @@ public class EpmetRequestHolder { return getHeader(AppClientConstant.CLIENT); } + /** + * 获取登陆用户的token + * @return + */ + public static String getLoginUserAuthorizationToken() { + return getHeader(Constant.AUTHORIZATION_HEADER); + } + /** * 获取所有 * @return diff --git a/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/ReportHouseTestFormDTO.java b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/ReportHouseTestFormDTO.java new file mode 100644 index 0000000000..b5206162e4 --- /dev/null +++ b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/form/ReportHouseTestFormDTO.java @@ -0,0 +1,12 @@ +package com.epmet.dto.form; + +import lombok.Data; + +@Data +public class ReportHouseTestFormDTO { + + private Integer pageNo = 1; + private Integer pageSize = 20; + private String id; + +} diff --git a/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/result/ReportHouseTestResultDTO.java b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/result/ReportHouseTestResultDTO.java new file mode 100644 index 0000000000..b9f074683d --- /dev/null +++ b/epmet-module/gov-org/gov-org-client/src/main/java/com/epmet/dto/result/ReportHouseTestResultDTO.java @@ -0,0 +1,14 @@ +package com.epmet.dto.result; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ReportHouseTestResultDTO { + private String houseId; + private String doorName; + private String fullName; +} diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/ReportController.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/ReportController.java new file mode 100644 index 0000000000..7576b1fa6d --- /dev/null +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/controller/ReportController.java @@ -0,0 +1,29 @@ +package com.epmet.controller; + +import com.epmet.commons.tools.page.PageData; +import com.epmet.commons.tools.utils.Result; +import com.epmet.dto.form.ReportHouseTestFormDTO; +import com.epmet.dto.result.ReportHouseTestResultDTO; +import com.epmet.service.HouseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("report") +public class ReportController { + + @Autowired + private HouseService houseService; + + /** + * todo 测试用的 + * @return + */ + @PostMapping("houses/test") + public Result> listHouses(@RequestBody ReportHouseTestFormDTO input, @RequestParam(value="id", required = false) String id) { + System.out.println("id:"+id); + PageData pageData = houseService.listHouses4ReportTest(id, input.getPageNo(), input.getPageSize()); + return new Result>().ok(pageData); + } + +} diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/HouseService.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/HouseService.java index a5c940b0c1..3461cc3530 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/HouseService.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/HouseService.java @@ -175,4 +175,6 @@ public interface HouseService { Result getHomeInfoByHouseCode(HouseInfoFormDTO dto); void orgIdPathInit(); + + PageData listHouses4ReportTest(String houseId, Integer pageNo, Integer pageSize); } diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java index 0112a2a34c..c0e3933392 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/HouseServiceImpl.java @@ -1222,4 +1222,20 @@ public class HouseServiceImpl implements HouseService, ResultDataResolver { } recureAppendParentAgencyId(pid, result); } + + @Override + public PageData listHouses4ReportTest(String houseId, Integer pageNo, Integer pageSize) { + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(IcHouseEntity::getBuildingId, "1514143207495208961"); + query.eq(StringUtils.isNotBlank(houseId), IcHouseEntity::getId, houseId); + + PageHelper.startPage(pageNo, pageSize); + List icHouseEntities = icHouseDao.selectList(query); + + PageInfo pi = new PageInfo<>(icHouseEntities); + + List houseDtos = icHouseEntities.stream().map(house -> new ReportHouseTestResultDTO(house.getId(), house.getDoorName(), house.getFullName())).collect(Collectors.toList()); + + return new PageData<>(houseDtos, pi.getTotal()); + } } diff --git a/epmet-module/oper-customize/oper-customize-client/src/main/java/com/epmet/dto/form/ReportBatchExportFormDTO.java b/epmet-module/oper-customize/oper-customize-client/src/main/java/com/epmet/dto/form/ReportBatchExportFormDTO.java new file mode 100644 index 0000000000..a2673061a5 --- /dev/null +++ b/epmet-module/oper-customize/oper-customize-client/src/main/java/com/epmet/dto/form/ReportBatchExportFormDTO.java @@ -0,0 +1,15 @@ +package com.epmet.dto.form; + +import lombok.Data; + +/** + * 报表批量导出form dto + */ +@Data +public class ReportBatchExportFormDTO { + + private String token; + private String paramKey; + private String reportId; + +} diff --git a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/controller/IcCustomerReportController.java b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/controller/IcCustomerReportController.java index 0c4cdcc0fa..afd9fabde9 100644 --- a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/controller/IcCustomerReportController.java +++ b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/controller/IcCustomerReportController.java @@ -2,12 +2,11 @@ package com.epmet.controller; import com.epmet.commons.feignclient.dtos.result.JiMuReportResultDTO; import com.epmet.commons.tools.annotation.LoginUser; +import com.epmet.commons.tools.annotation.ReportRequest; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; -import com.epmet.dto.form.PreviewReportFormDTO; -import com.epmet.dto.form.ReportEditFormDTO; -import com.epmet.dto.form.ReportListFormDTO; +import com.epmet.dto.form.*; import com.epmet.dto.result.PreviewReportResDTO; import com.epmet.dto.result.ReportResultDTO; import com.epmet.service.IcCustomerReportService; @@ -17,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; import java.util.List; @@ -84,4 +85,24 @@ public class IcCustomerReportController { return new Result>().ok(icCustomerReportService.reportIdAndName(formDTO)); } + /** + * 批量导出 + */ + @PostMapping("batch-export") + public void batchExport(@RequestBody ReportBatchExportFormDTO input, HttpServletResponse response) { + + String reportId = input.getReportId(); + String paramKey = input.getParamKey(); + String token = input.getToken(); + + icCustomerReportService.batchExport(reportId, token, paramKey, response); + } + + @ReportRequest + @PostMapping("test") + public void test(@RequestBody ExportResiUserFormDTO input) { + + System.out.println(input); + } + } diff --git a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/IcCustomerReportService.java b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/IcCustomerReportService.java index b833104c97..6bd359dd57 100644 --- a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/IcCustomerReportService.java +++ b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/IcCustomerReportService.java @@ -9,6 +9,7 @@ import com.epmet.dto.result.PreviewReportResDTO; import com.epmet.dto.result.ReportResultDTO; import com.epmet.entity.IcCustomerReportEntity; +import javax.servlet.http.HttpServletResponse; import java.util.List; /** @@ -49,4 +50,6 @@ public interface IcCustomerReportService extends BaseService reportIdAndName(ReportListFormDTO formDTO); + + void batchExport(String reportId, String token, String paramKey, HttpServletResponse response); } \ No newline at end of file diff --git a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java index 9abbc699f7..41bef27c2d 100644 --- a/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java +++ b/epmet-module/oper-customize/oper-customize-server/src/main/java/com/epmet/service/impl/IcCustomerReportServiceImpl.java @@ -1,24 +1,28 @@ package com.epmet.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.epmet.commons.feignclient.dtos.JiMuPage; import com.epmet.commons.feignclient.dtos.JiMuResult; import com.epmet.commons.feignclient.dtos.form.JiMuReportFormDTO; +import com.epmet.commons.feignclient.dtos.form.JimuReportExportRequestDTO; import com.epmet.commons.feignclient.dtos.result.JiMuReportDetailResultDTO; import com.epmet.commons.feignclient.dtos.result.JiMuReportResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportDbDataResultDTO; +import com.epmet.commons.feignclient.dtos.result.JimuReportFieldTreeResultDTO; import com.epmet.commons.feignclient.feigns.JiMuReportOpenFeignClient; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; -import com.epmet.commons.tools.constant.FieldConstant; -import com.epmet.commons.tools.constant.NumConstant; -import com.epmet.commons.tools.constant.StrConstant; +import com.epmet.commons.tools.constant.*; import com.epmet.commons.tools.dto.form.DictListFormDTO; import com.epmet.commons.tools.dto.result.DictListResultDTO; +import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetException; +import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.redis.RedisUtils; -import com.epmet.commons.tools.utils.ConvertUtils; -import com.epmet.commons.tools.utils.EpmetRequestHolder; -import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.utils.*; import com.epmet.constant.CustomerFunctionConstant; import com.epmet.dao.IcCustomerReportDao; import com.epmet.dto.CustomerDTO; @@ -33,8 +37,10 @@ import com.epmet.feign.EpmetAdminOpenFeignClient; import com.epmet.feign.OperCrmOpenFeignClient; import com.epmet.service.IcCustomerReportService; import com.epmet.service.IcReportFunService; +import feign.Response; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -42,11 +48,20 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.MultiValueMap; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** * 客户报表关系表 @@ -70,8 +85,8 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl getWrapper(Map params){ - String id = (String)params.get(FieldConstant.ID_HUMP); + private QueryWrapper getWrapper(Map params) { + String id = (String) params.get(FieldConstant.ID_HUMP); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(id), FieldConstant.ID, id); @@ -81,6 +96,7 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl headers = new HttpHeaders(); headers.add("token", authorization); JiMuResult> result = jiMuReportOpenFeignClient.getList(1, 10000, "", "datainfo", authorization, headers); - if (!result.isSuccess()){ + if (!result.isSuccess()) { throw new EpmetException("获取所有jm报表失败"); } - List> records = (List>)result.getResult().getRecords(); + List> records = (List>) result.getResult().getRecords(); List allReports = new ArrayList<>(); for (Map record : records) { - allReports.add(ConvertUtils.mapToEntity(record,JiMuReportResultDTO.class)); + allReports.add(ConvertUtils.mapToEntity(record, JiMuReportResultDTO.class)); } - if (CollectionUtils.isEmpty(allReports)){ + if (CollectionUtils.isEmpty(allReports)) { return new ArrayList<>(); } List allReportList = new ArrayList<>(); @@ -110,26 +126,26 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl reportList = baseDao.reportList(null,null); - if (CollectionUtils.isEmpty(reportList)){ + List reportList = baseDao.reportList(null, null); + if (CollectionUtils.isEmpty(reportList)) { return allReportList; } Result> dictList = adminOpenFeignClient.dictList(new DictListFormDTO(CustomerFunctionConstant.REPORT_CATEGORY)); - if (!dictList.success()){ + if (!dictList.success()) { throw new EpmetException("查询字典信息失败:" + CustomerFunctionConstant.REPORT_CATEGORY); } - if (CollectionUtils.isNotEmpty(dictList.getData())){ + if (CollectionUtils.isNotEmpty(dictList.getData())) { dictList.getData().forEach(d -> reportList.stream().filter(r -> d.getValue().equals(r.getCategoryKey())).forEach(r -> r.setCategoryName(d.getLabel()))); } Result> allCustomerList = operCrmOpenFeignClient.getAllCustomerList(); - if (!allCustomerList.success()){ + if (!allCustomerList.success()) { throw new EpmetException("获取客户信息失败..."); } - if (CollectionUtils.isNotEmpty(allCustomerList.getData())){ + if (CollectionUtils.isNotEmpty(allCustomerList.getData())) { allCustomerList.getData().forEach(c -> reportList.stream().filter(r -> c.getId().equals(r.getCustomerId())).forEach(r -> r.setCustomerName(c.getCustomerName()))); } Map> groupByReport = reportList.stream().collect(Collectors.groupingBy(ReportResultDTO::getReportId)); - groupByReport.forEach((reportId,l) -> allReportList.stream().filter(a -> a.getReportId().equals(reportId)).forEach(a -> a.setCustomerList(ConvertUtils.sourceToTarget(l, ReportResultDTO.CustomerList.class)))); + groupByReport.forEach((reportId, l) -> allReportList.stream().filter(a -> a.getReportId().equals(reportId)).forEach(a -> a.setCustomerList(ConvertUtils.sourceToTarget(l, ReportResultDTO.CustomerList.class)))); return allReportList; } @@ -152,6 +168,7 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl reportIds = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(formDTO.getCustomerList())){ + if (CollectionUtils.isNotEmpty(formDTO.getCustomerList())) { reportIds = baseDao.getIdsByCustomer(formDTO.getCustomerList().stream().map(m -> m.getCustomerId()).collect(Collectors.toList()), formDTO.getReportId()); } - if (formDTO.getType().equals(CustomerFunctionConstant.REPORT_EDIT)){ + if (formDTO.getType().equals(CustomerFunctionConstant.REPORT_EDIT)) { // 单个编辑 baseDao.delCustomerReport(Arrays.asList(formDTO.getCustomerList().get(NumConstant.ZERO).getCustomerId()), formDTO.getReportId()); - }else if (formDTO.getType().equals(CustomerFunctionConstant.REPORT_ADD)){ + } else if (formDTO.getType().equals(CustomerFunctionConstant.REPORT_ADD)) { baseDao.delCustomerReport(null, formDTO.getReportId()); } - if (CollectionUtils.isNotEmpty(reportIds)){ + if (CollectionUtils.isNotEmpty(reportIds)) { baseDao.delCustomerReportFun(reportIds); } - if (CollectionUtils.isNotEmpty(formDTO.getCustomerList())){ + if (CollectionUtils.isNotEmpty(formDTO.getCustomerList())) { List entities = new ArrayList<>(); List funEntities = new ArrayList<>(); formDTO.getCustomerList().forEach(c -> { @@ -180,7 +197,7 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl { IcReportFunEntity funEntity = new IcReportFunEntity(); funEntity.setCustomerReportId(reportEntity.getId()); @@ -190,12 +207,13 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl reportIdAndName(ReportListFormDTO formDTO) { List reportList = baseDao.reportList(formDTO.getCategoryKeys(), formDTO.getCustomerId()); - if (CollectionUtils.isEmpty(reportList)){ + if (CollectionUtils.isEmpty(reportList)) { return new ArrayList<>(); } List recordIds = reportList.stream().map(m -> m.getReportId()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList()); @@ -216,9 +234,9 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl headers = new HttpHeaders(); headers.add("token", authorization); headers.add("Authorization", authorization); - JiMuResult> report = jiMuReportOpenFeignClient.getReport(r,"","",headers); - if (!report.isSuccess()){ - throw new EpmetException("获取jm报表详情失败:"+r); + JiMuResult> report = jiMuReportOpenFeignClient.getReport(r, "", "", headers); + if (!report.isSuccess()) { + throw new EpmetException("获取jm报表详情失败:" + r); } JiMuReportResultDTO data = ConvertUtils.mapToEntity(report.getResult(), JiMuReportResultDTO.class); data.setReportId(data.getId()); @@ -229,13 +247,235 @@ public class IcCustomerReportServiceImpl extends BaseServiceImpl reportEntities,List funEntities){ - if (CollectionUtils.isNotEmpty(reportEntities)){ + public void insetCustomerReportAndFun(List reportEntities, List funEntities) { + if (CollectionUtils.isNotEmpty(reportEntities)) { insertBatch(reportEntities); } - if (CollectionUtils.isNotEmpty(funEntities)){ + if (CollectionUtils.isNotEmpty(funEntities)) { reportFunService.insertBatch(funEntities); } } + @Override + public void batchExport(String reportId, String token, String paramKey, HttpServletResponse response) { + + // 1. 首先去积木服务,查询出报表数据源的详细信息,包括报表url,isList;获取到哪一列是id(idFieldName) + + // 根据报表id查询数据源列表,取出第一个 + JiMuResult>> fResult = jiMuReportOpenFeignClient.fieldTree(reportId); + String datasourceId = fResult.getResult().get(0).get(0).getDbId(); + + // 根据数据源ID查询数据源信息 + JiMuResult dbData = jiMuReportOpenFeignClient.loadDbData(datasourceId); + JimuReportDbDataResultDTO.ReportDB reportDb = dbData.getResult().getReportDb(); + + // api的url + String apiUrl = reportDb.getApiUrl(); + // api方法 + //String apiMethod = reportDb.getApiMethod(); + // 是否是集合 + //String isList = reportDb.getIsList(); + // 返回的列表中,哪个字段是ID + String idFieldName = null; + boolean isHttps = false; + + Matcher matcher = Pattern.compile("(http://|https://).+idFieldName=(\\w+).*").matcher(apiUrl); + if (matcher.matches()) { + String proto = matcher.group(1); + if ("https://".equals(proto)) { + isHttps = true; + } + idFieldName = matcher.group(2); + } + + if (StringUtils.isBlank(idFieldName)) { + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "配置的业务api url缺少idFieldName列"); + } + + // 2. 调用该url的接口,获取到一个列表,根据idFieldName取出ID列 + List ids = listIds(apiUrl, isHttps, idFieldName); + + // 3. 然后以这一列作为查询条件,循环,继续调用该接口,得到单条数据,每一条数据都下载一个excel,最后将其打包为一个压缩包下载 + Path storePath = makeTemporaryDownloadDir(reportId); + + // 4.生成压缩文件 + Path zipFile = downloadAndComppress(storePath, reportId, paramKey, ids); + + // 5.下载 + try (FileInputStream fis = new FileInputStream(zipFile.toFile())) { + ServletOutputStream os = response.getOutputStream(); + response.setCharacterEncoding("UTF-8"); + response.setHeader("content-Type", "application/zip"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getFileName().toString(), "UTF-8")); + + IOUtils.copy(fis, os); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + Files.delete(zipFile); + } catch (IOException e) { + log.error("【报表批量导出】删除临时zip文件失败"); + } + } + } + + /** + * 批量下载 + * @param storePath 本次导出文件的存储路径,子目录包括:zip文件,时间戳命名的子目录用于存放临时excel(导出完成会删掉) + * @param ids + * @return 压缩文件路径 + */ + private Path downloadAndComppress(Path storePath, String reportId, String paramKey, List ids) { + // 请求头 + Map headers = new HashMap<>(); + headers.put(Constant.AUTHORIZATION_HEADER, EpmetRequestHolder.getHeader(Constant.AUTHORIZATION_HEADER)); + + // 请求体 + JimuReportExportRequestDTO param = new JimuReportExportRequestDTO(); + param.setExcelConfigId(reportId); + param.getQueryParam().setCurrentPageNo("1"); + param.getQueryParam().setCurrentPageSize(20); + param.getQueryParam().setPageNo("1"); + param.getQueryParam().setPageSize(20); + param.getQueryParam().setParamKey(paramKey); + param.getQueryParam().setToken(EpmetRequestHolder.getLoginUserAuthorizationToken()); + + final ArrayList files = new ArrayList<>(); + + final String currentTimeStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")); + + // 创建存放xlsx的临时目录 + Path xlsxStorePath = storePath.resolve(currentTimeStr); + try { + Files.createDirectories(xlsxStorePath); + } catch (IOException e) { + throw new EpmetException("【报表批量导出】生成临时目录失败:" + xlsxStorePath.toString()); + } + + + // 1. 循环下载所有id对应的excel + for (String id : ids) { + param.getQueryParam().setId(id); + final Response response = jiMuReportOpenFeignClient.exportAllExcelStream(param); + // 取出文件后缀 + final LinkedList header = (LinkedList) response.headers().get("content-disposition"); + final Matcher matcher = Pattern.compile("attachment;filename=(.+)(.xls|.xlsx)").matcher(header.get(0)); + if (!matcher.matches()) { + log.error("【报表批量导出】批量导出失败,后缀不匹配。文件名:{}, 报表ID:{}, 业务数据ID:{}", header.get(0), reportId, id); + continue; + } + + final File excelFile = xlsxStorePath.resolve("file_" + id + matcher.group(2)).toFile(); + + files.add(excelFile); + try (FileOutputStream fos = new FileOutputStream(excelFile)) { + IOUtils.copy(response.body().asInputStream(), fos); + } catch (Exception e) { + log.error("【报表批量导出】生成临时表格文件失败,文件名:{},报表ID:{}, 业务数据ID:{},错误信息:{}", header.get(0), reportId, id, ExceptionUtils.getErrorStackTrace(e)); + } + } + + // 2,打包 + Path zipFile = storePath.resolve("report_export_" + currentTimeStr + ".zip"); + try (final FileOutputStream fos = new FileOutputStream(zipFile.toFile()); + final ZipOutputStream zos = new ZipOutputStream(fos)) { + + // 循环每一个文件 + for (File file : files) { + try (final FileInputStream fis = new FileInputStream(file.getAbsolutePath())) { + zos.putNextEntry(new ZipEntry("report_export_" + currentTimeStr + "/" + file.getName())); + final byte[] buffer = new byte[10240]; + for (int len; (len = fis.read(buffer)) > 0; ) { + zos.write(buffer, 0, len); + } + } finally { + zos.closeEntry(); + } + } + + zos.flush(); + } catch (Exception e) { + log.error("【报表批量导出】压缩文件失败,错误信息:{}", ExceptionUtils.getErrorStackTrace(e)); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "压缩失败", "压缩失败"); + } finally { + // 删除临时文件 + cleanTemporaryFiles(storePath.resolve(currentTimeStr)); + } + + return zipFile; + } + + /** + * 清理临时保存文件 + */ + private void cleanTemporaryFiles(Path tempStorePath) { + // 删除内部文件 + for (File file : tempStorePath.toFile().listFiles()) { + try { + Files.delete(file.toPath()); + } catch (IOException e) { + log.error("【报表批量导出】删除临时文件失败:{}", ExceptionUtils.getErrorStackTrace(e)); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), null, null); + } + } + + // 删除文件夹 + try { + Files.delete(tempStorePath); + } catch (IOException e) { + log.error("【报表批量导出】删除临时目录失败:{}", ExceptionUtils.getErrorStackTrace(e)); + throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), null, null); + } + } + + /** + * 创建临时目录 + * + * @param reportId + * @return + */ + private Path makeTemporaryDownloadDir(String reportId) { + + String userId = EpmetRequestHolder.getLoginUserId(); + try { + return FileUtils.getAndCreateDirUnderEpmetFilesDir("report/batch_export/", reportId, userId); + } catch (IOException e) { + throw new EpmetException("报表】批量导出-创建临时目录失败,错误信息:" + ExceptionUtils.getErrorStackTrace(e)); + } + } + + /** + * 列出id列表,批量下载用 + * + * @param apiUrl + * @param isHttps + * @param idFieldName + * @return + */ + public List listIds(String apiUrl, boolean isHttps, String idFieldName) { + apiUrl = apiUrl.replace("${id}", ""); + + Map headers = new HashMap<>(); + headers.put(Constant.AUTHORIZATION_HEADER, EpmetRequestHolder.getHeader(Constant.AUTHORIZATION_HEADER)); + Result stringResult = HttpClientManager.getInstance().sendPost(apiUrl, isHttps, "{}", headers); + + JSONObject dataJsonObject = JSON.parseObject(stringResult.getData()); + Object data = dataJsonObject.get("data"); + + JSONArray array = new JSONArray(); + ArrayList ids = new ArrayList<>(); + + // 这种可能是pageData的 + if (data instanceof JSONObject) { + JSONObject jo = (JSONObject) data; + array = jo.getJSONArray("list"); + } + + for (ListIterator it = array.listIterator(); it.hasNext(); ) { + JSONObject e = (JSONObject) it.next(); + ids.add(e.getString(idFieldName)); + } + return ids; + } } \ No newline at end of file