| | |
| | | import com.ruoyi.production.mapper.ProductMaterialSkuMapper; |
| | | import com.ruoyi.production.pojo.ProductMaterial; |
| | | import com.ruoyi.production.pojo.ProductMaterialSku; |
| | | import com.ruoyi.production.pojo.ProductMaterialSkuImportDto; |
| | | import com.ruoyi.production.dto.ProductMaterialSkuImportDto; |
| | | import com.ruoyi.production.service.ProductMaterialSkuService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | public void addProductMaterialSku(ProductMaterialSku sku) { |
| | | validateProductMaterialSku(sku, false); |
| | | // 校验物料是否存在 |
| | | ProductMaterial material = productMaterialMapper.selectById(sku.getMaterialId()); |
| | | ProductMaterial material = productMaterialMapper.selectById(sku.getProductId()); |
| | | if (material == null) { |
| | | throw new ServiceException("物料不存在"); |
| | | } |
| | | // 校验规格是否重复 |
| | | if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), null)) { |
| | | if (existsSameSpecification(sku.getProductId(), sku.getModel(), null)) { |
| | | throw new ServiceException("该物料已存在相同规格"); |
| | | } |
| | | LocalDateTime now = LocalDateTime.now(); |
| | |
| | | if (!this.save(sku)) { |
| | | throw new ServiceException("新增物料规格失败"); |
| | | } |
| | | log.info("新增物料规格成功 materialId={}, specification={}", sku.getMaterialId(), sku.getSpecification()); |
| | | log.info("新增物料规格成功 materialId={}, specification={}", sku.getProductId(), sku.getModel()); |
| | | } |
| | | |
| | | /** |
| | |
| | | public void updateProductMaterialSku(ProductMaterialSku sku) { |
| | | validateProductMaterialSku(sku, true); |
| | | // 校验规格是否重复 |
| | | if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), sku.getId())) { |
| | | if (existsSameSpecification(sku.getProductId(), sku.getModel(), sku.getId())) { |
| | | throw new ServiceException("该物料已存在相同规格"); |
| | | } |
| | | sku.setUpdateTime(LocalDateTime.now()); |
| | |
| | | if (requireId && sku.getId() == null) { |
| | | throw new ServiceException("主键ID不能为空"); |
| | | } |
| | | if (sku.getMaterialId() == null) { |
| | | if (sku.getProductId() == null) { |
| | | throw new ServiceException("物料ID不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(sku.getSpecification())) { |
| | | if (StringUtils.isEmpty(sku.getModel())) { |
| | | throw new ServiceException("规格不能为空"); |
| | | } |
| | | } |
| | |
| | | */ |
| | | private boolean existsSameSpecification(Long materialId, String specification, Long excludeId) { |
| | | LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId) |
| | | .eq(ProductMaterialSku::getSpecification, specification); |
| | | queryWrapper.eq(ProductMaterialSku::getProductId, materialId) |
| | | .eq(ProductMaterialSku::getModel, specification); |
| | | if (excludeId != null) { |
| | | queryWrapper.ne(ProductMaterialSku::getId, excludeId); |
| | | } |
| | |
| | | |
| | | Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>(); |
| | | for (ProductMaterialSkuImportDto dto : importList) { |
| | | if (dto == null || StringUtils.isEmpty(dto.getSpecification())) { |
| | | if (dto == null || StringUtils.isEmpty(dto.getModel())) { |
| | | continue; |
| | | } |
| | | String specification = dto.getSpecification().trim(); |
| | | String specification = dto.getModel().trim(); |
| | | if (specification.isEmpty()) { |
| | | continue; |
| | | } |
| | |
| | | Set<String> specifications = specMap.keySet(); |
| | | |
| | | List<ProductMaterialSku> existList = this.list(new LambdaQueryWrapper<ProductMaterialSku>() |
| | | .eq(ProductMaterialSku::getMaterialId, materialId) |
| | | .in(ProductMaterialSku::getSpecification, specifications)); |
| | | .eq(ProductMaterialSku::getProductId, materialId) |
| | | .in(ProductMaterialSku::getModel, specifications)); |
| | | Map<String, ProductMaterialSku> existMap = existList.stream() |
| | | .collect(Collectors.toMap(ProductMaterialSku::getSpecification, sku -> sku, (a, b) -> a)); |
| | | .collect(Collectors.toMap(ProductMaterialSku::getModel, sku -> sku, (a, b) -> a)); |
| | | |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | List<ProductMaterialSku> saveList = new ArrayList<>(); |
| | |
| | | ProductMaterialSku exist = existMap.get(specification); |
| | | if (exist == null) { |
| | | ProductMaterialSku sku = new ProductMaterialSku(); |
| | | sku.setMaterialId(materialId); |
| | | sku.setSpecification(specification); |
| | | sku.setProductId(materialId); |
| | | sku.setModel(specification); |
| | | sku.setSupplyType(supplyType); |
| | | sku.setCreateTime(now); |
| | | sku.setUpdateTime(now); |