package com.mzl.flower.service.warehouse; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.dto.response.payment.OrderDTO; import com.mzl.flower.dto.response.warehouse.WarehouseLocationDTO; import com.mzl.flower.entity.payment.Order; import com.mzl.flower.entity.payment.OrderItem; import com.mzl.flower.entity.warehouse.Warehouse; import com.mzl.flower.entity.warehouse.WarehouseLocation; import com.mzl.flower.mapper.payment.OrderItemMapper; import com.mzl.flower.mapper.payment.OrderMapper; import com.mzl.flower.mapper.warehouse.WarehouseLocationMapper; import com.mzl.flower.mapper.warehouse.WarehouseMapper; import com.mzl.flower.service.payment.OrderService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.LinkedList; import java.util.List; import java.util.Queue; @Service @Transactional @Slf4j public class WarehouseService { private final WarehouseMapper warehouseMapper; private final WarehouseLocationMapper warehouseLocationMapper; private final OrderMapper orderMapper; private final OrderItemMapper orderItemMapper; private final OrderService orderService; public WarehouseService(WarehouseMapper warehouseMapper, WarehouseLocationMapper warehouseLocationMapper, OrderMapper orderMapper, OrderItemMapper orderItemMapper, OrderService orderService) { this.warehouseMapper = warehouseMapper; this.warehouseLocationMapper = warehouseLocationMapper; this.orderMapper = orderMapper; this.orderItemMapper = orderItemMapper; this.orderService = orderService; } public void addWarehouse(Warehouse dto) { dto.create(SecurityUtils.getUserId()); warehouseMapper.insert(dto); } public void updateWarehouse(Warehouse dto) { Warehouse warehouse = warehouseMapper.selectById(dto.getId()); if (warehouse == null) { throw new ValidationException("库位不存在"); } warehouse.setName(dto.getName()); warehouse.setSeq(dto.getSeq()); warehouse.update(SecurityUtils.getUserId()); warehouseMapper.updateById(warehouse); } public void deleteWarehouse(Long id) { Warehouse warehouse = warehouseMapper.selectById(id); if (warehouse == null) { throw new ValidationException("库位不存在"); } warehouseMapper.deleteById(id); } public List queryWarehouse() { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper(); lambdaQueryWrapper.orderByAsc(Warehouse::getSeq); return warehouseMapper.selectList(lambdaQueryWrapper); } public void addWarehouseLocation(WarehouseLocation dto) { if(existWarehouseLocation(dto.getCode(),null)){ throw new ValidationException("库位名称已存在"); } dto.create(SecurityUtils.getUserId()); warehouseLocationMapper.insert(dto); } private boolean existWarehouseLocation(String code, Long id) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper(); lambdaQueryWrapper.eq(WarehouseLocation::getCode,code); if(id!=null && id>0){ lambdaQueryWrapper.ne(WarehouseLocation::getId,id); } return warehouseLocationMapper.selectCount(lambdaQueryWrapper)>0; } public void updateWarehouseLocation(WarehouseLocation dto) { WarehouseLocation location = warehouseLocationMapper.selectById(dto.getId()); if(location==null){ throw new ValidationException("库位不存在"); } if(existWarehouseLocation(dto.getCode(),location.getId())){ throw new ValidationException("库位名称已存在"); } location.setWarehouseId(dto.getWarehouseId()); location.setCode(dto.getCode()); location.setSeq(dto.getSeq()); location.update(SecurityUtils.getUserId()); warehouseLocationMapper.updateById(location); } public void deleteWarehouseLocation(Long id) { WarehouseLocation location = warehouseLocationMapper.selectById(id); if(location==null){ throw new ValidationException("库位不存在"); } warehouseLocationMapper.deleteById(id); } public List queryWarehouseLocation(Long warehouseId, String code) { List locations = warehouseLocationMapper.findAll(warehouseId, code); for (WarehouseLocationDTO location : locations) { OrderDTO orderDTO = getOrderByWarehouseLocation(location.getId()); if(orderDTO == null){ location.setUsed(false); }else{ location.setItems(orderService.getPtCuOrderItems(orderDTO.getId())); location.setUsed(true); } location.setOrderDTO(orderDTO); } return locations; } /** * 查询库位订单信息 * */ public OrderDTO getOrderByWarehouseLocation(Long warehouseLocationId){ LocalDateTime start = LocalDate.now().plusDays(-1).atTime(17, 0, 0); LocalDateTime end = LocalDate.now().atTime(16, 59, 59); Order order = orderMapper.selectOne(new QueryWrapper().eq("deleted", 0) .eq("warehouse_location_id", warehouseLocationId) .gt("payment_time", start).le("payment_time", end)); if(order!=null){ OrderDTO dto = new OrderDTO(); BeanUtils.copyProperties(order, dto); return dto; } return null; } /** * 查询待分配的库位列表 * @return */ public Queue selectAllocatedWarehouseLocation() { List warehouses = warehouseMapper.selectList(new LambdaQueryWrapper() .ne(Warehouse::getName, "手动分配区").orderByAsc(Warehouse::getSeq)); if(warehouses!=null&&warehouses.size()>0){ Queue warehouseLocations = new LinkedList<>(); for (Warehouse warehouse : warehouses) { List locations = queryWarehouseLocation(warehouse.getId(),null); if(locations!=null&&locations.size()>0){ warehouseLocations.addAll(locations); } } return warehouseLocations; } log.error("系统库区查询失败"); return null; } }