2026-07-01 6b5f7c66fc40d7f6099d561e31a34fbd50dd20d3
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
package cn.iocoder.yudao.module.wms.service.md.item;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.brand.WmsItemBrandPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.brand.WmsItemBrandSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.md.item.WmsItemBrandDO;
import cn.iocoder.yudao.module.wms.dal.mysql.md.item.WmsItemBrandMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.ITEM_BRAND_CODE_DUPLICATE;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.ITEM_BRAND_HAS_ITEM;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.ITEM_BRAND_NAME_DUPLICATE;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.ITEM_BRAND_NOT_EXISTS;
 
/**
 * WMS 商品品牌 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WmsItemBrandServiceImpl implements WmsItemBrandService {
 
    @Resource
    private WmsItemBrandMapper brandMapper;
    @Resource
    private WmsItemService itemService;
 
    @Override
    public Long createItemBrand(WmsItemBrandSaveReqVO createReqVO) {
        validateBrandSaveData(null, createReqVO);
 
        // 新增
        WmsItemBrandDO brand = BeanUtils.toBean(createReqVO, WmsItemBrandDO.class);
        brandMapper.insert(brand);
        return brand.getId();
    }
 
    @Override
    public void updateItemBrand(WmsItemBrandSaveReqVO updateReqVO) {
        // 校验存在
        validateItemBrandExists(updateReqVO.getId());
        validateBrandSaveData(updateReqVO.getId(), updateReqVO);
 
        // 更新
        WmsItemBrandDO updateObj = BeanUtils.toBean(updateReqVO, WmsItemBrandDO.class);
        brandMapper.updateById(updateObj);
    }
 
    private void validateBrandSaveData(Long id, WmsItemBrandSaveReqVO reqVO) {
        validateBrandCodeUnique(id, reqVO.getCode());
        validateBrandNameUnique(id, reqVO.getName());
    }
 
    private void validateBrandCodeUnique(Long id, String code) {
        WmsItemBrandDO brand = brandMapper.selectByCode(code);
        if (brand == null) {
            return;
        }
        // 如果 id 为空,说明新增;和数据库已有 code 重复
        if (id == null) {
            throw exception(ITEM_BRAND_CODE_DUPLICATE);
        }
        if (ObjectUtil.notEqual(brand.getId(), id)) {
            throw exception(ITEM_BRAND_CODE_DUPLICATE);
        }
    }
 
    private void validateBrandNameUnique(Long id, String name) {
        WmsItemBrandDO brand = brandMapper.selectByName(name);
        if (brand == null) {
            return;
        }
        if (id == null) {
            throw exception(ITEM_BRAND_NAME_DUPLICATE);
        }
        if (ObjectUtil.notEqual(brand.getId(), id)) {
            throw exception(ITEM_BRAND_NAME_DUPLICATE);
        }
    }
 
    @Override
    public void deleteItemBrand(Long id) {
        // 校验存在
        validateItemBrandExists(id);
        // 校验品牌下不存在商品
        if (itemService.getItemCountByBrandId(id) > 0) {
            throw exception(ITEM_BRAND_HAS_ITEM);
        }
 
        // 删除
        brandMapper.deleteById(id);
    }
 
    @Override
    public WmsItemBrandDO validateItemBrandExists(Long id) {
        WmsItemBrandDO brand = brandMapper.selectById(id);
        if (brand == null) {
            throw exception(ITEM_BRAND_NOT_EXISTS);
        }
        return brand;
    }
 
    @Override
    public WmsItemBrandDO getItemBrand(Long id) {
        return brandMapper.selectById(id);
    }
 
    @Override
    public PageResult<WmsItemBrandDO> getItemBrandPage(WmsItemBrandPageReqVO pageReqVO) {
        return brandMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<WmsItemBrandDO> getItemBrandList() {
        return brandMapper.selectList();
    }
 
    @Override
    public List<WmsItemBrandDO> getItemBrandList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return brandMapper.selectByIds(ids);
    }
 
}