<?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.ytpm.middle.dao.MiddleDeptMapper">
    <insert id="insertOne">
        insert into yt_middle_dept
        (
         parent_id,
         dept_name,
         `describe`,
         sort,
         create_time,
         create_user_id,
         available
        )
        values
        (
         #{parentId},
         #{deptName},
         #{describe},
         #{sort},
         #{createTime},
         #{createUserId},
         #{available}
        )
    </insert>
    <update id="updateById">
        update yt_middle_dept
        <set>
            <if test=" parentId!=null" >
                parent_id = #{parentId},
            </if>
            <if test=" deptName!=null" >
                dept_name = #{deptName},
            </if>
            <if test=" describe!=null" >
                `describe` = #{describe},
            </if>
            <if test="sort !=null" >
                sort = #{sort},
            </if>
            <if test="updateTime !=null" >
                update_time = #{updateTime},
            </if>
            <if test="updateUserId !=null" >
                update_user_id = #{updateUserId},
            </if>
            <if test="available !=null" >
                available = #{available}
            </if>
        </set>
        where dept_id = #{deptId}
    </update>

    <update id="updateChildren">
        update yt_middle_dept set available = 0, update_time = now(), update_user_id = #{operatorId} where parent_id = #{deptId}
    </update>

    <select id="queryList" resultType="com.ytpm.middle.view.MiddleDeptVO">
        select
            dept_id, parent_id, dept_name, `describe`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_dept
        <where>
            <if test="deptName != null and deptName != ''">
                and dept_name like concat('%',#{deptName},'%')
            </if>
        </where>
        order by sort desc
    </select>
    <select id="getAllDeptList" resultType="com.ytpm.middle.view.DropDownVO">
        select
            dept_id optionVal, dept_name optionName, parent_id
        from yt_middle_dept
        where available = 1
    </select>
    <select id="selectByName" resultType="com.ytpm.middle.model.YtMiddleDept">
        select
            dept_id, parent_id, dept_name, `describe`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_dept
        where available = 1 and dept_name = #{deptName}
    </select>
    <select id="selectPrimary" resultType="com.ytpm.middle.model.YtMiddleDept">
        select
            dept_id, parent_id, dept_name, `describe`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_dept
        where dept_id = #{deptId}
    </select>
    <select id="selectByParentId" resultType="com.ytpm.middle.model.YtMiddleDept">
        select
            dept_id, parent_id, dept_name, `describe`, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_dept
        where available = 1 and parent_id = #{parentId}
    </select>
</mapper>