TASK:mitu报表

This commit is contained in:
shahaibo
2024-04-03 11:24:20 +08:00
parent 5ba762e15c
commit e4b32abc67
4 changed files with 113 additions and 44 deletions

View File

@@ -1,21 +1,26 @@
package com.mixi.common.tasks;
import com.mixi.common.tasks.mituExportEntity.*;
import com.mixi.common.utils.MinioUtil;
import com.mixi.mapper.MiTuExportMapper;
import com.mixi.mapper.entity.MiTuExport;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
@@ -32,6 +37,9 @@ import java.util.stream.Collectors;
@Component
public class MiTuExportScheduledTask {
@Resource
private MinioUtil minioUtil;
static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL = "jdbc:sqlserver://118.142.0.178:1550;databaseName=Hayman_prod";
@@ -45,10 +53,10 @@ public class MiTuExportScheduledTask {
@PostConstruct
public void executeWeeklyHeavyStockReport() {
// customerPurchaseReport();
// NewJoinVIPReport();
// weeklySellThrReport();
// WeeklyHeavyStockReport();
// QuarterlyProductGroupingReport();
NewJoinVIPReport();
weeklySellThrReport();
WeeklyHeavyStockReport();
QuarterlyProductGroupingReport();
}
/**
@@ -63,8 +71,9 @@ public class MiTuExportScheduledTask {
List<String> userMembers = transactionData.stream().map(TransactionData::getUserMember).collect(Collectors.toList());
List<CustomerData> customerData = retrieveCustomerData(userMembers);
updateCustomerDataWithTransactionData(customerData, transactionData);
String filePath = "C:\\Users\\10233\\Desktop\\MiTuExport\\"+miTuExport.getExportName()+".xlsx";
String filePath = miTuExport.getExportName()+".xlsx";
exportToExcelCustomerPurchaseReport(customerData, filePath);
miTuExport.setUrl("mi-tu/export/" + filePath);
miTuExport.setStatus(1);
} catch (Exception e) {
miTuExport.setStatus(0);
@@ -147,7 +156,8 @@ public class MiTuExportScheduledTask {
MiTuExport miTuExport = createMiTuExport("New Join VIP report", "week");
try {
List<TransactionNewCustomer> transactionNewCustomerList = getTransactionNewCustomerList();
String filePath = "C:\\Users\\10233\\Desktop\\MiTuExport\\"+miTuExport.getExportName()+".xlsx";
String filePath = miTuExport.getExportName()+".xlsx";
miTuExport.setUrl("mi-tu/export/" + filePath);
exportNewJoinVIPReport(transactionNewCustomerList, filePath);
miTuExport.setStatus(1);
} catch (Exception e) {
@@ -159,12 +169,11 @@ public class MiTuExportScheduledTask {
}
}
private static void exportNewJoinVIPReport(List<TransactionNewCustomer> transactionNewCustomerList, String filePathName) throws IOException {
File file = new File(filePathName);
boolean fileExists = file.exists();
// 如果文件不存在,则创建一个新的 Workbook
Workbook workbook = fileExists ? WorkbookFactory.create(file) : new XSSFWorkbook();
private void exportNewJoinVIPReport(List<TransactionNewCustomer> transactionNewCustomerList, String filePathName) throws IOException {
String currentPath = Paths.get("").toAbsolutePath().toString();
File file = new File(currentPath + "/" + filePathName);
// 如果文件不存在或者为空,则创建一个新的 Workbook
Workbook workbook = new XSSFWorkbook();
// 创建一个新的 Sheet 或获取现有的 Sheet
Sheet sheet = workbook.getSheet("Transaction New Customer");
@@ -207,7 +216,9 @@ public class MiTuExportScheduledTask {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
}
FileItem a = getMultipartFile(file, file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(a);
minioUtil.upload("mi-tu", "export", multipartFile);
System.out.println("Excel file has been created successfully!");
}
@@ -221,7 +232,8 @@ public class MiTuExportScheduledTask {
MiTuExport miTuExport = createMiTuExport("weeklySellThrReport", "week");
try {
List<TransactionSummary> transactionSummaryList = getTransactionSummaryList();
String filePath = "C:\\Users\\10233\\Desktop\\MiTuExport\\"+miTuExport.getExportName()+".xlsx";
String filePath = miTuExport.getExportName()+".xlsx";
miTuExport.setUrl("mi-tu/export/" + filePath);
exportWeeklySellThrReport(transactionSummaryList, filePath);
miTuExport.setStatus(1);
} catch (Exception e) {
@@ -233,12 +245,11 @@ public class MiTuExportScheduledTask {
}
}
private void exportWeeklySellThrReport(List<TransactionSummary> transactionSummaryList, String weeklySellThrReportName) throws IOException {
File file = new File(weeklySellThrReportName);
boolean fileExists = file.exists();
// 如果文件不存在,则创建一个新的 Workbook
Workbook workbook = fileExists ? WorkbookFactory.create(file) : new XSSFWorkbook();
private void exportWeeklySellThrReport(List<TransactionSummary> transactionSummaryList, String filePathName) throws IOException {
String currentPath = Paths.get("").toAbsolutePath().toString();
File file = new File(currentPath + "/" + filePathName);
// 如果文件不存在或者为空,则创建一个新的 Workbook
Workbook workbook = new XSSFWorkbook();
// 创建一个新的 Sheet 或获取现有的 Sheet
Sheet sheet = workbook.getSheet("Weekly Sell Through Report");
@@ -305,6 +316,9 @@ public class MiTuExportScheduledTask {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
}
FileItem a = getMultipartFile(file, file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(a);
minioUtil.upload("mi-tu", "export", multipartFile);
System.out.println("Excel file has been created successfully!");
}
@@ -319,7 +333,8 @@ public class MiTuExportScheduledTask {
MiTuExport miTuExport = createMiTuExport("WeeklyHeavyStockReport", "week");
try {
List<WeeklyHeavyStock> weeklyHeavyStockList = getWeeklyHeavyStockList();
String filePath = "C:\\Users\\10233\\Desktop\\MiTuExport\\"+miTuExport.getExportName()+".xlsx";
String filePath = miTuExport.getExportName()+".xlsx";
miTuExport.setUrl("mi-tu/export/" + filePath);
exportWeeklyHeavyStock(weeklyHeavyStockList, filePath);
miTuExport.setStatus(1);
} catch (Exception e) {
@@ -331,7 +346,9 @@ public class MiTuExportScheduledTask {
}
}
private static void exportWeeklyHeavyStock(List<WeeklyHeavyStock> weeklyHeavyStockList, String weeklyHeavyStockReportName) {
private void exportWeeklyHeavyStock(List<WeeklyHeavyStock> weeklyHeavyStockList, String weeklyHeavyStockReportName) {
String currentPath = Paths.get("").toAbsolutePath().toString();
File file = new File(currentPath + "/" + weeklyHeavyStockReportName);
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
// 创建工作表
@@ -386,11 +403,14 @@ public class MiTuExportScheduledTask {
}
// 将工作簿写入文件
try (FileOutputStream outputStream = new FileOutputStream(weeklyHeavyStockReportName)) {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
FileItem a = getMultipartFile(file, file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(a);
minioUtil.upload("mi-tu", "export", multipartFile);
System.out.println("Excel file has been created successfully!");
}
@@ -405,7 +425,8 @@ public class MiTuExportScheduledTask {
MiTuExport miTuExport = createMiTuExport("Quarterly Product Grouping Report", "week");
try {
List<WeeklyHeavyStock> QuarterlyProductGroupingList = getQuarterlyProductGroupingList();
String filePath = "C:\\Users\\10233\\Desktop\\MiTuExport\\"+miTuExport.getExportName()+".xlsx";
String filePath = miTuExport.getExportName()+".xlsx";
miTuExport.setUrl("mi-tu/export/" + filePath);
exportQuarterlyProductGrouping(QuarterlyProductGroupingList, filePath);
miTuExport.setStatus(1);
} catch (Exception e) {
@@ -417,7 +438,9 @@ public class MiTuExportScheduledTask {
}
}
private static void exportQuarterlyProductGrouping(List<WeeklyHeavyStock> weeklyHeavyStockList, String weeklyHeavyStockReportName) {
private void exportQuarterlyProductGrouping(List<WeeklyHeavyStock> weeklyHeavyStockList, String weeklyHeavyStockReportName) {
String currentPath = Paths.get("").toAbsolutePath().toString();
File file = new File(currentPath + "/" + weeklyHeavyStockReportName);
// 创建工作簿
Workbook workbook = new XSSFWorkbook();
// 创建工作表
@@ -482,11 +505,14 @@ public class MiTuExportScheduledTask {
}
// 将工作簿写入文件
try (FileOutputStream outputStream = new FileOutputStream(weeklyHeavyStockReportName)) {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
FileItem a = getMultipartFile(file, file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(a);
minioUtil.upload("mi-tu", "export", multipartFile);
System.out.println("Excel file has been created successfully!");
}
@@ -1180,12 +1206,13 @@ public class MiTuExportScheduledTask {
return transactionNewCustomerList;
}
public static void exportToExcelCustomerPurchaseReport(List<CustomerData> customerDataList, String filePath) throws IOException {
File file = new File(filePath);
boolean fileExists = file.exists() && file.length() > 0; // 检查文件是否存在且非空
public final static String MITU = "mi-tu";
public void exportToExcelCustomerPurchaseReport(List<CustomerData> customerDataList, String fileName) throws IOException {
String currentPath = Paths.get("").toAbsolutePath().toString();
File file = new File(currentPath + "/" + fileName);
// 如果文件不存在或者为空,则创建一个新的 Workbook
Workbook workbook = fileExists ? WorkbookFactory.create(file) : new XSSFWorkbook();
Workbook workbook = new XSSFWorkbook();
// 创建一个新的 Sheet 或获取现有的 Sheet
Sheet sheet = workbook.getSheet("Customer Data");
@@ -1264,10 +1291,46 @@ public class MiTuExportScheduledTask {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
workbook.write(outputStream);
}
FileItem a = getMultipartFile(file, file.getName());
MultipartFile multipartFile = new CommonsMultipartFile(a);
minioUtil.upload("mi-tu", "export", multipartFile);
System.out.println("Excel file has been created successfully!");
}
private static FileItem getMultipartFile(File file, String fieldName) {
// 使用 DiskFileItemFactory 创建一个 FileItemFactory
FileItemFactory factory = new DiskFileItemFactory(16, null);
// 使用 FileItemFactory 创建一个 FileItem 对象
String mimeType = getMimeType(file);
FileItem item = factory.createItem(fieldName, mimeType, true, file.getName());
byte[] buffer = new byte[8192];
int bytesRead;
try (FileInputStream fis = new FileInputStream(file);
OutputStream os = item.getOutputStream()) {
// 从文件中读取数据并写入 FileItem
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
return item;
}
private static String getMimeType(File file) {
try {
return Files.probeContentType(file.toPath());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static List<CustomerData> retrieveCustomerData(List<String> collect) {
List<CustomerData> customerDataList = new ArrayList<>();
Connection conn = null;

View File

@@ -136,7 +136,7 @@ public class MinioUtil {
public String upload(String bucketName, String path, MultipartFile file) {
String fileName = file.getOriginalFilename();
// String[] split = fileName.split("\\.");
fileName = path;
fileName = path + "/" + fileName;
InputStream in = null;
try {
in = file.getInputStream();

View File

@@ -4,11 +4,13 @@ import com.mixi.common.response.PageBaseResponse;
import com.mixi.common.response.Response;
import com.mixi.mapper.entity.MiTuExport;
import com.mixi.model.dto.QueryMiTuExportPageDTO;
import io.minio.errors.MinioException;
import org.springframework.core.io.InputStreamResource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 店铺服务实现类
@@ -21,5 +23,5 @@ public interface MiTuExportService {
PageBaseResponse<MiTuExport> queryMiTuExportPage(QueryMiTuExportPageDTO query);
void exportMiTuReport(Long id, HttpServletResponse response) throws FileNotFoundException;
void exportMiTuReport(Long id, HttpServletResponse response) throws IOException, MinioException;
}

View File

@@ -6,10 +6,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mixi.common.config.exception.BusinessException;
import com.mixi.common.response.PageBaseResponse;
import com.mixi.common.response.Response;
import com.mixi.common.utils.MinioUtil;
import com.mixi.mapper.MiTuExportMapper;
import com.mixi.mapper.entity.MiTuExport;
import com.mixi.model.dto.QueryMiTuExportPageDTO;
import com.mixi.service.MiTuExportService;
import io.minio.errors.MinioException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
@@ -21,10 +23,7 @@ import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.*;
//import com.ai.da.common.utils.SendSmsUtil;
@@ -41,6 +40,9 @@ public class MiTuExportServiceImpl implements MiTuExportService {
@Resource
private MiTuExportMapper miTuExportMapper;
@Resource
private MinioUtil minioUtil;
@Override
public PageBaseResponse<MiTuExport> queryMiTuExportPage(QueryMiTuExportPageDTO query) {
// 分页数据
@@ -71,20 +73,22 @@ public class MiTuExportServiceImpl implements MiTuExportService {
}
@Override
public void exportMiTuReport(Long id, HttpServletResponse response) throws FileNotFoundException {
public void exportMiTuReport(Long id, HttpServletResponse response) throws IOException, MinioException {
MiTuExport miTuExport = miTuExportMapper.selectById(id);
if (ObjectUtils.isEmpty(miTuExport)) {
throw new BusinessException("Report is not exist");
}
String filePath = miTuExport.getUrl(); // 获取文件路径
String[] split = filePath.split("/");
// 读取文件
File file = new File(filePath);
InputStream inputStream = new FileInputStream(file);
InputStream inputStream = minioUtil.download(filePath);
// File file = new File(filePath);
// InputStream inputStream = new FileInputStream(file);
// 设置响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setHeader("Content-Disposition", "attachment; filename=" + split[2]);
// 将文件内容写入响应输出流
try {