<?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.huiju.estateagents.property.dao.UserMapper">
    <select id="selectByIdUserName" parameterType="int" resultType="string">
        SELECT
         user_name
        FROM
        ta_user
        WHERE user_id=#{id,jdbcType=INTEGER}
    </select>

    <select id="userList" resultType="com.huiju.estateagents.property.model.User" >
        SELECT
        tu.create_user,
        tu.create_date,
        tu.update_date,
        tu.user_name,
        tu.remark,
        tu.login_name,
        tu.status,
        tu.id
        FROM
        tp_user tu
        <where>
            <if test="loginName != null and loginName !=''" >
                and tu.login_name like concat('%',#{loginName,jdbcType=VARCHAR},'%')
            </if>
            <if test="userName != null and userName !=''" >
                and tu.user_name like concat('%',#{userName,jdbcType=VARCHAR},'%')
            </if>
            and  tu.org_id=#{orgId,jdbcType=INTEGER}
        </where>
        order by  tu.create_date desc
    </select>

    <update id="updateByPrimaryKeySelective" parameterType="com.huiju.estateagents.property.model.User" >
    update tp_user
        <set >
            <if test="userName != null" >
                user_name = #{userName,jdbcType=VARCHAR},
            </if>
            <if test="loginName != null" >
                login_name = #{loginName,jdbcType=VARCHAR},
            </if>
            <if test="loginPassword != null" >
                login_password = #{loginPassword,jdbcType=VARCHAR},
            </if>
            <if test="email != null" >
                email = #{email,jdbcType=VARCHAR},
            </if>
            <if test="headPortrait != null" >
                head_portrait = #{headPortrait,jdbcType=VARCHAR},
            </if>
            <if test="remark != null" >
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="status != null" >
                status = #{status,jdbcType=CHAR},
            </if>
            <if test="createUser != null" >
                create_user = #{createUser,jdbcType=INTEGER},
            </if>
            <if test="createDate != null" >
                create_date = #{createDate,jdbcType=TIMESTAMP},
            </if>
            <if test="updateUser != null" >
                update_user = #{updateUser,jdbcType=INTEGER},
            </if>
            <if test="updateDate != null" >
                update_date = #{updateDate,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="selectUser"  resultType="com.huiju.estateagents.property.vo.UserVO" >
        SELECT
        sr.role_name,
        sr.description,
        sr.create_user,
        sr.create_date,
        sr.update_date,
        sr.update_user,
        tu.user_name,
        tu.remark,
        tu.login_name,
        tu.status,
        tu.id,
        tp_sys_user_role.role_id
        FROM
        tp_user tu
        LEFT JOIN tp_sys_user_role ON tu.id = tp_sys_user_role.user_id
        LEFT JOIN tp_sys_role sr ON tp_sys_user_role.role_id = sr.id
        WHERE tu.id=#{id,jdbcType=INTEGER}
    </select>
    <select id="selectRole"  resultType="com.huiju.estateagents.property.vo.UserVO" parameterType="int">
        SELECT
        sr.role_name
        FROM
        tp_user tu
        LEFT JOIN tp_sys_user_role ON tu.id = tp_sys_user_role.user_id
        LEFT JOIN tp_sys_role sr ON tp_sys_user_role.role_id = sr.id
        WHERE tu.id=#{id,jdbcType=INTEGER}
    </select>

    <select id="getPermission" resultType="java.util.Map">
        SELECT
            r.id,
            r.menu_name as menuName,
            r.menu_url as menuUrl,
            r.parent_id
        FROM
            tp_sys_user_role t
            LEFT JOIN tp_sys_role_menu m ON t.role_id = m.role_id AND m.org_id = #{orgId}
            LEFT JOIN tp_sys_menu r on m.menu_id = r.id and r.status = 0
        WHERE
            t.user_id = #{userId}
            AND t.org_id = #{orgId}
        GROUP BY r.id
    </select>

    <select id="selectByPermissionList" resultType="java.util.Map">
        SELECT id,menu_name as menuName,menu_url as menuUrl,parent_id FROM tp_sys_menu WHERE id in
        (
        <foreach item="item" index="index" collection="array" separator=",">
        #{item}
        </foreach>
        )
    </select>

    <select id="getUser" resultType="com.huiju.estateagents.property.model.User">
        SELECT
        tu.create_user,
        tu.create_date,
        tu.update_date,
        tu.user_name,
        tu.remark,
        tu.login_name,
        tu.status,
        tu.id
        FROM
         tp_user tu
        WHERE   tu.org_id=#{orgId,jdbcType=INTEGER}
        and tu.id!=#{id,jdbcType=INTEGER}
    </select>
</mapper>
