package com.xmomen.module.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.base.entity.CdCompany;
import com.xmomen.module.base.mapper.PublicMapper;
import com.xmomen.module.base.model.CompanyCustomerManager;

@RestController
public class PublicController {

    //注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    //注入PublicMapper
    @Autowired
    PublicMapper publicMapper;

    //获取公司列表
    @RequestMapping(value = "/companyList", method = RequestMethod.GET)
    public List<CdCompany> getCompany() {
        //创建CdCompany对象
        CdCompany company = new CdCompany();
        //根据CdCompany对象查询公司列表
        List<CdCompany> companys = mybatisDao.selectByModel(company);
        //返回公司列表
        return companys;
    }

    //查询客服经理
    @RequestMapping(value = "/customerManagerList", method = RequestMethod.GET)
    public List<CompanyCustomerManager> getCustomerManager(
            //用户类型
            @RequestParam(value = "userType", required = false) String userType,
            //关键字
            @RequestParam(value = "keyword", required = false) String keyword) {
        //创建Map对象
        Map map = new HashMap<String, Object>();
        //将用户类型放入Map中
        map.put("userType", userType);
        //如果用户类型为客服经理
        if ("customer_manager".equals(userType)) {
            //客服经理过滤 如果有客服组权限则不过滤
            //判断当前用户是否有客服组权限
            if (SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_MANAGER_PERMISSION_CODE)
                    //判断当前用户是否有客服权限
                    && !SecurityUtils.getSubject().hasRole(AppConstants.CUSTOMER_PERMISSION_CODE)
                    //判断当前用户是否有后台权限
                    && !SecurityUtils.getSubject().hasRole(AppConstants.HOU_TAI_CODE)
                    //判断当前用户是否有管理员权限
                    && !SecurityUtils.getSubject().hasRole(AppConstants.ADMIN)
                    //判断当前用户是否有超级管理员权限
                    && !SecurityUtils.getSubject().hasRole(AppConstants.SUPER_ADMIN)
                    //判断当前用户是否有物流中心权限
                    && !SecurityUtils.getSubject().hasRole(AppConstants.WULIUZXB)) {
                //获取当前用户ID
                Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
                //将当前用户ID放入Map中
                map.put("managerId", userId);
            }
        }
        //如果关键字不为空
        if (StringUtils.trimToNull(keyword) != null) {
            //将关键字放入Map中
            map.put("keyword", StringUtils.trimToEmpty(keyword));
        }
        //根据Map查询客服经理列表
        List<CompanyCustomerManager> customerManagerList = mybatisDao.getSqlSessionTemplate().selectList(PublicMapper.PublicMapperNameSpace + "getManagerList", map);
        //返回客服经理列表
        return customerManagerList;
    }
}
