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

import cn.fw.common.web.auth.LoginAuthBean;
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.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.dto.FollowAttachmentDTO;
import cn.fw.valhalla.domain.enums.ApproveTypeEnum;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.SettingTypeEnum;
import cn.fw.valhalla.domain.enums.TaskStateEnum;
import cn.fw.valhalla.domain.vo.follow.*;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.sdk.enums.DataTypeEnum;
import cn.fw.valhalla.service.bus.follow.strategy.AbstractFollowStrategy;
import cn.fw.valhalla.service.event.CancelApproveEvent;
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 java.util.stream.Collectors;

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 = queryCustomerInfo(followRecord.getCustomerId());
            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 completeRecord(Long id, LoginAuthBean currentUser) {
        super.completeRecord(id, currentUser);
    }

    @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) {
        FollowTask followTask = followTaskService.getOne(Wrappers.<FollowTask>lambdaQuery()
                .eq(FollowTask::getCustomerId, customerId)
                .eq(FollowTask::getType, getFollowType())
                .eq(FollowTask::getGroupId, groupId)
                .last("limit 1")
        );
        if (Objects.isNull(followTask)) {
            return;
        }
        if (TaskStateEnum.WAITING.equals(followTask.getState())) {
            followTaskService.removeById(followTask.getId());
            followNoticeRecordService.removeByTaskId(followTask.getId());
        }
        if (TaskStateEnum.ONGOING.equals(followTask.getState())) {
            followTaskService.removeById(followTask.getId());
            followNoticeRecordService.removeByTaskId(followTask.getId());
            cancelFollowTodo(followTask.getId());
        }
        //在首保后退车这种情况不考虑
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingChanged(List<SettingVO> setting, Long groupId) {
        List<FollowTask> list = followTaskService.list(Wrappers.<FollowTask>lambdaQuery()
                .eq(FollowTask::getState, TaskStateEnum.WAITING)
                .eq(FollowTask::getType, getFollowType())
        );
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        updateTask(list, setting);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createFirstRecord(FollowTask task) {
        return super.createFirstRecord(task);
    }

    @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) {
        Customer customer = customerService.queryById(originalData.getCustomerId());
        if (Objects.isNull(customer)) {
            return true;
        }
        if (DataTypeEnum.FM.equals(originalData.getType())) {
            completeTask(customer, originalData.getGenerateTime());
            return true;
        }
        FollowTask task = followTaskService.getOne(Wrappers.<FollowTask>lambdaQuery()
                .eq(FollowTask::getType, getFollowType())
                .eq(FollowTask::getCustomerId, customer.getId())
                .last("limit 1")
        );
        if (Objects.nonNull(task)) {
            return true;
        }
        task = createTask(originalData, getFollowType());
        if (Objects.isNull(task)) {
            return true;
        }
        task.setFollowUser(customer.getAdviserId());
        task.setFollowShop(customer.getShopId());
        task.setOriginUser(customer.getAdviserId());
        task.setOriginShop(customer.getShopId());
        ShopDTO shop = oopService.shop(customer.getShopId());
        if (Objects.nonNull(shop)) {
            task.setDealerId(shop.getDealerId());
        }
        boolean saved = followTaskService.save(task);
        if (!saved) {
            return true;
        }
        return createFirstNotice(task);
    }

    /**
     * 完成之前的跟进
     *
     * @param customer
     * @param completeTime
     */
    private void completeTask(Customer customer, Date completeTime) {
        List<FollowTask> list = followTaskService.list(Wrappers.<FollowTask>lambdaQuery()
                .eq(FollowTask::getType, getFollowType())
                .ne(FollowTask::getState, TaskStateEnum.END)
                .eq(FollowTask::getCustomerId, customer.getId())
        );
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<Long> idList = new ArrayList<>();
        List<FollowTask> taskList = new ArrayList<>();
        for (FollowTask task : list) {
            if (TaskStateEnum.WAITING.equals(task.getState())) {
                idList.add(task.getId());
            }
            if (TaskStateEnum.ONGOING.equals(task.getState())) {
                task.setFinished(Boolean.TRUE);
                task.setFinishTime(completeTime);
                task.setState(TaskStateEnum.END);
                task.setFinishShop(customer.getShopId());
                task.setFinishUser(customer.getAdviserId());
                completeTodo(task.getId());
                taskList.add(task);
                CancelApproveEvent event = new CancelApproveEvent(task.getId(), ApproveTypeEnum.FOLLOW_DEFEAT);
                eventPublisher.publishEvent(event);
            }
        }
        List<Long> ids = list.stream().map(FollowTask::getId).collect(Collectors.toList());
        followNoticeRecordService.removeByTaskIds(ids);
        if (idList.size() > 0) {
            followTaskService.removeByIds(idList);
        }
        if (taskList.size() > 0) {
            followTaskService.updateBatchById(taskList);
        }
    }

    @Override
    public FMDetailVO assemble(Long customerId) {
        CustomerDetailDto customerDetailDto = queryCustomerInfo(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;
    }
}