package com.ywt.mg.services;

import com.ywt.mg.core.SqlHelper;
import com.ywt.mg.core.utils.CacheUtil;
import com.ywt.mg.domain.entities.MgAdminMenusView;
import com.ywt.mg.domain.entities.MgAdminMenusViewRepository;
import com.ywt.mg.domain.entities.MgAdminRightView;
import com.ywt.mg.domain.entities.MgAdminRightViewRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class MgRightCacheService {

    private static Logger logger = LoggerFactory.getLogger(MgRightCacheService.class);

    private final String MG_ADMIN_MENUS_VIEW_LIST = "MgAdminMenusViewList";
    private final String MG_ADMIN_RIGHT_VIEW_LIST = "MgAdminRightViewList";

    @Autowired
    private MgAdminRightViewRepository mgAdminRightViewRepository;

    @Autowired
    private MgAdminMenusViewRepository mgAdminMenusViewRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SqlHelper sqlHelperYwtcenter;

    /**
     * 从数据库返回所有菜单数据
     *
     * @return
     */
    public List<MgAdminMenusView> getMenusList() {
        try {
            return mgAdminMenusViewRepository.findAll();
        } catch (Exception e) {
            logger.error("MgRightCacheService#getMenusList():\n {}", e.getMessage(), e);
        }
        return new LinkedList<>();
    }

    /**
     * 从缓存中返回菜单列表
     *
     * @return
     */
    public List<MgAdminMenusView> getMenusListHigh() {
        try {
            return (List<MgAdminMenusView>) CacheUtil.getInstance().get(MG_ADMIN_MENUS_VIEW_LIST, () ->getMenusList());
        } catch (Exception e) {
            logger.error("MgRightCacheService#getMenusListHigh():\n {}", e.getMessage(), e);
        }
        return new LinkedList<>();
    }

    /**
     * 从数据库返回权限点数据
     *
     * @return
     */
    public List<MgAdminRightView> getRightList() {
        try {
            return mgAdminRightViewRepository.findAll();

        } catch (Exception e) {
            logger.error("MgRightCacheService#getRightList():\n {}", e.getMessage(), e);
        }
        return new LinkedList<>();
    }

    /**
     * 从缓存中返回权限点列表
     *
     * @return
     */
    public List<MgAdminRightView> getRightListHigh() {
        try {
            return (List<MgAdminRightView>) CacheUtil.getInstance().get(MG_ADMIN_RIGHT_VIEW_LIST, ()->getRightList());
        } catch (Exception e) {
            logger.error("MgRightCacheService#getRightListHigh():\n {}", e.getMessage(), e);
        }
        return new LinkedList<>();
    }



}
