package com.qmth.paper.library.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.qmth.paper.library.common.bean.dto.syssetting.SettingObject;
import com.qmth.paper.library.common.bean.dto.syssetting.SimpleObject;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.*;
import com.qmth.paper.library.common.service.*;
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.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonCacheServiceImpl implements CommonCacheService {

    @Resource
    TBSessionService tbSessionService;

    @Resource
    SysSettingService sysSettingService;

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysSettingGlobalService sysSettingGlobalService;

    @Resource
    LibraryCommonService libraryCommonService;

    @Override
    @CachePut(value = SystemConstant.CACHE_NAME, key = "'tbSession' + #sessionId")
    public TBSession saveTbSession(String sessionId, TBSession tbSession) {
        return tbSession;
    }

    @Override
    @Cacheable(value = SystemConstant.CACHE_NAME, key = "'tbSession' + #sessionId")
    public TBSession getTbSession(String sessionId) {
        return tbSessionService.getById(sessionId);
    }

    @Override
    @CacheEvict(value = SystemConstant.CACHE_NAME, key = "'tbSession' + #sessionId")
    public String removeTbSession(String sessionId) {
        return null;
    }

    @Override
    @Cacheable(value = SystemConstant.CACHE_NAME, key = "'sysSetting' + #schoolId")
    public Map<String, SimpleObject> getSysSetting(Long schoolId) {
        Map<String, SimpleObject> map = new HashMap<>();
        SysSetting sysSetting = sysSettingService.getBySchoolId(schoolId);
        if (sysSetting != null) {
            String parameterObject = sysSetting.getParameterObject();
            List<SettingObject> settingObjects = JSON.parseArray(parameterObject, SettingObject.class);
            for (SettingObject settingObject : settingObjects) {
                for (SimpleObject simpleObject : settingObject.getData()) {
                    map.put(simpleObject.getCode(), simpleObject);
                }
            }
        }
        return map;
    }

    @Override
    @CacheEvict(value = SystemConstant.CACHE_NAME, key = "'sysSetting' + #schoolId")
    public String removeSysSetting(Long schoolId) {
        return null;
    }

    @Override
    @Cacheable(value = SystemConstant.CACHE_NAME, key = "'sysSetting'")
    public Map<String, String> getSysSettingGlobal() {
        SysSettingGlobal sysSettingGlobal = sysSettingGlobalService.getOnlyOne();
        String parameterObject = sysSettingGlobal.getParameterObject();
        List<SettingObject> settingObjects = JSON.parseArray(parameterObject, SettingObject.class);
        Map<String, String> map = new HashMap<>();
        for (SettingObject settingObject : settingObjects) {
            for (SimpleObject simpleObject : settingObject.getData()) {
                map.put(simpleObject.getCode(), simpleObject.getValue());
            }
        }
        return map;
    }

    @Override
    @CacheEvict(value = SystemConstant.CACHE_NAME, key = "'sysSetting'")
    public String removeSysSettingGlobal() {
        return null;
    }

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

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