gaoluyang
昨天 8c65f898ed6e5b0c7a94c792c4562f6ec831dccd
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
package cn.iocoder.yudao.module.crm.service.customer;
 
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.address.CrmCustomerAddressSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerAddressDO;
import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerAddressMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_ADDRESS_NOT_EXISTS;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_NOT_EXISTS;
 
/**
 * CRM 客户收货地址 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CrmCustomerAddressServiceImpl implements CrmCustomerAddressService {
 
    @Resource
    private CrmCustomerAddressMapper customerAddressMapper;
    @Resource
    private CrmCustomerService customerService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCustomerAddress(CrmCustomerAddressSaveReqVO reqVO) {
        // 1. 校验客户存在
        validateCustomerExists(reqVO.getCustomerId());
        // 2. 默认地址只保留一个
        clearDefaultIfNeeded(reqVO.getCustomerId(), reqVO.getDefaultFlag(), null);
        // 3. 插入
        CrmCustomerAddressDO address = BeanUtils.toBean(reqVO, CrmCustomerAddressDO.class);
        address.setDefaultFlag(Boolean.TRUE.equals(reqVO.getDefaultFlag()));
        customerAddressMapper.insert(address);
        return address.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomerAddress(CrmCustomerAddressSaveReqVO reqVO) {
        // 1. 校验存在
        CrmCustomerAddressDO oldAddress = validateAddressExists(reqVO.getId());
        // 2. 校验客户存在(不允许修改所属客户)
        validateCustomerExists(oldAddress.getCustomerId());
        // 3. 默认地址只保留一个
        clearDefaultIfNeeded(oldAddress.getCustomerId(), reqVO.getDefaultFlag(), reqVO.getId());
        // 4. 更新(客户编号以库中为准,防止跨客户修改)
        CrmCustomerAddressDO updateObj = BeanUtils.toBean(reqVO, CrmCustomerAddressDO.class);
        updateObj.setCustomerId(oldAddress.getCustomerId());
        updateObj.setDefaultFlag(Boolean.TRUE.equals(reqVO.getDefaultFlag()));
        customerAddressMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteCustomerAddress(Long id) {
        validateAddressExists(id);
        customerAddressMapper.deleteById(id);
    }
 
    @Override
    public List<CrmCustomerAddressDO> getCustomerAddressList(Long customerId) {
        return customerAddressMapper.selectListByCustomerId(customerId);
    }
 
    @Override
    public CrmCustomerAddressDO getDefaultCustomerAddress(Long customerId) {
        return customerAddressMapper.selectDefaultByCustomerId(customerId);
    }
 
    private CrmCustomerAddressDO validateAddressExists(Long id) {
        CrmCustomerAddressDO address = id == null ? null : customerAddressMapper.selectById(id);
        if (address == null) {
            throw exception(CUSTOMER_ADDRESS_NOT_EXISTS);
        }
        return address;
    }
 
    private void validateCustomerExists(Long customerId) {
        if (customerService.getCustomer(customerId) == null) {
            throw exception(CUSTOMER_NOT_EXISTS);
        }
    }
 
    /**
     * 设置为默认地址时,清除同一客户的其他默认地址
     */
    private void clearDefaultIfNeeded(Long customerId, Boolean defaultFlag, Long excludeId) {
        if (!Boolean.TRUE.equals(defaultFlag)) {
            return;
        }
        for (CrmCustomerAddressDO address : customerAddressMapper.selectListByCustomerId(customerId)) {
            if (Boolean.TRUE.equals(address.getDefaultFlag()) && !address.getId().equals(excludeId)) {
                customerAddressMapper.updateById(new CrmCustomerAddressDO()
                        .setId(address.getId()).setDefaultFlag(false));
            }
        }
    }
 
}