2026-06-29 940c8481d2fdcf51341db4ccd6fd6fcc2d43debb
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
package cn.iocoder.yudao.module.mdm.service.brand;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.controller.admin.brand.vo.MdmBrandPageReqVO;
import cn.iocoder.yudao.module.mdm.controller.admin.brand.vo.MdmBrandSaveReqVO;
import cn.iocoder.yudao.module.mdm.dal.dataobject.brand.MdmBrandDO;
import cn.iocoder.yudao.module.mdm.dal.mysql.brand.MdmBrandMapper;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.*;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*;
 
/**
 * MDM 品牌 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MdmBrandServiceImpl implements MdmBrandService {
 
    @Resource
    private MdmBrandMapper brandMapper;
 
    @Resource
    private MdmItemMapper itemMapper;
 
    @Override
    public Long createBrand(MdmBrandSaveReqVO createReqVO) {
        // 校验数据
        validateBrandSaveData(createReqVO);
 
        // 插入
        MdmBrandDO brand = BeanUtils.toBean(createReqVO, MdmBrandDO.class);
        brandMapper.insert(brand);
        return brand.getId();
    }
 
    @Override
    public void updateBrand(MdmBrandSaveReqVO updateReqVO) {
        // 校验存在
        validateBrandExists(updateReqVO.getId());
        // 校验数据
        validateBrandSaveData(updateReqVO);
 
        // 更新
        MdmBrandDO updateObj = BeanUtils.toBean(updateReqVO, MdmBrandDO.class);
        brandMapper.updateById(updateObj);
    }
 
    @Override
    public void updateBrandStatus(Long id, Integer status) {
        // 校验存在
        validateBrandExists(id);
        // 更新状态
        brandMapper.updateById(new MdmBrandDO().setId(id).setStatus(status));
    }
 
    @Override
    public void deleteBrand(Long id) {
        // 校验存在
        validateBrandExists(id);
        // 校验是否被物料使用(直接使用 Mapper 避免循环依赖)
        if (itemMapper.selectCountByBrandId(id) > 0) {
            throw exception(MDM_BRAND_EXISTS_ITEM);
        }
        // 删除
        brandMapper.deleteById(id);
    }
 
    @Override
    public MdmBrandDO validateBrandExists(Long id) {
        MdmBrandDO brand = brandMapper.selectById(id);
        if (brand == null) {
            throw exception(MDM_BRAND_NOT_EXISTS);
        }
        return brand;
    }
 
    private void validateBrandSaveData(MdmBrandSaveReqVO reqVO) {
        // 校验编码唯一
        MdmBrandDO brand = brandMapper.selectByCode(reqVO.getCode());
        if (brand != null && ObjUtil.notEqual(brand.getId(), reqVO.getId())) {
            throw exception(MDM_BRAND_CODE_DUPLICATE);
        }
        // 校验名称唯一
        MdmBrandDO sameName = brandMapper.selectByName(reqVO.getName());
        if (sameName != null && ObjUtil.notEqual(sameName.getId(), reqVO.getId())) {
            throw exception(MDM_BRAND_NAME_DUPLICATE);
        }
    }
 
    @Override
    public MdmBrandDO getBrand(Long id) {
        return brandMapper.selectById(id);
    }
 
    @Override
    public PageResult<MdmBrandDO> getBrandPage(MdmBrandPageReqVO pageReqVO) {
        return brandMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MdmBrandDO> getBrandList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return brandMapper.selectBatchIds(ids);
    }
 
    @Override
    public MdmBrandDO getBrandByCode(String code) {
        return brandMapper.selectByCode(code);
    }
 
}