From 4f573f33d9e1b497c642ac30019a1c8a01ade1d6 Mon Sep 17 00:00:00 2001 From: nixj Date: Wed, 8 May 2024 15:23:52 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90add=E3=80=91=E6=B6=88=E8=B4=B9?= =?UTF-8?q?=E6=98=8E=E7=BB=86=E6=9F=A5=E8=AF=A2=E5=AF=BC=E5=87=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jdbc/JdbcOrderCarRecordRepository.java | 2 + .../jdbc/JdbcOrderFlightRecordRepository.java | 2 + .../jdbc/JdbcOrderHotelRecordRepository.java | 2 + .../jdbc/JdbcOrderTrainRecordRepository.java | 3 + .../manage/controller/ManageController.java | 44 +++++++- .../com/chint/manage/entity/BaseExcel.java | 20 ++++ .../manage/entity/ConsumptionDetailExcel.java | 21 +--- .../entity/dto/ConsumptionDetailDto.java | 16 +++ .../JdbcConsumptionDetailRepository.java | 14 +++ .../JdbcConsumptionDetailRepositoryImpl.java | 105 ++++++++++++++++++ .../chint/manage/service/ManageService.java | 12 ++ .../service/impl/ManageServiceImpl.java | 32 +++++- .../java/com/chint/manage/util/ExcelUtil.java | 51 +++++++++ src/test/java/com/chint/ManageTest.java | 39 +++++++ 14 files changed, 343 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/chint/manage/entity/BaseExcel.java create mode 100644 src/main/java/com/chint/manage/entity/dto/ConsumptionDetailDto.java create mode 100644 src/main/java/com/chint/manage/mapper/JdbcConsumptionDetailRepository.java create mode 100644 src/main/java/com/chint/manage/mapper/impl/JdbcConsumptionDetailRepositoryImpl.java create mode 100644 src/main/java/com/chint/manage/util/ExcelUtil.java create mode 100644 src/test/java/com/chint/ManageTest.java diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderCarRecordRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderCarRecordRepository.java index d5fde564..63611abc 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderCarRecordRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderCarRecordRepository.java @@ -48,4 +48,6 @@ public interface JdbcOrderCarRecordRepository extends CrudRepository findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable); } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderFlightRecordRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderFlightRecordRepository.java index 4ef8daf5..d9570ea4 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderFlightRecordRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderFlightRecordRepository.java @@ -47,4 +47,6 @@ public interface JdbcOrderFlightRecordRepository extends CrudRepository findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable); } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderHotelRecordRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderHotelRecordRepository.java index a74369ff..2b1a45e6 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderHotelRecordRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderHotelRecordRepository.java @@ -48,4 +48,6 @@ public interface JdbcOrderHotelRecordRepository extends CrudRepository findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable); } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderTrainRecordRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderTrainRecordRepository.java index e9a52072..e6c9e029 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderTrainRecordRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcOrderTrainRecordRepository.java @@ -46,4 +46,7 @@ public interface JdbcOrderTrainRecordRepository extends CrudRepository findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable); } diff --git a/src/main/java/com/chint/manage/controller/ManageController.java b/src/main/java/com/chint/manage/controller/ManageController.java index aab08ca1..d57179b3 100644 --- a/src/main/java/com/chint/manage/controller/ManageController.java +++ b/src/main/java/com/chint/manage/controller/ManageController.java @@ -1,11 +1,51 @@ package com.chint.manage.controller; +import com.chint.application.dtos.AuthenticationSignDto; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; +import com.chint.manage.service.ManageService; +import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; @Slf4j @RestController @RequestMapping("/manage") public class ManageController { + + @Autowired + private ManageService manageService; + + + @ApiOperation("消费明细查询接口") + @PostMapping("/query") + public PageResult query(@RequestBody ConsumptionDetailDto dto){ + return manageService.pageConsumptionDetail(dto); + } + + @ApiOperation("消费明细导出接口") + @PostMapping("/export") + public ResponseEntity export(@RequestBody ConsumptionDetailDto dto) { + File file = manageService.saveConsumptionDetail(dto); + Resource resource; + try { + resource = new InputStreamResource(new FileInputStream(file)); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + return ResponseEntity.ok() + .header("Content-Disposition", "attachment; filename=" + file.getName()) + .contentType(MediaType.parseMediaType("application/octet-stream")) + .body(resource); + } } diff --git a/src/main/java/com/chint/manage/entity/BaseExcel.java b/src/main/java/com/chint/manage/entity/BaseExcel.java new file mode 100644 index 00000000..38380637 --- /dev/null +++ b/src/main/java/com/chint/manage/entity/BaseExcel.java @@ -0,0 +1,20 @@ +package com.chint.manage.entity; + +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.alibaba.excel.annotation.write.style.HeadFontStyle; +import com.alibaba.excel.annotation.write.style.HeadStyle; +import com.alibaba.excel.enums.poi.FillPatternTypeEnum; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * + * @Author:nxj + * @Date:2024/5/8 14:57 + */ +@NoArgsConstructor +@HeadStyle(fillPatternType = FillPatternTypeEnum.NO_FILL) // 设置表头背景为无色 +@HeadFontStyle(fontHeightInPoints = 10) +@ColumnWidth(16) +public class BaseExcel{ +} diff --git a/src/main/java/com/chint/manage/entity/ConsumptionDetailExcel.java b/src/main/java/com/chint/manage/entity/ConsumptionDetailExcel.java index 0e69f9f2..72905207 100644 --- a/src/main/java/com/chint/manage/entity/ConsumptionDetailExcel.java +++ b/src/main/java/com/chint/manage/entity/ConsumptionDetailExcel.java @@ -1,32 +1,21 @@ package com.chint.manage.entity; import com.alibaba.excel.annotation.ExcelProperty; -import com.alibaba.excel.annotation.write.style.ColumnWidth; -import com.alibaba.excel.annotation.write.style.HeadFontStyle; -import com.alibaba.excel.annotation.write.style.HeadStyle; -import com.alibaba.excel.enums.poi.FillPatternTypeEnum; import lombok.Data; -import lombok.NoArgsConstructor; - -import java.time.LocalDateTime; /** - * 消费明细 + * 消费明细-order_detail * @Author:nxj * @Date:2024/5/7 14:17 */ @Data -@NoArgsConstructor -@HeadStyle(fillPatternType = FillPatternTypeEnum.NO_FILL) // 设置表头背景为无色 -@HeadFontStyle(fontHeightInPoints = 10) -@ColumnWidth(16) -public class ConsumptionDetailExcel { +public class ConsumptionDetailExcel extends BaseExcel{ @ExcelProperty("出行人姓名") - private String supplierName; + private String userName; @ExcelProperty("出发时间") - private String startTime; + private String startDate; @ExcelProperty("到达时间") - private String arriveTime; + private String arriveDate; @ExcelProperty("出发城市") private String fromCity; @ExcelProperty("到达城市") diff --git a/src/main/java/com/chint/manage/entity/dto/ConsumptionDetailDto.java b/src/main/java/com/chint/manage/entity/dto/ConsumptionDetailDto.java new file mode 100644 index 00000000..79b667e9 --- /dev/null +++ b/src/main/java/com/chint/manage/entity/dto/ConsumptionDetailDto.java @@ -0,0 +1,16 @@ +package com.chint.manage.entity.dto; + +import com.chint.domain.value_object.BaseQuery; +import lombok.Data; + +/** + * + * @Author:nxj + * @Date:2024/5/8 8:58 + */ +@Data +public class ConsumptionDetailDto extends BaseQuery{ + private Integer productType; + private String supplierName; + private String orderNo; +} diff --git a/src/main/java/com/chint/manage/mapper/JdbcConsumptionDetailRepository.java b/src/main/java/com/chint/manage/mapper/JdbcConsumptionDetailRepository.java new file mode 100644 index 00000000..e03e399b --- /dev/null +++ b/src/main/java/com/chint/manage/mapper/JdbcConsumptionDetailRepository.java @@ -0,0 +1,14 @@ +package com.chint.manage.mapper; + +import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +public interface JdbcConsumptionDetailRepository { + Page findAllByProductType(String productType, String param2, String param3, Pageable pageable); + + PageResult pageConsumptionDetail(ConsumptionDetailDto dto); +} \ No newline at end of file diff --git a/src/main/java/com/chint/manage/mapper/impl/JdbcConsumptionDetailRepositoryImpl.java b/src/main/java/com/chint/manage/mapper/impl/JdbcConsumptionDetailRepositoryImpl.java new file mode 100644 index 00000000..904308f6 --- /dev/null +++ b/src/main/java/com/chint/manage/mapper/impl/JdbcConsumptionDetailRepositoryImpl.java @@ -0,0 +1,105 @@ +package com.chint.manage.mapper.impl; + +import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.domain.aggregates.order.order_record.*; +import com.chint.infrastructure.constant.LegConstant; +import com.chint.infrastructure.repository.jdbc.*; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; +import com.chint.manage.mapper.JdbcConsumptionDetailRepository; +import org.apache.poi.ss.formula.functions.T; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.*; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.util.*; + +import static com.chint.infrastructure.constant.LegConstant.*; + +/** + * + * @Author:nxj + * @Date:2024/5/8 9:57 + */ +@Repository +public class JdbcConsumptionDetailRepositoryImpl implements JdbcConsumptionDetailRepository { + + @Autowired + private NamedParameterJdbcTemplate jdbcTemplate; + + @Autowired + private JdbcOrderTrainRecordRepository jdbcOrderTrainRecordRepository; + @Autowired + private JdbcOrderCarRecordRepository jdbcOrderCarRecordRepository; + @Autowired + private JdbcOrderHotelRecordRepository jdbcOrderHotelRecordRepository; + @Autowired + private JdbcOrderFlightRecordRepository jdbcOrderFlightRecordRepository; + + + @Override + public Page findAllByProductType(String productType, String supplierName, String orderNo, Pageable pageable) { + String query = "SELECT * FROM order_detail " + + "WHERE product_type = :productType " + + "AND (:supplierName IS NULL OR supplier_name = :supplierName) " + + "AND (:orderNo IS NULL OR order_no = :orderNo) " + + "ORDER BY order_id"; + + Map params = new HashMap<>(); + params.put("productType", productType); + params.put("supplierName", supplierName); + params.put("orderNo", orderNo); + params.put("offset", pageable.getOffset()); + params.put("limit", pageable.getPageSize()); + + String pagedQuery = query + " LIMIT :limit OFFSET :offset"; + + List results = jdbcTemplate.query(pagedQuery, params, BeanPropertyRowMapper.newInstance(OrderDetail.class)); + long count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM (" + query + ") AS count", params, Long.class); + + return new PageImpl<>(results, pageable, count); + } + + @Override + public PageResult pageConsumptionDetail(ConsumptionDetailDto dto) { + if (dto.getSupplierName()==null){ + dto.setSupplierName(""); + } + if (dto.getOrderNo()==null){ + dto.setOrderNo(""); + } + PageRequest pageRequest = PageRequest + .of(dto.getPageNum() - 1, dto.getPageSize(), Sort.by("id")); + Page data=new PageImpl<>(Collections.emptyList()); + switch (dto.getProductType()) { + case LEG_TYPE_TRAIN -> data=jdbcOrderTrainRecordRepository.findAllByUserNameContainsAndOrderNoContaining(dto.getSupplierName(),dto.getOrderNo(),pageRequest); + case LEG_TYPE_AIRPLANE -> data=jdbcOrderFlightRecordRepository.findAllByUserNameContainsAndOrderNoContaining(dto.getSupplierName(),dto.getOrderNo(),pageRequest); + case LEG_TYPE_HOTEL -> data=jdbcOrderHotelRecordRepository.findAllByUserNameContainsAndOrderNoContaining(dto.getSupplierName(),dto.getOrderNo(),pageRequest); + case LEG_TYPE_TAXI -> data=jdbcOrderCarRecordRepository.findAllByUserNameContainsAndOrderNoContaining(dto.getSupplierName(),dto.getOrderNo(),pageRequest); + } + //todo 数据处理 + Page result=mapOrderTrainRecordToConsumptionDetailExcel(data); + return PageResult.totalPageNum(result.getTotalElements(), result.getContent()); + } + + public Page mapOrderTrainRecordToConsumptionDetailExcel(Page page) { + List orderRecords = page.getContent(); + List consumptionDetailExcels = new ArrayList<>(); + + // 遍历 OrderBaseRecord,将其转换为 ConsumptionDetailExcel + for (OrderBaseRecord orderRecord : orderRecords) { + ConsumptionDetailExcel consumptionDetailExcel = new ConsumptionDetailExcel(); + // 使用 BeanUtils.copyProperties 进行属性拷贝 + BeanUtils.copyProperties(orderRecord, consumptionDetailExcel); + // 添加到结果列表中 + consumptionDetailExcels.add(consumptionDetailExcel); + } + + // 构建一个新的 Page 对象并返回 + return new PageImpl<>(consumptionDetailExcels, page.getPageable(), page.getTotalElements()); + } +} diff --git a/src/main/java/com/chint/manage/service/ManageService.java b/src/main/java/com/chint/manage/service/ManageService.java index 91829fb0..da9225d9 100644 --- a/src/main/java/com/chint/manage/service/ManageService.java +++ b/src/main/java/com/chint/manage/service/ManageService.java @@ -1,8 +1,20 @@ package com.chint.manage.service; + +import com.chint.domain.aggregates.order.order_record.OrderBaseRecord; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; + +import java.io.File; + /** * * @Author:nxj * @Date:2024/5/7 14:18 */ public interface ManageService { + + PageResult pageConsumptionDetail(ConsumptionDetailDto dto); + + File saveConsumptionDetail(ConsumptionDetailDto dto); } diff --git a/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java b/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java index 888c461a..9d067f7e 100644 --- a/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java +++ b/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java @@ -1,12 +1,40 @@ package com.chint.manage.service.impl; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.infrastructure.export.OrderDetailExport; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; +import com.chint.manage.mapper.impl.JdbcConsumptionDetailRepositoryImpl; import com.chint.manage.service.ManageService; -import org.springframework.stereotype.Component; +import com.chint.manage.util.ExcelUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.List; +import java.util.Objects; + /** * * @Author:nxj * @Date:2024/5/7 14:18 */ -@Component +@Service public class ManageServiceImpl implements ManageService { + @Autowired + private JdbcConsumptionDetailRepositoryImpl jdbcConsumptionDetailRepository; + + @Override + public PageResult pageConsumptionDetail(ConsumptionDetailDto dto) { + return jdbcConsumptionDetailRepository.pageConsumptionDetail(dto); + } + + @Override + public File saveConsumptionDetail(ConsumptionDetailDto dto) { + return ExcelUtil.exportReport(jdbcConsumptionDetailRepository.pageConsumptionDetail(dto).getRecords()); + } } diff --git a/src/main/java/com/chint/manage/util/ExcelUtil.java b/src/main/java/com/chint/manage/util/ExcelUtil.java new file mode 100644 index 00000000..90e7648b --- /dev/null +++ b/src/main/java/com/chint/manage/util/ExcelUtil.java @@ -0,0 +1,51 @@ +package com.chint.manage.util; + +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.chint.infrastructure.export.OrderDetailExport; +import com.chint.manage.entity.BaseExcel; +import com.chint.manage.entity.ConsumptionDetailExcel; + +import java.io.File; +import java.util.List; + +/** + * + * @Author:nxj + * @Date:2024/5/8 14:56 + */ +public class ExcelUtil { + + public static File exportReport(List excels) { + if (excels.isEmpty()) { + return null; + } + // 方法1 如果写到同一个sheet + String filePath = System.getProperty("user.dir") + File.separator + "export" + File.separator; + if (!new File(filePath).exists()) { + new File(filePath).mkdirs(); + } + String fileName = filePath + "Excel" + ".xlsx"; + + ExcelWriter excelWriter = null; + try { + // 这里 需要指定写用哪个class去写 + excelWriter = EasyExcel + .write(fileName, excels.get(0).getClass()) + .build(); + // 这里注意 如果同一个sheet只要创建一次 + WriteSheet writeSheet = EasyExcel + .writerSheet("导出数据").build(); + // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来 + excelWriter + .write(excels, writeSheet); + } finally { + // 千万别忘记finish 会帮忙关闭流 + if (excelWriter != null) { + excelWriter.finish(); + } + } + return new File(fileName); + } +} diff --git a/src/test/java/com/chint/ManageTest.java b/src/test/java/com/chint/ManageTest.java new file mode 100644 index 00000000..96921bc3 --- /dev/null +++ b/src/test/java/com/chint/ManageTest.java @@ -0,0 +1,39 @@ +package com.chint; + +import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.infrastructure.repository.jdbc.JdbcOrderDetailRepository; +import com.chint.infrastructure.util.PageResult; +import com.chint.manage.entity.ConsumptionDetailExcel; +import com.chint.manage.entity.dto.ConsumptionDetailDto; +import com.chint.manage.service.ManageService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class ManageTest { + @Autowired + private ManageService manageService; + + @Test + void pageManageService(){ + ConsumptionDetailDto dto=new ConsumptionDetailDto(); + dto.setPageNum(1); + dto.setPageSize(10); + dto.setProductType(0); + dto.setSupplierName("王玲峰"); + PageResult result=manageService.pageConsumptionDetail(dto); + System.out.println(result); + } + + @Autowired + private JdbcOrderDetailRepository jdbcOrderDetailRepository; + + @Test + public void LyFlight() { + OrderDetail byOrderNo = jdbcOrderDetailRepository.findFirstByOrderNo("DT24031767012916938"); + System.out.println(byOrderNo); + } + + +}