package com.liang.common.base;

import com.liang.common.utils.SecurityUtil;
import com.liang.entity.SysUserInfo;
import com.liang.service.SysPermissionInfoService;
import com.liang.service.SysRolePermissionService;
import com.liang.service.SysUserInfoService;
import com.liang.service.SysUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

public abstract class BaseController {

    @Value("${server.upload}")
    public String uploadFolder;

    @Value("${server.webfileport}")
    public String webfileport;

    @Value("${server.webfileip}")
    public String webfileip;

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

    public static final String RESULT_ROWS = "rows";

    public static final String RESULT_TOTAL = "total";
    @Resource
    private SysUserInfoService sysUserInfoService;
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Resource
    private SysPermissionInfoService sysPermissionInfoService;


    public BaseController() {
    }

    /**
     * 获取登录用户
     *
     * @return
     */
    public SysUserInfo getSysUserInfo() {
        SysUserInfo sysUserInfo = SecurityUtil.getSysUserInfo();
        if (sysUserInfo != null) {
            return sysUserInfo;
        } else
            return null;
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public Long getUserId() {
        SysUserInfo sysUserInfo = getSysUserInfo();
        if (sysUserInfo != null)
            return sysUserInfo.getUserId().longValue();
        else
            return 0L;
    }

    /**
     * 获取登录用户ID
     *
     * @return
     */
    public Integer getSysUserId() {
        SysUserInfo sysUserInfo = getSysUserInfo();
        if (sysUserInfo != null)
            return sysUserInfo.getUserId();
        else
            return 0;
    }

    /**
     * 获取登录用户账户account
     *
     * @return
     */
    public String getSysUserAccount() {
        SysUserInfo sysUserInfo = getSysUserInfo();
        if (sysUserInfo != null)
            return sysUserInfo.getAccount();
        else
            return null;
    }

    /**
     * 权限检查
     *
     * @param permissionName
     * @return
     */
    public Boolean CheckPermissionInfo(String permissionName) {
        SysUserInfo sysUserInfo = getSysUserInfo();
        int ret = sysPermissionInfoService.checkPermission(permissionName, sysUserInfo.getUserId());
        if (ret == 1)
            return true;
        else
            return false;
    }

    /**
     * ISO-8859-1 to UTF-8
     *
     * @param str
     * @return String
     * @throws UnsupportedEncodingException
     */
    public String ISOToUTF8(String str) throws UnsupportedEncodingException {
        return new String(str.getBytes("ISO-8859-1"), "UTF-8");
    }

    /**
     * 获取用户token
     *
     * @return
     */
    public String getUserKey() {
        return SecurityUtil.getUserKey();
    }
}
