<?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.judong.chuanyiserver.dao.DepartmentDao">

    <insert id="addDepartment">
        insert into sys_department (parent_id, department_name, department_describe, department_num, create_time)
            VALUE (#{parentId}, #{departmentName}, #{departmentDescribe}, #{departmentNum}, now())
    </insert>
    <update id="updateDepartment">
        update sys_department
        set parent_id=#{parentId},
            department_name=#{departmentName},
            department_describe=#{departmentDescribe},
            department_num=#{departmentNum},
            update_time=now()
        where id = #{id}
    </update>

    <select id="getDepartmentByName" resultType="com.judong.chuanyiserver.dao.DepartmentDao">
        select id, parent_id, department_name, department_describe, department_num, create_time, update_time
        from sys_department
        where department_name = #{departmentName}
    </select>

    <select id="getDepartmentByNoIdName" resultType="com.judong.chuanyiserver.entity.Department">
        select id, parent_id, department_name, department_describe, department_num, create_time, update_time
        from sys_department
        where id != #{id}
          and department_name = #{departmentName}
    </select>

    <select id="getDepartmentCount" resultType="java.lang.Long">
        select count(*)
        from sys_department
    </select>

    <select id="getDepartmentListByPage" resultType="com.judong.chuanyiserver.entity.Department">
        select id, parent_id, department_name, department_describe, department_num, create_time, update_time
        from sys_department
        limit #{startNum},#{limitNum}
    </select>

    <select id="getDepartmentById" resultType="com.judong.chuanyiserver.entity.Department">
        select id, parent_id, department_name, department_describe, department_num, create_time, update_time
        from sys_department
        where id = #{id}
    </select>

    <!--根据父id查子数据-->
    <select id="queryDepartmentListByparentId" resultType="com.judong.chuanyiserver.entity.Department">
        SELECT id, parent_id, department_name, department_describe, department_num, create_time, update_time
        FROM sys_department WHERE parent_id=#{parentId} and is_delete !=1
    </select>

    <!--根据id查自己是哪些的父亲-->
    <select id="queryDepartmentListByid" resultType="com.judong.chuanyiserver.entity.Department">
        SELECT * FROM sys_department WHERE parent_id=#{id}
    </select>

    <!--逻辑删除-->
    <update id="isDeleteByid">
        UPDATE sys_department SET is_delete=1 WHERE id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </update>


</mapper>