FMFollowStrategy.java 8.12 KB
package cn.fw.valhalla.service.bus.follow.strategy.impl;

import cn.fw.valhalla.common.utils.MobileUtil;
import cn.fw.valhalla.domain.db.OriginalData;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.follow.FollowRecord;
import cn.fw.valhalla.domain.db.follow.FollowTask;
import cn.fw.valhalla.domain.db.pool.CustomerCluePool;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.dto.FollowAttachmentDTO;
import cn.fw.valhalla.domain.enums.ClueStatusEnum;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.SettingTypeEnum;
import cn.fw.valhalla.domain.vo.follow.*;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.sdk.enums.DataTypeEnum;
import cn.fw.valhalla.service.bus.follow.strategy.AbstractFollowStrategy;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

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

/**
 * @author : kurisu
 * @className : FMFollowStrategy
 * @description : 首保策略
 * @date: 2020-08-17 10:46
 */
@Slf4j
@Component
public class FMFollowStrategy extends AbstractFollowStrategy {
    @Override
    public FollowTypeEnum getFollowType() {
        return FollowTypeEnum.FM;
    }

    @Override
    public List<FollowTodoListVO> getList(Integer startIndex, Integer pageSize, Long userId) {
        List<FollowRecord> list = list(startIndex, pageSize, userId, getFollowType());
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<FollowTodoListVO> voList = new ArrayList<>();
        for (FollowRecord followRecord : list) {
            FMTodoListVO vo = new FMTodoListVO();
            vo.setId(followRecord.getId());
            vo.setTaskId(followRecord.getTaskId());
            vo.setCustomerId(followRecord.getCustomerId());
            vo.setDeadline(followRecord.getDeadline());
            CustomerDetailDto customerDetailDto = customerBizService.queryById(followRecord.getCustomerId());
            if (Objects.nonNull(customerDetailDto)) {
                vo.setName(customerDetailDto.getName());
                vo.setPlateNo(customerDetailDto.getPlateNo());
                vo.setCarImage(customerDetailDto.getCarImage());
                vo.setBuyDate(customerDetailDto.getBuyDate());
            }
            Optional<FollowTask> followTask = Optional.ofNullable(followTaskService.getById(followRecord.getTaskId()));
            followTask.ifPresent(task -> vo.setFMExpiration(task.getDeadline()));
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public FollowDetailVO getDetail(Long id) {
        FollowRecord followRecord = followRecordService.getById(id);
        BV.notNull(followRecord, "跟进记录不存在");
        FollowTask followTask = followTaskService.getById(followRecord.getTaskId());
        BV.notNull(followTask, "跟进信息不存在");
        FMDetailVO vo = assemble(followRecord.getCustomerId());
        vo.setId(followRecord.getId());
        vo.setTaskId(followRecord.getTaskId());
        vo.setFMExpiration(followTask.getDeadline());
        return vo;
    }

    @Override
    public List<FollowRecordVO> getRecordList(Long taskId) {
        return super.getRecordList(taskId);
    }

    @Override
    public void uploadAtt(FollowAttachmentDTO dto, Long userId) {
        super.uploadAtt(dto, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelFollowTask(Long customerId, Date time, Long groupId) {
        CustomerCluePool cluePool = customerCluePoolService.queryByRefererId(customerId, groupId, getFollowType());
        if (Objects.isNull(cluePool)) {
            return;
        }
        if (ClueStatusEnum.WAITING.equals(cluePool.getClueStatus())) {
            customerCluePoolService.removeById(cluePool.getId());
            followNoticeRecordService.removeByClueId(cluePool.getId());
        }
        if (ClueStatusEnum.ONGOING.equals(cluePool.getClueStatus())) {
            customerCluePoolService.removeById(cluePool.getId());
            followNoticeRecordService.removeByClueId(cluePool.getId());
            cancelFollowTodo(cluePool.getRefererId(), getFollowType());
        }
        //在首保后退车这种情况不考虑
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingChanged(List<SettingVO> setting, Long groupId) {
        List<CustomerCluePool> poolList = customerCluePoolService.list(Wrappers.<CustomerCluePool>lambdaQuery()
                .eq(CustomerCluePool::getClueType, getFollowType())
                .eq(CustomerCluePool::getGroupId, groupId)
                .eq(CustomerCluePool::getClueStatus, ClueStatusEnum.WAITING)
        );

        if (CollectionUtils.isEmpty(poolList)) {
            return;
        }
        updateClue(poolList, setting);

        SettingVO noticeSetting = setting.stream().filter(r -> SettingTypeEnum.FIRST_NOTICE_TIME.getValue().equals(r.getType())).findFirst().orElse(new SettingVO());
        updateNoticeRecord(poolList, noticeSetting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overdueProcessing(FollowRecord record) {
        super.overdueProcessing(record);
    }

    @Override
    public FollowDetailVO followPoolDetail(FollowTask task) {
        FMDetailVO vo = assemble(task.getCustomerId());
        vo.setTaskId(task.getId());
        vo.setFMExpiration(task.getDeadline());
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean origin2task(OriginalData originalData) throws Exception {
        Customer customer = customerService.queryById(originalData.getCustomerId());
        BV.notNull(customer, () -> "档案不存在");
        boolean bool = Objects.isNull(customer.getAdviserId()) || Objects.isNull(customer.getShopId());
        BV.isFalse(bool, () -> "档案信息不完整");
        CustomerCluePool cluePool = customerCluePoolService.queryByRefererId(customer.getId(), customer.getGroupId(), getFollowType());
        if (DataTypeEnum.FM.equals(originalData.getType())) {
            completeClue(cluePool, originalData);
            return true;
        }
        if (DataTypeEnum.OD.equals(originalData.getType()) && Objects.nonNull(cluePool) && ClueStatusEnum.WAITING.equals(cluePool.getClueStatus())) {
            customerCluePoolService.removeById(cluePool.getId());
        }
        CustomerCluePool clue = createClueInfo(originalData, getFollowType(), customer);
        customerCluePoolService.save(clue);
        createFirstNotice(clue, originalData.getGenerateTime());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTask(Long customerId) {
        Customer customer = customerService.queryById(customerId);
        if (Objects.isNull(customer) || Objects.isNull(customer.getAdviserId()) || Objects.isNull(customer.getShopId())) {
            return;
        }
        CustomerCluePool cluePool = customerCluePoolService.queryByRefererId(customerId, customer.getGroupId(), getFollowType());
        super.renewalTask(customer, cluePool);
    }

    @Override
    public FMDetailVO assemble(Long customerId) {
        CustomerDetailDto customerDetailDto = customerBizService.queryById(customerId);
        FMDetailVO vo = new FMDetailVO();
        vo.setVin(customerDetailDto.getFrameNo());
        vo.setCustomerId(customerId);
        vo.setName(customerDetailDto.getName());
        vo.setMobile(customerDetailDto.getMobile());
        vo.setRegion(MobileUtil.attribution(customerDetailDto.getMobile()));
        vo.setRealMobile(customerDetailDto.getMobile());
        vo.setPlateNo(customerDetailDto.getPlateNo());
        vo.setAdviserId(customerDetailDto.getAdviserId());
        vo.setAdviserName(customerDetailDto.getAdviserName());
        vo.setCarModel(customerDetailDto.getBrandName() + " " + customerDetailDto.getSeriesName());
        vo.setBuyDate(customerDetailDto.getBuyDate());
        return vo;
    }
}