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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.teachcloud.exchange.common.bean.dto.syssetting.SettingObject;
import com.qmth.teachcloud.exchange.common.bean.dto.syssetting.SimpleObject;
import com.qmth.teachcloud.exchange.common.contant.SystemConstant;
import com.qmth.teachcloud.exchange.common.entity.BasicSchool;
import com.qmth.teachcloud.exchange.common.entity.SysSetting;
import com.qmth.teachcloud.exchange.common.service.BasicSchoolService;
import com.qmth.teachcloud.exchange.common.service.CommonCacheService;
import com.qmth.teachcloud.exchange.common.service.SysSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @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
    BasicSchoolService basicSchoolService;

    @Resource
    SysSettingService sysSettingService;

    /**
     * 添加学校缓存
     *
     * @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);
    }

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

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

    @Override
    @CacheEvict(value = SystemConstant.SYS_SETTING_CACHE, key = "#p0")
    public String removeSysSetting(Long schoolId) {
        return null;
    }

    @Override
    @Cacheable(value = SystemConstant.SYS_SETTING_CACHE, key = "#p0")
    public Map<String, SimpleObject> getSysSetting(Long schoolId) {
        Map<String, SimpleObject> map = new LinkedHashMap<>();
        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;
    }
}
