| | |
| | | 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; |
| | | |
| | |
| | | * @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; |
| | | |
| | |
| | | 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) { |
| | |
| | | if (orderItems.isEmpty()) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Long> longList = orderItems.stream().map(WkOrderItem::getAsnId).collect(Collectors.toList()); |
| | | List<Long> longList = orderItems.stream().map(WkOrderItem::getOrderId).collect(Collectors.toList()); |
| | | |
| | | return this.listByIds(longList); |
| | | } |
| | |
| | | public void svaeOrUpdateOrderItem(AsnOrderAndItemsParams params, Long loginUserId) throws Exception { |
| | | WkOrder orders = params.getOrders(); |
| | | params.getItems().forEach(item -> { |
| | | item.put("asnId", orders.getId()); |
| | | item.put("asnCode", orders.getCode()); |
| | | item.put("orderId", orders.getId()); |
| | | item.put("orderCode", orders.getCode()); |
| | | item.put("poCode", orders.getPoCode()); |
| | | item.put("createBy", loginUserId); |
| | | item.put("updateBy", loginUserId); |
| | |
| | | } |
| | | }); |
| | | List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>() |
| | | .eq(WkOrderItem::getAsnId, params.getOrders().getId())); |
| | | .eq(WkOrderItem::getOrderId, params.getOrders().getId())); |
| | | double sum = orderItems.stream().mapToDouble(WkOrderItem::getAnfme).sum(); |
| | | orders.setAnfme(sum); |
| | | if (!this.updateById(orders)) { |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | throw new CoolException("新建单据数量不能大于计划数量!!"); |
| | | } |
| | | orderItem.setAnfme(item.getAnfme()) |
| | | .setAsnId(order.getId()) |
| | | .setOrderId(order.getId()) |
| | | .setSplrName(item.getSplrName()) |
| | | .setAsnCode(code) |
| | | .setOrderCode(code) |
| | | .setSplrBatch(item.getSplrBatch()) |
| | | .setSplrCode(item.getSplrCode()) |
| | | .setPoDetlId(item.getId()) |
| | |
| | | public R removeOrders(List<Long> ids) { |
| | | for (Long id : ids) { |
| | | List<WkOrderItem> list = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>() |
| | | .eq(WkOrderItem::getAsnId, id)); |
| | | .eq(WkOrderItem::getOrderId, id)); |
| | | if (list.isEmpty()) { |
| | | continue; |
| | | } |
| | |
| | | throw new CoolException("任务中单据不可删除!!"); |
| | | } |
| | | |
| | | |
| | | if (!asnOrderItemService.remove(new LambdaQueryWrapper<WkOrderItem>() |
| | | .in(WkOrderItem::getAsnId, ids))) { |
| | | .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); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (!this.updateById(asrder)) { |
| | | throw new CoolException("单据关闭失败!!"); |
| | | } |
| | | List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getAsnId, asrder.getId())); |
| | | List<WkOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, asrder.getId())); |
| | | if (orderItems.isEmpty()) { |
| | | throw new CoolException("收货明细为空!!"); |
| | | } |
| | |
| | | throw new CoolException("主单历史档添加失败!!"); |
| | | } |
| | | List<AsnOrderItemLog> logs = new ArrayList<>(); |
| | | List<WkOrderItem> items = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getAsnId, order.getId())); |
| | | List<WkOrderItem> items = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, order.getId())); |
| | | items.forEach(item -> { |
| | | AsnOrderItemLog itemLog = new AsnOrderItemLog(); |
| | | BeanUtils.copyProperties(item, itemLog); |
| | | itemLog.setAsnItemId(itemLog.getId()) |
| | | .setLogId(orderLog.getId()) |
| | | .setAsnId(item.getAsnId()); |
| | | .setAsnId(item.getOrderId()); |
| | | logs.add(itemLog); |
| | | }); |
| | | |
| | | if (!asnOrderItemLogService.saveBatch(logs)) { |
| | | throw new CoolException("通知单明细历史档保存失败!!"); |
| | | } |
| | | if (!asnOrderItemService.remove(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getAsnId, order.getId()))) { |
| | | if (!asnOrderItemService.remove(new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, order.getId()))) { |
| | | throw new CoolException("原单据明细删除失败!!"); |
| | | } |
| | | if (!this.removeById(asrder.getId())) { |
| | | 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(); |
| | | } |
| | | } |