package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.core.cache.service.CacheService;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.model.BasicSchool;
import com.qmth.jkserver.model.SysConfig;
import com.qmth.jkserver.model.User;
import com.qmth.jkserver.service.BasicSchoolService;
import com.qmth.jkserver.service.CommonCacheService;
import com.qmth.jkserver.service.SysConfigService;
import com.qmth.jkserver.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

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

    @Resource
    UserService userService;

    @Resource
    BasicSchoolService basicSchoolService;

    @Resource
    SysConfigService sysConfigService;

//    @Resource
//    CacheManager cacheManager;

    @Resource
    CacheService cacheService;

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

    /**
     * 修改用户缓存
     *
     * @param userId
     * @return
     */
    @Override
//    @CachePut(value = SystemConstant.USER_ACCOUNT_CACHE, key = "#p0", condition = "#result != null")
    public User updateUserCache(Long userId) {
        cacheService.evict(SystemConstant.USER_ACCOUNT_CACHE, String.valueOf(userId));
        return userCacheCommon(userId);
    }

    /**
     * 用户缓存共用
     *
     * @param userId
     * @return
     */
    private User userCacheCommon(Long userId) {
        User user = (User) cacheService.get(SystemConstant.USER_ACCOUNT_CACHE, String.valueOf(userId));
        if (Objects.isNull(user)) {
            user = userService.getById(userId);
            if (Objects.nonNull(user)) {
                cacheService.put(SystemConstant.USER_ACCOUNT_CACHE, String.valueOf(userId), user);
            } else {
                throw new JkServerException("userId[" + userId + "]用户不存在");
            }
        }
        return user;
    }

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

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

    /**
     * 学校id缓存共用
     *
     * @param schoolId
     * @return
     */
    private BasicSchool schoolCacheCommon(Long schoolId) {
        if (Objects.nonNull(schoolId) && schoolId.longValue() > -1L) {
            BasicSchool basicSchool = (BasicSchool) cacheService.get(SystemConstant.SCHOOL_CACHE, String.valueOf(schoolId));
            if (Objects.isNull(basicSchool)) {
                basicSchool = basicSchoolService.getById(schoolId);
                if (Objects.nonNull(basicSchool)) {
                    cacheService.put(SystemConstant.SCHOOL_CACHE, String.valueOf(schoolId), basicSchool);
                } else {
                    throw new JkServerException("schoolId[" + schoolId + "]学校不存在");
                }
            }
            return basicSchool;
        } else {
            return null;
        }
    }

    /**
     * 学校code缓存共用
     *
     * @param code
     * @return
     */
    private BasicSchool schoolCacheCommon(String code) {
        BasicSchool basicSchool = (BasicSchool) cacheService.get(SystemConstant.SCHOOL_CODE_CACHE, code);
        if (Objects.isNull(basicSchool)) {
            basicSchool = basicSchoolService.getOne(new QueryWrapper<BasicSchool>().lambda().eq(BasicSchool::getCode, code));
            if (Objects.nonNull(basicSchool)) {
                cacheService.put(SystemConstant.SCHOOL_CODE_CACHE, code, basicSchool);
            } else {
                throw new JkServerException("schoolCode[" + code + "]学校不存在");
            }
        }
        return basicSchool;
    }

    /**
     * 添加学校缓存
     *
     * @param code
     * @return
     */
    @Override
//    @Cacheable(value = SystemConstant.SCHOOL_CODE_CACHE, key = "#p0", unless = "#result == null")
    public BasicSchool schoolCache(String code) {
        return schoolCacheCommon(code);
    }

    /**
     * 更新学校缓存
     *
     * @param schoolId
     * @return
     */
    @Override
//    @CachePut(value = SystemConstant.SCHOOL_CACHE, key = "#p0", condition = "#result != null")
    public BasicSchool updateSchoolCache(Long schoolId) {
        cacheService.evict(SystemConstant.SCHOOL_CACHE, String.valueOf(schoolId));
        return schoolCacheCommon(schoolId);
    }

    /**
     * 更新学校缓存
     *
     * @param code
     * @return
     */
    @Override
//    @CachePut(value = SystemConstant.SCHOOL_CODE_CACHE, key = "#p0", condition = "#result != null")
    public BasicSchool updateSchoolCache(String code) {
        cacheService.evict(SystemConstant.SCHOOL_CODE_CACHE, code);
        return schoolCacheCommon(code);
    }

    /**
     * 删除学校缓存
     *
     * @param schoolId
     */
    @Override
