|
|
@@ -0,0 +1,247 @@
|
|
|
+package org.dromara.customer.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.dromara.common.core.context.PlatformContext;
|
|
|
+import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
+import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
+import org.dromara.customer.domain.CustomerInfo;
|
|
|
+import org.dromara.customer.domain.CrmStaff;
|
|
|
+import org.dromara.customer.domain.TeamMember;
|
|
|
+import org.dromara.customer.domain.bo.CustomerClaimBo;
|
|
|
+import org.dromara.customer.domain.bo.CustomerInfoBo;
|
|
|
+import org.dromara.customer.domain.bo.CustomerListBo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerInfoVo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerListVo;
|
|
|
+import org.dromara.customer.domain.vo.TeamMemberVo;
|
|
|
+import org.dromara.customer.mapper.CustomerPoolMapper;
|
|
|
+import org.dromara.customer.mapper.TeamMemberMapper;
|
|
|
+import org.dromara.customer.mapper.CrmStaffMapper;
|
|
|
+import org.dromara.customer.service.ICustomerPoolService;
|
|
|
+import org.dromara.customer.service.ICustomerInfoService;
|
|
|
+import org.dromara.system.api.*;
|
|
|
+import org.dromara.system.api.domain.vo.RemoteDictDataVo;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 客户公海与有效客户Service业务层处理
|
|
|
+ *
|
|
|
+ * @author Antigravity
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Service
|
|
|
+public class CustomerPoolServiceImpl implements ICustomerPoolService {
|
|
|
+
|
|
|
+ @DubboReference
|
|
|
+ private RemoteComCompanyService remoteComCompanyService;
|
|
|
+ @DubboReference
|
|
|
+ private RemoteComCustomerLevelService remoteComCustomerLevelService;
|
|
|
+ @DubboReference
|
|
|
+ private RemoteComStaffService remoteComStaffService;
|
|
|
+ @DubboReference
|
|
|
+ private RemoteDeptService remoteDeptService;
|
|
|
+ @DubboReference
|
|
|
+ private RemoteCreditLevelService remoteCreditLevelService;
|
|
|
+ @DubboReference
|
|
|
+ private RemoteDictService remoteDictService;
|
|
|
+
|
|
|
+ private final CustomerPoolMapper customerPoolMapper;
|
|
|
+ private final TeamMemberMapper teamMemberMapper;
|
|
|
+ private final CrmStaffMapper crmStaffMapper;
|
|
|
+ private final ICustomerInfoService customerInfoService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<CustomerListVo> queryCustomerListPage(CustomerListBo bo, PageQuery pageQuery) {
|
|
|
+ // 强行填充默认值
|
|
|
+ if (StringUtils.isBlank(bo.getIsHighSeas())) {
|
|
|
+ Object isHighSeasObj = bo.getParams().get("isHighSeas");
|
|
|
+ bo.setIsHighSeas(isHighSeasObj != null ? String.valueOf(isHighSeasObj) : "false");
|
|
|
+ }
|
|
|
+
|
|
|
+ Page<CustomerListVo> page = customerPoolMapper.selectCustomerListPage(pageQuery.build(), bo);
|
|
|
+ List<CustomerListVo> records = page.getRecords();
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(records)) {
|
|
|
+ Set<Long> staffIds = new HashSet<>();
|
|
|
+ Set<Long> creditLevelIds = new HashSet<>();
|
|
|
+ Set<Long> companyIds = new HashSet<>();
|
|
|
+ Set<Long> customerLevelIds = new HashSet<>();
|
|
|
+ Set<Long> deptIds = new HashSet<>();
|
|
|
+
|
|
|
+ records.forEach(vo -> {
|
|
|
+ if (!"true".equals(bo.getIsHighSeas())) {
|
|
|
+ if (vo.getSalesPersonId() != null) staffIds.add(vo.getSalesPersonId());
|
|
|
+ if (vo.getServiceStaffId() != null) staffIds.add(vo.getServiceStaffId());
|
|
|
+ if (vo.getCreditLevelId() != null) creditLevelIds.add(vo.getCreditLevelId());
|
|
|
+ }
|
|
|
+ if (vo.getBelongCompanyId() != null) companyIds.add(vo.getBelongCompanyId());
|
|
|
+ if (vo.getCustomerLevelId() != null) customerLevelIds.add(vo.getCustomerLevelId());
|
|
|
+ if (vo.getBelongingDepartmentId() != null) deptIds.add(vo.getBelongingDepartmentId());
|
|
|
+ });
|
|
|
+
|
|
|
+ Map<Long, String> staffMap = staffIds.isEmpty() ? Collections.emptyMap() : remoteComStaffService.selectStaffNameByIds(staffIds);
|
|
|
+ Map<Long, String> creditLevelMap = creditLevelIds.isEmpty() ? Collections.emptyMap() : remoteCreditLevelService.selectCreditLevelNameByIds(creditLevelIds);
|
|
|
+ Map<Long, String> companyMap = companyIds.isEmpty() ? Collections.emptyMap() : remoteComCompanyService.selectCompanyNameByIds(companyIds);
|
|
|
+ Map<Long, String> customerLevelMap = customerLevelIds.isEmpty() ? Collections.emptyMap() : remoteComCustomerLevelService.selectCustomerLevelNameByIds(customerLevelIds);
|
|
|
+ Map<Long, String> deptMap = deptIds.isEmpty() ? Collections.emptyMap() : remoteDeptService.selectDeptNameByIds(deptIds);
|
|
|
+
|
|
|
+ List<RemoteDictDataVo> enterpriseTypeDicts = remoteDictService.selectDictDataByType("enterprise_type");
|
|
|
+ Map<String, String> enterpriseTypeMap = enterpriseTypeDicts == null ? Collections.emptyMap() : enterpriseTypeDicts.stream()
|
|
|
+ .collect(Collectors.toMap(RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel, (k1, k2) -> k1));
|
|
|
+
|
|
|
+ List<RemoteDictDataVo> cooperationDicts = remoteDictService.selectDictDataByType("cooperation_status");
|
|
|
+ Map<String, String> cooperationMap = cooperationDicts == null ? Collections.emptyMap() : cooperationDicts.stream()
|
|
|
+ .collect(Collectors.toMap(RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel, (k1, k2) -> k1));
|
|
|
+
|
|
|
+ records.forEach(vo -> {
|
|
|
+ if (!"true".equals(bo.getIsHighSeas())) {
|
|
|
+ if (StringUtils.isBlank(vo.getSalesPersonName())) {
|
|
|
+ vo.setSalesPersonName(staffMap.get(vo.getSalesPersonId()));
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(vo.getServiceStaffName())) {
|
|
|
+ vo.setServiceStaffName(staffMap.get(vo.getServiceStaffId()));
|
|
|
+ }
|
|
|
+ vo.setCreditLevelName(creditLevelMap.get(vo.getCreditLevelId()));
|
|
|
+ }
|
|
|
+ vo.setCompanyName(companyMap.get(vo.getBelongCompanyId()));
|
|
|
+ vo.setCustomerLevelName(customerLevelMap.get(vo.getCustomerLevelId()));
|
|
|
+ vo.setDeptName(deptMap.get(vo.getBelongingDepartmentId()));
|
|
|
+
|
|
|
+ String typeValue = vo.getEnterpriseTypeId() != null ? String.valueOf(vo.getEnterpriseTypeId()) : null;
|
|
|
+ vo.setEnterpriseTypeName(enterpriseTypeMap.get(typeValue));
|
|
|
+ vo.setCooperationName(cooperationMap.get(vo.getStatus()));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ return TableDataInfo.build(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CustomerInfoVo queryById(Long id) {
|
|
|
+ return customerInfoService.queryById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean claimPool(CustomerClaimBo claimBo) {
|
|
|
+ Long customerId = claimBo.getId();
|
|
|
+ CustomerInfo customer = customerPoolMapper.selectById(customerId);
|
|
|
+ if (customer == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 认领操作更新客户主表
|
|
|
+ customer.setSalesPersonId(claimBo.getSalesPersonId());
|
|
|
+ customer.setServiceStaffId(claimBo.getServiceStaffId());
|
|
|
+ customer.setBelongingDepartmentId(claimBo.getDeptId());
|
|
|
+ customer.setStatus("0"); // 认领成功后,将客户主表状态改为有效
|
|
|
+ boolean updated = customerPoolMapper.updateById(customer) > 0;
|
|
|
+
|
|
|
+ if (updated) {
|
|
|
+ // 2. 将业务员加入团队成员
|
|
|
+ if (claimBo.getSalesPersonId() != null) {
|
|
|
+ saveOrUpdateTeamMember(customer.getCustomerNo(), claimBo.getSalesPersonId(), "1", 1, 1);
|
|
|
+ }
|
|
|
+ // 3. 将客服支持加入团队成员
|
|
|
+ if (claimBo.getServiceStaffId() != null) {
|
|
|
+ saveOrUpdateTeamMember(customer.getCustomerNo(), claimBo.getServiceStaffId(), "3", 0, 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return updated;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveOrUpdateTeamMember(String objectNo, Long userNo, String roleCode, Integer izManager, Integer updateAccredit) {
|
|
|
+ TeamMemberVo existing = teamMemberMapper.selectByObjectNoAndUserNoWithDeleted(objectNo, userNo);
|
|
|
+ String realName = "";
|
|
|
+ CrmStaff staff = crmStaffMapper.selectById(userNo);
|
|
|
+ if (staff != null) {
|
|
|
+ realName = staff.getStaffName();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (existing != null) {
|
|
|
+ teamMemberMapper.restoreMemberById(
|
|
|
+ existing.getId(),
|
|
|
+ userNo,
|
|
|
+ realName,
|
|
|
+ roleCode,
|
|
|
+ updateAccredit,
|
|
|
+ izManager,
|
|
|
+ LoginHelper.getUserId(),
|
|
|
+ LoginHelper.getDeptId(),
|
|
|
+ PlatformContext.getPlatform()
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ TeamMember member = new TeamMember();
|
|
|
+ member.setDataType(12);
|
|
|
+ member.setObjectNo(objectNo);
|
|
|
+ member.setUserNo(userNo);
|
|
|
+ member.setRealName(realName);
|
|
|
+ member.setRoleCode(roleCode);
|
|
|
+ member.setIzManager(izManager);
|
|
|
+ member.setUpdateAccredit(updateAccredit);
|
|
|
+ member.setPlatformCode(PlatformContext.getPlatform());
|
|
|
+ member.setCreateUserId(LoginHelper.getUserId());
|
|
|
+ member.setCreateOrgId(LoginHelper.getDeptId());
|
|
|
+ member.setIsDelete(0);
|
|
|
+ teamMemberMapper.insert(member);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int releaseToPool(List<Long> customerIds, String reason) {
|
|
|
+ if (customerIds == null || customerIds.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 获取要退回客户的 customer_no 列表
|
|
|
+ List<CustomerInfo> customers = customerPoolMapper.selectBatchIds(customerIds);
|
|
|
+ List<String> customerNos = customers.stream()
|
|
|
+ .map(CustomerInfo::getCustomerNo)
|
|
|
+ .filter(StringUtils::isNotBlank)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 2. 如果存在客户编号,则逻辑删除对应客户(dataType = 12)的所有团队成员
|
|
|
+ if (!customerNos.isEmpty()) {
|
|
|
+ teamMemberMapper.delete(new LambdaQueryWrapper<TeamMember>()
|
|
|
+ .eq(TeamMember::getDataType, 12)
|
|
|
+ .in(TeamMember::getObjectNo, customerNos)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 调用原有的 releaseToPool 方法更新客户主表状态
|
|
|
+ return customerInfoService.releaseToPool(customerIds, reason);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int transferSalesPerson(List<Long> customerIds, Long salesPersonId, Long deptId) {
|
|
|
+ return customerInfoService.transferSalesPerson(customerIds, salesPersonId, deptId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int transferServiceStaff(List<Long> customerIds, Long serviceStaffId) {
|
|
|
+ return customerInfoService.transferServiceStaff(customerIds, serviceStaffId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean insertByBo(CustomerInfoBo bo) {
|
|
|
+ return customerInfoService.insertByBo(bo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateByBo(CustomerInfoBo bo) {
|
|
|
+ return customerInfoService.updateByBo(bo);
|
|
|
+ }
|
|
|
+}
|