|
@@ -0,0 +1,185 @@
|
|
|
+package com.sd.business.service.inventory.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ruoyi.common.constant.StatusConstant;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import com.sd.business.entity.inventory.dto.InventoryFinishedSelectDto;
|
|
|
+import com.sd.business.entity.inventory.po.InventoryFinished;
|
|
|
+import com.sd.business.entity.inventory.po.InventoryFinishedOrder;
|
|
|
+import com.sd.business.entity.inventory.vo.InventoryFinishedVo;
|
|
|
+import com.sd.business.entity.order.po.OrderSku;
|
|
|
+import com.sd.business.mapper.inventory.InventoryFinishedMapper;
|
|
|
+import com.sd.business.service.inventory.InventoryFinishedOrderService;
|
|
|
+import com.sd.business.service.inventory.InventoryFinishedService;
|
|
|
+import com.sd.business.service.order.OrderService;
|
|
|
+import com.sd.business.service.order.OrderSkuService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 成品仓库 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2023-10-10
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class InventoryFinishedServiceImpl extends ServiceImpl<InventoryFinishedMapper, InventoryFinished> implements InventoryFinishedService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderService orderService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderSkuService orderSkuService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private InventoryFinishedOrderService inventoryFinishedOrderService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<InventoryFinishedVo> getPage(InventoryFinishedSelectDto dto) {
|
|
|
+ IWrapper<InventoryFinished> wrapper = getWrapper();
|
|
|
+ wrapper.orderByDesc("if", InventoryFinished::getId);
|
|
|
+ Page<InventoryFinishedVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void productionWarehousing(List<Long> orderIdList) {
|
|
|
+ if (orderIdList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询订单sku明细
|
|
|
+ List<OrderSku> orderSkuList = orderSkuService.list(q -> q.in(OrderSku::getOrderId, orderIdList));
|
|
|
+ if (orderSkuList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加成品仓库存
|
|
|
+ List<InventoryFinished> inventoryFinishedList = orderSkuList.stream().map(item -> {
|
|
|
+ InventoryFinished inventoryFinished = new InventoryFinished();
|
|
|
+ inventoryFinished.setSkuSpecId(item.getSkuSpecId());
|
|
|
+ inventoryFinished.setQuantity(item.getQuantity());
|
|
|
+ return inventoryFinished;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ inOut(inventoryFinishedList, true);
|
|
|
+ inventoryFinishedOrderService.productionWarehousing(orderSkuList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void removeOrder(Long orderId) {
|
|
|
+
|
|
|
+ // 查询成品仓库
|
|
|
+ List<InventoryFinishedOrder> list = inventoryFinishedOrderService.list(q -> q
|
|
|
+ .eq(InventoryFinishedOrder::getOrderInfoId, orderId)
|
|
|
+ .eq(InventoryFinishedOrder::getStatus, StatusConstant.YES));
|
|
|
+
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 成品库存出库
|
|
|
+ outByFinishedOrder(list);
|
|
|
+
|
|
|
+ // 删除成品仓库订单以及明细
|
|
|
+ inventoryFinishedOrderService.removeByOrderId(orderId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saleOutOfWarehouse(List<Long> orderIdList) {
|
|
|
+ if (orderIdList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询成品仓库
|
|
|
+ List<InventoryFinishedOrder> list = inventoryFinishedOrderService.list(q -> q
|
|
|
+ .in(InventoryFinishedOrder::getOrderInfoId, orderIdList)
|
|
|
+ .eq(InventoryFinishedOrder::getStatus, StatusConstant.YES));
|
|
|
+
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 成品库存出库
|
|
|
+ outByFinishedOrder(list);
|
|
|
+
|
|
|
+ // 产品库订单出库
|
|
|
+ inventoryFinishedOrderService.saleOutOfWarehouse(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 成品仓sku规格出入库
|
|
|
+ */
|
|
|
+ private void inOut(List<InventoryFinished> list, boolean isIn) {
|
|
|
+
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 相同sku合并
|
|
|
+ Map<Long, InventoryFinished> map = list.stream().collect(Collectors.toMap(
|
|
|
+ InventoryFinished::getSkuSpecId,
|
|
|
+ Function.identity(),
|
|
|
+ (v1, v2) -> {
|
|
|
+ v1.setQuantity(v1.getQuantity().add(v2.getQuantity()));
|
|
|
+ return v1;
|
|
|
+ }));
|
|
|
+
|
|
|
+ synchronized (this) {
|
|
|
+
|
|
|
+ // 获取原库存数据
|
|
|
+ Map<Long, InventoryFinished> oldStorageMap = mapKEntity(InventoryFinished::getSkuSpecId,
|
|
|
+ q -> q.in(InventoryFinished::getSkuSpecId, map.keySet()));
|
|
|
+
|
|
|
+ // 计算入库后库存数量
|
|
|
+ for (InventoryFinished item : map.values()) {
|
|
|
+ InventoryFinished inventoryFinished = oldStorageMap.get(item.getSkuSpecId());
|
|
|
+ if (inventoryFinished == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ BigDecimal quantity;
|
|
|
+ if (isIn) {
|
|
|
+ quantity = inventoryFinished.getQuantity().add(item.getQuantity());
|
|
|
+ } else {
|
|
|
+ quantity = inventoryFinished.getQuantity().subtract(item.getQuantity());
|
|
|
+ }
|
|
|
+ item.setId(inventoryFinished.getId());
|
|
|
+ item.setQuantity(quantity);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新数据库
|
|
|
+ saveOrUpdateBatch(map.values());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 成品库存出库
|
|
|
+ *
|
|
|
+ * @param list 成品仓库
|
|
|
+ */
|
|
|
+ private void outByFinishedOrder(List<InventoryFinishedOrder> list) {
|
|
|
+
|
|
|
+ List<InventoryFinished> inventoryFinishedList = list.stream().map(item -> {
|
|
|
+ InventoryFinished inventoryFinished = new InventoryFinished();
|
|
|
+ inventoryFinished.setQuantity(item.getQuantity());
|
|
|
+ inventoryFinished.setSkuSpecId(item.getSkuSpecId());
|
|
|
+ return inventoryFinished;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ inOut(inventoryFinishedList, false);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|