<?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="cn.cordys.crm.system.mapper.ExtUserRoleMapper">

    <delete id="deleteUserRoleByUserId">
        DELETE
        FROM sys_user_role
        WHERE user_id = #{userId}
    </delete>

    <delete id="deleteByIds">
        DELETE
        FROM sys_user_role
        WHERE id in
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteUserRoleByUserIds">
        DELETE
        FROM sys_user_role
        WHERE user_id in
        <foreach collection="userIds" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </delete>

    <select id="list" resultType="cn.cordys.crm.system.dto.response.RoleUserListResponse">
        select sur.id, su.id as userId, su.name as userName, sou.enable, sou.position, sou.department_id, sur.create_time, sur.update_time
        from sys_user_role sur
        join sys_user su
             on sur.user_id = su.id and sur.role_id = #{request.roleId}
        join sys_organization_user sou
             on su.id = sou.user_id and sou.organization_id = #{orgId}
        <where>
            1=1
            <if test="request.keyword != null and request.keyword != ''">
                and su.name like concat('%', #{request.keyword},'%')
            </if>

            <include refid="filters">
                <property name="filters" value="request.filters"/>
            </include>
        </where>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <sql id="filters">
        <if test="${filters} != null and ${filters}.size() > 0">
            <trim prefix="and">
                <foreach collection="${filters}" item="condition" open="(" close=")" index="i" separator="and">
                    <choose>
                        <when test="condition.name == 'enable'">
                            <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                                <property name="condition" value="condition"/>
                                <property name="column" value="sou.enable"/>
                            </include>
                        </when>
                    </choose>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type" />
                order by
                <choose>
                    <when test="${sort}.name == 'name'">
                        su.`name` ${sortType}
                    </when>
                    <when test="${sort}.name == 'department_id'">
                        sou.`department_id` ${sortType}
                    </when>
                    <when test="${sort}.name == 'position'">
                        sou.`position` ${sortType}
                    </when>
                    <when test="${sort}.name == 'enable'">
                        sou.`enable` ${sortType}
                    </when>
                    <when test="${sort}.name == 'user_name'">
                        su.`name` ${sortType}
                    </when>
                    <otherwise>
                        sur.create_time ${sortType}
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by sur.create_time desc, sur.id asc
            </otherwise>
        </choose>
    </sql>

    <select id="selectUserDeptForRelevance" resultType="cn.cordys.common.dto.DeptUserTreeNode">
        select temp.id, temp.name, temp.department_id as parentId, if(sur.id, 0, 1) as enable, 'USER' as nodeType
        from
        (
            select su.id, su.name, sou.department_id from sys_organization_user sou
            join sys_user su
            on sou.organization_id = #{orgId} and sou.user_id = su.id
        ) as temp
        left join sys_user_role sur
        on temp.id = sur.user_id and sur.role_id = #{roleId}
    </select>

    <select id="selectUserDeptForOrg" resultType="cn.cordys.common.dto.DeptUserTreeNode">
        select su.id, su.name, sou.department_id as parentId, 'USER' as nodeType from sys_organization_user sou
        join sys_user su on sou.organization_id = #{orgId} and sou.user_id = su.id and sou.enable = true
    </select>

    <select id="selectUserRoleForRelevance" resultType="cn.cordys.common.dto.RoleUserTreeNode">
        select su.id, su.name, sur.role_id as parentId, 1 as enable, 'USER' as nodeType
        from sys_organization_user sou
        join sys_user su
            on sou.organization_id = #{orgId} and sou.user_id = su.id
        join sys_user_role sur
            on su.id = sur.user_id and sur.role_id != #{roleId}
    </select>

    <select id="selectUserRoleForOrg" resultType="cn.cordys.common.dto.RoleUserTreeNode">
        select su.id, su.name, sur.role_id as parentId, 1 as enable, 'USER' as nodeType
        from sys_organization_user sou
                 join sys_user su
                      on sou.organization_id = #{orgId} and sou.user_id = su.id
                 join sys_user_role sur
                      on su.id = sur.user_id
    </select>

    <select id="getUserIdsByRoleIds" resultType="java.lang.String">
        select user_id
        from sys_user_role
        where role_id in
        <foreach collection="roleIds" item="roleId" open="(" close=")" separator=",">
            #{roleId}
        </foreach>
    </select>

    <select id="selectUserOption" resultType="cn.cordys.crm.system.dto.response.RoleUserOptionResponse">
        select su.id, su.name, sue.avatar
        from sys_user su
        join sys_organization_user sou
            on su.id = sou.user_id and sou.organization_id = #{orgId} and sou.enable = true
        left join sys_user_extend sue
            on su.id = sue.id
    </select>

    <select id="selectUserOptionByRoleId" resultType="cn.cordys.common.dto.OptionDTO">
        select su.id, su.name
        from sys_user su
        join sys_organization_user sou
            on su.id = sou.user_id and sou.organization_id = #{orgId}  and sou.enable = true
        join sys_user_role sur
            on su.id = sur.user_id and sur.role_id = #{roleId}
    </select>


    <select id="selectUserRole" resultType="java.lang.String">
        SELECT
            sys_user_role.id
        FROM
            sys_user_role
                INNER JOIN sys_role ON sys_user_role.role_id = sys_role.id
        where sys_user_role.user_id = #{userId}
          and sys_role.organization_id = #{orgId}
    </select>

    <select id="selectPermissionsByUserId" resultType="java.lang.String">
        SELECT sys_role_permission.permission_id
        from sys_role_permission
        where sys_role_permission.role_id in (SELECT sys_user_role.role_id
                                              from sys_user_role
                                              where sys_user_role.user_id = #{userId})
    </select>

</mapper>
