【update】订单导出功能接入文件上传功能,修复pageResult字段转换问题,新增File转MultipartFile工具类

This commit is contained in:
nixj 2024-06-14 09:32:21 +08:00
parent d9e130ff61
commit 871e659b4c
6 changed files with 130 additions and 2 deletions

View File

@ -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

View File

@ -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"))

View File

@ -9,5 +9,5 @@ import java.util.List;
@Repository
public interface JdbcOrderDownloadRecordRepository extends CrudRepository<OrderDownloadRecord, Long> {
List<OrderDownloadRecord> findAllByEmployeeNo(String employeeNo);
List<OrderDownloadRecord> findAllByEmployeeNoOrderByIdDesc(String employeeNo);
}

View File

@ -165,7 +165,7 @@ public class ManageServiceImpl implements ManageService {
@Override
public List<OrderDownloadRecord> orderDownloadRecordQuery() {
User user=BaseContext.getCurrentUser();
return jdbcOrderDownloadRecordRepository.findAllByEmployeeNo(user.getEmployeeNo());
return jdbcOrderDownloadRecordRepository.findAllByEmployeeNoOrderByIdDesc(user.getEmployeeNo());
}
@Override

View File

@ -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<Long> getRouteIds(){
List<String> list = BaseContext.getCurrentUser().loadRoleOrg().getRoleOrgList()
.stream().filter(s->s.getOrgShortCode()!=null).map(SystemOrganizationVO::getOrgShortCode).toList();
@ -39,4 +44,5 @@ public class BaseUtil {
List<Long> routeIds = getRouteIds();
return jdbcOrderDetailRepository.findAllByRouteIdIn(routeIds).stream().map(OrderDetail::getOrderNo).toList();
}
}

View File

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