package com.yunzhi.marketing.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhi.marketing.center.taUser.entity.TaUser;
import com.yunzhi.marketing.center.taUser.service.ITaUserService;
import com.yunzhi.marketing.common.JWTUtils;
import com.yunzhi.marketing.entity.TaBuilding;
import com.yunzhi.marketing.entity.TaPersonBuilding;
import com.yunzhi.marketing.service.ITaBuildingService;
import com.yunzhi.marketing.service.ITaPersonBuildingService;
import com.yunzhi.marketing.xlk.entity.Institution;
import com.yunzhi.marketing.xlk.service.IInstitutionService;
import org.springframework.beans.factory.annotation.Autowired;

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

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

    @Autowired
    private ITaPersonBuildingService taPersonBuildingService;

    @Autowired
    private ITaBuildingService taBuildingService;

    @Autowired
    public IInstitutionService iInstitutionService;

    /**
     * 获取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 1;
    }

    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;
        }

        List<TaPersonBuilding> taPersonBuildingList = new ArrayList<>();

        // 新的过滤条件组织机构
        List<TaBuilding> list = taBuildingService.selectByUserId(taUser.getUserId());
        for (TaBuilding taBuilding: list) {
            TaPersonBuilding taPersonBuilding = new TaPersonBuilding();
            taPersonBuilding.setBuildingId(taBuilding.getBuildingId());
            taPersonBuilding.setUserId(taUser.getUserId());
            taPersonBuildingList.add(taPersonBuilding);
        }

        if (null != taUser.getIsAdmin() && taUser.getIsAdmin()) {
            taPersonBuildingList = new ArrayList<>();
        }
        return taPersonBuildingList;
    }
}

