package com.qmth.teachcloud.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.core.solar.model.AppInfo;
import com.qmth.boot.core.solar.model.OrgInfo;
import com.qmth.boot.core.solar.service.SolarService;
import com.qmth.teachcloud.common.bean.auth.AuthBean;
import com.qmth.teachcloud.common.bean.dto.AuthOrgInfoDto;
import com.qmth.teachcloud.common.bean.result.MenuResult;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.*;
import com.qmth.teachcloud.common.enums.PrivilegePropertyEnum;
import com.qmth.teachcloud.common.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 缓存操作serviceImpl 实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/3/25
 */
@Service
public class CommonCacheServiceImpl implements CommonCacheService {
    private final static Logger log = LoggerFactory.getLogger(CommonCacheServiceImpl.class);

    @Resource
    SysUserService sysUserService;

    @Resource
    BasicSchoolService basicSchoolService;

    @Resource
    TeachcloudCommonService commonService;

    @Resource
    SysOrgService sysOrgService;

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SolarService solarService;

    /**
     * 添加角色缓存
     *
     * @param roleId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.ROLE_CACHE, key = "#p0")
    public SysRole roleCache(Long roleId) {
        return sysRoleService.getById(roleId);
    }

    /**
     * 修改角色缓存
     *
     * @param roleId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.ROLE_CACHE, key = "#p0")
    public SysRole updateRoleCache(Long roleId) {
        return sysRoleService.getById(roleId);
    }

    /**
     * 删除角色缓存
     *
     * @param roleId
     * @return
     */
    @Override
    @CacheEvict(value = SystemConstant.ROLE_CACHE, key = "#p0")
    public void removeRoleCache(Long roleId) {

    }

    /**
     * 添加用户菜单缓存
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.USER_MENU_CACHE, key = "#p0")
    public MenuResult userMenuCache(Long userId) {
        return commonService.getUserMenu(userId);
    }

    /**
     * 修改用户菜单缓存
     *
     * @param userId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.USER_MENU_CACHE, key = "#p0")
    public MenuResult updateUserMenuCache(Long userId) {
        return commonService.getUserMenu(userId);
    }

    /**
     * 删除用户菜单缓存
     *
     * @param userId
     */
    @Override
    @CacheEvict(value = SystemConstant.USER_MENU_CACHE, key = "#p0")
    public void removeUserMenuCache(Long userId) {

    }

