大数据与最优化研究所 vor 1 Jahr
Ursprung
Commit
b5a1bdac20

+ 95 - 30
src/main/java/com/xjrsoft/module/ledger/controller/LedgerExportController.java

@@ -1,82 +1,99 @@
 package com.xjrsoft.module.ledger.controller;
 
+import cn.hutool.core.convert.Convert;
+import cn.hutool.db.Entity;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.support.ExcelTypeEnum;
 import com.alibaba.excel.util.ListUtils;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.xjrsoft.common.exception.MyException;
+import com.xjrsoft.common.model.result.RT;
 import com.xjrsoft.module.ledger.dto.LedgerExportDataDto;
+import com.xjrsoft.module.ledger.entity.LedgerConfig;
 import com.xjrsoft.module.ledger.entity.LedgerConfigRelease;
 import com.xjrsoft.module.ledger.service.ILedgerConfigReleaseService;
 import com.xjrsoft.module.ledger.service.ILedgerConfigService;
-import com.xjrsoft.module.system.service.IMenuService;
+import com.xjrsoft.module.magicapi.service.IMagicApiService;
+import com.xjrsoft.module.magicapi.vo.MagicApiInfoVo;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.BeanUtils;
 import org.springframework.http.ResponseEntity;
 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.ssssssss.magicapi.core.service.MagicAPIService;
+import org.ssssssss.magicapi.modules.db.model.PageResult;
 
 import javax.validation.Valid;
+import java.io.ByteArrayOutputStream;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
-* @title: 台账导出
-* @Author szs
-* @Date: 2024-03-20
-* @Version 1.0
-*/
+ * @title: 台账导出
+ * @Author szs
+ * @Date: 2024-03-20
+ * @Version 1.0
+ */
 @RestController
 @RequestMapping("/ledger" + "/ledgerConfig")
-@Api(value = "/ledger"  + "/ledgerConfig",tags = "台账导出")
+@Api(value = "/ledger" + "/ledgerConfig", tags = "台账导出")
 @AllArgsConstructor
 public class LedgerExportController {
 
     private ILedgerConfigReleaseService ledgerConfigReleaseService;
 
+    private ILedgerConfigService ledgerConfigService;
+
+    private IMagicApiService magicApiService;
+
+    private MagicAPIService magicAPIService;
+
     @PostMapping("/export-query")
     @ApiOperation(value = "导出")
     public ResponseEntity<byte[]> exportData(@Valid @RequestBody LedgerExportDataDto dto) {
         LedgerConfigRelease release = ledgerConfigReleaseService.getById(dto.getConfigIntoId());
-        if(release == null){
+        if (release == null) {
             throw new MyException("导出数据失败");
         }
 
-        JsonParser parser = new JsonParser();
-        JsonObject jsonObject = parser.parse(release.getConfigJson()).getAsJsonObject();
-
-        JsonArray columnConfigs = jsonObject.get("columnConfigs").getAsJsonArray();
-        //构建excel头
-        buildExcelHeader(columnConfigs);
-
-        for (JsonElement element : columnConfigs) {
-
-//            System.out.println(element.getAsJsonObject().get("key").getAsString());
-
-
+        LedgerConfig ledgerConfig = ledgerConfigService.getById(release.getLedgerConfigId());
 
+        if (ledgerConfig == null) {
+            throw new MyException("导出数据失败");
         }
 
+        JsonParser parser = new JsonParser();
+        JsonObject jsonObject = parser.parse(release.getConfigJson()).getAsJsonObject();
 
-//        List<WfSubscriptionExcelVo> customerList = wfSubscriptionService.getList(dto);
-//        ByteArrayOutputStream bot = new ByteArrayOutputStream();
-//        EasyExcel.write(bot, WfSubscriptionExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);
+        JsonArray columnConfigs = jsonObject.get("columnConfigs").getAsJsonArray();
 
-//        return RT.fileStream(bot.toByteArray(), "WfSubscription" + ExcelTypeEnum.XLSX.getValue());
+        JsonObject apiInfo = jsonObject.get("apiInfo").getAsJsonObject();
+        //构建excel头
+        List<List<String>> headlist = buildExcelHeader(columnConfigs);
 
+        List<List<Object>> dataList = buildExcelDataList(apiInfo, headlist, dto.getQueryParam());
 
+        ByteArrayOutputStream bot = new ByteArrayOutputStream();
+        EasyExcel.write(bot).head(headlist).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);
 
-//        // 写法1
-//        String fileName = TestFileUtil.getPath() + "noModelWrite" + System.currentTimeMillis() + ".xlsx";
-//        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
-//        EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
-        return null;
+        return RT.fileStream(bot.toByteArray(), ledgerConfig.getName() + ExcelTypeEnum.XLSX.getValue());
     }
 
-    private List<List<String>> buildExcelHeader(JsonArray columnConfigs){
+    /**
+     * 构建excel标题列
+     *
+     * @param columnConfigs
+     * @return
+     */
+    private List<List<String>> buildExcelHeader(JsonArray columnConfigs) {
         List<List<String>> list = ListUtils.newArrayList();
         for (JsonElement element : columnConfigs) {
             String key = element.getAsJsonObject().get("label").getAsString();
@@ -87,4 +104,52 @@ public class LedgerExportController {
         return list;
     }
 
+    /**
+     * 构建数据集合
+     *
+     * @param apiInfo
+     * @return
+     */
+    private List<List<Object>> buildExcelDataList(JsonObject apiInfo, List<List<String>> headlist, Map<String, Object> queryParam) {
+        //获取magicApi的id
+        String apiId = apiInfo.get("apiId").getAsString();
+
+        //获取magicApi的入参
+        JsonArray apiParams = apiInfo.get("apiParams").getAsJsonArray();
+        JsonArray tableInfo = apiParams.get(0).getAsJsonObject().get("tableInfo").getAsJsonArray();
+
+        MagicApiInfoVo info = magicApiService.info(apiId);
+        Map<String, Object> params = new HashMap<>();
+
+
+        for (JsonElement element : tableInfo) {
+            String key = element.getAsJsonObject().get("name").getAsString();
+            if (queryParam != null && queryParam.containsKey(key) && queryParam.get(key) != null) {
+                params.put(key, queryParam.get(key));
+            }
+        }
+        if (!params.containsKey("limit")) {
+            params.put("limit", 1);
+        }
+        if (!params.containsKey("size")) {
+            params.put("size", Integer.MAX_VALUE);
+        }
+        PageResult res = magicAPIService.execute(info.getMethod(), info.getPath(), params);
+
+        List<List<Object>> list = ListUtils.newArrayList();
+
+        for (Object obj : res.getList()) {
+            List<Object> data = ListUtils.newArrayList();
+
+            Entity rowValue = Convert.convert(Entity.class, obj);
+
+            for (List<String> head : headlist){
+                data.add(rowValue.get(head.get(0)));
+            }
+
+            list.add(data);
+        }
+        return list;
+    }
+
 }

+ 1 - 1
src/main/java/com/xjrsoft/module/ledger/dto/LedgerExportDataDto.java

@@ -19,5 +19,5 @@ import java.util.Map;
 public class LedgerExportDataDto{
     private Long configIntoId;
 
-    private Map<String, String> queryParam;
+    private Map<String, Object> queryParam;
 }