<?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.goldenwater.gx.mapper.DocDirectoryMapper">

    <resultMap type="com.goldenwater.gx.domain.DocDirectory" id="DocDirectoryMap">
        <id property="id" column="ID" />
        <result property="dirName" column="DIR_NAME" />
        <result property="parentId" column="PARENT_ID" />
        <result property="orderNum" column="ORDER_NUM" />
        <result property="status" column="STATUS" />
        <result property="remark" column="REMARK" />
        <result property="createBy" column="CREATE_BY" />
        <result property="createTime" column="CREATE_TIME" />
        <result property="updateBy" column="UPDATE_BY" />
        <result property="updateTime" column="UPDATE_TIME" />
    </resultMap>

    <select id="selectDirectoryTree" resultMap="DocDirectoryMap">
        SELECT ID, DIR_NAME, PARENT_ID, ORDER_NUM, STATUS, REMARK, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME
        FROM DOC_DIRECTORY
        WHERE STATUS = '0'
        ORDER BY PARENT_ID, ORDER_NUM
    </select>

    <select id="selectDirectoryByParentId" resultMap="DocDirectoryMap">
        SELECT ID, DIR_NAME, PARENT_ID, ORDER_NUM, STATUS, REMARK, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME
        FROM DOC_DIRECTORY
        WHERE STATUS = '0'
          AND PARENT_ID = #{parentId}
        ORDER BY ORDER_NUM
    </select>

    <select id="selectDirectoryById" resultMap="DocDirectoryMap">
        SELECT ID, DIR_NAME, PARENT_ID, ORDER_NUM, STATUS, REMARK, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME
        FROM DOC_DIRECTORY
        WHERE ID = #{id}
    </select>

    <insert id="insertDirectory" parameterType="com.goldenwater.gx.domain.DocDirectory">
        INSERT INTO DOC_DIRECTORY (DIR_NAME, PARENT_ID, ORDER_NUM, STATUS, REMARK, CREATE_BY, CREATE_TIME)
        VALUES (#{dirName}, #{parentId}, #{orderNum}, #{status}, #{remark}, #{createBy}, SYSDATE)
    </insert>

    <update id="updateDirectory" parameterType="com.goldenwater.gx.domain.DocDirectory">
        UPDATE DOC_DIRECTORY
        SET DIR_NAME = #{dirName},
            PARENT_ID = #{parentId},
            ORDER_NUM = #{orderNum},
            STATUS = #{status},
            REMARK = #{remark},
            UPDATE_BY = #{updateBy},
            UPDATE_TIME = SYSDATE
        WHERE ID = #{id}
    </update>

    <delete id="deleteDirectoryById" parameterType="Long">
        DELETE FROM DOC_DIRECTORY WHERE ID = #{id}
    </delete>

    <select id="countChildByParentId" resultType="int">
        SELECT COUNT(*) FROM DOC_DIRECTORY WHERE PARENT_ID = #{parentId} AND STATUS = '0'
    </select>
</mapper>
