Ver Fonte

汇总下载

Signed-off-by: liuyc <56808083@qq.com>
liuyc há 8 meses atrás
pai
commit
43ac76d0eb

+ 17 - 0
blade-service-api/blade-business-api/src/main/java/org/springblade/business/entity/TrialSummaryRecordDataJson.java

@@ -0,0 +1,17 @@
+package org.springblade.business.entity;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+@Data
+public class TrialSummaryRecordDataJson implements Serializable {
+
+    private Long id;
+    private Long recordId;
+    private Long excelId;
+    private String jsonData;
+    private Date createTime;
+
+}

+ 125 - 7
blade-service/blade-business/src/main/java/org/springblade/business/controller/TrialSummaryController.java

@@ -1,5 +1,7 @@
 package org.springblade.business.controller;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -11,6 +13,7 @@ import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
 import lombok.AllArgsConstructor;
 import org.apache.commons.lang.StringUtils;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@@ -22,6 +25,7 @@ import org.springblade.business.dto.TrialSummaryRecordDTO;
 import org.springblade.business.dto.TrialSummaryRecordPageDTO;
 import org.springblade.business.entity.TrialSelfInspectionRecord;
 import org.springblade.business.entity.TrialSummaryRecord;
+import org.springblade.business.entity.TrialSummaryRecordDataJson;
 import org.springblade.business.service.impl.TrialSelfInspectionRecordServiceImpl;
 import org.springblade.business.service.impl.TrialSummaryRecordServiceImpl;
 import org.springblade.business.vo.TrialSummaryRecordVO;
@@ -29,6 +33,7 @@ import org.springblade.common.constant.CommonConstant;
 import org.springblade.common.utils.CommonUtil;
 import org.springblade.common.utils.SnowFlakeUtil;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.core.oss.model.BladeFile;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.*;
 import org.springblade.manager.entity.ContractInfo;
@@ -641,14 +646,127 @@ public class TrialSummaryController {
         return R.status(trialSummaryRecordServiceImpl.deleteLogic(Func.toLongList(ids)));
     }
 
-    @PostMapping("/download")
+    @GetMapping("/download")
     @ApiOperationSupport(order = 5)
