package com.tofly.common.oauth.feign;

import com.tofly.base.api.entity.MemberUser;
import com.tofly.base.api.entity.User;
import com.tofly.base.api.feign.FlowableService;
import com.tofly.common.core.constant.SecurityConstants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.Map;

/**
 * @author tuonbed
 * @date 2019/12/3  16:32
 */
@Slf4j
@AllArgsConstructor
public class FlowableListener {
    private FlowableService flowableService;

    @Async
    @Order
    @EventListener(FlowableAddUserEvent.class)
    public void addUser(FlowableAddUserEvent event) {
        User user = (User) event.getSource();
        flowableService.addUser(user,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableAddUserGroupEvent.class)
    public void addUserGrop(FlowableAddUserGroupEvent event) {
        Map map = (Map) event.getSource();
        flowableService.addUserGroup(map,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableAddUserToGroupEvent.class)
    public void addUserToGroup(FlowableAddUserToGroupEvent event) {
        MemberUser user = (MemberUser) event.getSource();
        flowableService.addUserToGroup(user,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableDeleteUserEvent.class)
    public void deleteUser(FlowableDeleteUserEvent event) {
        User user = (User) event.getSource();
        flowableService.deleteUser(user,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableDeleteUserToGroupEvent.class)
    public void deleteUserToGroup(FlowableDeleteUserToGroupEvent event) {
        MemberUser user = (MemberUser) event.getSource();
        flowableService.deleteUserToGroup(user,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableUpdateUserEvent.class)
    public void updateUser(FlowableUpdateUserEvent event) {
        User user = (User) event.getSource();
        flowableService.updateUser(user,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableUpdateGroupEvent.class)
    public void updateGroup(FlowableUpdateGroupEvent event) {
        Map map = (Map) event.getSource();
        flowableService.updateGroup(map,SecurityConstants.FROM_IN);
    }

    @Async
    @Order
    @EventListener(FlowableDeleteGroupEvent.class)
    public void updateUser(FlowableDeleteGroupEvent event) {
        String id = (String) event.getSource();
        flowableService.deleteGroup(id,SecurityConstants.FROM_IN);
    }
}
