<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nokia.dao.UserDao">
    <resultMap id="UserResultMap" type="com.nokia.pojo.User">
        <id column="user_id" property="userId"></id>
        <result column="login_name" property="loginName" />
        <result column="user_name" property="userName" />
        <result column="phone" property="phone" />
        <result column="email" property="email" />
        <result column="province_id" property="provinceId" />
        <result column="city_id" property="cityId" />
        <result column="area_id" property="areaId" />
        <association column="province_id" property="province">
            <id column="province_id" property="areaId"></id>
            <result column="province_Name" property="areaName" />
        </association>
        <association column="city_id" property="city">
            <id column="city_id" property="areaId"></id>
            <result column="city_name" property="areaName" />
            <result column="type_code" property="typeCode" />
            <result column="city_parent_id" property="parentId" />
        </association>
        <association column="area_id" property="area">
            <id column="area_id" property="areaId"></id>
            <result column="area_name" property="areaName" />
            <result column="area_type_code" property="typeCode" />
            <result column="area_parent_id" property="parentId" />
        </association>
        <collection property="roles" resultMap="RoleResultMap" />
    </resultMap>

    <resultMap id="RoleResultMap" type="com.nokia.pojo.Role">
        <id column="role_id" property="roleId"></id>
        <result column="role_name" property="roleName" />
        <result column="system" property="system" />
        <collection property="cities" ofType="com.nokia.pojo.Area">
            <result column="role_city_id" property="areaId" />
            <result column="role_area_name" property="areaName" />
        </collection>
    </resultMap>

    <select id="getRoleCityByUserId" resultMap="RoleResultMap">
        select ar.role_id, ar.role_name, ar."system", aurc.city_id as role_city_id, ac.area_name as role_area_name 
            from sqmdb_rpt.acl_role ar, sqmdb_rpt.acl_user_role_city aurc,  sqmdb_rpt.acl_area ac
            where ar.role_id = aurc.role_id and aurc.city_id = ac.area_id  and aurc.user_id = #{userId}
    </select>

    <select id="getByLoginName" resultMap="UserResultMap">
        select tmp.*, ac4.area_name, ac4.parent_id as area_parent_id
            from ( select au.user_id, au.login_name, au.user_name, au.phone, au.email, au.province_id,
                ac.area_name as province_name, au.city_id, ac2.area_name as city_name, ac2.parent_id as city_parent_id,
                ac2.type_code, au.area_id, ar.role_id, ar.role_name, ar."system",
                aurc.city_id as role_city_id, ac3.area_name as role_area_name
            from
                sqmdb_rpt.acl_user au, sqmdb_rpt.acl_role ar,
                sqmdb_rpt.acl_user_role_city aurc, sqmdb_rpt.acl_area ac,
                sqmdb_rpt.acl_area ac2, sqmdb_rpt.acl_area ac3
            where
                au.user_id = aurc.user_id and ar.role_id = aurc.role_id
                and au.province_id = ac.area_id and au.city_id = ac2.area_id
                and aurc.city_id = ac3.area_id ) tmp
            left join sqmdb_rpt.acl_area ac4
            on tmp.area_id = ac4.area_id
        where 1=1
            and tmp.login_name = #{loginName}
    </select>

    <select id="getByRoleIds" resultMap="UserResultMap">
        select tmp.*, ac4.area_name, ac4.type_code as area_type_code, ac4.parent_id as area_parent_id
            from ( select au.user_id, au.login_name, au.user_name, au.phone, au.email, au.province_id,
                ac.area_name as province_name, au.city_id, ac2.area_name as city_name, ac2.parent_id as city_parent_id,
                ac2.type_code, au.area_id, ar.role_id, ar.role_name, ar."system",
                aurc.city_id as role_city_id, ac3.area_name as role_area_name
            from
                sqmdb_rpt.acl_user au, sqmdb_rpt.acl_role ar,
                sqmdb_rpt.acl_user_role_city aurc, sqmdb_rpt.acl_area ac,
                sqmdb_rpt.acl_area ac2, sqmdb_rpt.acl_area ac3
            where
                au.user_id = aurc.user_id and ar.role_id = aurc.role_id
                and au.province_id = ac.area_id and au.city_id = ac2.area_id
                and aurc.city_id = ac3.area_id ) tmp
            left join sqmdb_rpt.acl_area ac4
            on tmp.area_id = ac4.area_id
        where tmp.user_id != 1
        <if test="roleIds != null">
            and tmp.role_id in
            <foreach collection="roleIds" item="item" open="(" separator="," close=")" index="index">
                #{item}
            </foreach>
        </if>
        <if test="cityIds != null">
            and tmp.role_city_id in
            <foreach collection="cityIds" item="item" open="(" separator="," close=")" index="index">
                #{item}
            </foreach>
        </if>
    </select>
</mapper>