package com.huiju.estateagents.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.center.taUser.service.ITaUserService;
import com.huiju.estateagents.common.JWTUtils;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.service.ITaPersonBuildingService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2018-12-18
 */
public class BaseController {
    @Autowired
    private ITaUserService taUserService;

    @Autowired
    private ITaPersonBuildingService taPersonBuildingService;

    /**
     * 获取orgId
     *
     * @param request
     * @return
     */
    public Integer getOrgId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("orgId")) {
            return Integer.valueOf(map.get("orgId").toString());
        }

        return null;
    }

    public Integer getChannelId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("channelId")) {
            return Integer.valueOf(map.get("channelId").toString());
        }

        return null;
    }

    public String getChannelUserName(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("userName")) {
            return map.get("userName").toString();
        }

        return null;
    }

    public String getOpenId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("openId")) {
            return map.get("openId").toString();
        }

        return null;
    }

    public String getPersonId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("personId")) {
            return map.get("personId").toString();
        }

        return null;
    }

    /**
     * 获取orgId
     *
     * @param request
     * @return
     */
    public Integer getUserId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("userId")) {
            return Integer.valueOf(map.get("userId").toString());
        }

        return null;
    }

    /**
     * 获取服务管理后台用户ID
     *
     * @param request
     * @return
     */
    public Integer getSysUserId(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("userId")) {
            return Integer.valueOf(map.get("userId").toString());
        }
        return null;
    }

    /**
     * 获取服务管理后台用户ID
     *
     * @param request
     * @return
     */
    public String getSysUserName(HttpServletRequest request) {

        Map map = JWTUtils.getUserIdAndOrgId(request);
        if (null != map && null != map.get("userName")) {
            return map.get("userName") + "";
        }
        return null;
    }

    public List<TaPersonBuilding> getTaPersonBuildingListByUserId(HttpServletRequest request) {
        TaUser taUser = taUserService.getById(getUserId(request));
        if (taUser == null) {
            return null;
        }
        QueryWrapper<TaPersonBuilding> taPersonBuildingQueryWrapper = new QueryWrapper<>();
        taPersonBuildingQueryWrapper.eq("user_id", taUser.getUserId());
        List<TaPersonBuilding> taPersonBuildingList = taPersonBuildingService.list(taPersonBuildingQueryWrapper);
        if (null != taUser.getIsAdmin() && taUser.getIsAdmin()) {
            taPersonBuildingList = new ArrayList<>();
        }
        return taPersonBuildingList;
    }
}

