|
|
@@ -10,16 +10,20 @@ import org.apache.poi.ss.usermodel.Sheet;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import shop.alien.entity.store.*;
|
|
|
import shop.alien.entity.store.vo.ManagementInfoVo;
|
|
|
import shop.alien.mapper.*;
|
|
|
import shop.alien.store.service.ManagementInfoService;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import shop.alien.util.ali.AliOSSUtil;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
import java.text.DateFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.*;
|
|
|
@@ -53,6 +57,8 @@ public class ManagementInfoServiceImpl implements ManagementInfoService {
|
|
|
|
|
|
private final StoreUserMapper storeUserMapper;
|
|
|
|
|
|
+ private final AliOSSUtil aliOSSUtil;
|
|
|
+
|
|
|
@Override
|
|
|
public IPage<ManagementInfo> getAllManagementInfo(int pageNo, int pageSize, String storeName) {
|
|
|
IPage<LifeDiscountCoupon> iPage = new Page<>(pageNo, pageSize);
|
|
|
@@ -504,7 +510,16 @@ public class ManagementInfoServiceImpl implements ManagementInfoService {
|
|
|
}
|
|
|
|
|
|
public String generateReceiptFile(String storeName, String storeAccount, String storePhone, String invoiceAmount, String amountPaid, String paymentTime, String fileName) {
|
|
|
- String filePath = "";
|
|
|
+ String filePath = excelPath + excelGeneratePath + fileName + ".xlsx";
|
|
|
+ String osName = System.getProperty("os.name").toLowerCase();
|
|
|
+ if (osName.contains("win")) {
|
|
|
+ filePath = "d:/" + filePath;
|
|
|
+ String substring = filePath.substring(0, filePath.lastIndexOf("/"));
|
|
|
+ File dir = new File(substring);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
// 创建 File 对象
|
|
|
File file = new File(excelPath + excelGeneratePath + fileName + ".xlsx");
|
|
|
// 判断文件是否存在
|
|
|
@@ -512,8 +527,8 @@ public class ManagementInfoServiceImpl implements ManagementInfoService {
|
|
|
filePath = fileUrl + "excel" + excelGeneratePath + fileName + ".xlsx";
|
|
|
return filePath;
|
|
|
}
|
|
|
-
|
|
|
- try (FileInputStream fis = new FileInputStream(excelPath + excelClearingReceipt);
|
|
|
+ String templateUrl = fileUrl +"excel/" +excelClearingReceipt;
|
|
|
+ try (FileInputStream fis = toFileInputStream(downloadAsInputStream(templateUrl));
|
|
|
Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
// 获取第一个工作表
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
@@ -581,14 +596,41 @@ public class ManagementInfoServiceImpl implements ManagementInfoService {
|
|
|
sheet.autoSizeColumn(i);
|
|
|
}
|
|
|
// 保存填充后的工作簿到本地文件
|
|
|
- try (FileOutputStream fos = new FileOutputStream(excelPath + excelGeneratePath + fileName + ".xlsx")) {
|
|
|
+// try (FileOutputStream fos = new FileOutputStream(excelPath + excelGeneratePath + fileName + ".xlsx")) {
|
|
|
+// workbook.write(fos);
|
|
|
+// filePath = fileUrl + "excel" + excelGeneratePath + fileName + ".xlsx";
|
|
|
+// }
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(filePath)) {
|
|
|
workbook.write(fos);
|
|
|
- filePath = fileUrl + "excel" + excelGeneratePath + fileName + ".xlsx";
|
|
|
}
|
|
|
+
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- return filePath;
|
|
|
+
|
|
|
+ return aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
|
|
|
+ }
|
|
|
+
|
|
|
+ public InputStream downloadAsInputStream(String fileUrl) throws IOException {
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ return connection.getInputStream(); // 直接返回 HTTP 流
|
|
|
+ }
|
|
|
+ public static FileInputStream toFileInputStream(InputStream inputStream) throws IOException {
|
|
|
+ Path tempFile = Files.createTempFile("temp-", ".tmp");
|
|
|
+ tempFile.toFile().deleteOnExit();
|
|
|
+ try (InputStream is = inputStream) {
|
|
|
+ Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+ return new FileInputStream(tempFile.toFile());
|
|
|
+ }
|
|
|
+ public FileInputStream getExcelTemplate() throws IOException {
|
|
|
+ // 1. 通过 ClassPathResource 获取文件
|
|
|
+ ClassPathResource resource = new ClassPathResource("templates/clearing_receipt.xlsx");
|
|
|
+ // 2. 转换为 FileInputStream(仅在文件系统有效,JAR 内会报错)
|
|
|
+ File file = resource.getFile(); // 注意:JAR 中会抛出 FileNotFoundException
|
|
|
+ return new FileInputStream(file);
|
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) {
|