//    @CacheEvict(value = SystemConstant.SCHOOL_CACHE, key = "#p0")
    public void removeSchoolCache(Long schoolId) {
        cacheService.evict(SystemConstant.SCHOOL_CACHE, String.valueOf(schoolId));
    }

    /**
     * 删除学校缓存
     */
    @Override
//    @CacheEvict(value = SystemConstant.SCHOOL_CACHE, allEntries = true)
    public void removeSchoolIdCache() {
        cacheService.clear(SystemConstant.SCHOOL_CACHE);
    }

    /**
     * 删除学校缓存
     *
     * @param code
     */
    @Override
//    @CacheEvict(value = SystemConstant.SCHOOL_CODE_CACHE, key = "#p0")
    public void removeSchoolCache(String code) {
        cacheService.evict(SystemConstant.SCHOOL_CODE_CACHE, code);
    }

    /**
     * 删除学校缓存
     */
    @Override
//    @CacheEvict(value = SystemConstant.SCHOOL_CODE_CACHE, allEntries = true)
    public void removeSchoolCodeCache() {
        cacheService.clear(SystemConstant.SCHOOL_CODE_CACHE);
    }

    /**
     * 系统参数key缓存共用
     *
     * @param key
     * @return
     */
    private SysConfig sysConfigCacheCommon(String key) {
        SysConfig sysConfig = (SysConfig) cacheService.get(SystemConstant.SYS_CONFIG_CACHE, key);
        if (Objects.isNull(sysConfig)) {
            sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, key));
            if (Objects.nonNull(sysConfig)) {
                cacheService.put(SystemConstant.SYS_CONFIG_CACHE, key, sysConfig);
            }
        }
        return sysConfig;
    }

    /**
     * 添加系统参数缓存
     *
     * @param key
     * @return
     */
    @Override
//    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", unless = "#result == null")
    public SysConfig addSysConfigCache(String key) {
        return sysConfigCacheCommon(key);
    }

    /**
     * 更新系统参数缓存
     *
     * @param key
     * @return
     */
    @Override
//    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0", condition = "#result != null")
    public SysConfig updateSysConfigCache(String key) {
        cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, key);
        return sysConfigCacheCommon(key);
    }

    /**
     * 删除系统参数缓存
     *
     * @param key
     */
    @Override
//    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0")
    public void removeSysConfigCache(String key) {
        cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, key);
    }

    /**
     * 更新系统参数缓存
     *
     * @param key
     * @return
     */
    @Override
    public void updateSysConfigCacheForDb(String key, SysConfig sysConfig) {
//        cacheManager.getCache(SystemConstant.SYS_CONFIG_CACHE).put(key, sysConfig);
        cacheService.put(SystemConstant.SYS_CONFIG_CACHE, key, sysConfig);
    }

    /**
     * 系统参数key缓存共用
     *
     * @param key
     * @return
     */
    private SysConfig sysConfigCacheCommon(Long schoolId, String key) {
        SysConfig sysConfig = (SysConfig) cacheService.get(SystemConstant.SYS_CONFIG_CACHE, schoolId + "_" + key);
        if (Objects.isNull(sysConfig)) {
            sysConfig = sysConfigService.getOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getSchoolId, schoolId).eq(SysConfig::getConfigKey, key));
            if (Objects.nonNull(sysConfig)) {
                cacheService.put(SystemConstant.SYS_CONFIG_CACHE, schoolId + "_" + key, sysConfig);
            }
        }
        return sysConfig;
    }

    /**
     * 添加系统参数缓存
     *
     * @param schoolId
     * @param key
     * @return
     */
    @Override
//    @Cacheable(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", unless = "#result == null")
    public SysConfig addSysConfigCache(Long schoolId, String key) {
        return sysConfigCacheCommon(schoolId, key);
    }

    /**
     * 更新系统参数缓存
     *
     * @param schoolId
     * @param key
     * @return
     */
    @Override
//    @CachePut(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1", condition = "#result != null")
    public SysConfig updateSysConfigCache(Long schoolId, String key) {
        cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, schoolId + "_" + key);
        return sysConfigCacheCommon(schoolId, key);
    }

    /**
     * 删除系统参数缓存
     *
     * @param schoolId
     * @param key
     */
    @Override
//    @CacheEvict(value = SystemConstant.SYS_CONFIG_CACHE, key = "#p0 + '-' + #p1")
    public void removeSysConfigCache(Long schoolId, String key) {
        cacheService.evict(SystemConstant.SYS_CONFIG_CACHE, schoolId + "_" + key);
    }
}
