CommonService.java 8.88 KB
package cn.fw.valhalla.service.bus;

import cn.fw.common.util.ValidationUtils;
import cn.fw.valhalla.common.constant.RoleCode;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.domain.db.customer.AccidentPool;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.db.pool.CustomerCluePool;
import cn.fw.valhalla.domain.db.pool.StammkundePool;
import cn.fw.valhalla.domain.enums.ClueStatusEnum;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.StammkundeStatusEnum;
import cn.fw.valhalla.domain.vo.PostUserVO;
import cn.fw.valhalla.rpc.ehr.EhrRpcService;
import cn.fw.valhalla.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.PostUserDTO;
import cn.fw.valhalla.rpc.pstn.PstnService;
import cn.fw.valhalla.service.bus.cust.CustomerChangeBizService;
import cn.fw.valhalla.service.data.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static cn.fw.common.businessvalidator.Validator.BV;

/**
 * @author : kurisu
 * @className : CommonService
 * @description : 公共服务
 * @date: 2020-10-22 11:07
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommonService {
    private final UserService userService;
    private final CustomerService customerService;
    private final CustomerBaseInfoService customerBaseInfoService;
    private final StammkundePoolService stammkundePoolService;
    private final PstnService pstnService;
    private final EhrRpcService ehrRpcService;
    private final AccidentPoolService accidentPoolService;
    private final CustomerChangeBizService customerChangeBizService;
    private final CustomerCluePoolService customerCluePoolService;


    public List<PostUserVO> getUsers(final Long shopId) {
        List<PostUserVO> list = new ArrayList<>();
        List<PostUserDTO> userByRole = userService.getUserByRole(shopId, RoleCode.FWGW);
        if (CollectionUtils.isEmpty(userByRole)) {
            return list;
        }

        for (PostUserDTO userDTO : userByRole) {
            PostUserVO vo = new PostUserVO(userDTO.getUserId(), userDTO.getUserName(), 0);
            int count = stammkundePoolService.count(Wrappers.<StammkundePool>lambdaQuery()
                    .eq(StammkundePool::getAdviserId, userDTO.getUserId())
                    .eq(StammkundePool::getShopId, shopId)
                    .eq(StammkundePool::getPoolStatus, StammkundeStatusEnum.KUNDE)
                    .eq(StammkundePool::getAktiv, Boolean.TRUE)
                    .isNull(StammkundePool::getRejectTime)
            );
            vo.setCurQuantity(count);
            list.add(vo);
        }
        return list;
    }

    public void rollBack(String vin, Long groupId) {
        rollBack(vin);
        customerChangeBizService.rollBack(vin, groupId);
    }

    public void rollBack(String vin) {
        //todo 去订单系统校验vin是否是退换车
    }

    /**
     * 通过智能通话系统获取所拨打的电话号码
     *
     * @param userId
     * @param cusId
     * @param accidentCar
     * @return
     */
    public String call(Long userId, Long cusId, Boolean accidentCar) {
        StaffInfoDTO staffInfo = ehrRpcService.queryStaffInfo(userId);
        BV.notNull(staffInfo, () -> "用户身份验证异常,请重试");
        String becallNo;
        if (Boolean.TRUE.equals(accidentCar)) {
            AccidentPool accidentPool = accidentPoolService.getById(cusId);
            BV.notNull(accidentPool, () -> "用户不存在");
            becallNo = accidentPool.getReportMobile();
            if (!ValidationUtils.checkMobile(becallNo)) {
                return becallNo;
            }
        } else {
            Customer customer = customerService.queryByIdWithInvalid(cusId);
            BV.notNull(customer, () -> "用户不存在");
            BV.isTrue(customer.getYn(), () -> "此档案已经作废,请主动放弃本次跟进");
            CustomerBaseInfo baseInfo = customerBaseInfoService.getById(customer.getBaseId());
            BV.notNull(baseInfo, () -> "用户不存在");
            becallNo = baseInfo.getMobile();
        }
        BV.isNotBlank(becallNo, () -> "客户电话号码不正确,无法拨打");
        assert staffInfo != null;
        BV.isNotBlank(staffInfo.getMobile(), () -> "用户电话号码不正确,无法拨打");
        String virtualNumber = pstnService.virtualNumber(staffInfo.getMobile(), becallNo, staffInfo.getGroupId());
        BV.isNotBlank(virtualNumber, () -> "号码不正确,无法拨号");
        return virtualNumber;
    }

    @Transactional(rollbackFor = Exception.class)
    public int createClue() {
        List<CustomerCluePool> list = customerCluePoolService.list(Wrappers.<CustomerCluePool>lambdaQuery()
                .eq(CustomerCluePool::getClueType, FollowTypeEnum.IR)
                .in(CustomerCluePool::getClueStatus, ClueStatusEnum.FAILURE, ClueStatusEnum.COMPLETE)
                .lt(CustomerCluePool::getUpdateTime, DateUtil.localDate2Date(LocalDate.now()))
                .ne(CustomerCluePool::getGroupId, 1L)
                .last(" limit 2000")
        );
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }

        Long[] shopArr = new Long[]{65L, 66L, 67L};
        String[] shopNameArr = new String[]{"宁夏瑞静售后店", "宁夏翔之星售后店", "贺兰捷联售后店"};

        for (int i = 0; i < list.size(); i++) {
            CustomerCluePool pool = list.get(i);
            Long customerId = pool.getRefererId();
            int count = customerCluePoolService.count(Wrappers.<CustomerCluePool>lambdaQuery()
                    .eq(CustomerCluePool::getClueType, FollowTypeEnum.IR)
                    .eq(CustomerCluePool::getRefererId, customerId)
                    .in(CustomerCluePool::getClueStatus, ClueStatusEnum.WAITING, ClueStatusEnum.ONGOING));
            if (count > 0) {
                continue;
            }
            Customer customer = customerService.queryById(customerId);
            if (Objects.isNull(customer)) {
                continue;
            }
            Long shopId = pool.getOriginalShopId();
            String shopName = pool.getOriginalShopName();
            if (Long.valueOf(146L).equals(shopId)) {
                shopId = shopArr[i % 3];
                shopName = shopNameArr[i % 3];
            }
            CustomerCluePool customerCluePool = new CustomerCluePool();
            List<PostUserDTO> dtoList = userService.getUserByRole(shopId, RoleCode.XBGJ);
            BV.isNotEmpty(dtoList, "门店[" + shopId + "]没有续保跟进人员");
            Collections.shuffle(dtoList);
            PostUserDTO userDTO = dtoList.get(0);
            BeanUtils.copyProperties(pool, customerCluePool);
            customerCluePool.setId(null);
            customerCluePool.setOriginalUserId(userDTO.getUserId());
            customerCluePool.setOriginalUserName(userDTO.getUserName());
            customerCluePool.setOriginalShopId(shopId);
            customerCluePool.setOriginalShopName(shopName);
            customerCluePool.setClueStatus(ClueStatusEnum.WAITING);
            customerCluePool.setRedistribution(Boolean.FALSE);
            customerCluePool.setClueType(FollowTypeEnum.IR);
            customerCluePool.setFinishShopId(null);
            customerCluePool.setFinishShopName(null);
            customerCluePool.setFinishUserId(null);
            customerCluePool.setFinishUserName(null);
            customerCluePool.setCreateTime(null);
            customerCluePool.setUpdateTime(null);

            LocalDate addTime = DateUtil.date2LocalDate(pool.getAddTime());
            while (!addTime.minusDays(40L).isAfter(LocalDate.now())) {
                addTime = addTime.plusYears(1L);
            }

            customerCluePool.setStartTime(DateUtil.localDate2Date(addTime.minusDays(40L)));
            customerCluePool.setAddTime(DateUtil.localDate2Date(addTime));
            customerCluePool.setDeadline(DateUtil.localDate2Date(addTime));
            customerCluePool.setCloseTime(null);
            customerCluePoolService.save(customerCluePool);
        }

        customerCluePoolService.updateBatchById(list);
        return list.size();
    }

    private String getRoleCode(FollowTypeEnum followTypeEnum) {
        switch (followTypeEnum) {
            case AC:
                return RoleCode.SGCGJ;
            case IR:
                return RoleCode.XBGJ;
            case FM:
            case RM:
            default:
                return RoleCode.FWGW;
        }
    }
}