EntmicroFollowService.java 7.43 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.entity.Customer;
import cn.com.glsx.entmicro.entity.EntmicroCustomerFollow;
import cn.com.glsx.entmicro.mapper.CustomerMapper;
import cn.com.glsx.entmicro.mapper.EntmicroCustomerFollowMapper;
import cn.com.glsx.entmicro.model.request.GetStaffFollowCustomerRequest;
import cn.com.glsx.entmicro.model.response.CustomerFollowStaffResponse;
import cn.com.glsx.entmicro.model.response.CustomerInfo;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author fengzhi
 * @Description: 客户Service
 * @date 2021/9/16 14:40
 * matches 120
 */
@Service
@Slf4j
public class EntmicroFollowService {
    @Autowired
    private EntmicroCustomerFollowMapper customerFollowMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Resource
    private EntmicroStaffService entmicroStaffService;

    @Resource
    private MerchantService merchantService;

    /**
     * 批量插入跟进人信息
     *
     * @param customerFollowRelations 跟进人
     * @return
     */
    @Transactional(rollbackFor = RpcServiceException.class)
    public EntmicroCustomerFollow insert(List<EntmicroCustomerFollow> customerFollowRelations) {
        EntmicroCustomerFollow entmicroCustomerFollow = new EntmicroCustomerFollow();
        if (CollectionUtils.isEmpty(customerFollowRelations)) {
            return entmicroCustomerFollow;
        }
        log.info("当前客户和跟进员关联信息{}", customerFollowRelations);
        //判断如果当前跟进人和客户关联存在则不给重复插入
        List<Integer> customerIdList = customerFollowRelations.stream().map(EntmicroCustomerFollow::getCustomerId).collect(Collectors.toList());
        Set<Integer> staffId = customerFollowRelations.stream().map(EntmicroCustomerFollow::getStaffId).collect(Collectors.toSet());
        Integer customerId = customerIdList.get(0);
        List<EntmicroCustomerFollow> localList = customerFollowMapper.findRelationByStaffId(customerId, staffId);
        //对比两端数据
        List<EntmicroCustomerFollow> saveList = customerFollowRelations.stream().filter(index -> !localList.contains(index)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(saveList)) {
            return entmicroCustomerFollow;
        }
        if (saveList.size() == 1) {
            entmicroCustomerFollow = saveList.get(0);
            customerFollowMapper.insertUseGeneratedKeys(entmicroCustomerFollow);
        } else {
            customerFollowMapper.insertList(saveList);
        }
        return entmicroCustomerFollow;
    }

    /**
     * 删除跟进人关系
     */
    public boolean delete(String userId, String externalUserId, Integer merchantId) {
        // 删除跟进人之后判断这个客户是否还有其他的跟进人,没有更新删除好友状态
        List<EntmicroCustomerFollow> entmicroCustomerFollowList = customerFollowMapper.findFollowByExternalUserId(externalUserId, merchantId);
        log.info("客户与跟进人关系:{}", JSONArray.toJSONString(entmicroCustomerFollowList));
        //空就说明没有跟进人 是删除状态
        if (CollectionUtils.isEmpty(entmicroCustomerFollowList)) {
            return true;
        }
        log.info("跟进人数量: {}", entmicroCustomerFollowList.size());
        List<EntmicroCustomerFollow> collect = entmicroCustomerFollowList.stream().filter(index -> index.getUserid().equals(userId)).collect(Collectors.toList());
        log.info("当前需要删除的跟进人userId: {}", collect);
        if (!CollectionUtils.isEmpty(collect)) {
            EntmicroCustomerFollow entmicroCustomerFollow = collect.get(0);
            Integer followId = entmicroCustomerFollow.getId();
            customerFollowMapper.deleteRelation(followId);
            // 删除好友状态
            Integer customerId = entmicroCustomerFollow.getCustomerId();

            if (entmicroCustomerFollowList.size() == 1) {
                log.info("删除好友:{}", externalUserId);
                customerMapper.deleteaById(customerId);
                return true;
            } else {
                log.info("删除好友失败: {}", externalUserId);
                return false;
            }
        }
        return false;
    }


    public void deleteList(List<EntmicroCustomerFollow> buildFollowParam) {
        buildFollowParam.stream().forEach(follow -> {
            customerFollowMapper.deleteCustomer(follow.getId());
        });
    }

    public void updatelist(List<EntmicroCustomerFollow> buildFollowParam) {
        customerFollowMapper.updatelist(buildFollowParam);
    }


    public List<EntmicroCustomerFollow> findAllFollowByExternalUserId(String externalUserId) {
        return customerFollowMapper.findAllFollowByExternalUserId(externalUserId);
    }

    /**
     * id获取关联数据
     *
     * @return
     */
    public CustomerFollowStaffResponse findRelationById(Integer id) {
        return customerFollowMapper.findRelationById(id);
    }

    public List<CustomerFollowStaffResponse> findRelationByUserId(String id) {
        return customerFollowMapper.findRelationByUserId(id);
    }

    /**
     * 修改手机号
     */
    public void updatePhone(EntmicroCustomerFollow entmicroCustomerFollow) {
        customerFollowMapper.updatePhone(entmicroCustomerFollow);
    }

    public CustomerInfo findByRelationId(Integer id) {
        return customerFollowMapper.findByRelationId(id);
    }

    /**
     * 员工id查客户
     *
     * @return
     */
    public List<EntmicroCustomerFollow> findByStaffId(Integer staffId) {
        return customerFollowMapper.findByStaffId(staffId);
    }

    /**
     * 补充修改手机号
     *
     * @param customer
     */
    public void updateBatchInfo(List<Customer> customer) {
        customerFollowMapper.updateBatchInfo(customer);
    }

    /**
     * 获取客户集团员工跟进关系
     *
     * @param request
     * @return
     */
    public Boolean getStaffFollowCustomer(GetStaffFollowCustomerRequest request) {
        Integer merchantId = request.getMerchantId();
        String customerPhone = request.getCustomerPhone();
        List<String> staffUserIdList = request.getStaffUserIdList();
        if (merchantId == null || StringUtils.isEmpty(customerPhone) || CollectionUtils.isEmpty(staffUserIdList)) {
            throw new RpcServiceException("param must not be empty");
        }
        Integer supMerchantNo = entmicroStaffService.findSupMerchantNo(merchantId);
        log.info("获取客户集团员工跟进关系-授权商户 supMerchantNo:{}",supMerchantNo);
        List<Integer> merchantTreeId = merchantService.findMerchantTreeId(supMerchantNo);
        log.info("获取客户集团员工跟进关系-商户树 merchantTreeId:{}",merchantTreeId);
        if (CollectionUtils.isEmpty(merchantTreeId)) return false;
        List<EntmicroCustomerFollow> customerStaffFollow = customerFollowMapper.findCustomerStaffFollow(merchantTreeId, customerPhone, staffUserIdList);
        return !CollectionUtils.isEmpty(customerStaffFollow);
    }
}