package com.eitc.system.utils;

import com.eitc.common.core.domain.entity.SysDept;
import com.eitc.common.core.domain.entity.SysUser;
import com.eitc.common.core.redis.RedisCache;
import com.eitc.common.utils.spring.SpringUtils;
import com.eitc.system.service.ISysDeptService;
import com.eitc.system.service.ISysPostService;
import com.eitc.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * UserCacheUtil 用户缓存工具类
 *
 * @author mtx
 * @date: 2024/05/24 9:55
 */
public class UserCacheUtil {

    private static final String USER_CACHE_ID = "user:id:";
    private static final String USER_CACHE_USER_NAME = "user:user_name:";
    private static final String DEPT_CACHE_ID = "dept:id:";
    private static final String POST_CACHE_ID = "post:id:";
    private static final String CLINIC_CACHE_NAME = "clinic:name:0:";
    private static final RedisCache REDIS_CACHE = SpringUtils.getBean(RedisCache.class);
    private static final ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class);
    private static final ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
    private static final ISysPostService sysPostService = SpringUtils.getBean(ISysPostService.class);

    public static SysUser getSysUserById(Long userId) {
        String key = USER_CACHE_ID + userId;
        SysUser sysUser = new SysUser();
        if (REDIS_CACHE.hasKey(key)) {
            SysUser user = REDIS_CACHE.getCacheObject(key);
            if (Objects.nonNull(user)) {
                sysUser = user;
            }
        } else {
            SysUser user = sysUserService.selectUserById(userId);
            if (Objects.nonNull(user)) {
                REDIS_CACHE.setCacheObject(key, user, 30, TimeUnit.MINUTES);
                sysUser = user;
            }
        }
        return sysUser;
    }

    public static SysUser getSysUserByUserName(String userName) {
        String key = USER_CACHE_USER_NAME + userName;
        SysUser sysUser = new SysUser();
        if (REDIS_CACHE.hasKey(key)) {
            SysUser user = REDIS_CACHE.getCacheObject(key);
            if (Objects.nonNull(user)) {
                sysUser = user;
            }
        } else {
            SysUser user = sysUserService.selectUserByNickName(userName);
            if (Objects.nonNull(user)) {
                REDIS_CACHE.setCacheObject(key, user, 30, TimeUnit.MINUTES);
                sysUser = user;
            }
        }
        return sysUser;
    }

    public static SysUser getSysUserById(String userId) {
        return getSysUserById(Long.valueOf(userId));
    }

    public static SysDept getSysDeptByUserId(Long userId) {
        SysUser user = getSysUserById(userId);
        return getSysDeptById(user.getDeptId());
    }


    /**
     * 获取诊所名称
     *
     * @return
     */
    public static String getClinicName() {
        String key = CLINIC_CACHE_NAME;
        String str = "";
        if (REDIS_CACHE.hasKey(key)) {
            String clinicName = REDIS_CACHE.getCacheObject(key);
            if (StringUtils.isNotBlank(clinicName)) {
                str = clinicName;
            }
        } else {
            String clinicName = sysDeptService.getClinicName();
            if (StringUtils.isNotBlank(clinicName)) {
                REDIS_CACHE.setCacheObject(key, clinicName, 30, TimeUnit.MINUTES);
                str = clinicName;
            }
        }
        return str;
    }

    public static List<Long> getPostNameByUserId(Long userId) {
        String key = POST_CACHE_ID + userId;
        List<Long> postList = new ArrayList<>();
        if (REDIS_CACHE.hasKey(key)) {
            List<Long> posts = REDIS_CACHE.getCacheObject(key);
            if (Objects.nonNull(posts)) {
                postList = posts;
            }
        } else {
            List<Long> posts = sysPostService.selectPostListByUserId(userId);
            if (Objects.nonNull(posts)) {
                REDIS_CACHE.setCacheObject(key, posts, 30, TimeUnit.MINUTES);
                postList = posts;
            }
        }

        return postList;
    }

    public static SysDept getSysDeptById(Long deptId) {
        String key = DEPT_CACHE_ID + deptId;
        SysDept sysDept = new SysDept();
        if (REDIS_CACHE.hasKey(key)) {
            SysDept dept = REDIS_CACHE.getCacheObject(key);
            if (Objects.nonNull(dept)) {
                sysDept = dept;
            }
        } else {
            SysDept dept = sysDeptService.selectDeptById(deptId);
            if (Objects.nonNull(dept)) {
                REDIS_CACHE.setCacheObject(key, dept, 30, TimeUnit.MINUTES);
                sysDept = dept;
            }
        }
        return sysDept;
    }

    public static SysDept getSysDeptById(String deptId) {
        return getSysDeptById(Long.valueOf(deptId));
    }

}
