package com.my.bus.cache;

import com.my.bus.constant.BusCacheConstants;
import com.my.common.constant.CacheConstants;
import com.my.common.core.domain.entity.SysDept;
import com.my.common.core.domain.entity.SysUser;
import com.my.common.core.redis.RedisCache;
import com.my.system.service.ISysDeptService;
import com.my.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 业务数据缓存
 */
@RequiredArgsConstructor
@Slf4j
@Component
public class BusCache {

    private final RedisCache redisCache;
    private final ISysDeptService deptService;
    private final ISysUserService userService;

    /**
     * 关区代码获取部门信息
     * @param code
     * @return
     */
    public SysDept getDeptByCode(String code){
        SysDept d = redisCache.getCacheObject(CacheConstants.SYS_DEPT_CODE+code);
        if(Objects.isNull(d)){
            d = deptService.selectDeptByCode(code);
            if(Objects.isNull(d)){
                log.error("BusCache.getDeptByCode {} is null",code);
            }
            redisCache.setCacheObject(CacheConstants.SYS_DEPT_CODE+code,d);
        }
        return d;
    }

    /**
     * 部门id获取关区代码
     * @param deptId
     * @return
     */
    public SysDept getOrgDeptById(Long deptId){
        SysDept d = redisCache.getCacheObject(CacheConstants.ORG_DEPT_CODE+deptId);
        if(Objects.isNull(d)){
            d = deptService.selectOrgDeptById(deptId);
            if(Objects.isNull(d)){
                log.error("BusCache.getCustomsCodeById {} is null",deptId);
                return null;
            }
            redisCache.setCacheObject(CacheConstants.ORG_DEPT_CODE+deptId,d);
        }
        return d;
    }
    /**
     * 部门id获取部门信息
     * @param deptId
     * @return
     */
    public SysDept getDeptById(Long deptId){
        SysDept d = redisCache.getCacheObject(CacheConstants.DEPT_CODE+deptId);
        if(Objects.isNull(d)){
            d = deptService.selectDeptById(deptId);
            if(Objects.isNull(d)){
                log.error("BusCache.getCustomsCodeById {} is null",deptId);
                return null;
            }
            redisCache.setCacheObject(CacheConstants.DEPT_CODE+deptId,d);
        }
        return d;
    }
    /**
     * 部门名称获取部门信息
     * @param name
     * @return
     */
    public SysDept getDeptByName(String name){
        SysDept d = redisCache.getCacheObject(CacheConstants.DEPT_CODE+name);
        if(Objects.isNull(d)){
            d = deptService.selectDeptByName(name);
            if(d != null){
                redisCache.setCacheObject(CacheConstants.DEPT_CODE+name,d);
            }
        }
        return d;
    }

    /**
     * 用户名称获取用户id
     * @param name
     * @return
     */
    public Long getUserByName(String name){
        Long userId = redisCache.getCacheObject(CacheConstants.USER_NAME+name);
        if(Objects.isNull(userId)){
            userId = userService.checkUserNameUnique(name);
            if(userId != null){
                redisCache.setCacheObject(CacheConstants.USER_NAME+name, userId);
            }
        }
        return userId;
    }
    /**
     * 用户名称获取用户信息
     * @param userId
     * @return
     */
    public SysUser getUserById(Long userId){
        SysUser user = redisCache.getCacheObject(CacheConstants.USER_NAME+userId);
        if(Objects.isNull(user)){
            user = userService.selectById(userId);
            if(user != null){
                redisCache.setCacheObject(CacheConstants.USER_NAME+userId, user);
            }
        }
        return user;
    }

    public void clearDeptUser(){
        redisCache.deleteObject(CacheConstants.DEPT_CODE+"*");
        redisCache.deleteObject(CacheConstants.ORG_DEPT_CODE+"*");
        redisCache.deleteObject(CacheConstants.SYS_DEPT_CODE+"*");
        redisCache.deleteObject(CacheConstants.USER_NAME+"*");
    }
    public void clearUser(Long id){
        redisCache.deleteObject(CacheConstants.USER_NAME+ id);
    }


    public void clearProcessLink(Long orgId){
        redisCache.deleteObject(BusCacheConstants.PROCESS_KEY+orgId);
    }




}