-    @ApiOperation(value = "下载", notes = "调page接口的pdfUrl下载")
-    public R<Object> download() {
-        /*如需下载excel,获取u_trial_summary_record_data_json表数据,解析json_data,根据classId获取分类配置的清表excel模板,插入excel对应位置。
-        json_data格式:{"2:2@9:9@pageSize@0": "AAA", .... };@pageSize@0表示第一个sheet;2:2@9:9表示位置信息,x1=2,x2=2;y1=9,y2=9 ;值=AAA。*/
-        //TODO
-        return R.success("操作成功");
+    @ApiOperation(value = "下载excel", notes = "传入记录的id,拼接成英文逗号分割的ids")
+    public R<Object> download(@RequestParam String ids) throws Exception {
+        if (StringUtils.isNotEmpty(ids)) {
+            List<TrialSummaryRecord> trialSummaryRecords = trialSummaryRecordServiceImpl.getBaseMapper().selectBatchIds(Func.toStrList(ids));
+            if (trialSummaryRecords.size() > 0) {
+                List<TrialSummaryRecordDataJson> query = jdbcTemplate.query("SELECT * FROM u_trial_summary_record_data_json WHERE record_id in(" + ids + ")", new BeanPropertyRowMapper<>(TrialSummaryRecordDataJson.class));
+                if (query.size() <= 0) {
+                    throw new ServiceException("未获取到数据信息");
+                }
+                Set<Long> excelIds = query.stream().map(TrialSummaryRecordDataJson::getExcelId).collect(Collectors.toSet());
+                Map<Long, TrialSummaryRecordDataJson> jsonMap = query.stream().collect(Collectors.toMap(TrialSummaryRecordDataJson::getRecordId, Function.identity()));
+                Map<Long, String> excelUrlMaps = jdbcTemplate.query("SELECT id,file_url FROM m_excel_tab WHERE id in(" + StringUtils.join(excelIds, ",") + ")", new BeanPropertyRowMapper<>(ExcelTab.class)).stream().collect(Collectors.toMap(ExcelTab::getId, ExcelTab::getFileUrl));
+
+                List<MultipartFile> files = new LinkedList<>();
+
+                for (TrialSummaryRecord record : trialSummaryRecords) {
+                    TrialSummaryRecordDataJson jsonData = jsonMap.getOrDefault(record.getId(), null);
+                    String excelUrl = excelUrlMaps.getOrDefault(jsonData.getExcelId(), null);
+                    if (StringUtils.isNotEmpty(excelUrl) && ObjectUtil.isNotEmpty(jsonData)) {
+
+                        String jsonStr = jsonData.getJsonData();
+                        JSONObject jsonObject = JSON.parseObject(jsonStr);
+                        @SuppressWarnings("unchecked")
+                        Map<String, String> map = jsonObject.toJavaObject(Map.class);
+
+                        Map<String, List<Map.Entry<String, String>>> groupedByPageSize = map.entrySet().stream()
+                                .collect(Collectors.groupingBy(entry -> {
+                                    String[] indexAndPageSize = entry.getKey().split("@pageSize@");
+                                    return indexAndPageSize[1];
+                                }));
+
+                        for (Map.Entry<String, List<Map.Entry<String, String>>> page : groupedByPageSize.entrySet()) {
+                            InputStream modInput = CommonUtil.getOSSInputStream(excelUrl);
+                            if (modInput != null) {
+                                try (Workbook workbook = new XSSFWorkbook(modInput)) {
+                                    Sheet sheet = workbook.getSheetAt(0);
+                                    for (Map.Entry<String, String> entry : page.getValue()) {
+                                        String key = entry.getKey();
+                                        String value = entry.getValue();
+                                        String[] indexAndPageSize = key.split("@pageSize@");
+
+                                        String index = indexAndPageSize[0];
+
+                                        String[] coordinates = index.split("@");
+                                        String x = coordinates[0];
+                                        String y = coordinates[1];
+
+                                        int x1 = Integer.parseInt(x.split(":")[0]) - 1;
+                                        int x2 = Integer.parseInt(x.split(":")[1]) - 1;
+                                        int y1 = Integer.parseInt(y.split(":")[0]) - 1;
+                                        int y2 = Integer.parseInt(y.split(":")[1]) - 1;
+
+                                        mergeAndCenterCells(sheet, x1, x2, y1, y2, value);
+                                    }
+
+                                    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
+                                        workbook.write(byteArrayOutputStream);
+                                        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
+                                            files.add(new MockMultipartFile("file", excelUrl, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", byteArrayInputStream));
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+
+                if (files.size() > 0) {
+                    MultipartFile multipartFile = mergeExcelFiles(files);
+                    if (multipartFile != null) {
+                        BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
+                        if (bladeFile != null) {
+                            return R.data(bladeFile.getLink());
+                        }
+                    }
+                }
+            }
+        }
+        return R.fail("操作失败");
+    }
+
+    private MultipartFile mergeExcelFiles(List<MultipartFile> files) throws IOException {
+        Workbook mergedWorkbook = new XSSFWorkbook();
+        int sheetNumber = 1;
+        for (MultipartFile file : files) {
+            try (InputStream inputStream = file.getInputStream()) {
+                Workbook workbook = WorkbookFactory.create(inputStream);
+                int numberOfSheets = workbook.getNumberOfSheets();
+                for (int i = 0; i < numberOfSheets; i++) {
+                    Sheet sheet = workbook.getSheetAt(i);
+                    Sheet newSheet = mergedWorkbook.createSheet("Sheet" + sheetNumber++);
+                    copySheet(sheet, newSheet);
+                }
+            } catch (InvalidFormatException e) {
+                e.printStackTrace();
+                return null;
+            }
+        }
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        mergedWorkbook.write(outputStream);
+        return new MockMultipartFile("file", SnowFlakeUtil.getId() + "_file.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", outputStream.toByteArray());
+    }
+
+    private void copySheet(Sheet sourceSheet, Sheet targetSheet) {
+        int rowCount = sourceSheet.getLastRowNum();
+        for (int i = 0; i <= rowCount; i++) {
+            Row sourceRow = sourceSheet.getRow(i);
+            Row newRow = targetSheet.createRow(i);
+            if (sourceRow != null) {
+                int columnCount = sourceRow.getLastCellNum();
+                for (int j = 0; j < columnCount; j++) {
+                    Cell sourceCell = sourceRow.getCell(j);
+                    Cell newCell = newRow.createCell(j);
+                    if (sourceCell != null) {
+                        newCell.setCellValue(sourceCell.getStringCellValue());
+                    }
+                }
+            }
+        }
     }
 
 

+ 27 - 14
blade-service/blade-meter/src/main/java/org/springblade/meter/controller/MidPayItemController.java

@@ -425,25 +425,38 @@ public class MidPayItemController extends BladeController {
         if (collect.size() > 0) {
             meterMidPayItemProjects = payItemProjectService.getBaseMapper().selectList(Wrappers.<MeterMidPayItemProject>lambdaQuery()
                     .eq(MeterMidPayItemProject::getProjectId, projectId)
-                    .notIn(MeterMidPayItemProject::getId, collect)
-                    .orderByAsc(MeterMidPayItemProject::getCreateTime));
+                    .notIn(MeterMidPayItemProject::getId, collect));
         } else {
             meterMidPayItemProjects = payItemProjectService.getBaseMapper().selectList(Wrappers.<MeterMidPayItemProject>lambdaQuery()
-                    .eq(MeterMidPayItemProject::getProjectId, projectId)
-                    .orderByAsc(MeterMidPayItemProject::getCreateTime));
+                    .eq(MeterMidPayItemProject::getProjectId, projectId));
         }
 
-        List<MeterMidPayItemProjectVO> VOS = BeanUtil.copyProperties(meterMidPayItemProjects, MeterMidPayItemProjectVO.class);
-        for (MeterMidPayItemProjectVO vo : VOS) {
-            if (ObjectUtil.isNotEmpty(vo.getPayApplicableType())) {
-                vo.setPayApplicableTypeName(meterPayAppTypeMap.getOrDefault(vo.getPayApplicableType().toString(), null));
-            }
-            if (ObjectUtil.isNotEmpty(vo.getPayType())) {
-                vo.setPayTypeName(meterPayTypeMap.getOrDefault(vo.getPayType().toString(), null));
-            }
-        }
+        List<MeterMidPayItemProjectVO> sortVOS = meterMidPayItemProjects.stream()
+                .map(project -> {
+                    MeterMidPayItemProjectVO vo = new MeterMidPayItemProjectVO();
+                    BeanUtil.copyProperties(project, vo);
+                    if (ObjectUtil.isNotEmpty(project.getPayApplicableType())) {
+                        vo.setPayApplicableTypeName(meterPayAppTypeMap.getOrDefault(project.getPayApplicableType().toString(), null));
+                    }
+                    if (ObjectUtil.isNotEmpty(project.getPayType())) {
+                        vo.setPayTypeName(meterPayTypeMap.getOrDefault(project.getPayType().toString(), null));
+                    }
+                    return vo;
+                }).sorted(
+                        Comparator.nullsLast(
+                                Comparator.<MeterMidPayItemProjectVO, Integer>comparing(
+                                        projectVO -> {
+                                            Integer sortValue = projectVO.getSort();
+                                            return sortValue != null ? sortValue : Integer.MAX_VALUE;
+                                        }
+                                )
+                        )
+                                .thenComparing(
+                                        Comparator.nullsLast(Comparator.comparing(MeterMidPayItemProjectVO::getCreateTime))
+                                )
+                ).collect(Collectors.toList());
 
-        return R.data(VOS);
+        return R.data(sortVOS);
     }
 
     @GetMapping("/contract/detail")

+ 1 - 1
blade-service/blade-meter/src/main/java/org/springblade/meter/controller/TaskController.java

@@ -3019,7 +3019,7 @@ public class TaskController extends BladeController {
         Page<Map<String, Object>> resultMap = new Page<>();
         resultMap.setCurrent(dto.getCurrent());
         resultMap.setSize(dto.getSize());
-        resultMap.setTotal(jdbcTemplate.queryForObject("SELECT count(1) FROM u_fixed_flow WHERE is_deleted = 0 AND is_meter != 1 AND project_id = ? AND contract_id = ?", Long.class, dto.getProjectId(), dto.getContractId()));
+        resultMap.setTotal(jdbcTemplate.queryForObject("SELECT count(1) FROM u_fixed_flow WHERE is_deleted = 0 AND is_meter = 1 AND project_id = ? AND contract_id = ?", Long.class, dto.getProjectId(), dto.getContractId()));
         resultMap.setRecords(taskClient.getFixedFlowPage(dto));
         return R.data(resultMap);
     }