gongchunyi
17 小时以前 ef7458a6eb8ad72f45b65a4e417a9d04f5015246
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
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.ProductMaterialConfigDto;
import com.ruoyi.production.enums.MaterialConfigTypeEnum;
import com.ruoyi.production.mapper.ProductMaterialConfigMapper;
import com.ruoyi.production.pojo.ProductMaterialConfig;
import com.ruoyi.production.service.ProductMaterialConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * <br>
 * 物料信息表配置接口实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/11 16:59
 */
@Slf4j
@Service
public class ProductMaterialConfigServiceImpl extends ServiceImpl<ProductMaterialConfigMapper, ProductMaterialConfig> implements ProductMaterialConfigService {
 
    @Override
    public List<ProductMaterialConfig> materialTypeList() {
        return getByType(MaterialConfigTypeEnum.MATERIAL_TYPE);
    }
 
    @Override
    public List<ProductMaterialConfig> inventoryCategoryList() {
        return getByType(MaterialConfigTypeEnum.INVENTORY_CAT);
    }
 
    private List<ProductMaterialConfig> getByType(MaterialConfigTypeEnum type) {
        return list(new LambdaQueryWrapper<ProductMaterialConfig>().eq(ProductMaterialConfig::getConfigType, type.name()));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProductMaterialConfig(ProductMaterialConfigDto config) {
        config.setConfigType(MaterialConfigTypeEnum.getConfigType(config.getType()));
        validateConfig(config, false);
        if (existsConfig(config.getConfigType(), config.getConfigName(), null)) {
            throw new ServiceException("配置名称已存在");
        }
        if (!this.save(config)) {
            throw new ServiceException("新增配置失败");
        }
        log.info("新增物料配置成功 type={}, name={}", config.getConfigType(), config.getConfigName());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProductMaterialConfig(ProductMaterialConfigDto config) {
        config.setConfigType(MaterialConfigTypeEnum.getConfigType(config.getType()));
        validateConfig(config, true);
        ProductMaterialConfig exist = this.getById(config.getId());
        if (exist == null) {
            throw new ServiceException("配置不存在");
        }
        if (existsConfig(config.getConfigType(), config.getConfigName(), config.getId())) {
            throw new ServiceException("配置名称已存在");
        }
        if (!this.updateById(config)) {
            throw new ServiceException("修改配置失败");
        }
        log.info("修改物料配置成功 id={}", config.getId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProductMaterialConfig(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择至少一条数据");
        }
        if (!this.removeByIds(ids)) {
            throw new ServiceException("删除配置失败");
        }
        log.info("删除物料配置成功 ids={}", ids);
    }
 
    private void validateConfig(ProductMaterialConfig config, boolean requireId) {
        if (config == null) {
            throw new ServiceException("参数不能为空");
        }
        if (requireId && config.getId() == null) {
            throw new ServiceException("主键ID不能为空");
        }
        if (StringUtils.isEmpty(config.getConfigType())) {
            throw new ServiceException("配置类型不能为空");
        }
        try {
            MaterialConfigTypeEnum.valueOf(config.getConfigType());
        } catch (IllegalArgumentException e) {
            throw new ServiceException("配置类型不合法");
        }
        if (StringUtils.isEmpty(config.getConfigName())) {
            throw new ServiceException("配置名称不能为空");
        }
    }
 
    private boolean existsConfig(String configType, String configName, Integer excludeId) {
        if (StringUtils.isEmpty(configName) || StringUtils.isEmpty(configType)) {
            return false;
        }
        LambdaQueryWrapper<ProductMaterialConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductMaterialConfig::getConfigType, configType).eq(ProductMaterialConfig::getConfigName, configName);
        if (excludeId != null) {
            wrapper.ne(ProductMaterialConfig::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }
}