CustomEventListener.java 4.85 KB
package cn.fw.valhalla.service.bus;

import cn.fw.valhalla.domain.db.ApproveRecord;
import cn.fw.valhalla.domain.db.follow.FollowTask;
import cn.fw.valhalla.domain.enums.ApproveStateEnum;
import cn.fw.valhalla.domain.enums.ApproveTypeEnum;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.rpc.flow.FlowApproveRpc;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
import cn.fw.valhalla.service.bus.setting.SettingBizService;
import cn.fw.valhalla.service.data.ApproveRecordService;
import cn.fw.valhalla.service.event.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author : kurisu
 * @className : CustomEventListener
 * @description : 事件监听器
 * @date: 2020-08-18 16:28
 */
@Component
@Slf4j
public class CustomEventListener {
    private final SettingBizService settingBizService;
    private final FollowBizService followBizService;
    private final ApproveRecordService approveRecordService;
    private final FlowApproveRpc flowApproveRpc;

    @Autowired
    public CustomEventListener(final SettingBizService settingBizService,
                               final FollowBizService followBizService,
                               final ApproveRecordService approveRecordService,
                               final FlowApproveRpc flowApproveRpc) {
        this.settingBizService = settingBizService;
        this.followBizService = followBizService;
        this.approveRecordService = approveRecordService;
        this.flowApproveRpc = flowApproveRpc;
    }

    /**
     * 监听跟进任务取消事件
     *
     * @param event
     */
    @EventListener(TaskCancelEvent.class)
    public void onDataCancel(final TaskCancelEvent event) {
        followBizService.onDataCancel(event);
    }

    /**
     * 当设置发生改变时
     *
     * @param event
     */
    @EventListener(SettingChangeEvent.class)
    public void onSettingChange(final SettingChangeEvent event) {
        final FollowTypeEnum type = event.getType();
        if (Objects.isNull(type)) {
            return;
        }
        Optional<List<SettingVO>> listOptional = Optional.ofNullable(settingBizService.querySettingByCat(type.getValue(), event.getGroupId()));
        listOptional.ifPresent(settingList -> followBizService.synFollowTask(settingList, type, event.getGroupId()));
    }

    /**
     * 档案进入公共池
     *
     * @param event
     */
    @EventListener(PublicPoolEvent.class)
    public void stopTaskAndAddPublic(final PublicPoolEvent event) {
        Long getCustomerId = event.getCustomerId();
        Boolean forbidden = event.getForbidden();
        if (Boolean.TRUE.equals(forbidden)) {
            followBizService.onForbidden(getCustomerId, event.getGroupId());
        } else {
            followBizService.stopTask(getCustomerId, event.getGroupId());
        }
    }

    /**
     * 终止任务
     *
     * @param event
     */
    @EventListener(StopTaskEvent.class)
    public void stopTask(final StopTaskEvent event) {
        FollowTask task = event.getTask();
        followBizService.stopTask(task);
    }

    /**
     * 取消审批
     *
     * @param event
     */
    @EventListener(CancelApproveEvent.class)
    public void cancelApprove(final CancelApproveEvent event) {
        Long taskId = event.getTaskId();
        ApproveTypeEnum typeEnum = event.getTypeEnum();
        ApproveRecord approveRecord = approveRecordService.getOne(Wrappers.<ApproveRecord>lambdaQuery()
                .eq(ApproveRecord::getDataId, taskId)
                .eq(ApproveRecord::getType, typeEnum)
                .eq(ApproveRecord::getState, ApproveStateEnum.WAIT)
                .last("limit 1")
        );
        if (Objects.isNull(approveRecord)) {
            return;
        }
        //FIXME 优化项 处理审批取消失败的场景
        boolean canceled = flowApproveRpc.cancel(approveRecord.getOrderNo());
        approveRecord.setState(ApproveStateEnum.CANCELED);
        approveRecordService.updateById(approveRecord);
    }

    /**
     * 档案专属顾问发生改变后
     *
     * @param event
     */
    @EventListener(CustomerDefeatedEvent.class)
    public void updateTask(final CustomerDefeatedEvent event) {
        Long customerId = event.getCustomerId();
        FollowTypeEnum eventFollowTypeEnum = event.getFollowTypeEnum();
        if (FollowTypeEnum.FM.equals(eventFollowTypeEnum)) {
            followBizService.updateTask(customerId, FollowTypeEnum.RM);
        } else if (FollowTypeEnum.RM.equals(eventFollowTypeEnum)) {
            followBizService.updateTask(customerId, FollowTypeEnum.FM);
        }
    }
}