<?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.ruoyi.dz.mapper.DzAgentMapper">

    <resultMap type="DzAgent" id="DzAgentResult">
        <result property="agentId"    column="agent_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="name"    column="name"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="parentId"    column="parent_id"    />
        <result property="schools"    column="schools"    />
        <result property="remark"    column="remark"    />
        <association property="dept"     javaType="SysDept"         resultMap="deptResult" />
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id     property="deptId"    column="dept_id"     />
        <result property="parentId"  column="parent_id"   />
        <result property="deptName"  column="dept_name"   />
        <result property="ancestors" column="ancestors"   />
        <result property="orderNum"  column="order_num"   />
        <result property="leader"    column="leader"      />
        <result property="status"    column="dept_status" />
    </resultMap>

    <sql id="selectDzAgentVo">
        select t1.agent_id, t1.dept_id, t1.user_id, t1.name, t1.phonenumber, t1.parent_id, t1.schools, t1.remark,
        d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.status as dept_status
        from dz_agent t1
        left join sys_dept d on t1.dept_id = d.dept_id
    </sql>

    <select id="selectDzAgentList" parameterType="DzAgent" resultMap="DzAgentResult">
        <include refid="selectDzAgentVo"/>
        <where>
            <if test="agentId != null "> and t1.agent_id = #{agentId}</if>
            <if test="userId != null "> and t1.user_id = #{userId}</if>
<!--            <if test="deptId != null "> and t1.dept_id = #{deptId}</if>-->
            <if test="deptId != null ">
                AND (t1.dept_id = #{deptId} OR t1.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
            </if>
            <if test="teacherId != null ">
                AND t1.agent_id in (select distinct t.agent_id  from `dz_teacher` t where t.`teacher_id` = 1 and t.agent_id is not null)
            </if>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
            <if test="phonenumber != null  and phonenumber != ''"> and t1.phonenumber  like concat('%', #{phonenumber}, '%')</if>
            <if test="parentId != null "> and t1.parent_id = #{parentId}</if>
            <if test="schools != null  and schools != ''"> and t1.schools = #{schools}</if>
        </where>
    </select>

    <select id="selectDzAgentByAgentId" parameterType="Long" resultMap="DzAgentResult">
        <include refid="selectDzAgentVo"/>
        where t1.agent_id = #{agentId}
    </select>

    <select id="selectDzAgentByUserId" parameterType="Long" resultMap="DzAgentResult">
        <include refid="selectDzAgentVo"/>
        where t1.user_id = #{userId}
    </select>

    <select id="selectDzAgentByAgentIds" resultMap="DzAgentResult">
        <include refid="selectDzAgentVo"/>
        where t1.agent_id in <foreach collection="ids" item="id" open="(" separator="," close=")">#{id}</foreach>
    </select>

    <select id="selectDzAgentByUserIds" resultMap="DzAgentResult">
        <include refid="selectDzAgentVo"/>
        where t1.user_id in <foreach collection="userIds" item="userId" open="(" separator="," close=")">#{userId}</foreach>
    </select>

    <insert id="insertDzAgent" parameterType="DzAgent" useGeneratedKeys="true" keyProperty="agentId">
        insert into dz_agent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="name != null">name,</if>
            <if test="phonenumber != null">phonenumber,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="schools != null">schools,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="name != null">#{name},</if>
            <if test="phonenumber != null">#{phonenumber},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="schools != null">#{schools},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateDzAgent" parameterType="DzAgent">
        update dz_agent
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="schools != null">schools = #{schools},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where agent_id = #{agentId}
    </update>

    <delete id="deleteDzAgentByAgentId" parameterType="Long">
        delete from dz_agent where agent_id = #{agentId}
    </delete>

    <delete id="deleteDzAgentByAgentIds" parameterType="String">
        delete from dz_agent where agent_id in
        <foreach item="agentId" collection="array" open="(" separator="," close=")">
            #{agentId}
        </foreach>
    </delete>
</mapper>
