gongchunyi
2 天以前 a274b897e58c958903c3e00da6c1ccb16646a979
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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;
 
/**
 * <br>
 * 物料规格接口实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/12 10:05
 */
@Slf4j
@Service
public class ProductMaterialSkuServiceImpl
        extends ServiceImpl<ProductMaterialSkuMapper, ProductMaterialSku>
        implements ProductMaterialSkuService {
 
    @Autowired
    private ProductMaterialMapper productMaterialMapper;
 
    /**
     * 查询物料规格列表
     */
    @Override
    public List<ProductMaterialSkuDto> productMaterialSkuList(Long materialId) {
 
        if (materialId == null) {
            return Collections.emptyList();
        }
 
        LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId)
                .orderByAsc(ProductMaterialSku::getId);
        List<ProductMaterialSku> 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<ProductMaterialSkuDto> 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<Long> 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<ProductMaterialSku> 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;
    }
}