<?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.cool.store.mapper.UserAuthMappingMapper" >
    <resultMap id="baseResult" type="com.cool.store.entity.UserAuthMappingDO">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="mappingId" column="mapping_id"/>
        <result property="type" column="type"/>
        <result property="source" column="source"/>
        <result property="createId" column="create_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateId" column="update_id"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="listUserAuthMappingByUserId" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        where user_id=#{userId}
    </select>
    <select id="listUserAuthMappingByUserIdList" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        <where>
            <if test="userList!=null and userList.size>0">
                <foreach collection="userList" item="userId" open="user_id in (" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listUserAuthMappingByMappingList" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        <where>
            <if test="mappingIdList!=null and mappingIdList.size>0">
                <foreach collection="mappingIdList" item="mappingId" open="mapping_id in (" separator="," close=")">
                    #{mappingId}
                </foreach>
            </if>
            <if test="type!=null and type!=''">
                and type =#{type}
            </if>
        </where>
    </select>

    <select id="listUserAuthMappingByAuth" resultMap="baseResult">
        select a.id,a.user_id,mapping_id,a.type from user_auth_mapping_${enterpriseId} a
        join enterprise_user_${enterpriseId} e on a.user_id= e.user_id
        JOIN enterprise_user_role_${enterpriseId} ur on e.user_id =ur.user_id
        JOIN sys_role_${enterpriseId} b on ur.role_id=b.id
          <where>
             e.user_status= '1' and e.active= true
            <if test="mappingIdList!=null and mappingIdList.size>0">
                <foreach collection="mappingIdList" item="mappingId" open="and a.mapping_id in (" separator="," close=")">
                    #{mappingId}
                </foreach>
            </if>
            <if test="positionType!=null and positionType!=''">
                and b.position_type =#{positionType}
            </if>
              <if test="notRoleAuth!=null and notRoleAuth!=''">
                  and b.role_auth !=#{notRoleAuth}
              </if>
              <if test="type!=null and type!=''">
                  and a.type =#{type}
              </if>


        </where>
    </select>
    <select id="listUserAuthMappingByUserList" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        <where>
            <if test="userIdList!=null and userIdList.size>0">
                <foreach collection="userIdList" item="userId" open="user_id in (" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listUserAuthMappingByUserAndType" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        <where>
           <if test="userId!=null and userId !=''">
               and user_id=#{userId}
           </if>
            <if test="type!=null and type!=''">
                and type =#{type}
            </if>
        </where>
    </select>

    <select id="selectIdsByUserId" resultType="java.lang.Long">
        select id from user_auth_mapping_${enterpriseId} where user_id = #{userId}
    </select>

    <select id="selectIdsByUserIds" resultType="java.lang.Long">
        select id from user_auth_mapping_${enterpriseId} where user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </select>
    <select id="listByUserIdAndSource" resultMap="baseResult">
        select *
        from user_auth_mapping_${enterpriseId}
        where user_id = #{userId}
          and source = #{source}
    </select>

    <select id="getUserAuthByUserIdAndMappingId" resultMap="baseResult">
        select * from user_auth_mapping_${enterpriseId}
        where user_id = #{userId} and mapping_id =#{mappingId} and type =#{type}
    </select>

    <select id="listStoreAuthByEid" resultMap="baseResult">
        select * FROM
        user_auth_mapping_${enterpriseId}
        WHERE  type = 'store'
        order by id asc
    </select>

    <select id="getAllAuthMapping" resultMap="baseResult">
        select *
        from user_auth_mapping_${enterpriseId}
    </select>

    <select id="getUserIdsByMappingIds" resultType="string">
        select
            user_id
        from
            user_auth_mapping_${enterpriseId}
        where
            mapping_id in
        <foreach collection="mappingIdList" item="mappingId" open="(" separator="," close=")">
            #{mappingId}
        </foreach>
    </select>

    <select id="getUserAuthByMappingIds" resultMap="baseResult">
        select
            user_id,
            mapping_id
        from
            user_auth_mapping_${enterpriseId}
        where
            mapping_id in
            <foreach collection="mappingIdList" item="mappingId" open="(" separator="," close=")">
                #{mappingId}
            </foreach>
    </select>

    <select id="getMappingIdsByUserId" resultType="string">
        select
            mapping_id
        from
            user_auth_mapping_${enterpriseId}
        where
            user_id = #{userId}
    </select>
    <select id="getRegionIdByUserId" resultType="java.lang.String">
        select mapping_id
        from user_auth_mapping_${enterpriseId}
        where user_id = #{userId}
    </select>

    <select id="listByUserIdListAndSource" resultMap="baseResult">
        select *
        from user_auth_mapping_${enterpriseId}
        where `source` =#{source}
        and user_id in
        <foreach collection="userIdList" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </select>
    <select id="getAllByUserIds" resultMap="baseResult">
        select *
        from user_auth_mapping_${enterpriseId}
        <where>
            user_id in
            <foreach collection="userIdsByRoleIdList" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </where>

    </select>

</mapper>