✨ 毛利分析订单支持异步导出
This commit is contained in:
+19
-9
@@ -8,6 +8,7 @@ import java.nio.file.Paths;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import com.bms.common.annotation.Log;
|
import com.bms.common.annotation.Log;
|
||||||
@@ -39,6 +41,7 @@ import com.bms.price.domain.vo.PrcProfitReportProductRow;
|
|||||||
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||||
import com.bms.price.domain.vo.PrcProfitReportSummary;
|
import com.bms.price.domain.vo.PrcProfitReportSummary;
|
||||||
import com.bms.price.service.IPrcProfitReportService;
|
import com.bms.price.service.IPrcProfitReportService;
|
||||||
|
import com.bms.price.task.PrcProfitReportExportRunner;
|
||||||
|
|
||||||
/** 产品/订单维度毛利报表。 */
|
/** 产品/订单维度毛利报表。 */
|
||||||
@RestController
|
@RestController
|
||||||
@@ -51,6 +54,9 @@ public class PrcProfitReportController extends BaseController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private OssStorage ossStorage;
|
private OssStorage ossStorage;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PrcProfitReportExportRunner exportRunner;
|
||||||
|
|
||||||
/** 客户筛选选项,沿用毛利报表的访问权限和查询范围。 */
|
/** 客户筛选选项,沿用毛利报表的访问权限和查询范围。 */
|
||||||
@PreAuthorize("@ss.hasPermi('report:profit:list')")
|
@PreAuthorize("@ss.hasPermi('report:profit:list')")
|
||||||
@GetMapping("/customer-options")
|
@GetMapping("/customer-options")
|
||||||
@@ -124,19 +130,23 @@ public class PrcProfitReportController extends BaseController
|
|||||||
@Log(title = "毛利报表-订单维度", businessType = BusinessType.EXPORT)
|
@Log(title = "毛利报表-订单维度", businessType = BusinessType.EXPORT)
|
||||||
@PreAuthorize("@ss.hasPermi('report:profit:export')")
|
@PreAuthorize("@ss.hasPermi('report:profit:export')")
|
||||||
@PostMapping("/orders/export")
|
@PostMapping("/orders/export")
|
||||||
public AjaxResult exportOrders(PrcProfitReportQuery query)
|
public AjaxResult exportOrders(@RequestBody PrcProfitReportQuery query)
|
||||||
{
|
{
|
||||||
PrcProfitReportExport export = beginExport("ORDER", "订单维度毛利分析");
|
return success(Map.of("taskId", exportRunner.submit(query, getUserId())));
|
||||||
try
|
|
||||||
{
|
|
||||||
List<PrcProfitReportOrderRow> rows = profitReportService.selectOrderRows(query);
|
|
||||||
return finishExport(export, rows, PrcProfitReportOrderRow.class, "订单毛利报表", query.getProfitRule());
|
|
||||||
}
|
}
|
||||||
catch (RuntimeException e)
|
|
||||||
|
@PreAuthorize("@ss.hasPermi('report:profit:export')")
|
||||||
|
@GetMapping("/orders/export/task/{taskId}")
|
||||||
|
public AjaxResult orderExportTask(@PathVariable String taskId)
|
||||||
{
|
{
|
||||||
failExport(export, e);
|
return success(exportRunner.getTask(taskId, getUserId()));
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("@ss.hasPermi('report:profit:export')")
|
||||||
|
@GetMapping("/orders/export/download/{taskId}")
|
||||||
|
public void downloadOrderExport(@PathVariable String taskId, HttpServletResponse response) throws Exception
|
||||||
|
{
|
||||||
|
exportRunner.download(taskId, getUserId(), response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Log(title = "毛利报表-产品维度", businessType = BusinessType.EXPORT)
|
@Log(title = "毛利报表-产品维度", businessType = BusinessType.EXPORT)
|
||||||
|
|||||||
@@ -0,0 +1,226 @@
|
|||||||
|
package com.bms.report;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.attribute.FileTime;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.springframework.core.task.TaskRejectedException;
|
||||||
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
import com.bms.common.config.BmsConfig;
|
||||||
|
import com.bms.common.core.redis.RedisCache;
|
||||||
|
import com.bms.common.exception.ServiceException;
|
||||||
|
import com.bms.price.domain.PrcProfitReportExportTask;
|
||||||
|
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
||||||
|
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||||
|
import com.bms.price.enums.ProfitAreaRule;
|
||||||
|
import com.bms.price.service.IPrcProfitReportService;
|
||||||
|
import com.bms.price.task.PrcProfitReportExportRunner;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
|
||||||
|
class ProfitReportAsyncExportTest
|
||||||
|
{
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
private String previousProfile;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void useTempProfile()
|
||||||
|
{
|
||||||
|
previousProfile = BmsConfig.getProfile();
|
||||||
|
new BmsConfig().setProfile(tempDir.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void restoreProfile()
|
||||||
|
{
|
||||||
|
new BmsConfig().setProfile(previousProfile);
|
||||||
|
PageHelper.clearPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void submissionQueuesWorkAndExportsAllRowsUsingTheSubmittedFiltersAndRegionColumns() throws Exception
|
||||||
|
{
|
||||||
|
for (String area : List.of("C", "GZ"))
|
||||||
|
{
|
||||||
|
AtomicInteger calls = new AtomicInteger();
|
||||||
|
HoldingExecutor executor = new HoldingExecutor();
|
||||||
|
PrcProfitReportExportRunner runner = new PrcProfitReportExportRunner(service(query -> {
|
||||||
|
calls.incrementAndGet();
|
||||||
|
assertNull(PageHelper.getLocalPage());
|
||||||
|
assertEquals(area, query.getCustomerArea());
|
||||||
|
assertEquals("UNBILLED", query.getArBillStatus());
|
||||||
|
assertEquals("P1", query.getProductCode());
|
||||||
|
query.setProfitRule(ProfitAreaRule.fromCustomerArea(area).name());
|
||||||
|
return List.of(row("ORDER-A"), row("ORDER-B"));
|
||||||
|
}), new MemoryRedis(), executor);
|
||||||
|
PrcProfitReportQuery query = query(area);
|
||||||
|
query.setProductCode("P1");
|
||||||
|
String taskId = runner.submit(query, 1L);
|
||||||
|
|
||||||
|
assertEquals(0, calls.get());
|
||||||
|
assertEquals("PENDING", runner.getTask(taskId, 1L).getStatus());
|
||||||
|
assertThrows(ServiceException.class, () -> runner.download(taskId, 1L, new MockHttpServletResponse()));
|
||||||
|
query.setProductCode("CHANGED-AFTER-SUBMIT");
|
||||||
|
PageHelper.startPage(2, 1);
|
||||||
|
executor.work.run();
|
||||||
|
|
||||||
|
PrcProfitReportExportTask task = runner.getTask(taskId, 1L);
|
||||||
|
assertEquals("SUCCESS", task.getStatus());
|
||||||
|
assertEquals(100, task.getPercent());
|
||||||
|
assertEquals(2, task.getRowCount());
|
||||||
|
assertEquals(1, calls.get());
|
||||||
|
assertNull(PageHelper.getLocalPage());
|
||||||
|
assertThrows(ServiceException.class, () -> runner.getTask(taskId, 2L));
|
||||||
|
assertThrows(ServiceException.class, () -> runner.download(taskId, 2L, new MockHttpServletResponse()));
|
||||||
|
Path file = tempDir.resolve("download").resolve(task.getStorageKey());
|
||||||
|
try (InputStream input = Files.newInputStream(file); Workbook workbook = WorkbookFactory.create(input))
|
||||||
|
{
|
||||||
|
var sheet = workbook.getSheetAt(0);
|
||||||
|
assertEquals(2, sheet.getLastRowNum());
|
||||||
|
Row header = sheet.getRow(0);
|
||||||
|
assertEquals(area.equals("C"), hasColumn(header, "产品价(目录价)"));
|
||||||
|
assertEquals(area.equals("C"), hasColumn(header, "红线毛利"));
|
||||||
|
assertTrue(hasColumn(header, "销售价"));
|
||||||
|
}
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
runner.download(taskId, 1L, response);
|
||||||
|
assertArrayEquals(Files.readAllBytes(file), response.getContentAsByteArray());
|
||||||
|
assertTrue(response.getHeader("Content-Disposition").contains(".xlsx"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectedExecutionAndQueryFailuresBecomeVisibleFailedTasks() throws Exception
|
||||||
|
{
|
||||||
|
ThreadPoolTaskExecutor rejected = new ThreadPoolTaskExecutor() {
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable task) { throw new TaskRejectedException("busy"); }
|
||||||
|
};
|
||||||
|
PrcProfitReportExportRunner runner = new PrcProfitReportExportRunner(service(query -> {
|
||||||
|
fail("Rejected tasks must not query orders");
|
||||||
|
return List.of();
|
||||||
|
}), new MemoryRedis(), rejected);
|
||||||
|
String rejectedId = runner.submit(query("C"), 1L);
|
||||||
|
assertEquals("FAIL", runner.getTask(rejectedId, 1L).getStatus());
|
||||||
|
assertEquals("导出任务较多,请稍后重试", runner.getTask(rejectedId, 1L).getError());
|
||||||
|
|
||||||
|
HoldingExecutor executor = new HoldingExecutor();
|
||||||
|
PrcProfitReportExportRunner failing = new PrcProfitReportExportRunner(service(query -> {
|
||||||
|
throw new ServiceException("该客户区域未配置毛利规则");
|
||||||
|
}), new MemoryRedis(), executor);
|
||||||
|
String failedId = failing.submit(query("INVALID"), 1L);
|
||||||
|
executor.work.run();
|
||||||
|
PrcProfitReportExportTask failed = failing.getTask(failedId, 1L);
|
||||||
|
assertEquals("FAIL", failed.getStatus());
|
||||||
|
assertEquals("该客户区域未配置毛利规则", failed.getError());
|
||||||
|
assertNull(failed.getStorageKey());
|
||||||
|
assertThrows(ServiceException.class, () -> failing.download(failedId, 1L, new MockHttpServletResponse()));
|
||||||
|
assertFalse(Files.exists(tempDir.resolve("download/profitReportExport/" + failedId + ".xlsx")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cleanupRemovesOnlyExpiredExportFiles() throws Exception
|
||||||
|
{
|
||||||
|
Path dir = Files.createDirectories(tempDir.resolve("download/profitReportExport"));
|
||||||
|
Path old = Files.writeString(dir.resolve("a".repeat(32) + ".xlsx"), "old");
|
||||||
|
Path recent = Files.writeString(dir.resolve("b".repeat(32) + ".xlsx"), "new");
|
||||||
|
Path other = Files.writeString(dir.resolve("unrelated.xlsx"), "other");
|
||||||
|
FileTime expired = FileTime.from(Instant.now().minusSeconds(TimeUnit.HOURS.toSeconds(25)));
|
||||||
|
Files.setLastModifiedTime(old, expired);
|
||||||
|
Files.setLastModifiedTime(other, expired);
|
||||||
|
PrcProfitReportExportRunner runner = new PrcProfitReportExportRunner(service(query -> List.of()),
|
||||||
|
new MemoryRedis(), new HoldingExecutor());
|
||||||
|
runner.cleanupExpiredFiles();
|
||||||
|
assertFalse(Files.exists(old));
|
||||||
|
assertTrue(Files.exists(recent));
|
||||||
|
assertTrue(Files.exists(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void jsonSubmissionRetainsTheListDateRangeAndFilters() throws Exception
|
||||||
|
{
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||||
|
PrcProfitReportQuery query = mapper.readValue("""
|
||||||
|
{"customerArea":"C", "beginBizDate":"2026-10-18 00:00:00",
|
||||||
|
"endBizDate":"2026-11-17 23:59:59", "arBillStatus":"UNBILLED", "orderNo":"A\\nB"}
|
||||||
|
""", PrcProfitReportQuery.class);
|
||||||
|
assertEquals(Instant.parse("2026-10-17T16:00:00Z"), query.getBeginBizDate().toInstant());
|
||||||
|
assertEquals(Instant.parse("2026-11-17T15:59:59Z"), query.getEndBizDate().toInstant());
|
||||||
|
assertEquals("A\nB", query.getOrderNo());
|
||||||
|
assertEquals("UNBILLED", query.getArBillStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PrcProfitReportQuery query(String area)
|
||||||
|
{
|
||||||
|
PrcProfitReportQuery query = new PrcProfitReportQuery();
|
||||||
|
query.setCustomerArea(area);
|
||||||
|
query.setArBillStatus("UNBILLED");
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PrcProfitReportOrderRow row(String orderNo)
|
||||||
|
{
|
||||||
|
PrcProfitReportOrderRow row = new PrcProfitReportOrderRow();
|
||||||
|
row.setOrderNo(orderNo);
|
||||||
|
row.setSalesAmount(new BigDecimal("26.60"));
|
||||||
|
row.setArBillStatus("UNBILLED");
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasColumn(Row header, String name)
|
||||||
|
{
|
||||||
|
for (var cell : header) if (name.equals(cell.getStringCellValue())) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IPrcProfitReportService service(Function<PrcProfitReportQuery, List<PrcProfitReportOrderRow>> query)
|
||||||
|
{
|
||||||
|
return (IPrcProfitReportService) Proxy.newProxyInstance(ProfitReportAsyncExportTest.class.getClassLoader(),
|
||||||
|
new Class<?>[] { IPrcProfitReportService.class }, (proxy, method, args) -> {
|
||||||
|
if ("selectOrderRows".equals(method.getName())) return query.apply((PrcProfitReportQuery) args[0]);
|
||||||
|
throw new AssertionError("Unexpected service call: " + method.getName());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class HoldingExecutor extends ThreadPoolTaskExecutor
|
||||||
|
{
|
||||||
|
private Runnable work;
|
||||||
|
@Override
|
||||||
|
public void execute(Runnable task) { work = task; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MemoryRedis extends RedisCache
|
||||||
|
{
|
||||||
|
private final Map<String, Object> values = new HashMap<>();
|
||||||
|
@Override
|
||||||
|
public <T> void setCacheObject(String key, T value, Integer timeout, TimeUnit unit)
|
||||||
|
{
|
||||||
|
values.put(key, value);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T getCacheObject(String key) { return (T) values.get(key); }
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
package com.bms.price.config;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
/** 毛利分析导出专用线程池,限制全量订单查询与 Excel 生成的并发。 */
|
||||||
|
@Configuration
|
||||||
|
public class ProfitReportExportTaskConfig
|
||||||
|
{
|
||||||
|
@Bean(name = "profitReportExportTaskExecutor")
|
||||||
|
public ThreadPoolTaskExecutor profitReportExportTaskExecutor()
|
||||||
|
{
|
||||||
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
|
executor.setCorePoolSize(1);
|
||||||
|
executor.setMaxPoolSize(1);
|
||||||
|
executor.setQueueCapacity(2);
|
||||||
|
executor.setThreadNamePrefix("profit-report-export-");
|
||||||
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.bms.price.domain;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/** 毛利分析订单异步导出任务的临时状态。 */
|
||||||
|
@Data
|
||||||
|
public class PrcProfitReportExportTask implements Serializable
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private String taskId;
|
||||||
|
private Long userId;
|
||||||
|
private String status;
|
||||||
|
private int percent;
|
||||||
|
private String message;
|
||||||
|
private String error;
|
||||||
|
private int rowCount;
|
||||||
|
private String fileName;
|
||||||
|
private String storageKey;
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.bms.price.domain.vo;
|
|||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import com.bms.common.core.domain.BaseEntity;
|
import com.bms.common.core.domain.BaseEntity;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -15,8 +16,10 @@ public class PrcProfitReportQuery extends BaseEntity
|
|||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private Date beginBizDate;
|
private Date beginBizDate;
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
private Date endBizDate;
|
private Date endBizDate;
|
||||||
private String orderNo;
|
private String orderNo;
|
||||||
/** 订单号拆分后的多值查询条件。 */
|
/** 订单号拆分后的多值查询条件。 */
|
||||||
|
|||||||
+188
@@ -0,0 +1,188 @@
|
|||||||
|
package com.bms.price.task;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.LinkOption;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.core.task.TaskRejectedException;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import com.bms.common.config.BmsConfig;
|
||||||
|
import com.bms.common.core.redis.RedisCache;
|
||||||
|
import com.bms.common.exception.ServiceException;
|
||||||
|
import com.bms.common.utils.DateUtils;
|
||||||
|
import com.bms.common.utils.file.FileUtils;
|
||||||
|
import com.bms.common.utils.poi.ExcelUtil;
|
||||||
|
import com.bms.common.utils.uuid.IdUtils;
|
||||||
|
import com.bms.price.domain.PrcProfitReportExportTask;
|
||||||
|
import com.bms.price.domain.vo.PrcProfitReportOrderRow;
|
||||||
|
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||||
|
import com.bms.price.enums.ProfitAreaRule;
|
||||||
|
import com.bms.price.service.IPrcProfitReportService;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
|
||||||
|
/** 毛利分析订单异步导出,沿用应收生成预览的任务、轮询、下载流程。 */
|
||||||
|
@Component
|
||||||
|
public class PrcProfitReportExportRunner
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(PrcProfitReportExportRunner.class);
|
||||||
|
private static final String CACHE_PREFIX = "report:profit:orders:export:";
|
||||||
|
private static final String REL_DIR = "profitReportExport";
|
||||||
|
private static final int TTL_MINUTES = 60;
|
||||||
|
private static final long FILE_RETENTION_MILLIS = TimeUnit.HOURS.toMillis(24);
|
||||||
|
|
||||||
|
private final IPrcProfitReportService reportService;
|
||||||
|
private final RedisCache redisCache;
|
||||||
|
private final ThreadPoolTaskExecutor executor;
|
||||||
|
|
||||||
|
public PrcProfitReportExportRunner(IPrcProfitReportService reportService, RedisCache redisCache,
|
||||||
|
@Qualifier("profitReportExportTaskExecutor") ThreadPoolTaskExecutor executor)
|
||||||
|
{
|
||||||
|
this.reportService = reportService;
|
||||||
|
this.redisCache = redisCache;
|
||||||
|
this.executor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String submit(PrcProfitReportQuery query, Long userId)
|
||||||
|
{
|
||||||
|
if (query == null || userId == null)
|
||||||
|
{
|
||||||
|
throw new ServiceException("导出参数无效");
|
||||||
|
}
|
||||||
|
PrcProfitReportQuery snapshot = new PrcProfitReportQuery();
|
||||||
|
BeanUtils.copyProperties(query, snapshot);
|
||||||
|
String taskId = IdUtils.fastSimpleUUID();
|
||||||
|
String fileName = "订单维度毛利分析_" + DateUtils.dateTimeNow() + ".xlsx";
|
||||||
|
put(taskId, userId, "PENDING", 0, "排队中", null, 0, fileName, null);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
executor.execute(() -> run(taskId, userId, snapshot, fileName));
|
||||||
|
}
|
||||||
|
catch (TaskRejectedException e)
|
||||||
|
{
|
||||||
|
put(taskId, userId, "FAIL", 100, "导出失败", "导出任务较多,请稍后重试", 0, fileName, null);
|
||||||
|
}
|
||||||
|
return taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void run(String taskId, Long userId, PrcProfitReportQuery query, String fileName)
|
||||||
|
{
|
||||||
|
Path file = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 导出全部匹配订单,不使用页面分页,也不执行分页 COUNT。
|
||||||
|
PageHelper.clearPage();
|
||||||
|
put(taskId, userId, "RUNNING", 5, "正在查询订单", null, 0, fileName, null);
|
||||||
|
List<PrcProfitReportOrderRow> rows = reportService.selectOrderRows(query);
|
||||||
|
put(taskId, userId, "RUNNING", 40, "正在生成 Excel", null, rows.size(), fileName, null);
|
||||||
|
ExcelUtil<PrcProfitReportOrderRow> util = new ExcelUtil<>(PrcProfitReportOrderRow.class);
|
||||||
|
if (ProfitAreaRule.GUANGZHOU.name().equals(query.getProfitRule()))
|
||||||
|
{
|
||||||
|
util.hideColumn("costAmount", "redlineGrossProfit", "redlineGrossMarginRate");
|
||||||
|
}
|
||||||
|
Path dir = Paths.get(BmsConfig.getDownloadPath(), REL_DIR).toAbsolutePath().normalize();
|
||||||
|
Files.createDirectories(dir);
|
||||||
|
file = dir.resolve(taskId + ".xlsx").normalize();
|
||||||
|
try (OutputStream output = Files.newOutputStream(file))
|
||||||
|
{
|
||||||
|
util.exportExcelToStream(rows, "订单毛利报表", output);
|
||||||
|
}
|
||||||
|
put(taskId, userId, "SUCCESS", 100, "导出完成,正在下载", null, rows.size(), fileName,
|
||||||
|
REL_DIR + "/" + taskId + ".xlsx");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.error("毛利分析订单导出失败 taskId={}", taskId, e);
|
||||||
|
if (file != null)
|
||||||
|
{
|
||||||
|
try { Files.deleteIfExists(file); }
|
||||||
|
catch (Exception cleanupError) { log.warn("清理导出文件失败 taskId={}", taskId, cleanupError); }
|
||||||
|
}
|
||||||
|
String error = e instanceof ServiceException ? e.getMessage() : "导出失败,请稍后重试";
|
||||||
|
put(taskId, userId, "FAIL", 100, "导出失败", error, 0, fileName, null);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
PageHelper.clearPage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PrcProfitReportExportTask getTask(String taskId, Long userId)
|
||||||
|
{
|
||||||
|
PrcProfitReportExportTask task = redisCache.getCacheObject(CACHE_PREFIX + taskId);
|
||||||
|
if (task == null || userId == null || !userId.equals(task.getUserId()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("导出任务不存在或已过期");
|
||||||
|
}
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void download(String taskId, Long userId, HttpServletResponse response) throws Exception
|
||||||
|
{
|
||||||
|
PrcProfitReportExportTask task = getTask(taskId, userId);
|
||||||
|
String storageKey = task.getStorageKey();
|
||||||
|
if (!"SUCCESS".equals(task.getStatus()) || storageKey == null || !FileUtils.checkAllowDownload(storageKey))
|
||||||
|
{
|
||||||
|
throw new ServiceException("导出文件尚未生成");
|
||||||
|
}
|
||||||
|
Path root = Paths.get(BmsConfig.getDownloadPath(), REL_DIR).toAbsolutePath().normalize();
|
||||||
|
Path file = Paths.get(BmsConfig.getDownloadPath()).toAbsolutePath().normalize().resolve(storageKey).normalize();
|
||||||
|
if (!file.startsWith(root) || !Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS))
|
||||||
|
{
|
||||||
|
throw new ServiceException("导出文件不存在");
|
||||||
|
}
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||||
|
FileUtils.setAttachmentResponseHeader(response, task.getFileName());
|
||||||
|
FileUtils.writeBytes(file.toString(), response.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 25 * * * ?")
|
||||||
|
public void cleanupExpiredFiles()
|
||||||
|
{
|
||||||
|
Path dir = Paths.get(BmsConfig.getDownloadPath(), REL_DIR).toAbsolutePath().normalize();
|
||||||
|
if (!Files.isDirectory(dir)) return;
|
||||||
|
long cutoff = System.currentTimeMillis() - FILE_RETENTION_MILLIS;
|
||||||
|
try (Stream<Path> files = Files.list(dir))
|
||||||
|
{
|
||||||
|
files.filter(file -> file.getFileName().toString().matches("[a-f0-9]{32}\\.xlsx"))
|
||||||
|
.filter(file -> Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS))
|
||||||
|
.forEach(file -> {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (Files.getLastModifiedTime(file, LinkOption.NOFOLLOW_LINKS).toMillis() < cutoff)
|
||||||
|
{
|
||||||
|
Files.deleteIfExists(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) { log.warn("清理过期毛利导出文件失败: {}", file, e); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e) { log.warn("扫描过期毛利导出文件失败: {}", dir, e); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private void put(String taskId, Long userId, String status, int percent, String message, String error,
|
||||||
|
int rowCount, String fileName, String storageKey)
|
||||||
|
{
|
||||||
|
PrcProfitReportExportTask task = new PrcProfitReportExportTask();
|
||||||
|
task.setTaskId(taskId);
|
||||||
|
task.setUserId(userId);
|
||||||
|
task.setStatus(status);
|
||||||
|
task.setPercent(percent);
|
||||||
|
task.setMessage(message);
|
||||||
|
task.setError(error);
|
||||||
|
task.setRowCount(rowCount);
|
||||||
|
task.setFileName(fileName);
|
||||||
|
task.setStorageKey(storageKey);
|
||||||
|
redisCache.setCacheObject(CACHE_PREFIX + taskId, task, TTL_MINUTES, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -48,7 +48,7 @@ export function listProfitProducts(params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function exportProfitOrders(params) {
|
export function exportProfitOrders(params) {
|
||||||
return createProfitExport('/report/profit/orders/export', params)
|
return request({ url: '/report/profit/orders/export', method: 'post', data: params })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function exportProfitProducts(params) {
|
export function exportProfitProducts(params) {
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="app-container profit-report">
|
<div class="app-container profit-report">
|
||||||
|
<Teleport to="body">
|
||||||
|
<div v-if="exportFloatVisible" class="export-status-float">
|
||||||
|
<el-tooltip :content="exportMessage || '点击查看详情'" placement="bottom">
|
||||||
|
<el-button class="export-status-button" type="warning" plain size="small" @click="exportProgressVisible = true">
|
||||||
|
<span class="export-progress-ring" :style="{ '--export-progress': exportDegree + 'deg' }" aria-hidden="true">
|
||||||
|
<span class="export-progress-value">{{ exportPercent }}%</span>
|
||||||
|
</span>
|
||||||
|
<span class="export-status-text">
|
||||||
|
<strong>{{ exportFloatTitle }}</strong>
|
||||||
|
<small>{{ exportFloatSummary }}</small>
|
||||||
|
</span>
|
||||||
|
</el-button>
|
||||||
|
</el-tooltip>
|
||||||
|
</div>
|
||||||
|
</Teleport>
|
||||||
<el-card shadow="never" class="search-card">
|
<el-card shadow="never" class="search-card">
|
||||||
<el-form :model="query" label-width="88px" @submit.prevent>
|
<el-form :model="query" label-width="88px" @submit.prevent>
|
||||||
<el-row :gutter="16">
|
<el-row :gutter="16">
|
||||||
@@ -156,7 +171,7 @@
|
|||||||
icon="Download"
|
icon="Download"
|
||||||
v-hasPermi="['report:profit:export']"
|
v-hasPermi="['report:profit:export']"
|
||||||
:loading="exporting"
|
:loading="exporting"
|
||||||
:disabled="!hasProfitRule() || !hasSearched"
|
:disabled="!hasProfitRule() || !hasSearched || loading"
|
||||||
@click="handleExport"
|
@click="handleExport"
|
||||||
>导出订单</el-button>
|
>导出订单</el-button>
|
||||||
</div>
|
</div>
|
||||||
@@ -202,6 +217,17 @@
|
|||||||
/>
|
/>
|
||||||
</el-card>
|
</el-card>
|
||||||
|
|
||||||
|
<el-dialog v-model="exportProgressVisible" :title="exportDialogTitle" width="480px" append-to-body :close-on-click-modal="false">
|
||||||
|
<el-progress :percentage="exportPercent" :status="exportStatus" :stroke-width="14" striped :striped-flow="exporting" />
|
||||||
|
<div class="export-progress__msg">{{ exportMessage }}</div>
|
||||||
|
<div class="export-progress__text">导出行数 {{ exportRowCount }}</div>
|
||||||
|
<p v-if="exporting" class="export-progress__hint">导出在后台进行,可收起弹窗继续操作;请勿关闭或刷新本页</p>
|
||||||
|
<template #footer>
|
||||||
|
<el-button v-if="exporting" @click="exportProgressVisible = false">收起</el-button>
|
||||||
|
<el-button v-else type="primary" @click="exportProgressVisible = false">关闭</el-button>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
|
|
||||||
<!-- 提成设置与编辑弹窗暂停使用。
|
<!-- 提成设置与编辑弹窗暂停使用。
|
||||||
<el-dialog v-model="commissionDialogVisible" title="业务员固定提成比例" width="760px" append-to-body>
|
<el-dialog v-model="commissionDialogVisible" title="业务员固定提成比例" width="760px" append-to-body>
|
||||||
<div class="commission-toolbar">
|
<div class="commission-toolbar">
|
||||||
@@ -240,11 +266,11 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup name="ProfitReport">
|
<script setup name="ProfitReport">
|
||||||
import { downloadProfitExport, exportProfitOrders, listProfitAreaRules, listProfitCustomerOptions, listProfitOrders, listProfitSummary } from '@/api/report/profit'
|
import { listProfitAreaRules, listProfitCustomerOptions, listProfitOrders, listProfitSummary } from '@/api/report/profit'
|
||||||
// 提成功能暂停使用:import { deleteProfitCommissionRate, listProfitCommissionRates, saveProfitCommissionRate } from '@/api/report/profit'
|
// 提成功能暂停使用:import { deleteProfitCommissionRate, listProfitCommissionRates, saveProfitCommissionRate } from '@/api/report/profit'
|
||||||
import { parseTime } from '@/utils/bms'
|
import { parseTime } from '@/utils/bms'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
import { saveAs } from 'file-saver'
|
import { exportAsync } from '@/utils/excelExport'
|
||||||
// 提成功能暂停使用:import { ElMessageBox } from 'element-plus'
|
// 提成功能暂停使用:import { ElMessageBox } from 'element-plus'
|
||||||
|
|
||||||
const { customer_area, bms_currency } = useDict('customer_area', 'bms_currency')
|
const { customer_area, bms_currency } = useDict('customer_area', 'bms_currency')
|
||||||
@@ -275,6 +301,23 @@ let rowsRequestVersion = 0
|
|||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const hasSearched = ref(false)
|
const hasSearched = ref(false)
|
||||||
const exporting = ref(false)
|
const exporting = ref(false)
|
||||||
|
const exportFinished = ref(false)
|
||||||
|
const exportProgressVisible = ref(false)
|
||||||
|
const exportPercent = ref(0)
|
||||||
|
const exportStatus = ref(undefined)
|
||||||
|
const exportMessage = ref('')
|
||||||
|
const exportRowCount = ref(0)
|
||||||
|
let exportHideTimer = null
|
||||||
|
const exportFloatVisible = computed(() => exporting.value || exportFinished.value)
|
||||||
|
const exportDegree = computed(() => Math.round(exportPercent.value / 100 * 360))
|
||||||
|
const exportFloatTitle = computed(() => exporting.value ? '导出中' : (exportStatus.value === 'exception' ? '导出失败' : '导出完成'))
|
||||||
|
const exportFloatSummary = computed(() => exportRowCount.value > 0
|
||||||
|
? `${exportPercent.value}% / ${exportRowCount.value} 行`
|
||||||
|
: `${exportPercent.value}%`)
|
||||||
|
const exportDialogTitle = computed(() => exporting.value ? '异步导出进行中' : (exportStatus.value === 'exception' ? '异步导出失败' : '异步导出完成'))
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
clearTimeout(exportHideTimer)
|
||||||
|
})
|
||||||
const pageNum = ref(1)
|
const pageNum = ref(1)
|
||||||
const pageSize = ref(10)
|
const pageSize = ref(10)
|
||||||
// 汇总按币种分组,分页总数复用各币种订单数之和。
|
// 汇总按币种分组,分页总数复用各币种订单数之和。
|
||||||
@@ -441,23 +484,47 @@ function arBillStatusLabel(value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleExport() {
|
async function handleExport() {
|
||||||
if (!hasSearched.value || !validateRequiredFilters()) return
|
if (exporting.value || !hasSearched.value || !validateRequiredFilters()) return
|
||||||
if (!hasProfitRule() || !validateMultiValueFilters()) return
|
if (!hasProfitRule() || !validateMultiValueFilters()) return
|
||||||
|
clearTimeout(exportHideTimer)
|
||||||
exporting.value = true
|
exporting.value = true
|
||||||
|
exportFinished.value = false
|
||||||
|
exportProgressVisible.value = false
|
||||||
|
exportPercent.value = 0
|
||||||
|
exportStatus.value = undefined
|
||||||
|
exportMessage.value = '正在提交导出任务'
|
||||||
|
exportRowCount.value = 0
|
||||||
try {
|
try {
|
||||||
const response = await exportProfitOrders(buildQuery(false))
|
const result = await exportAsync({
|
||||||
const exportResult = response.data
|
baseUrl: '/report/profit/orders/export',
|
||||||
const file = await downloadProfitExport(exportResult.exportId)
|
query: buildQuery(false),
|
||||||
if (file.type?.includes('json')) {
|
filePrefix: '订单维度毛利分析',
|
||||||
const error = JSON.parse(await file.text())
|
showLoading: false,
|
||||||
throw new Error(error.msg || '下载导出文件失败')
|
onProgress: task => {
|
||||||
|
exportPercent.value = Math.max(0, Math.min(100, Number(task.percent) || 0))
|
||||||
|
exportRowCount.value = Number(task.rowCount) || 0
|
||||||
|
exportMessage.value = task.error || task.message || '导出中'
|
||||||
|
if (task.status === 'FAIL' || task.status === 'FAILED') exportStatus.value = 'exception'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (result) {
|
||||||
|
exportPercent.value = 100
|
||||||
|
exportStatus.value = 'success'
|
||||||
|
exportMessage.value = '导出完成'
|
||||||
|
} else {
|
||||||
|
if (exportStatus.value !== 'exception') exportMessage.value = '导出失败,请查看提示后重试'
|
||||||
|
exportStatus.value = 'exception'
|
||||||
}
|
}
|
||||||
saveAs(file, exportResult.fileName)
|
|
||||||
ElMessage.success('报表已导出')
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ElMessage.error(error?.message || '报表导出失败')
|
exportStatus.value = 'exception'
|
||||||
|
exportMessage.value = error?.message || '导出失败,请稍后重试'
|
||||||
} finally {
|
} finally {
|
||||||
exporting.value = false
|
exporting.value = false
|
||||||
|
exportFinished.value = true
|
||||||
|
exportHideTimer = window.setTimeout(() => {
|
||||||
|
exportFinished.value = false
|
||||||
|
exportHideTimer = null
|
||||||
|
}, 1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,6 +612,18 @@ watch(() => [
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
|
.export-status-float { position: fixed; top: 92px; right: 24px; z-index: 3001; }
|
||||||
|
.export-status-button { display: inline-flex; align-items: center; gap: 10px; min-width: 200px; min-height: 68px; padding: 6px 16px 6px 7px; border-radius: 36px; }
|
||||||
|
.export-progress-ring { position: relative; display: inline-flex; align-items: center; justify-content: center; flex: 0 0 54px; width: 54px; height: 54px; border-radius: 50%; background: conic-gradient(var(--el-color-warning) 0deg var(--export-progress, 0deg), var(--el-color-warning-light-8) var(--export-progress, 0deg) 360deg); }
|
||||||
|
.export-progress-ring::after { position: absolute; inset: 5px; content: ''; border-radius: 50%; background: var(--el-bg-color); }
|
||||||
|
.export-progress-value { position: relative; z-index: 1; color: var(--el-color-warning); font-size: 12px; font-weight: 600; line-height: 1; }
|
||||||
|
.export-status-text { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; line-height: 1.2; white-space: nowrap; }
|
||||||
|
.export-status-text strong { font-size: 14px; font-weight: 600; }
|
||||||
|
.export-status-text small { color: var(--el-text-color-secondary); font-size: 11px; }
|
||||||
|
.export-progress__msg { margin-top: 12px; color: var(--el-text-color-regular); }
|
||||||
|
.export-progress__text { margin-top: 8px; color: var(--el-text-color-secondary); font-size: 13px; }
|
||||||
|
.export-progress__hint { margin-top: 12px; color: var(--el-color-warning); font-size: 12px; }
|
||||||
|
|
||||||
.search-card,
|
.search-card,
|
||||||
.table-card {
|
.table-card {
|
||||||
margin-bottom: 14px;
|
margin-bottom: 14px;
|
||||||
|
|||||||
Reference in New Issue
Block a user