package com.ywt.service;

import com.ywt.core.SqlHelper;
import com.ywt.core.utils.Checker;
import com.ywt.domain.entity.center.*;

import com.ywt.interceptors.WebAppContext;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Service
public class UserService {

    @Autowired
    private SqlHelper sqlHelper;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MGAdminsRepository mgAdminsRepository;

    @Autowired
    private MgAdminMenusViewRepository mgAdminMenusViewRepository;

    @Autowired
    private MgAdminRightViewRepository mgAdminRightViewRepository;

    @Autowired
    private MgRightCacheService mgRightCacheService;


    public MGAdmins getCurrentAdmin() {
        return mgAdminsRepository.findById(getAdminId()).orElse(null);
    }


    /**
     * 得到登录用户ID(mg_admin表ID)
     *
     * @return adminId
     */
    public int getAdminId() {
        int adminId = WebAppContext.current().getAdminId();
//        return adminId > 0 ? adminId : 1 ;
        return adminId;
    }


    /**
     * 得到当前眼镜店管理员登录的店铺ID
     *
     * @return shopId 店铺ID
     */
    public int getShopId() {
        int shopId = WebAppContext.current().getShopId();
//        return shopId > 0 ? shopId : 1 ;
        return shopId;
    }

    /**
     * 得到当前用户访问的医院ID
     *
     * @return hospitalId 医院ID
     */
    public int getHospitalId() {
        int hospitalId = WebAppContext.current().getHospitalId();
//        return hospitalId > 0 ? hospitalId : 41 ;
        return hospitalId;
    }

    /**
     * 当前登录用户Id（user表ID）
     *
     * @return userID
     */
    public int getCurrentUserId() {
        return WebAppContext.current().getUserId();
    }

    /**
     * 根据用户ID得到用户信息
     *
     * @param userId
     * @return {@link User}
     */
    public User getUserByUserId(int userId) {
        return userRepository.findById(userId).orElse(null);
    }

    /**
     * 根据用户ID得到对象
     *
     * @param adminId
     * @return
     */
    public MGAdmins getAdminsByAdminId(int adminId) {
        return mgAdminsRepository.findById(adminId).orElse(null);
    }

    /**
     * 得到当前用户的account
     *
     * @return 当前登陆着的account
     */
    public String getCurrentAdminAcount() {
        int adminId = getAdminId();
        MGAdmins admins = getAdminsByAdminId(adminId);
        if (!Checker.isNone(admins)) {
            return Checker.getStringValue(admins.getAccount());
        }

        return "";
    }

    /**
     * 这里校验单个地址，采取直接从数据库读取的方式（没有采用缓存），适合调用频率低的方法使用
     *
     * @param rightCode 权限点
     * @return true: 有该权限， false：没有权限
     */
    public boolean checkRight(int adminId, String rightCode) {
        List<MgAdminRightView> rightViewList = mgAdminRightViewRepository.getAllByAdminId(adminId);
        MgAdminRightView rightView = rightViewList.stream().filter(p -> Checker.getStringValue(p.getRightCode()).equals(rightCode)).findFirst().orElse(null);
        if (rightView != null) {
            return true;
        }
        return false;
    }

    public boolean checkRight(String rightCode) {
        int adminId = getAdminId();
        return checkRight(adminId, rightCode);
    }

    /**
     * 这里校验单个地址，采取直接从数据库读取的方式（采用缓存），适合调用频率高的方法使用
     *
     * @param rightCode 权限点
     * @return true: 有该权限， false：没有权限
     */
    public boolean checkCacheRight(int adminId, String rightCode) {
        List<MgAdminRightView> mgAdminRightViewList = mgRightCacheService.getRightListHigh();
        List<MgAdminRightView> rightViewList = mgAdminRightViewList.stream().filter(p -> p.getAdminId().intValue() == adminId).collect(Collectors.toList());
        MgAdminRightView rightView = rightViewList.stream().filter(p -> Checker.getStringValue(p.getRightCode()).equals(rightCode)).findFirst().orElse(null);
        if (rightView != null) {
            return true;
        }
        return false;
    }

    public boolean checkCacheRight(String rightCode) {
        int adminId = getAdminId();
        return checkRight(adminId, rightCode);
    }

    public int getCurrentTerminal() {
        return WebAppContext.current().getTerminal();
    }

    public String getCurrentOpenId() {
        return WebAppContext.current().getOpenId();
    }
}
