【add】消费明细查询导出功能实现

This commit is contained in:
nixj 2024-05-08 15:23:52 +08:00
parent 7f8e2bc283
commit 4f573f33d9
14 changed files with 343 additions and 20 deletions

View File

@ -48,4 +48,6 @@ public interface JdbcOrderCarRecordRepository extends CrudRepository<OrderCarRec
order by r1.create_time desc
""")
Integer countRecordByCustomFiled(String fieldName, Object fieldValue, String accountPeriod);
Page<OrderCarRecord> findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable);
}

View File

@ -47,4 +47,6 @@ public interface JdbcOrderFlightRecordRepository extends CrudRepository<OrderFli
order by r1.create_time desc
""")
Integer countRecordByCustomFiled(String fieldName, Object fieldValue, String accountPeriod);
Page<OrderFlightRecord> findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable);
}

View File

@ -48,4 +48,6 @@ public interface JdbcOrderHotelRecordRepository extends CrudRepository<OrderHote
order by r1.create_time desc
""")
Integer countRecordByCustomFiled(String fieldName, Object fieldValue, String accountPeriod);
Page<OrderHotelRecord> findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable);
}

View File

@ -46,4 +46,7 @@ public interface JdbcOrderTrainRecordRepository extends CrudRepository<OrderTrai
order by r1.create_time desc
""")
Integer countRecordByCustomFiled(String fieldName, Object fieldValue, String accountPeriod);
Page<OrderTrainRecord> findAllByUserNameContainsAndOrderNoContaining(String userName, String orderNo, Pageable pageable);
}

View File

@ -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<ConsumptionDetailExcel> query(@RequestBody ConsumptionDetailDto dto){
return manageService.pageConsumptionDetail(dto);
}
@ApiOperation("消费明细导出接口")
@PostMapping("/export")
public ResponseEntity<Resource> 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);
}
}

View File

@ -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;
/**
*
* @Authornxj
* @Date2024/5/8 14:57
*/
@NoArgsConstructor
@HeadStyle(fillPatternType = FillPatternTypeEnum.NO_FILL) // 设置表头背景为无色
@HeadFontStyle(fontHeightInPoints = 10)
@ColumnWidth(16)
public class BaseExcel{
}

View File

@ -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
* @Authornxj
* @Date2024/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("到达城市")

View File

@ -0,0 +1,16 @@
package com.chint.manage.entity.dto;
import com.chint.domain.value_object.BaseQuery;
import lombok.Data;
/**
*
* @Authornxj
* @Date2024/5/8 8:58
*/
@Data
public class ConsumptionDetailDto extends BaseQuery{
private Integer productType;
private String supplierName;
private String orderNo;
}

View File

@ -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<OrderDetail> findAllByProductType(String productType, String param2, String param3, Pageable pageable);
PageResult<ConsumptionDetailExcel> pageConsumptionDetail(ConsumptionDetailDto dto);
}

View File

@ -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.*;
/**
*
* @Authornxj
* @Date2024/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<OrderDetail> 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<String, Object> 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<OrderDetail> 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<ConsumptionDetailExcel> 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<? extends OrderBaseRecord> 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<ConsumptionDetailExcel> result=mapOrderTrainRecordToConsumptionDetailExcel(data);
return PageResult.totalPageNum(result.getTotalElements(), result.getContent());
}
public Page<ConsumptionDetailExcel> mapOrderTrainRecordToConsumptionDetailExcel(Page<? extends OrderBaseRecord> page) {
List<? extends OrderBaseRecord> orderRecords = page.getContent();
List<ConsumptionDetailExcel> consumptionDetailExcels = new ArrayList<>();
// 遍历 OrderBaseRecord将其转换为 ConsumptionDetailExcel
for (OrderBaseRecord orderRecord : orderRecords) {
ConsumptionDetailExcel consumptionDetailExcel = new ConsumptionDetailExcel();
// 使用 BeanUtils.copyProperties 进行属性拷贝
BeanUtils.copyProperties(orderRecord, consumptionDetailExcel);
// 添加到结果列表中
consumptionDetailExcels.add(consumptionDetailExcel);
}
// 构建一个新的 Page<ConsumptionDetailExcel> 对象并返回
return new PageImpl<>(consumptionDetailExcels, page.getPageable(), page.getTotalElements());
}
}

View File

@ -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;
/**
*
* @Authornxj
* @Date2024/5/7 14:18
*/
public interface ManageService {
PageResult<ConsumptionDetailExcel> pageConsumptionDetail(ConsumptionDetailDto dto);
File saveConsumptionDetail(ConsumptionDetailDto dto);
}

View File

@ -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;
/**
*
* @Authornxj
* @Date2024/5/7 14:18
*/
@Component
@Service
public class ManageServiceImpl implements ManageService {
@Autowired
private JdbcConsumptionDetailRepositoryImpl jdbcConsumptionDetailRepository;
@Override
public PageResult<ConsumptionDetailExcel> pageConsumptionDetail(ConsumptionDetailDto dto) {
return jdbcConsumptionDetailRepository.pageConsumptionDetail(dto);
}
@Override
public File saveConsumptionDetail(ConsumptionDetailDto dto) {
return ExcelUtil.exportReport(jdbcConsumptionDetailRepository.pageConsumptionDetail(dto).getRecords());
}
}

View File

@ -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;
/**
*
* @Authornxj
* @Date2024/5/8 14:56
*/
public class ExcelUtil {
public static File exportReport(List<? extends BaseExcel> 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);
}
}

View File

@ -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<ConsumptionDetailExcel> 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);
}
}