    /**
     * 添加用户缓存
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0")
    public SysUser userCache(Long userId) {
        return sysUserService.getById(userId);
    }

    /**
     * 修改用户缓存
     *
     * @param userId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0")
    public SysUser updateUserCache(Long userId) {
        return sysUserService.getById(userId);
    }

    /**
     * 删除用户缓存
     *
     * @param userId
     */
    @Override
    @CacheEvict(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0")
    public void removeUserCache(Long userId) {

    }

    /**
     * 添加用户鉴权缓存
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.USER_OAUTH_CACHE, key = "#p0")
    public AuthBean userAuthCache(Long userId) {
        return commonService.getUserAuth(userId);
    }

    /**
     * 修改用户鉴权缓存
     *
     * @param userId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.USER_OAUTH_CACHE, key = "#p0")
    public AuthBean updateUserAuthCache(Long userId) {
        return commonService.getUserAuth(userId);
    }

    /**
     * 删除用户鉴权缓存
     *
     * @param userId
     */
    @Override
    @CacheEvict(value = SystemConstant.USER_OAUTH_CACHE, key = "#p0")
    public void removeUserAuthCache(Long userId) {

    }

    /**
     * 添加学校缓存
     *
     * @param schoolId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.SCHOOL_CACHE, key = "#p0")
    public BasicSchool schoolCache(Long schoolId) {
        return basicSchoolService.getById(schoolId);
    }

    /**
     * 添加学校缓存
     *
     * @param code
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.SCHOOL_CODE_CACHE, key = "#p0")
    public BasicSchool schoolCache(String code) {
        QueryWrapper<BasicSchool> basicSchoolQueryWrapper = new QueryWrapper<>();
        basicSchoolQueryWrapper.lambda().eq(BasicSchool::getCode, code);
        return basicSchoolService.getOne(basicSchoolQueryWrapper);
    }

    /**
     * 添加机构缓存
     *
     * @param orgId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.ORG_CACHE, key = "#p0")
    public SysOrg orgCache(Long orgId) {
        return sysOrgService.getById(orgId);
    }

    /**
     * 获取鉴权url
     *
     * @param privilegePropertyEnum
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.PRIVILEGE_URL_CACHE, key = "#p0")
    public Set<String> privilegeUrlCache(PrivilegePropertyEnum privilegePropertyEnum) {
        return commonService.getPrivilegeUrl(privilegePropertyEnum);
    }

    @Override
    @Cacheable(value = SystemConstant.PRIVILEGE_URL_CACHE, key = "#p0 + '-' + #p1")
    public Set<String> privilegeUrlCache(PrivilegePropertyEnum privilegePropertyEnum, Long schoolId) {
        return commonService.getPrivilegeUrl(privilegePropertyEnum, schoolId);
    }

    /**
     * 修改鉴权url
     *
     * @param privilegePropertyEnum
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.PRIVILEGE_URL_CACHE, key = "#p0")
    public Set<String> updatePrivilegeUrlCache(PrivilegePropertyEnum privilegePropertyEnum) {
        return commonService.getPrivilegeUrl(privilegePropertyEnum);
    }

    /**
     * 删除鉴权url
     *
     * @param privilegePropertyEnum
     */
    @Override
    @CacheEvict(value = SystemConstant.PRIVILEGE_URL_CACHE, key = "#p0")
    public void removePrivilegeUrlCache(PrivilegePropertyEnum privilegePropertyEnum) {

    }

    /**
     * 获取角色权限缓存
     *
     * @param roleId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.ROLE_PRIVILEGE_CACHE, key = "#p0")
    public List<SysRolePrivilege> rolePrivilegeCache(Long roleId) {
        return commonService.getRolePrivilege(roleId);
    }

    /**
     * 修改角色权限缓存
     *
     * @param roleId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.ROLE_PRIVILEGE_CACHE, key = "#p0")
    public List<SysRolePrivilege> updateRolePrivilegeCache(Long roleId) {
        return commonService.getRolePrivilege(roleId);
    }

    /**
     * 删除角色权限缓存
     *
     * @param roleId
     */
    @Override
    @CacheEvict(value = SystemConstant.ROLE_PRIVILEGE_CACHE, key = "#p0")
    public void removeRolePrivilegeCache(Long roleId) {

    }

    /**
     * 添加用户角色权限缓存
     *
     * @param userId
     * @return
     */
    @Override
    @Cacheable(value = SystemConstant.USER_ROLE_PRIVILEGE_CACHE, key = "#p0")
    public List<SysUserRole> userRolePrivilegeCache(Long userId) {
        return commonService.getUserRolePrivilege(userId);
    }

    /**
     * 修改用户角色权限缓存
     *
     * @param userId
     * @return
     */
    @Override
    @CachePut(value = SystemConstant.USER_ROLE_PRIVILEGE_CACHE, key = "#p0")
    public List<SysUserRole> updateUserRolePrivilegeCache(Long userId) {
        return commonService.getUserRolePrivilege(userId);
    }

    /**
     * 删除用户角色权限缓存
     *
     * @param userId
     */
    @Override
    @CacheEvict(value = SystemConstant.USER_ROLE_PRIVILEGE_CACHE, key = "#p0")
    public void removeUserRolePrivilegeCache(Long userId) {

    }

    /**
     * 删除考生个人报告缓存
     */
    @Override
    @CacheEvict(value = SystemConstant.EXAM_STUDENT_REPORT, allEntries = true)
    public void removeExamStudentReportCache() {

    }

    /**
     * 删除院长缓存
     */
    @Override
    @CacheEvict(value = SystemConstant.COLLEGE_DEAN_REPORT, allEntries = true)
    public void removeCollegeDeanCache() {

    }

    /**
     * 删除学院学科缓存
     */
    @Override
    @CacheEvict(value = SystemConstant.COLLEGE_COURSE_REPORT, allEntries = true)
    public void removeCollegeCourseCache() {

    }

    /**
     * 删除开课课程考试总览图表缓存
     */
    @Override
    @CacheEvict(value = SystemConstant.SURVEY_TEACHER_VIEW_REPORT, allEntries = true)
    public void removeSurveyTeacherViewCache() {

    }

    /**
     * 删除考生个人报告缓存
     *
     * @param schoolId
     * @param studentCode
     * @param examId
     * @param courseCode
     */
    @Override
    @CacheEvict(value = SystemConstant.EXAM_STUDENT_REPORT, key = "#schoolId + '-' + #studentCode + '-' + #examId + '-' + #courseCode")
    public void removeExamStudentReportCache(Long schoolId, String studentCode, String examId, String courseCode) {

    }

    /**
     * 删除院长缓存
     *
     * @param schoolId
     * @param semester
     * @param examId
     * @param collegeId
     */
    @Override
    @CacheEvict(value = SystemConstant.COLLEGE_DEAN_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' +#collegeId")
    public void removeCollegeDeanCache(Long schoolId, String semester, Long examId, Long collegeId) {

    }

    /**
     * 删除学院学科缓存
     *
     * @param schoolId
     * @param semester
     * @param examId
     * @param courseCode
     * @param collegeId
     */
    @Override
    @CacheEvict(value = SystemConstant.COLLEGE_COURSE_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
    public void removeCollegeCourseCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId) {

    }

    /**
     * 删除开课课程考试总览图表缓存
     *
     * @param schoolId
     * @param semester
     * @param examId
     * @param courseCode
     * @param collegeId
     */
    @Override
    @CacheEvict(value = SystemConstant.SURVEY_TEACHER_VIEW_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
    public void removeSurveyTeacherViewCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId) {

    }

    @Override
    @Cacheable(value = SystemConstant.AUTH_INFO_CACHE, key = "#p0", unless = "#result == null")
    public AuthOrgInfoDto authInfoCache(String code) {
        AppInfo appInfo = solarService.getAppInfo();
        AuthOrgInfoDto authOrgInfoDto = null;
        if (Objects.nonNull(appInfo)) {
            List<OrgInfo> orgInfoList = solarService.getOrgList().stream().filter(s -> Objects.equals(s.getCode(), code)).collect(Collectors.toList());
            if (Objects.nonNull(orgInfoList) && orgInfoList.size() > 0) {
                authOrgInfoDto = new AuthOrgInfoDto(orgInfoList.get(0), appInfo.getControl());
            }
        }
        return authOrgInfoDto;
    }

    @Override
    @CachePut(value = SystemConstant.AUTH_INFO_CACHE, key = "#p0", unless = "#result == null")
    public AuthOrgInfoDto updateAuthInfoCache(String code) {
        AppInfo appInfo = solarService.getAppInfo();
        AuthOrgInfoDto authOrgInfoDto = null;
        if (Objects.nonNull(appInfo)) {
            List<OrgInfo> orgInfoList = solarService.getOrgList().stream().filter(s -> Objects.equals(s.getCode(), code)).collect(Collectors.toList());
            if (Objects.nonNull(orgInfoList) && orgInfoList.size() > 0) {
                authOrgInfoDto = new AuthOrgInfoDto(orgInfoList.get(0), appInfo.getControl());
            }
        }
        return authOrgInfoDto;
    }

    @Override
    @CacheEvict(value = SystemConstant.AUTH_INFO_CACHE, key = "#p0")
    public void removeAuthInfoCache(String code) {

    }
}
