<?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.ezhizao.project.system.mapper.SysDeptUserMapper">

    <resultMap type="SysDeptUser" id="SysDeptUserResult">
        <id property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <result property="userId" column="user_id"/>
        <association property="user" javaType="SysUser" column="user_id" select="getUser"/>
        <association property="dept" javaType="SysDept" column="dept_id" select="getDept"/>
    </resultMap>
    <resultMap id="SysUserResult" type="cn.ezhizao.project.system.domain.SysUser">
        <id property="userId" column="user_id"/>
    </resultMap>

    <resultMap id="SysDeptResult" type="cn.ezhizao.project.system.domain.SysDept">
        <id property="deptId" column="dept_id"/>
    </resultMap>
    <select id="getUser" resultMap="SysUserResult">
        select * from sys_user where user_id = #{userId}
    </select>
    <select id="getDept" resultMap="SysDeptResult">
        select * from sys_dept where dept_id = #{deptId}
    </select>
    <select id="getList" resultMap="SysDeptUserResult">
        select * from sys_dept_user where deleted = 0 and is_temporary = 0
        <if test="deptId!= null and deptId!= ''">
            and dept_id = #{deptId}
        </if>
        <if test="userId!= null and userId!= ''">
            and user_id = #{userId}
        </if>
        <if test="isFormman!= null and isFormman!= ''">
            and is_formman = #{isFormman}
        </if>
        <if test="isYardman!= null and isYardman!= ''">
            and is_yardman = #{isYardman}
        </if>
        <if test="deptName!= null and deptName!= ''">
            and dept_id in (select dept_id from sys_dept where dept_name like concat('%', #{deptName}, '%'))
        </if>
        <if test="nickName!= null and nickName!= ''">
            and user_id in (select user_id from sys_user where nick_name like concat('%', #{nickName}, '%'))
        </if>
        <if test="userName!= null and userName!= ''">
            and user_id in (select user_id from sys_user where user_name like concat('%', #{userName}, '%'))
        </if>
        <if test="tenantId != null  and tenantId != 0"> AND tenant_id = #{tenantId}</if>
    </select>
    <select id="getUserInfoByDeptId" resultType="cn.ezhizao.project.system.domain.SysUser">
        select t1.* from sys_user t1 left join sys_dept_user t2 on t1.user_id = t2.user_id
        where t1.del_flag = 0 and t2.deleted = 0 and t2.dept_id = #{deptId}
    </select>
    <select id="selectInnerUserByDeptId" resultType="cn.ezhizao.project.system.domain.SysUser">
        SELECT distinct
        t2.user_id,
        t2.user_name,
        t2.nick_name,
        t2.sex,
        t2.phone_number,
        t2.usage_company_name,
        t2.remark
        FROM
        `sys_dept_user` t1
        LEFT JOIN sys_user t2 on t1.user_id = t2.user_id

        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.dept_id = #{deptId} AND t1.deleted = 0 and t1.is_temporary = 0
        <if test="nickName!=null "> AND t2.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="userName!=null "> AND t2.user_name like concat('%', #{userName}, '%')</if>
        </trim>
    </select>
    <update id="deleteByDeptIdAndUserId" parameterType="SysDeptUser">
        update sys_dept_user
        set deleted = 1
        where user_id = #{userId}
          and dept_id = #{deptId}
    </update>

    <update id="updateDeptUser" parameterType="SysDeptUser">
        update sys_dept_user
        set is_formman = #{isFormman},
            is_yardman = #{isYardman},
            is_leader = #{isLeader},
            is_dispatcher = #{isDispatcher}
        where  user_id = #{userId} and
               dept_id = #{deptId}
    </update>
</mapper>
