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
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 cn.iocoder.yudao.module.wms.service.md.merchant;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
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.merchant.vo.WmsMerchantListReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.merchant.vo.WmsMerchantPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.merchant.vo.WmsMerchantSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.md.merchant.WmsMerchantDO;
import cn.iocoder.yudao.module.wms.dal.mysql.md.merchant.WmsMerchantMapper;
import cn.iocoder.yudao.module.wms.enums.md.WmsMerchantTypeEnum;
import cn.iocoder.yudao.module.wms.enums.order.WmsOrderTypeEnum;
import cn.iocoder.yudao.module.wms.service.order.receipt.WmsReceiptOrderService;
import cn.iocoder.yudao.module.wms.service.order.shipment.WmsShipmentOrderService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;
 
/**
 * WMS 往来企业 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WmsMerchantServiceImpl implements WmsMerchantService {
 
    @Resource
    private WmsMerchantMapper merchantMapper;
    @Resource
    @Lazy // 延迟加载,避免循环依赖
    private WmsReceiptOrderService receiptOrderService;
    @Resource
    @Lazy // 延迟加载,避免循环依赖
    private WmsShipmentOrderService shipmentOrderService;
 
    @Override
    public Long createMerchant(WmsMerchantSaveReqVO createReqVO) {
        validateMerchantSaveData(null, createReqVO);
 
        // 新增
        WmsMerchantDO merchant = BeanUtils.toBean(createReqVO, WmsMerchantDO.class);
        merchantMapper.insert(merchant);
        return merchant.getId();
    }
 
    @Override
    public void updateMerchant(WmsMerchantSaveReqVO updateReqVO) {
        // 校验存在
        validateMerchantExists(updateReqVO.getId());
        validateMerchantSaveData(updateReqVO.getId(), updateReqVO);
 
        // 更新
        WmsMerchantDO updateObj = BeanUtils.toBean(updateReqVO, WmsMerchantDO.class);
        merchantMapper.updateById(updateObj);
    }
 
    private void validateMerchantSaveData(Long id, WmsMerchantSaveReqVO reqVO) {
        // 校验 code 唯一
        validateMerchantCodeUnique(id, reqVO.getCode());
        // 校验 name 唯一
        validateMerchantNameUnique(id, reqVO.getName());
    }
 
    private void validateMerchantCodeUnique(Long id, String code) {
        WmsMerchantDO merchant = merchantMapper.selectByCode(code);
        if (merchant == null) {
            return;
        }
        if (id == null || ObjectUtil.notEqual(merchant.getId(), id)) {
            throw exception(MERCHANT_CODE_DUPLICATE);
        }
    }
 
    private void validateMerchantNameUnique(Long id, String name) {
        WmsMerchantDO merchant = merchantMapper.selectByName(name);
        if (merchant == null) {
            return;
        }
        if (id == null || ObjectUtil.notEqual(merchant.getId(), id)) {
            throw exception(MERCHANT_NAME_DUPLICATE);
        }
    }
 
    @Override
    public void deleteMerchant(Long id) {
        // 校验存在
        validateMerchantExists(id);
        // 校验未被单据使用
        validateMerchantUnused(id);
 
        // 删除
        merchantMapper.deleteById(id);
    }
 
    private void validateMerchantUnused(Long id) {
        if (receiptOrderService.getReceiptOrderCountByMerchantId(id) > 0) {
            throw exception(MERCHANT_HAS_ORDER, WmsOrderTypeEnum.RECEIPT.getName());
        }
        if (shipmentOrderService.getShipmentOrderCountByMerchantId(id) > 0) {
            throw exception(MERCHANT_HAS_ORDER, WmsOrderTypeEnum.SHIPMENT.getName());
        }
    }
 
    @Override
    public WmsMerchantDO validateMerchantExists(Long id) {
        WmsMerchantDO merchant = merchantMapper.selectById(id);
        if (merchant == null) {
            throw exception(MERCHANT_NOT_EXISTS);
        }
        return merchant;
    }
 
    @Override
    public WmsMerchantDO validateSupplierMerchantExists(Long id) {
        WmsMerchantDO merchant = validateMerchantExists(id);
        if (ObjectUtil.notEqual(merchant.getType(), WmsMerchantTypeEnum.SUPPLIER.getType())
                && ObjectUtil.notEqual(merchant.getType(), WmsMerchantTypeEnum.CUSTOMER_SUPPLIER.getType())) {
            throw exception(MERCHANT_NOT_SUPPLIER);
        }
        return merchant;
    }
 
    @Override
    public WmsMerchantDO validateCustomerMerchantExists(Long id) {
        WmsMerchantDO merchant = validateMerchantExists(id);
        if (ObjectUtil.notEqual(merchant.getType(), WmsMerchantTypeEnum.CUSTOMER.getType())
                && ObjectUtil.notEqual(merchant.getType(), WmsMerchantTypeEnum.CUSTOMER_SUPPLIER.getType())) {
            throw exception(MERCHANT_NOT_CUSTOMER);
        }
        return merchant;
    }
 
    @Override
    public WmsMerchantDO getMerchant(Long id) {
        return merchantMapper.selectById(id);
    }
 
    @Override
    public PageResult<WmsMerchantDO> getMerchantPage(WmsMerchantPageReqVO pageReqVO) {
        return merchantMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<WmsMerchantDO> getMerchantList(WmsMerchantListReqVO listReqVO) {
        return merchantMapper.selectList(listReqVO);
    }
 
    @Override
    public List<WmsMerchantDO> getMerchantList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.of();
        }
        return merchantMapper.selectByIds(ids);
    }
 
}