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 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)); } } } }