From 871e659b4ceeec639f57d9b8c3013f026d82ef61 Mon Sep 17 00:00:00 2001 From: nixj Date: Fri, 14 Jun 2024 09:32:21 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90update=E3=80=91=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=85=A5=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DpageResult=E5=AD=97=E6=AE=B5=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=96=B0=E5=A2=9EFile=E8=BD=ACMulti?= =?UTF-8?q?partFile=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chint/domain/value_object/BaseQuery.java | 4 + .../manage/controller/ManageController.java | 29 ++++++ .../JdbcOrderDownloadRecordRepository.java | 2 +- .../service/impl/ManageServiceImpl.java | 2 +- .../java/com/chint/manage/util/BaseUtil.java | 6 ++ .../manage/util/CustomMultipartFile.java | 89 +++++++++++++++++++ 6 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/chint/manage/util/CustomMultipartFile.java diff --git a/src/main/java/com/chint/domain/value_object/BaseQuery.java b/src/main/java/com/chint/domain/value_object/BaseQuery.java index c556bda2..5ef1690d 100644 --- a/src/main/java/com/chint/domain/value_object/BaseQuery.java +++ b/src/main/java/com/chint/domain/value_object/BaseQuery.java @@ -1,6 +1,7 @@ package com.chint.domain.value_object; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.data.domain.PageRequest; @@ -17,6 +18,9 @@ public class BaseQuery { private LocalDate endDate; @ApiModelProperty("排序字段") private Integer sort = 0; + //确保这个字段不被持久化或序列化,修复转换错误问题 + @JsonIgnore + private PageRequest pageResult; public PageRequest getPageResult() { PageRequest pageRequest = PageRequest diff --git a/src/main/java/com/chint/manage/controller/ManageController.java b/src/main/java/com/chint/manage/controller/ManageController.java index 54057712..2b04f689 100644 --- a/src/main/java/com/chint/manage/controller/ManageController.java +++ b/src/main/java/com/chint/manage/controller/ManageController.java @@ -2,6 +2,7 @@ package com.chint.manage.controller; import com.chint.infrastructure.util.PageResult; import com.chint.infrastructure.util.Result; +import com.chint.manage.entity.OrderDownloadRecord; import com.chint.manage.entity.dto.ItineraryPageDto; import com.chint.manage.entity.excel.BaseExcel; import com.chint.manage.entity.excel.OrderExceedStandardExcel; @@ -9,7 +10,11 @@ import com.chint.manage.entity.query.ConsumptionDetailQuery; import com.chint.manage.entity.query.ItineraryPageQuery; import com.chint.manage.entity.query.OrderPageQuery; import com.chint.manage.entity.query.StandardQuery; +import com.chint.manage.mapper.JdbcOrderDownloadRecordRepository; import com.chint.manage.service.ManageService; +import com.chint.manage.service.MinioService; +import com.chint.manage.util.BaseUtil; +import com.chint.manage.util.CustomMultipartFile; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +30,7 @@ import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.time.LocalDateTime; import static com.chint.dama.dc.basic.Result.SUCCESS; @@ -35,6 +41,12 @@ public class ManageController { @Autowired private ManageService manageService; + @Autowired + private BaseUtil baseUtil; + @Autowired + private MinioService minioService; + @Autowired + private JdbcOrderDownloadRecordRepository jdbcOrderDownloadRecordRepository; @ApiOperation("消费明细分页查询接口") @@ -112,6 +124,23 @@ public class ManageController { } catch (FileNotFoundException e) { throw new RuntimeException(e); } + //保存下载记录 + try { + OrderDownloadRecord orderDownloadRecord=new OrderDownloadRecord(); + orderDownloadRecord.setQueryCriteria(dto); + orderDownloadRecord.setEmployeeNo(baseUtil.getCurrentUser().getEmployeeNo()); + orderDownloadRecord.setStatus("已完成"); + String filePath = minioService.uploadFile(new CustomMultipartFile(file, "text/plain")); + orderDownloadRecord.setFileName(filePath); + orderDownloadRecord.setFileUrl(filePath); + orderDownloadRecord.setCreateTime(LocalDateTime.now()); + orderDownloadRecord.setExpireTime(LocalDateTime.now().plusDays(7)); + jdbcOrderDownloadRecordRepository.save(orderDownloadRecord); + }catch (Exception e){ + log.error("文件上传失败:{}",e.getMessage()); + } + + return ResponseEntity.ok() .header("Content-Disposition", "attachment; filename=" + file.getName()) .contentType(MediaType.parseMediaType("application/octet-stream")) diff --git a/src/main/java/com/chint/manage/mapper/JdbcOrderDownloadRecordRepository.java b/src/main/java/com/chint/manage/mapper/JdbcOrderDownloadRecordRepository.java index 6307c5a2..650726d7 100644 --- a/src/main/java/com/chint/manage/mapper/JdbcOrderDownloadRecordRepository.java +++ b/src/main/java/com/chint/manage/mapper/JdbcOrderDownloadRecordRepository.java @@ -9,5 +9,5 @@ import java.util.List; @Repository public interface JdbcOrderDownloadRecordRepository extends CrudRepository { - List findAllByEmployeeNo(String employeeNo); + List findAllByEmployeeNoOrderByIdDesc(String employeeNo); } 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 1fb5356a..2181ac2f 100644 --- a/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java +++ b/src/main/java/com/chint/manage/service/impl/ManageServiceImpl.java @@ -165,7 +165,7 @@ public class ManageServiceImpl implements ManageService { @Override public List orderDownloadRecordQuery() { User user=BaseContext.getCurrentUser(); - return jdbcOrderDownloadRecordRepository.findAllByEmployeeNo(user.getEmployeeNo()); + return jdbcOrderDownloadRecordRepository.findAllByEmployeeNoOrderByIdDesc(user.getEmployeeNo()); } @Override diff --git a/src/main/java/com/chint/manage/util/BaseUtil.java b/src/main/java/com/chint/manage/util/BaseUtil.java index 856b6200..d0ac2b5a 100644 --- a/src/main/java/com/chint/manage/util/BaseUtil.java +++ b/src/main/java/com/chint/manage/util/BaseUtil.java @@ -1,6 +1,7 @@ package com.chint.manage.util; import com.chint.domain.aggregates.order.OrderDetail; +import com.chint.domain.aggregates.user.User; import com.chint.domain.value_object.system.SystemOrganizationVO; import com.chint.infrastructure.repository.jdbc.JdbcOrderDetailRepository; import com.chint.infrastructure.util.BaseContext; @@ -23,6 +24,10 @@ public class BaseUtil { @Autowired private JdbcOrderDetailRepository jdbcOrderDetailRepository; + public User getCurrentUser() { + return BaseContext.getCurrentUser(); + } + public List getRouteIds(){ List list = BaseContext.getCurrentUser().loadRoleOrg().getRoleOrgList() .stream().filter(s->s.getOrgShortCode()!=null).map(SystemOrganizationVO::getOrgShortCode).toList(); @@ -39,4 +44,5 @@ public class BaseUtil { List routeIds = getRouteIds(); return jdbcOrderDetailRepository.findAllByRouteIdIn(routeIds).stream().map(OrderDetail::getOrderNo).toList(); } + } diff --git a/src/main/java/com/chint/manage/util/CustomMultipartFile.java b/src/main/java/com/chint/manage/util/CustomMultipartFile.java new file mode 100644 index 00000000..84e565bb --- /dev/null +++ b/src/main/java/com/chint/manage/util/CustomMultipartFile.java @@ -0,0 +1,89 @@ +package com.chint.manage.util; + +import org.springframework.web.multipart.MultipartFile; + +import java.io.*; + +public class CustomMultipartFile implements MultipartFile { + + + public static void main(String[] args) { + try { + // 示例文件 + File file = new File("D:\\用户目录\\Desktop\\1.txt"); + // 将File对象转换为MultipartFile对象 + MultipartFile multipartFile = new CustomMultipartFile(file, "text/plain"); + + // 打印MultipartFile相关信息 + System.out.println("MultipartFile Name: " + multipartFile.getName()); + System.out.println("MultipartFile Original Filename: " + multipartFile.getOriginalFilename()); + System.out.println("MultipartFile Content Type: " + multipartFile.getContentType()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private final byte[] fileContent; + private final String fileName; + private final String contentType; + + public CustomMultipartFile(File file, String contentType) throws IOException { + this.fileName = file.getName(); + this.contentType = contentType; + this.fileContent = convertFileToByteArray(file); + } + + private byte[] convertFileToByteArray(File file) throws IOException { + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); + FileInputStream fis = new FileInputStream(file)) { + byte[] buffer = new byte[1024]; + int read; + while ((read = fis.read(buffer)) != -1) { + bos.write(buffer, 0, read); + } + return bos.toByteArray(); + } + } + + @Override + public String getName() { + return fileName; + } + + @Override + public String getOriginalFilename() { + return fileName; + } + + @Override + public String getContentType() { + return contentType; + } + + @Override + public boolean isEmpty() { + return fileContent == null || fileContent.length == 0; + } + + @Override + public long getSize() { + return fileContent.length; + } + + @Override + public byte[] getBytes() throws IOException { + return fileContent; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(fileContent); + } + + @Override + public void transferTo(File dest) throws IOException, IllegalStateException { + try (FileOutputStream fos = new FileOutputStream(dest)) { + fos.write(fileContent); + } + } +}