package com.ywt.service;

import com.ywt.core.utils.CacheUtil;
import com.ywt.domain.entity.center.MgAdminMenusView;
import com.ywt.domain.entity.center.MgAdminMenusViewRepository;
import com.ywt.domain.entity.center.MgAdminRightView;
import com.ywt.domain.entity.center.MgAdminRightViewRepository;

import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@DubboService
@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;

    /**
     * 从数据库返回所有地区数据
     *
     * @return
     */
    public List<MgAdminMenusView> getMenusList() {
        try {
            return mgAdminMenusViewRepository.findAll();
        } catch (Exception 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) {
        }
        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<>();
    }


}
