package com.nokia.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nokia.pojo.Role;
import com.nokia.pojo.User;
import com.nokia.vo.flow.FlowRoleVo;
import com.nokia.vo.flow.FlowUserVo;

import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserDao extends BaseMapper<User> {

        /**
         * 支持username模糊查询
         */
        @Select("select distinct r.role_id, r.role_name from sqmdb_rpt.acl_user u, sqmdb_rpt.acl_role r, sqmdb_rpt.acl_user_role_city ur"
                        + " where u.user_id = ur.user_id and r.role_id = ur.role_id and r.system='flow' and u.user_name like concat('%',#{username},'%')")
        List<FlowRoleVo> findRoleList(String username);

        /**
         * 查询全部
         */
        @Select("select role_id, role_name from sqmdb_rpt.acl_role where system = 'flow'")
        List<FlowRoleVo> findRoleList2();

        /**
         * 查询全部用户（流程所需），未分页
         */
        @Results({
                        @Result(column = "org_id", property = "org.orgId"),
                        @Result(column = "org_name", property = "org.orgName"),
        })
        @Select("select a.user_id as user_id, a.login_name as login_id, a.user_name as user_name, a.phone as mobile, a.email as email,"
                        + " a.city_id as org_id, c1.area_name as org_name, a.area_id as area"
                        + " from sqmdb_rpt.acl_user a, sqmdb_rpt.acl_area c1"
                        + " where a.city_id = c1.area_id")
        List<FlowUserVo> findToPage();

        /**
         * 查询登录用户信息
         */
        @Results({
                        @Result(column = "org_id", property = "org.orgId"),
                        @Result(column = "org_name", property = "org.orgName"),
        })
        @Select("select a.user_id as user_id, a.login_name as login_id, a.user_name as user_name, a.phone as mobile, a.email as email,"
                        + " a.city_id as org_id, c.area_name as org_name, a.area_id as area"
                        + " from sqmdb_rpt.acl_user a, sqmdb_rpt.acl_area c"
                        + " where a.city_id = c.area_id and a.login_name=#{loginId}")
        FlowUserVo getFlowUserVoByLoginId(String loginId);

        /**
         * 根据用户ID获取用户接口
         */
        @Results({
                        @Result(column = "org_id", property = "org.orgId"),
                        @Result(column = "org_name", property = "org.orgName"),
        })
        @Select("select a.user_id as user_id, a.login_name as login_id, a.user_name as user_name, a.phone as mobile, a.email as email,"
                        + " a.city_id as org_id, c.area_name as org_name, a.area_id as area"
                        + " from sqmdb_rpt.acl_user a, sqmdb_rpt.acl_area c"
                        + " where a.city_id = c.area_id and user_id=#{userId}")
        FlowUserVo getFlowUserVoByUserId(Integer userId);

        @Select("select r.role_id from  sqmdb_rpt.acl_role r,  sqmdb_rpt.acl_user_role_city ur where r.role_id = ur.role_id and r.system='flow' and ur.user_id=#{userId}")
        List<Integer> findRoleIdByUserId(Integer userId);

        @Select("select u.user_id from sqmdb_rpt.acl_user u,  sqmdb_rpt.acl_user_role_city ur where u.user_id = ur.user_id and ur.role_id=#{roleId}")
        List<Integer> findUserIdByRoleId(Integer roleId);

        @Results({
                        @Result(column = "org_id", property = "org.orgId"),
                        @Result(column = "org_name", property = "org.orgName"),
        })
        @Select("<script>"
                        + "select a.user_id as user_id, a.login_name as login_id, a.user_name as user_name, a.phone as mobile, a.email as email,"
                        + " a.city_id as org_id, c.area_name as org_name, a.area_id as area"
                        + " from sqmdb_rpt.acl_user a, sqmdb_rpt.acl_area c, sqmdb_rpt.acl_user_role_city ur "
                        + " where a.user_id = ur.user_id and a.city_id = c.area_id"
                        + "<if test=\"loginId != null and loginId !=''\">"
                        + " and a.login_name=#{loginId} "
                        + "</if>"
                        + "<if test=\"roleId != null\">"
                        + " and ur.role_id=#{roleId} "
                        + "</if>"
                        + "<if test=\"userName != null and userName !=''\">"
                        + " and a.user_name like concat('%',#{userName},'%') "
                        + "</if>"
                        + "</script>")
        List<FlowUserVo> findAuthorizedUser(String loginId, Integer roleId, String userName);

        List<Role> getRoleCityByUserId(Integer userId);

        User getByLoginName(String loginName);

        @Select("select user_id from sqmdb_rpt.acl_user au where au.login_name = #{loginName}")
        Integer hasLoginName(String loginName);

        List<User> getByRoleIds(List<Integer> roleIds, List<Integer> cityIds);
}
