|
@@ -0,0 +1,257 @@
|
|
|
+package com.sd.business.service.purchase.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import com.sd.business.entity.apply.po.ApplyBuyBom;
|
|
|
+import com.sd.business.entity.bom.bo.BomSpecBo;
|
|
|
+import com.sd.business.entity.bom.po.BomSpec;
|
|
|
+import com.sd.business.entity.department.constant.DepartmentConstant;
|
|
|
+import com.sd.business.entity.in.dto.InOutStorageDto;
|
|
|
+import com.sd.business.entity.in.emums.InDetailTypeEnum;
|
|
|
+import com.sd.business.entity.in.emums.InOutTypeEnum;
|
|
|
+import com.sd.business.entity.in.po.InOutStorageBom;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchaseArrivalImportDataDto;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchasePendingStorageDto;
|
|
|
+import com.sd.business.entity.purchase.dto.PurchasePendingStorageSelectDto;
|
|
|
+import com.sd.business.entity.purchase.po.Purchase;
|
|
|
+import com.sd.business.entity.purchase.po.PurchaseBom;
|
|
|
+import com.sd.business.entity.purchase.po.PurchasePendingStorage;
|
|
|
+import com.sd.business.entity.purchase.po.PurchasePendingStorageBom;
|
|
|
+import com.sd.business.entity.purchase.vo.PurchasePendingStorageBomVo;
|
|
|
+import com.sd.business.entity.purchase.vo.PurchasePendingStorageVo;
|
|
|
+import com.sd.business.entity.warehouse.constant.WarehouseConstant;
|
|
|
+import com.sd.business.mapper.purchase.PurchasePendingStorageMapper;
|
|
|
+import com.sd.business.service.bom.BomSpecService;
|
|
|
+import com.sd.business.service.in.InOutStorageService;
|
|
|
+import com.sd.business.service.purchase.PurchaseBomService;
|
|
|
+import com.sd.business.service.purchase.PurchasePendingStorageBomService;
|
|
|
+import com.sd.business.service.purchase.PurchasePendingStorageService;
|
|
|
+import com.sd.business.service.purchase.PurchaseService;
|
|
|
+import com.sd.business.service.sku.SkuSpecService;
|
|
|
+import com.sd.business.util.CodeEnum;
|
|
|
+import com.sd.framework.util.Assert;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 采购待入库 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ * @since 2023-10-12
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class PurchasePendingStorageServiceImpl extends ServiceImpl<PurchasePendingStorageMapper, PurchasePendingStorage> implements PurchasePendingStorageService {
|
|
|
+ @Autowired
|
|
|
+ private PurchasePendingStorageBomService purchasePendingStorageBomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private InOutStorageService inOutStorageService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SkuSpecService skuSpecService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PurchaseService purchaseService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PurchaseBomService purchaseBomService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BomSpecService bomSpecService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<PurchasePendingStorageVo> getPage(PurchasePendingStorageSelectDto dto) {
|
|
|
+ IWrapper<PurchasePendingStorage> wrapper = getWrapper();
|
|
|
+ wrapper.orderByDesc("pps", PurchasePendingStorage::getId);
|
|
|
+ wrapper.like("pps", PurchasePendingStorage::getCode, dto.getCode());
|
|
|
+ wrapper.like("pps", PurchasePendingStorage::getArrivalCode, dto.getArrivalCode());
|
|
|
+ wrapper.like("p", Purchase::getCode, dto.getPurchaseCode());
|
|
|
+ Page<PurchasePendingStorageVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PurchasePendingStorageVo detail(Long id) {
|
|
|
+ PurchasePendingStorage purchasePendingStorage = this.getById(id);
|
|
|
+ PurchasePendingStorageVo result = BeanUtil.toBean(purchasePendingStorage, PurchasePendingStorageVo.class);
|
|
|
+
|
|
|
+ // 赋值采购待入库明细
|
|
|
+ List<PurchasePendingStorageBomVo> list = purchasePendingStorageBomService.getListByPurchasePendingStorageId(id);
|
|
|
+ result.setPurchasePendingStorageBomList(list);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public void add(PurchasePendingStorageDto dto) {
|
|
|
+ List<PurchasePendingStorageBom> purchasePendingStorageBomList = dto.getPurchasePendingStorageBomList();
|
|
|
+
|
|
|
+ BigDecimal arrivalQuantityTotal = purchasePendingStorageBomList.stream()
|
|
|
+ .map(PurchasePendingStorageBom::getArrivalQuantity)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ // 保存采购待入库
|
|
|
+ dto.setCode(CodeEnum.PURCHASE_STORAGE_CODE.getCode());
|
|
|
+ dto.setArrivalQuantity(arrivalQuantityTotal);
|
|
|
+ this.save(dto);
|
|
|
+
|
|
|
+ List<Long> bomSpecIds = purchasePendingStorageBomList.stream().map(PurchasePendingStorageBom::getBomSpecId).collect(Collectors.toList());
|
|
|
+ Map<Long, BomSpecBo> bomSpecBoMap = skuSpecService.getBomSpecBoByIdList(bomSpecIds);
|
|
|
+ purchasePendingStorageBomList.forEach(item -> {
|
|
|
+ BomSpecBo bomSpecBo = bomSpecBoMap.get(item.getBomSpecId());
|
|
|
+ if (bomSpecBo == null) {
|
|
|
+ throw new ServiceException("未知bom规格id:" + item.getBomSpecId());
|
|
|
+ }
|
|
|
+ // 判断入库仓库id
|
|
|
+ Long warehouseId;
|
|
|
+ if (ObjectUtil.equals(bomSpecBo.getClassifyParentId(), 1L)) {
|
|
|
+ warehouseId = WarehouseConstant.SEMI_FINISHED_PRODUCT;
|
|
|
+ } else {
|
|
|
+ warehouseId = WarehouseConstant.PACKAGING_MATERIAL;
|
|
|
+ }
|
|
|
+ item.setPurchasePendingStorageId(dto.getId());
|
|
|
+ item.setWarehouseId(warehouseId);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 根据采购合同分组
|
|
|
+ Map<Long, List<PurchasePendingStorageBom>> map = purchasePendingStorageBomList.stream().collect(Collectors.groupingBy(PurchasePendingStorageBom::getPurchaseId));
|
|
|
+ for (Long purchaseIds : map.keySet()) {
|
|
|
+ List<PurchasePendingStorageBom> purchaseStorageBomList = map.get(purchaseIds);
|
|
|
+ // 获取入库仓库id
|
|
|
+ Set<Long> warehouseIds = purchaseStorageBomList.stream().map(PurchasePendingStorageBom::getWarehouseId).collect(Collectors.toSet());
|
|
|
+ if (warehouseIds.size() != 1) {
|
|
|
+ throw new ServiceException("当前采购bom入仓不一致!");
|
|
|
+ }
|
|
|
+ Long warehouseId = warehouseIds.iterator().next();
|
|
|
+
|
|
|
+ // 入库
|
|
|
+ List<InOutStorageBom> inOutStorageBomList = purchaseStorageBomList.stream()
|
|
|
+ .map(item -> {
|
|
|
+ InOutStorageBom inOutStorageBom = new InOutStorageBom();
|
|
|
+ inOutStorageBom.setBomSpecId(item.getBomSpecId());
|
|
|
+ inOutStorageBom.setQuantity(item.getArrivalQuantity());
|
|
|
+ return inOutStorageBom;
|
|
|
+ })
|
|
|
+ .filter(item -> Objects.nonNull(item.getQuantity()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ InOutStorageDto inOutStorageDto = new InOutStorageDto();
|
|
|
+ inOutStorageDto.setType(InOutTypeEnum.IN.getKey());
|
|
|
+ inOutStorageDto.setPurchaseId(purchaseIds);
|
|
|
+ inOutStorageDto.setDetailType(InDetailTypeEnum.PURCHASE.getKey());
|
|
|
+ inOutStorageDto.setWarehouseId(warehouseId);
|
|
|
+ inOutStorageDto.setDepartmentId(DepartmentConstant.SD_SPORTS);
|
|
|
+ inOutStorageDto.setApplicant(SecurityUtils.getLoginUser().getUser().getNickName());
|
|
|
+ inOutStorageDto.setInOutStorageBomList(inOutStorageBomList);
|
|
|
+ inOutStorageService.add(inOutStorageDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存采购待入库明细
|
|
|
+ purchasePendingStorageBomService.saveBatch(purchasePendingStorageBomList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+ this.removeById(id);
|
|
|
+ purchasePendingStorageBomService.remove(q -> q.eq(PurchasePendingStorageBom::getPurchasePendingStorageId, id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PurchasePendingStorageVo purchaseArrivalImport(List<PurchaseArrivalImportDataDto> list) {
|
|
|
+ validatedImportData(list);
|
|
|
+
|
|
|
+ PurchasePendingStorageVo purchasePendingStorageVo = new PurchasePendingStorageVo();
|
|
|
+ List<String> purchaseCodes = list.stream().map(PurchaseArrivalImportDataDto::getPurchaseCode).collect(Collectors.toList());
|
|
|
+ // 采购合同数据
|
|
|
+ List<Purchase> purchaseList = purchaseService.list(q -> q.in(Purchase::getCode, purchaseCodes));
|
|
|
+ if (purchaseList.isEmpty()) {
|
|
|
+ throw new ServiceException("未知采购合同编号");
|
|
|
+ }
|
|
|
+ Map<String, Purchase> purchaseMap = purchaseList.stream().collect(Collectors.toMap(Purchase::getCode, Function.identity()));
|
|
|
+ List<Long> purchaseIds = purchaseList.stream().map(BaseIdPo::getId).collect(Collectors.toList());
|
|
|
+ // 采购合同bom 数据
|
|
|
+ List<PurchaseBom> purchaseBomAllList = purchaseBomService.list(q -> q.in(PurchaseBom::getPurchaseId, purchaseIds));
|
|
|
+ Map<Long, List<PurchaseBom>> purchaseBomMap = purchaseBomAllList.stream().collect(Collectors.groupingBy(PurchaseBom::getPurchaseId));
|
|
|
+ // bom数据
|
|
|
+ List<Long> bomSpecIds = purchaseBomAllList.stream().map(PurchaseBom::getBomSpecId).collect(Collectors.toList());
|
|
|
+ Map<Long, BomSpecBo> bomSpecBoMap = skuSpecService.getBomSpecBoByIdList(bomSpecIds);
|
|
|
+ List<String> bomSpecCodes = list.stream().map(PurchaseArrivalImportDataDto::getBomSpecCode).collect(Collectors.toList());
|
|
|
+ Map<String, BomSpec> bomSpecMap = bomSpecService.mapKEntity(BomSpec::getCode, q -> q.in(BomSpec::getCode, bomSpecCodes));
|
|
|
+
|
|
|
+ List<PurchasePendingStorageBomVo> purchasePendingStorageBomList = new ArrayList<>();
|
|
|
+ for (PurchaseArrivalImportDataDto dto : list) {
|
|
|
+ Purchase purchase = purchaseMap.get(dto.getPurchaseCode());
|
|
|
+ if (purchase == null) {
|
|
|
+ throw new ServiceException("未知采购合同编号:" + dto.getPurchaseCode());
|
|
|
+ }
|
|
|
+ // 判断采购合同是否存在当前bom
|
|
|
+ BomSpec bomSpec = bomSpecMap.get(dto.getBomSpecCode());
|
|
|
+ if (bomSpec == null) {
|
|
|
+ throw new ServiceException("未知bom品号:" + dto.getPurchaseCode());
|
|
|
+ }
|
|
|
+ List<PurchaseBom> purchaseBomList = purchaseBomMap.get(purchase.getId());
|
|
|
+ PurchaseBom purchaseBom = purchaseBomList.stream().filter(item -> ObjectUtil.equals(bomSpec.getId(), item.getBomSpecId())).findFirst().orElse(null);
|
|
|
+ if (purchaseBom == null) {
|
|
|
+ throw new ServiceException("采购合同" + dto.getPurchaseCode() + "清单中不存在bom品号:" + dto.getPurchaseCode());
|
|
|
+ }
|
|
|
+ // 判断到货数量是否超出采购可入库数量
|
|
|
+ BigDecimal arrivalQuantity = new BigDecimal(dto.getArrivalQuantity());
|
|
|
+ BigDecimal canInStorageQuantity = purchaseBom.getPurchaseQuantity().subtract(purchaseBom.getArrivalQuantity()).add(purchaseBom.getReturnQuantity());
|
|
|
+ if (canInStorageQuantity.compareTo(arrivalQuantity) < 0) {
|
|
|
+ throw new ServiceException("bom品号:" + dto.getPurchaseCode() + "超出可入库数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ BomSpecBo bomSpecBo = bomSpecBoMap.get(bomSpec.getId());
|
|
|
+
|
|
|
+ // 赋值明细
|
|
|
+ PurchasePendingStorageBomVo bomVo = new PurchasePendingStorageBomVo();
|
|
|
+ bomVo.setPurchaseCode(dto.getPurchaseCode());
|
|
|
+ bomVo.setPurchaseId(purchase.getId());
|
|
|
+ bomVo.setBomSpecId(bomSpec.getId());
|
|
|
+ bomVo.setArrivalQuantity(arrivalQuantity);
|
|
|
+ if (ObjectUtil.equals(bomSpecBo.getClassifyParentId(), 1L)) {
|
|
|
+ bomVo.setWarehouseName("半成品仓");
|
|
|
+ } else {
|
|
|
+ bomVo.setWarehouseName("包材仓");
|
|
|
+ }
|
|
|
+ bomVo.setBomSpecCode(bomSpec.getCode());
|
|
|
+ bomVo.setBomSpecName(bomSpec.getName());
|
|
|
+ bomVo.setBomSpecLength(bomSpec.getLength());
|
|
|
+ bomVo.setBomSpecWidth(bomSpec.getWidth());
|
|
|
+ bomVo.setBomSpecHeight(bomSpec.getHeight());
|
|
|
+ purchasePendingStorageBomList.add(bomVo);
|
|
|
+
|
|
|
+ if (purchasePendingStorageVo.getArrivalCode() == null) {
|
|
|
+ purchasePendingStorageVo.setArrivalCode(dto.getArrivalCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ purchasePendingStorageVo.setPurchasePendingStorageBomList(purchasePendingStorageBomList);
|
|
|
+ return purchasePendingStorageVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validatedImportData(List<PurchaseArrivalImportDataDto> list) {
|
|
|
+ // 删除第一行表头字段
|
|
|
+ list.remove(0);
|
|
|
+ for (PurchaseArrivalImportDataDto dto : list) {
|
|
|
+ Assert.notNull(dto.getPurchaseCode(), "采购合同编号不能为空");
|
|
|
+ Assert.notNull(dto.getBomSpecCode(), "bom品号不能为空");
|
|
|
+ Assert.notNull(dto.getArrivalCode(), "到货单号不能为空");
|
|
|
+ Assert.eqTrue(NumberUtil.isInteger(dto.getArrivalQuantity()), "到货数量格式不正确");
|
|
|
+ Assert.gtZero(new BigDecimal(dto.getArrivalQuantity()), "到货数量必须大于0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|