From 523365960513f297024a419f94b2b42eccd9456f Mon Sep 17 00:00:00 2001
From: zhou zhou <3272660260@qq.com>
Date: 星期四, 09 四月 2026 11:21:41 +0800
Subject: [PATCH] #
---
rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/AsnOrderServiceImpl.java | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 203 insertions(+), 15 deletions(-)
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/AsnOrderServiceImpl.java b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/AsnOrderServiceImpl.java
index 626d2d4..c74cd30 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/AsnOrderServiceImpl.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/manager/service/impl/AsnOrderServiceImpl.java
@@ -7,26 +7,38 @@
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.entity.dto.PoItemsDto;
-import com.vincent.rsf.server.api.service.ReceiveMsgService;
import com.vincent.rsf.server.api.service.ReportMsgService;
+import com.vincent.rsf.server.common.service.RedisService;
+import com.vincent.rsf.server.common.utils.DateUtils;
+import com.vincent.rsf.server.manager.controller.dto.DashboardDto;
+import com.vincent.rsf.server.manager.controller.dto.StockTrandDto;
+import com.vincent.rsf.server.manager.controller.dto.StockTransItemDto;
import com.vincent.rsf.server.manager.controller.params.AsnOrderAndItemsParams;
import com.vincent.rsf.server.manager.controller.params.BatchUpdateParam;
import com.vincent.rsf.server.manager.entity.*;
-import com.vincent.rsf.server.manager.enums.AsnExceStatus;
-import com.vincent.rsf.server.manager.enums.POExceStatus;
+import com.vincent.rsf.server.manager.enums.*;
import com.vincent.rsf.server.manager.mapper.AsnOrderMapper;
+import com.vincent.rsf.server.manager.mapper.TaskLogMapper;
+import com.vincent.rsf.server.manager.mapper.TaskMapper;
import com.vincent.rsf.server.manager.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.mapper.SerialRuleMapper;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
+import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import javax.annotation.Resource;
+import jakarta.annotation.Resource;
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.text.DateFormat;
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@@ -37,11 +49,17 @@
* @return
* @time 2025/3/7 08:02
*/
+@Slf4j
@Service("asnOrderService")
public class AsnOrderServiceImpl extends ServiceImpl<AsnOrderMapper, WkOrder> implements AsnOrderService {
- @Autowired
- private ReceiveMsgService receiveMsgService;
+ private static final String DASHBOARD_HEADER_CACHE_FLAG = "DASHBOARD_HEADER";
+ private static final String DASHBOARD_HEADER_CACHE_FRESH_SUFFIX = "FRESH";
+ private static final String DASHBOARD_HEADER_CACHE_STALE_SUFFIX = "STALE";
+ private static final int DASHBOARD_HEADER_CACHE_FRESH_TTL_SECONDS = 300;
+ private static final int DASHBOARD_HEADER_CACHE_STALE_TTL_SECONDS = 86400;
+ private static final DateTimeFormatter DASHBOARD_CACHE_DATE_FORMATTER = DateTimeFormatter.BASIC_ISO_DATE;
+
@Autowired
private ReportMsgService reportMsgService;
@@ -59,6 +77,12 @@
private PurchaseService purchaseService;
@Autowired
private PurchaseItemService purchaseItemService;
+ @Autowired
+ private TaskMapper taskMapper;
+ @Autowired
+ private TaskLogMapper taskLogMapper;
+ @Autowired
+ private RedisService redisService;
@Override
public boolean notifyInspect(List<WkOrder> orders) {
@@ -197,14 +221,32 @@
if (Objects.isNull(order)) {
throw new CoolException("淇敼鍙傛暟涓嶈兘涓虹┖锛侊紒");
}
- return this.update(new LambdaUpdateWrapper<WkOrder>()
- .in(WkOrder::getId, params.getIds())
- .set(!Objects.isNull(order.getRleStatus()), WkOrder::getRleStatus, order.getRleStatus())
- .set(!Objects.isNull(order.getNtyStatus()), WkOrder::getNtyStatus, order.getNtyStatus())
- .set(!Objects.isNull(order.getStatus()), WkOrder::getStatus, order.getStatus())
- .set(!Objects.isNull(order.getWkType()), WkOrder::getWkType, order.getWkType())
- .set(!Objects.isNull(order.getExceStatus()), WkOrder::getExceStatus, order.getExceStatus())
- .set(WkOrder::getUpdateBy, userId));
+ List<WkOrder> orders = this.listByIds(params.getIds());
+ if (orders.isEmpty()) {
+ return false;
+ }
+ for (WkOrder current : orders) {
+ if (!Objects.isNull(order.getRleStatus())) {
+ current.setRleStatus(order.getRleStatus());
+ }
+ if (!Objects.isNull(order.getNtyStatus())) {
+ current.setNtyStatus(order.getNtyStatus());
+ }
+ if (!Objects.isNull(order.getStatus())) {
+ current.setStatus(order.getStatus());
+ }
+ if (!Objects.isNull(order.getWkType())) {
+ current.setWkType(order.getWkType());
+ }
+ if (!Objects.isNull(order.getExceStatus())) {
+ current.setExceStatus(order.getExceStatus());
+ }
+ current.setUpdateBy(userId);
+ if (!this.updateById(current)) {
+ throw new CoolException("鎵归噺淇敼澶辫触锛侊紒");
+ }
+ }
+ return true;
}
/**
@@ -392,13 +434,77 @@
throw new CoolException("浠诲姟涓崟鎹笉鍙垹闄わ紒锛�");
}
-
if (!asnOrderItemService.remove(new LambdaQueryWrapper<WkOrderItem>()
.in(WkOrderItem::getOrderId, ids))) {
// throw new CoolException("Details Delete Fail");
}
return R.ok("鎿嶄綔鎴愬姛锛侊紒");
+ }
+
+ /**
+ * 鑾峰彇棣栭〉琛ㄥご鏁版嵁
+ * @return
+ */
+ @Override
+ public R getDashbord() {
+ String freshCacheKey = buildDashboardCacheKey(DASHBOARD_HEADER_CACHE_FRESH_SUFFIX);
+ DashboardDto freshSnapshot = getDashboardCache(freshCacheKey);
+ if (freshSnapshot != null) {
+ return R.ok().add(freshSnapshot);
+ }
+ String staleCacheKey = buildDashboardCacheKey(DASHBOARD_HEADER_CACHE_STALE_SUFFIX);
+ Exception dbException = null;
+ try {
+ DashboardDto snapshot = buildDashboardSnapshot();
+ cacheDashboard(freshCacheKey, snapshot, DASHBOARD_HEADER_CACHE_FRESH_TTL_SECONDS);
+ cacheDashboard(staleCacheKey, snapshot, DASHBOARD_HEADER_CACHE_STALE_TTL_SECONDS);
+ return R.ok().add(snapshot);
+ } catch (Exception ex) {
+ dbException = ex;
+ log.warn("Load dashboard snapshot from database failed, fallback to stale cache. message={}", ex.getMessage(), ex);
+ }
+
+ DashboardDto staleSnapshot = getDashboardCache(staleCacheKey);
+ if (staleSnapshot != null) {
+ return R.ok().add(staleSnapshot);
+ }
+ log.error("Load dashboard snapshot failed, returning empty snapshot.", dbException);
+ return R.ok().add(emptyDashboardSnapshot());
+ }
+
+ /**
+ * 鑾峰彇鍑哄叆搴撹秼鍔�
+ * @return
+ */
+ @Override
+ public R getStockTrand() {
+ List<String> days = DateUtils.getLastMonthDays("yyyy-MM-dd");
+ List<StockTransItemDto> items = this.baseMapper.getStockTrand();
+ if (items.isEmpty()) {
+ return R.ok();
+ }
+ List<StockTransItemDto> stockDtos = new ArrayList<>();
+ days.forEach(day -> {
+ StockTransItemDto itemDto = new StockTransItemDto();
+ itemDto.setInQty(0).setOutQty(0).setOutAnfme(0).setOutAnfme(0);
+ items.forEach(item -> {
+ if (item.getOrderTime().equals(day)) {
+ BeanUtils.copyProperties(item, itemDto);
+ }
+ });
+ itemDto.setOrderTime(day);
+ stockDtos.add(itemDto);
+ });
+
+ //鑾峰彇鏈�澶у��
+ Optional<Integer> max = stockDtos.stream().map(StockTransItemDto::getInQty).filter(Objects::nonNull).max(Comparator.naturalOrder());
+ Optional<Integer> maxOut = stockDtos.stream().map(StockTransItemDto::getOutQty).filter(Objects::nonNull).max(Comparator.naturalOrder());
+ int maxed = Math.max(max.orElse(Integer.MIN_VALUE), maxOut.orElse(Integer.MIN_VALUE));
+
+ StockTrandDto trandDto = new StockTrandDto();
+ trandDto.setMaxQty(maxed).setTrandItem(stockDtos);
+ return R.ok().add(trandDto);
}
/**
@@ -460,4 +566,86 @@
throw new CoolException("鍘熷崟鎹垹闄ゅけ璐ワ紒锛�");
}
}
+
+ private DashboardDto buildDashboardSnapshot() {
+ Date[] todayRange = buildTodayRange();
+ Date todayStart = todayRange[0];
+ Date tomorrowStart = todayRange[1];
+
+ int inAnf = safeToInt(this.count(new LambdaQueryWrapper<WkOrder>()
+ .eq(WkOrder::getType, OrderType.ORDER_IN.type)
+ .ge(WkOrder::getCreateTime, todayStart)
+ .lt(WkOrder::getCreateTime, tomorrowStart)));
+ int outAnf = safeToInt(this.count(new LambdaQueryWrapper<WkOrder>()
+ .eq(WkOrder::getType, OrderType.ORDER_OUT.type)
+ .ge(WkOrder::getCreateTime, todayStart)
+ .lt(WkOrder::getCreateTime, tomorrowStart)));
+ int taskIn = safeToInt(taskLogMapper.selectCount(new LambdaQueryWrapper<TaskLog>()
+ .eq(TaskLog::getTaskType, TaskType.TASK_TYPE_IN.type)
+ .ge(TaskLog::getCreateTime, todayStart)
+ .lt(TaskLog::getCreateTime, tomorrowStart)));
+ int taskOut = safeToInt(taskLogMapper.selectCount(new LambdaQueryWrapper<TaskLog>()
+ .eq(TaskLog::getTaskType, TaskType.TASK_TYPE_OUT.type)
+ .ge(TaskLog::getCreateTime, todayStart)
+ .lt(TaskLog::getCreateTime, tomorrowStart)));
+ int taskQty = safeToInt(taskMapper.selectCount(new LambdaQueryWrapper<Task>()));
+
+ return new DashboardDto()
+ .setInAnf(inAnf)
+ .setOutAnf(outAnf)
+ .setTaskIn(taskIn)
+ .setTaskOut(taskOut)
+ .setTaskQty(taskQty)
+ .setTotalIn(inAnf + taskIn)
+ .setTotalOut(outAnf + taskOut);
+ }
+
+ private DashboardDto getDashboardCache(String cacheKey) {
+ try {
+ String cacheValue = redisService.getValue(DASHBOARD_HEADER_CACHE_FLAG, cacheKey);
+ if (StringUtils.isBlank(cacheValue)) {
+ return null;
+ }
+ return JSONObject.parseObject(cacheValue, DashboardDto.class);
+ } catch (Exception ex) {
+ log.warn("Read dashboard cache failed, key={}, message={}", cacheKey, ex.getMessage(), ex);
+ return null;
+ }
+ }
+
+ private void cacheDashboard(String cacheKey, DashboardDto dto, int ttlSeconds) {
+ try {
+ redisService.setValue(DASHBOARD_HEADER_CACHE_FLAG, cacheKey, JSONObject.toJSONString(dto), ttlSeconds);
+ } catch (Exception ex) {
+ log.warn("Write dashboard cache failed, key={}, message={}", cacheKey, ex.getMessage(), ex);
+ }
+ }
+
+ private String buildDashboardCacheKey(String suffix) {
+ String dateBucket = LocalDate.now().format(DASHBOARD_CACHE_DATE_FORMATTER);
+ return dateBucket + "." + suffix;
+ }
+
+ private Date[] buildTodayRange() {
+ ZoneId zoneId = ZoneId.systemDefault();
+ LocalDate today = LocalDate.now(zoneId);
+ Date todayStart = Date.from(today.atStartOfDay(zoneId).toInstant());
+ Date tomorrowStart = Date.from(today.plusDays(1).atStartOfDay(zoneId).toInstant());
+ return new Date[]{todayStart, tomorrowStart};
+ }
+
+ private DashboardDto emptyDashboardSnapshot() {
+ return new DashboardDto()
+ .setInAnf(0)
+ .setOutAnf(0)
+ .setTaskIn(0)
+ .setTaskOut(0)
+ .setTaskQty(0)
+ .setTotalIn(0)
+ .setTotalOut(0);
+ }
+
+ private int safeToInt(Long count) {
+ return count == null ? 0 : count.intValue();
+ }
}
--
Gitblit v1.9.1