package cn.pluss.platform.controller;

import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.RoleInfo;
import cn.pluss.platform.entity.RoleUserInfo;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.roleInfo.RoleInfoService;
import cn.pluss.platform.roleUserInfo.RoleUserInfoService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.userInfo.UserInfoService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class BaseController<S extends IService<T>, T> extends BaseModelController<S, T> {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected UserInfoService uiService;

    @Autowired
    protected UserAppService uaService;

    @Autowired
    protected RoleUserInfoService ruiService;

    @Autowired
    protected RoleInfoService riService;
    /**
     * 获取登录用户信息
     *
     * @return
     */
    public ShiroUser queryCurrentShiroUser() {
        try {
            String userId = request.getHeader("userId");
            UserInfo ui = uiService.getById(userId);
            UserApp ua = new UserApp();
            ua.setUserId(ui.getId());
            ua = uaService.getOne(new QueryWrapper<>(ua));
            ShiroUser shiroUser = new ShiroUser();
            shiroUser.setUserType(ua.getUserType());
            shiroUser.setId(ui.getId());
            shiroUser.setStatus(ui.getStatus());
            shiroUser.setLoginName(ui.getLoginName());
            shiroUser.setStaffType(ua.getStaffType() + "");
            shiroUser.setUserType(ua.getUserType());
            shiroUser.setMerchantCode(ua.getMerchantCode());

            return shiroUser;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 判断是否是管理员
     *
     * @return
     */
    public Boolean verifyUserRole() {
        try {
            String userId = request.getHeader("userId");
            RoleUserInfo roleUserInfo = new RoleUserInfo();
            roleUserInfo.setUserId(Long.parseLong(userId));
            roleUserInfo = ruiService.getOne(new QueryWrapper<>(roleUserInfo));

            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(roleUserInfo.getRoleId());
            roleInfo = riService.getOne(new QueryWrapper<>(roleInfo));
            return roleInfo.getName().equals("超级系统管理员");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 读取request数据
     *
     * @param request
     * @return
     */
    public String readRequestMsg(HttpServletRequest request) {
        java.io.BufferedReader bis = null;
        String line = null;
        String result = "";
        try {
            bis = new java.io.BufferedReader(new java.io.InputStreamReader(
                    request.getInputStream(), "UTF-8"));
            while ((line = bis.readLine()) != null) {
                result += line + "\r\n";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 是否是管理员
     */
    public void getRole() {
        Boolean bol = verifyUserRole();
        if (bol) {
            request.setAttribute("admin", "admin");
        }
    }

    /**
     * 不是管理员 获取的权限  是商户
     */
    public void getPermission() {
        ShiroUser user = queryCurrentShiroUser();
        if (StringUtil.isNotEmpty(user.getMerchantCode())) {
            request.setAttribute("merchant", "merchant");
        }
    }
}
