package com.xjrsoft.common.runner;

import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.entity.Post;
import com.xjrsoft.module.organization.entity.Role;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.organization.entity.UserPostRelation;
import com.xjrsoft.module.organization.entity.UserRoleRelation;
import com.xjrsoft.module.organization.service.IDepartmentService;
import com.xjrsoft.module.organization.service.IPostService;
import com.xjrsoft.module.organization.service.IRoleService;
import com.xjrsoft.module.organization.service.IUserDeptRelationService;
import com.xjrsoft.module.organization.service.IUserPostRelationService;
import com.xjrsoft.module.organization.service.IUserRoleRelationService;
import com.xjrsoft.module.organization.service.IUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 组织架构缓存
 * @Author: tzx
 * @Date: 2022/12/6 14:50
 */
@Component
@AllArgsConstructor
@Slf4j
public class OrganizationCacheRunner implements CommandLineRunner {
    private RedisUtil redisUtil;

    private IUserService userService;

    private IDepartmentService departmentService;

    private IRoleService roleService;

    private IPostService postService;

    private IUserRoleRelationService userRoleRelationService;

    private IUserDeptRelationService userDeptRelationService;

    private IUserPostRelationService userPostRelationService;

    @Override
    public void run(String... args) {
        loadUserCache();
        loadDeptCache();
        loadRoleCache();
        loadPostCache();
        loadUserRoleRelationCache();
        loadUserDepartmentRelationCache();
        loadUserPostRelationCache();
    }

    @Async
    void loadUserCache(){
        log.info("XJRSOFT: 加载所有用户表缓存开始");
        List<User> list = userService.list();
        redisUtil.set(GlobalConstant.USER_CACHE_KEY,list);
        log.info("XJRSOFT: 加载所有用户表缓存结束");
    }

    @Async
    void loadDeptCache(){
        log.info("XJRSOFT: 加载所有机构表缓存开始");
        List<Department> list = departmentService.list();
        redisUtil.set(GlobalConstant.DEP_CACHE_KEY,list);
        log.info("XJRSOFT: 加载所有机构表缓存结束");

    }

    @Async
    void loadRoleCache(){
        log.info("XJRSOFT: 加载所有角色表缓存开始");
        List<Role> list = roleService.list();
        redisUtil.set(GlobalConstant.ROLE_CACHE_KEY,list);
        log.info("XJRSOFT: 加载所有角色表缓存结束");
    }

    @Async
    void loadPostCache(){
        log.info("XJRSOFT: 加载所有岗位表缓存开始");
        List<Post> list = postService.list();
        redisUtil.set(GlobalConstant.POST_CACHE_KEY,list);
        log.info("XJRSOFT: 加载所有岗位表缓存结束");
    }
    @Async
    void loadUserRoleRelationCache(){
        log.info("XJRSOFT: 加载所有用户角色关联表缓存开始");
        List<UserRoleRelation> list = userRoleRelationService.list();
        redisUtil.set(GlobalConstant.USER_ROLE_RELATION_CACHE_KEY,list);
        log.info("XJRSOFT: 加载所有用户角色关联表缓存结束");
    }

    void  loadUserDepartmentRelationCache(){
        log.info("XJRSOFT: 加载所有用户部门关联表缓存开始");
        List<UserDeptRelation> deptRelationList = userDeptRelationService.list();
        redisUtil.set(GlobalConstant.USER_DEPT_RELATION_CACHE_KEY, deptRelationList);
        log.info("XJRSOFT: 加载所有用户部门关联表缓存结束");
    }

    void  loadUserPostRelationCache(){
        log.info("XJRSOFT: 加载所有用户岗位关联表缓存开始");
        List<UserPostRelation> postRelationList = userPostRelationService.list();
        redisUtil.set(GlobalConstant.USER_POST_RELATION_CACHE_KEY, postRelationList);
        log.info("XJRSOFT: 加载所有用户岗位关联表缓存结束");
    }
}
