|
|
@@ -31,10 +31,12 @@ import com.fs.hisStore.vo.FsStoreOrderItemVO;
|
|
|
import com.fs.hisStore.vo.FsStoreOrderVO;
|
|
|
import com.fs.qw.domain.FsCompanyCustomer;
|
|
|
import com.fs.qw.domain.FsCompanyCustomerLog;
|
|
|
+import com.fs.qw.domain.FsCompanyQrcPhone;
|
|
|
import com.fs.qw.dto.ImportCustomerDTO;
|
|
|
import com.fs.qw.mapper.FsCompanyCustomerLogMapper;
|
|
|
import com.fs.qw.mapper.FsCompanyCustomerMapper;
|
|
|
import com.fs.qw.mapper.FsCompanyExternalPayReceiptMapper;
|
|
|
+import com.fs.qw.mapper.FsCompanyQrcPhoneMapper;
|
|
|
import com.fs.qw.param.TransferCustomerParam;
|
|
|
import com.fs.qw.service.IFsCompanyCustomerService;
|
|
|
import com.fs.qw.vo.ImportResult;
|
|
|
@@ -52,6 +54,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
|
* 客户基础信息Service业务层处理
|
|
|
@@ -92,6 +95,9 @@ public class FsCompanyCustomerServiceImpl implements IFsCompanyCustomerService {
|
|
|
@Autowired
|
|
|
private CompanyUserMapper companyUserMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private FsCompanyQrcPhoneMapper fsCompanyQrcPhoneMapper;
|
|
|
+
|
|
|
private static final Set<Integer> ALLOWED_STATUSES =
|
|
|
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(0, 5, 6))); // 允许操作完善客户信息的状态
|
|
|
|
|
|
@@ -257,60 +263,30 @@ public class FsCompanyCustomerServiceImpl implements IFsCompanyCustomerService {
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public int claimCustomer(FsCompanyCustomer fsCompanyCustomer, CompanyUser companyUser) {
|
|
|
- // 使用行锁查询客户信息(阻塞直到获得锁)
|
|
|
+ // 行锁查询
|
|
|
FsCompanyCustomer oldCustomer = fsCompanyCustomerMapper.selectByIdForUpdate(fsCompanyCustomer.getId());
|
|
|
- //校验认领号码与客户号码是否一致(优先匹配完整号码,其次匹配号码后4位,满足一个即可)
|
|
|
- if (!fsCompanyCustomer.getClaimPhone().equals(oldCustomer.getPhone())){
|
|
|
- String oldFourNumber = oldCustomer.getPhone().substring(7);
|
|
|
- String newFourNumber = fsCompanyCustomer.getClaimPhone().substring(fsCompanyCustomer.getClaimPhone().length()-4);
|
|
|
- if (!oldFourNumber.equals(newFourNumber)){
|
|
|
- log.error("认领号码:{},客户号码:{}",fsCompanyCustomer.getClaimPhone(),oldCustomer.getPhone());
|
|
|
- throw new CustomException("认领号码与客户号码不一致,请重试");
|
|
|
+ if (oldCustomer == null) {
|
|
|
+ throw new CustomException("客户不存在");
|
|
|
+ }
|
|
|
+ // 校验电话号码(完整匹配或后四位)
|
|
|
+ String claimPhone = fsCompanyCustomer.getClaimPhone();
|
|
|
+ if (!claimPhone.equals(oldCustomer.getPhone())) {
|
|
|
+ String oldLast4 = oldCustomer.getPhone().substring(7); // 11位手机号后4位
|
|
|
+ String newLast4 = claimPhone.substring(claimPhone.length() - 4);
|
|
|
+ if (!oldLast4.equals(newLast4)) {
|
|
|
+ log.error("认领号码:{},客户号码:{}", claimPhone, oldCustomer.getPhone());
|
|
|
+ throw new CustomException("认领号码与客户号码不一致,请重试");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //校验加微状态
|
|
|
- if (fsCompanyCustomer.getKdzlAddWechatStatus()==null){
|
|
|
- throw new CustomException("未选择加微,请选择");
|
|
|
+ // 校验加微状态
|
|
|
+ if (fsCompanyCustomer.getKdzlAddWechatStatus() == null) {
|
|
|
+ throw new CustomException("未选择加微,请选择");
|
|
|
}
|
|
|
- FsImportMember fsImportMember=new FsImportMember();
|
|
|
- fsImportMember.setId(oldCustomer.getImportMemberId());
|
|
|
- fsImportMember.setCompanyId(companyUser.getCompanyId());
|
|
|
- fsImportMember.setCompanyUserId(companyUser.getUserId());
|
|
|
- //更新关联的导入会员表信息
|
|
|
- importMemberMapper.updateFsImportMember(fsImportMember);
|
|
|
|
|
|
- FsCompanyCustomerLog claimLog = new FsCompanyCustomerLog();
|
|
|
- claimLog.setCompanyCustomerId(fsCompanyCustomer.getId());
|
|
|
- claimLog.setCompanyUserId(companyUser.getUserId());
|
|
|
- claimLog.setCompanyUserName(companyUser.getNickName());
|
|
|
- claimLog.setDeptId(companyUser.getDeptId());
|
|
|
- claimLog.setClaimTime(DateUtils.getNowDate());
|
|
|
- CompanyDept companyDept = companyDeptMapper.selectCompanyDeptById(companyUser.getDeptId());
|
|
|
- if (companyDept!=null&&companyDept.getDeptName()!=null){
|
|
|
- claimLog.setDeptName(companyDept.getDeptName());
|
|
|
- fsCompanyCustomer.setDeptName(companyDept.getDeptName());
|
|
|
- }
|
|
|
- //增加认领记录
|
|
|
- companyCustomerLogMapper.insertFsCompanyCustomerLog(claimLog);
|
|
|
- //认领的同时修改销售相关信息
|
|
|
- fsCompanyCustomer.setDeptId(companyUser.getDeptId());
|
|
|
- fsCompanyCustomer.setCompanyUserId(companyUser.getUserId());
|
|
|
- fsCompanyCustomer.setCompanyUserName(companyUser.getNickName());
|
|
|
- fsCompanyCustomer.setClaimStatus(BigDecimal.ONE.intValue());//已认领
|
|
|
- //获取销售绑定医生信息
|
|
|
- companyUser = companyUserMapper.selectCompanyUserById(companyUser.getUserId());
|
|
|
- if (companyUser==null||companyUser.getDoctorId()==null){
|
|
|
- throw new CustomException("销售未绑定医生");
|
|
|
- }
|
|
|
- //查询医生信息
|
|
|
- FsDoctor fsDoctor = fsDoctorMapper.selectFsDoctorByDoctorId(companyUser.getDoctorId());
|
|
|
- fsCompanyCustomer.setDoctorId(fsDoctor.getDoctorId());
|
|
|
- fsCompanyCustomer.setDoctorName(fsDoctor.getDoctorName());
|
|
|
- if (fsDoctor.getStatus()==0){
|
|
|
- throw new CustomException("医生已停用,请更换医生!");
|
|
|
- }
|
|
|
- return fsCompanyCustomerMapper.updateFsCompanyCustomer(fsCompanyCustomer);
|
|
|
+ // 执行认领逻辑
|
|
|
+ performClaim(oldCustomer, companyUser, fsCompanyCustomer.getKdzlAddWechatStatus());
|
|
|
+ return 1; // 成功返回1
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -535,4 +511,114 @@ public class FsCompanyCustomerServiceImpl implements IFsCompanyCustomerService {
|
|
|
return R.error("微信接口调用失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int canClaimCustomer(Long id, CompanyUser user) {
|
|
|
+ FsCompanyCustomer oldCustomer = fsCompanyCustomerMapper.selectByIdForUpdate(id);
|
|
|
+ if (oldCustomer == null) {
|
|
|
+ throw new CustomException("客户不存在");
|
|
|
+ }
|
|
|
+ // 查询中间表,判断是否可快捷认领
|
|
|
+ FsCompanyQrcPhone query = new FsCompanyQrcPhone();
|
|
|
+ query.setUserPhone(oldCustomer.getPhone());
|
|
|
+ query.setCompanyUserId(user.getUserId());
|
|
|
+ List<FsCompanyQrcPhone> list = fsCompanyQrcPhoneMapper.selectList(query);
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return 0; // 不能快捷认领
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int quickClaimAndImprove(FsCompanyCustomer customer, CompanyUser user) {
|
|
|
+ // 1. 行锁查询客户
|
|
|
+ FsCompanyCustomer oldCustomer = fsCompanyCustomerMapper.selectByIdForUpdate(customer.getId());
|
|
|
+ if (oldCustomer == null) {
|
|
|
+ throw new CustomException("客户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 校验四个字段至少有一个非空
|
|
|
+ String symptom = customer.getPatientMainComplaint();
|
|
|
+ String presentIllness = customer.getPresentIllness();
|
|
|
+ String currentMedication = customer.getCurrentMedication();
|
|
|
+ String allergyHistory = customer.getAllergyHistory();
|
|
|
+
|
|
|
+ boolean anyNotEmpty = Stream.of(symptom, presentIllness, currentMedication, allergyHistory)
|
|
|
+ .anyMatch(s -> s != null && !s.trim().isEmpty());
|
|
|
+ if (!anyNotEmpty) {
|
|
|
+ throw new CustomException("病情主诉、现病史、现用药情况、过敏史至少填写一项");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 将前端传入的所有字段复制到 oldCustomer(除了 id)
|
|
|
+ BeanUtils.copyProperties(customer, oldCustomer, "id");
|
|
|
+
|
|
|
+ // 4. 更新完善状态
|
|
|
+ setCompleteStatus(oldCustomer);
|
|
|
+
|
|
|
+ // 5. 执行认领核心逻辑(加微状态默认=1)
|
|
|
+ performClaim(oldCustomer, user, 1);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行认领核心逻辑(需在事务内调用)
|
|
|
+ * @param customer 行锁查询出的客户对象(将被修改并更新)
|
|
|
+ * @param companyUser 当前操作销售
|
|
|
+ * @param addWechatStatus 加微状态(1-已加,0-未加)
|
|
|
+ */
|
|
|
+ private void performClaim(FsCompanyCustomer customer, CompanyUser companyUser, Integer addWechatStatus) {
|
|
|
+ if (companyUser == null) {
|
|
|
+ throw new CustomException("登录信息已过期,请重新登录");
|
|
|
+ }
|
|
|
+ Long companyId = companyUser.getCompanyId();
|
|
|
+ Long userId = companyUser.getUserId();
|
|
|
+ Long deptId = companyUser.getDeptId();
|
|
|
+ String nickName = companyUser.getNickName();
|
|
|
+
|
|
|
+ // 1. 更新导入会员
|
|
|
+ if (customer.getImportMemberId() != null) {
|
|
|
+ FsImportMember updateMember = new FsImportMember();
|
|
|
+ updateMember.setId(customer.getImportMemberId());
|
|
|
+ updateMember.setCompanyId(companyId);
|
|
|
+ updateMember.setCompanyUserId(userId);
|
|
|
+ importMemberMapper.updateFsImportMember(updateMember);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 创建认领日志
|
|
|
+ FsCompanyCustomerLog claimLog = new FsCompanyCustomerLog();
|
|
|
+ claimLog.setCompanyCustomerId(customer.getId());
|
|
|
+ claimLog.setCompanyUserId(userId);
|
|
|
+ claimLog.setCompanyUserName(nickName);
|
|
|
+ claimLog.setDeptId(deptId);
|
|
|
+ claimLog.setClaimTime(DateUtils.getNowDate());
|
|
|
+ CompanyDept companyDept = companyDeptMapper.selectCompanyDeptById(deptId);
|
|
|
+ if (companyDept != null && companyDept.getDeptName() != null) {
|
|
|
+ claimLog.setDeptName(companyDept.getDeptName());
|
|
|
+ customer.setDeptName(companyDept.getDeptName());
|
|
|
+ }
|
|
|
+ companyCustomerLogMapper.insertFsCompanyCustomerLog(claimLog);
|
|
|
+
|
|
|
+ // 3. 填充客户认领信息
|
|
|
+ customer.setDeptId(deptId);
|
|
|
+ customer.setCompanyUserId(userId);
|
|
|
+ customer.setCompanyUserName(nickName);
|
|
|
+ customer.setClaimStatus(1); // 已认领
|
|
|
+ customer.setKdzlAddWechatStatus(addWechatStatus);
|
|
|
+
|
|
|
+ // 4. 获取销售绑定的医生信息
|
|
|
+ if (companyUser.getDoctorId() == null) {
|
|
|
+ throw new CustomException("销售未绑定医生,请绑定后再操作!");
|
|
|
+ }
|
|
|
+ FsDoctor fsDoctor = fsDoctorMapper.selectFsDoctorByDoctorId(companyUser.getDoctorId());
|
|
|
+ if (fsDoctor.getStatus() == 0) {
|
|
|
+ throw new CustomException("医生已停用,请更换医生!");
|
|
|
+ }
|
|
|
+ customer.setDoctorId(fsDoctor.getDoctorId());
|
|
|
+ customer.setDoctorName(fsDoctor.getDoctorName());
|
|
|
+
|
|
|
+ // 5. 更新客户
|
|
|
+ fsCompanyCustomerMapper.updateFsCompanyCustomer(customer);
|
|
|
+ }
|
|
|
}
|