package com.ruoyi.production.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.production.dto.ProductMaterialSkuDto; import com.ruoyi.production.mapper.ProductMaterialMapper; import com.ruoyi.production.mapper.ProductMaterialSkuMapper; import com.ruoyi.production.pojo.ProductMaterial; import com.ruoyi.production.pojo.ProductMaterialSku; import com.ruoyi.production.service.ProductMaterialSkuService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** *
* 物料规格接口实现类 *
* * @author deslrey * @version 1.0 * @since 2026/03/12 10:05 */ @Slf4j @Service public class ProductMaterialSkuServiceImpl extends ServiceImpl implements ProductMaterialSkuService { @Autowired private ProductMaterialMapper productMaterialMapper; /** * 查询物料规格列表 */ @Override public List productMaterialSkuList(Long materialId) { if (materialId == null) { return Collections.emptyList(); } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId) .orderByAsc(ProductMaterialSku::getId); List skuList = this.list(queryWrapper); if (skuList == null || skuList.isEmpty()) { return Collections.emptyList(); } // 查询物料信息 ProductMaterial material = productMaterialMapper.selectById(materialId); String materialName = material != null ? material.getMaterialName() : null; String baseUnit = material != null ? material.getBaseUnit() : null; List result = new ArrayList<>(skuList.size()); for (ProductMaterialSku sku : skuList) { ProductMaterialSkuDto dto = new ProductMaterialSkuDto(); dto.setMaterialId(materialId); dto.setMaterialName(materialName); dto.setBaseUnit(baseUnit); dto.setSkuId(sku.getId()); dto.setSpecification(sku.getSpecification()); dto.setSupplyType(sku.getSupplyType()); result.add(dto); } return result; } /** * 新增物料规格 */ @Override public void addProductMaterialSku(ProductMaterialSku sku) { validateProductMaterialSku(sku, false); // 校验物料是否存在 ProductMaterial material = productMaterialMapper.selectById(sku.getMaterialId()); if (material == null) { throw new ServiceException("物料不存在"); } // 校验规格是否重复 if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), null)) { throw new ServiceException("该物料已存在相同规格"); } LocalDateTime now = LocalDateTime.now(); if (sku.getCreateTime() == null) { sku.setCreateTime(now); } sku.setUpdateTime(now); if (!this.save(sku)) { throw new ServiceException("新增物料规格失败"); } log.info("新增物料规格成功 materialId={}, specification={}", sku.getMaterialId(), sku.getSpecification()); } /** * 修改物料规格 */ @Override public void updateProductMaterialSku(ProductMaterialSku sku) { validateProductMaterialSku(sku, true); // 校验规格是否重复 if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), sku.getId())) { throw new ServiceException("该物料已存在相同规格"); } sku.setUpdateTime(LocalDateTime.now()); if (!this.updateById(sku)) { throw new ServiceException("修改物料规格失败"); } log.info("修改物料规格成功 id={}", sku.getId()); } /** * 删除物料规格 */ @Override public void deleteProductMaterialSku(List ids) { if (ids == null || ids.isEmpty()) { throw new ServiceException("请选择至少一条数据"); } if (!this.removeByIds(ids)) { throw new ServiceException("删除物料规格失败"); } log.info("删除物料规格成功 ids={}", ids); } /** * 参数校验 */ private void validateProductMaterialSku(ProductMaterialSku sku, boolean requireId) { if (sku == null) { throw new ServiceException("参数不能为空"); } if (requireId && sku.getId() == null) { throw new ServiceException("主键ID不能为空"); } if (sku.getMaterialId() == null) { throw new ServiceException("物料ID不能为空"); } if (StringUtils.isEmpty(sku.getSpecification())) { throw new ServiceException("规格不能为空"); } } /** * 校验是否存在相同规格 */ private boolean existsSameSpecification(Long materialId, String specification, Long excludeId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId) .eq(ProductMaterialSku::getSpecification, specification); if (excludeId != null) { queryWrapper.ne(ProductMaterialSku::getId, excludeId); } return this.count(queryWrapper) > 0; } }