|
|
@@ -1,27 +1,38 @@
|
|
|
package org.dromara.customer.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.dromara.common.core.enums.IsDefault;
|
|
|
+import org.dromara.common.core.enums.SysPlatformYesNo;
|
|
|
import org.dromara.common.core.utils.MapstructUtils;
|
|
|
import org.dromara.common.core.utils.StringUtils;
|
|
|
-import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.dromara.system.api.RemoteDeptService;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.redis.utils.SequenceUtils;
|
|
|
+import org.dromara.customer.domain.CustomerContact;
|
|
|
+import org.dromara.customer.domain.CustomerShippingAddress;
|
|
|
import org.dromara.customer.domain.bo.CustomerContactBo;
|
|
|
+import org.dromara.customer.domain.bo.CustomerShippingAddressBo;
|
|
|
import org.dromara.customer.domain.vo.CustomerContactVo;
|
|
|
-import org.dromara.customer.domain.CustomerContact;
|
|
|
+import org.dromara.customer.domain.vo.CustomerInfoVo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerShippingAddressVo;
|
|
|
import org.dromara.customer.mapper.CustomerContactMapper;
|
|
|
+import org.dromara.customer.mapper.CustomerInfoMapper;
|
|
|
import org.dromara.customer.service.ICustomerContactService;
|
|
|
+import org.dromara.system.api.RemoteDeptService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Collection;
|
|
|
|
|
|
/**
|
|
|
* 客户联系人信息Service业务层处理
|
|
|
@@ -34,11 +45,14 @@ import java.util.Collection;
|
|
|
@Service
|
|
|
public class CustomerContactServiceImpl extends ServiceImpl<CustomerContactMapper, CustomerContact> implements ICustomerContactService {
|
|
|
|
|
|
+ private static final String CONTACT_NO_KEY = "customer_contact:contact_no";
|
|
|
@DubboReference
|
|
|
private RemoteDeptService remoteDeptService;
|
|
|
|
|
|
private final CustomerContactMapper baseMapper;
|
|
|
|
|
|
+ private final CustomerInfoMapper customerInfoMapper;
|
|
|
+
|
|
|
/**
|
|
|
* 查询客户联系人信息
|
|
|
*
|
|
|
@@ -116,7 +130,32 @@ public class CustomerContactServiceImpl extends ServiceImpl<CustomerContactMappe
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean insertByBo(CustomerContactBo bo) {
|
|
|
+ // 1. 获取并校验客户编号
|
|
|
+ String customerNo = null;
|
|
|
+ if (bo.getCustomerId() != null) {
|
|
|
+ CustomerInfoVo customerInfoVo = customerInfoMapper.selectVoById(bo.getCustomerId());
|
|
|
+
|
|
|
+ // 【关键修复】防御性编程:如果查不到客户信息,应直接抛错或返回失败,避免生成 "nullxxx" 数据
|
|
|
+ if (customerInfoVo == null) {
|
|
|
+ log.error("客户不存在,无法创建联系人 (ID: " + bo.getCustomerId() + ")");
|
|
|
+ throw new RuntimeException("客户不存在,无法创建联系人 (ID: " + bo.getCustomerId() + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ customerNo = customerInfoVo.getCustomerNo();
|
|
|
+
|
|
|
+ // 二次校验:防止 customerNo 本身为空
|
|
|
+ if (StringUtils.isBlank(customerNo)) {
|
|
|
+ log.error("客户编号为空,数据异常 (ID: " + bo.getCustomerId() + ")");
|
|
|
+ throw new RuntimeException("客户编号为空,数据异常 (ID: " + bo.getCustomerId() + ")");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 生成联系人编号
|
|
|
+ String seqId = SequenceUtils.nextPaddedIdStr(CONTACT_NO_KEY, Duration.ofDays(3650), 3);
|
|
|
+ String contactNo = customerNo + seqId;
|
|
|
+
|
|
|
CustomerContact add = MapstructUtils.convert(bo, CustomerContact.class);
|
|
|
+ add.setContactNo(contactNo);
|
|
|
validEntityBeforeSave(add);
|
|
|
boolean flag = baseMapper.insert(add) > 0;
|
|
|
if (flag) {
|
|
|
@@ -138,6 +177,42 @@ public class CustomerContactServiceImpl extends ServiceImpl<CustomerContactMappe
|
|
|
return baseMapper.updateById(update) > 0;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int changeIsPrimary(CustomerContactBo bo) {
|
|
|
+ if (bo == null || bo.getId() == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ CustomerContactVo contactVo = baseMapper.selectVoById(bo.getId());
|
|
|
+ if (SysPlatformYesNo.YES.getCode().equals(bo.getIsPrimary())) {
|
|
|
+ // 1. 清除该客户下所有主联系人
|
|
|
+ CustomerContact updateObj = new CustomerContact();
|
|
|
+ updateObj.setIsPrimary(SysPlatformYesNo.NO.getCode()); //
|
|
|
+
|
|
|
+ baseMapper.update(
|
|
|
+ updateObj,
|
|
|
+ new LambdaUpdateWrapper<CustomerContact>()
|
|
|
+ .eq(CustomerContact::getCustomerId, contactVo.getCustomerId()) //
|
|
|
+ .eq(CustomerContact::getIsPrimary, SysPlatformYesNo.YES.getCode())
|
|
|
+ );
|
|
|
+
|
|
|
+ // 2. 设置当前地址为默认
|
|
|
+ CustomerContact current = new CustomerContact();
|
|
|
+ current.setId(bo.getId());
|
|
|
+ current.setIsPrimary(SysPlatformYesNo.YES.getCode());
|
|
|
+ baseMapper.updateById(current);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ } else {
|
|
|
+ // 取消默认
|
|
|
+ CustomerContact current = new CustomerContact();
|
|
|
+ current.setId(bo.getId());
|
|
|
+ current.setIsPrimary(SysPlatformYesNo.NO.getCode());
|
|
|
+ baseMapper.updateById(current);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保存前的数据校验
|
|
|
*/
|