<?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.ho.user.mapper.SysDeptMapper">

    <sql id="Base_Column_List">
        id
        , dept_no, `name`, pid, `status`, relation_code, dept_manager_id, manager_name,
    phone, create_time, update_time, deleted,group_id
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        where id = #{id}
    </select>

    <select id="selectByPid" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        where id = #{id}
        and deleted=1
    </select>


    <select id="selectAll" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        <where>
            <if test="enableStatus != null ">
                and status = 1
            </if>
            <if test="query.status != null">
                and `status` = #{query.status}
            </if>
            <if test="query.name != null">
                and `name` LIKE CONCAT('%',#{query.name},'%')
            </if>
            and deleted=1
        </where>

    </select>

    <select id="selectBelow" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        where pid = #{id}
        and deleted=1
    </select>

    <select id="selectDeptsByPid" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        <where>
            <if test="status != null ">
                and status = 1
            </if>
            <if test="ids != null and ids.size() != 0">
                and pid in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            and deleted=1
        </where>

    </select>


    <select id="selectChildIds" resultType="java.lang.Integer">
        select id
        from sys_dept
        where relation_code LIKE CONCAT(#{relationCode}, '%')
          and deleted = 1
    </select>

    <select id="selectEnableByRelationCode" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        <where>
            <if test="enableStatus != null ">
                and status = 1
            </if>
            <if test="relationCode != null">
                and relation_code LIKE CONCAT(#{relationCode},'%')
            </if>
            and deleted=1
        </where>


    </select>

    <select id="selectAllByIds" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"/>
        from sys_dept
        where relation_code LIKE CONCAT(#{relationCode},'%') and deleted=1
    </select>


    <select id="selectChildByIds" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        <where>
            deleted=1
            <if test="list != null and list.size() != 0">
                and id in
                <foreach collection="list" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="enableStatus != null ">
                and status = 1
            </if>
            <if test="query.name != null">
                and `name` LIKE CONCAT('%',#{query.name},'%')
            </if>
            <if test="query.status != null">
                and `status` = #{query.status}
            </if>
        </where>
    </select>


    <select id="selectAllByNotContainChild" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        <where>
            deleted=1
            and id not in
            <foreach collection="list" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
            <if test="enableStatus != null ">
                and status = 1
            </if>
            <if test="query.status != null">
                and `status` = #{query.status}
            </if>
            <if test="query.name != null">
                and `name` LIKE CONCAT(#{query.name},'%')
            </if>
        </where>
    </select>

    <select id="selectByPidAndName" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        <where>
            <if test="name != null">
                and `name` =#{name}
            </if>
            <if test="pid != null">
                and pid =#{pid}
            </if>
            <if test="id != null">
                and id !=#{id}
            </if>
            and deleted=1
        </where>
    </select>

    <select id="selectByIdAndStatus" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        where id = #{deptId}
          and deleted = 1
    </select>


    <select id="selectByGroup" resultType="com.ho.user.api.entity.SysDept">
        select
        <include refid="Base_Column_List"></include>
        from sys_dept
        where group_id = #{groupId}
        and deleted = 1
    </select>


    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from sys_dept
        where id = #{id}
    </delete>

    <insert id="insert" parameterType="com.ho.user.api.entity.SysDept">
        insert into sys_dept (id, dept_no, `name`,
                              pid, `status`, relation_code,
                              dept_manager_id, manager_name, phone,
                              create_time, update_time, deleted,group_id)
        values (#{id,jdbcType=INTEGER}, #{deptNo,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
                #{pid,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{relationCode,jdbcType=VARCHAR},
                #{deptManagerId,jdbcType=VARCHAR},
                #{managerName,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{deleted,jdbcType=TINYINT}, #{groupId})
    </insert>

    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.user.api.entity.SysDept"
            useGeneratedKeys="true">
        insert into sys_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="deptNo != null">
                dept_no,
            </if>
            <if test="name != null">
                `name`,
            </if>
            <if test="pid != null">
                pid,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="relationCode != null">
                relation_code,
            </if>
            <if test="deptManagerId != null">
                dept_manager_id,
            </if>
            <if test="managerName != null">
                manager_name,
            </if>
            <if test="phone != null">
                phone,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="groupId != null">
                group_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER },
            </if>
            <if test="deptNo != null">
                #{deptNo,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="pid != null">
                #{pid,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=TINYINT},
            </if>
            <if test="relationCode != null">
                #{relationCode,jdbcType=VARCHAR},
            </if>
            <if test="deptManagerId != null">
                #{deptManagerId,jdbcType=VARCHAR},
            </if>
            <if test="managerName != null">
                #{managerName,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                #{phone,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="deleted != null">
                #{deleted,jdbcType=TINYINT},
            </if>
            <if test="groupId != null">
                #{groupId}
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.user.api.entity.SysDept">
        update sys_dept
        <set>
            <if test="deptNo != null">
                dept_no = #{deptNo,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="pid != null">
                pid = #{pid,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=TINYINT},
            </if>
            <if test="relationCode != null">
                relation_code = #{relationCode,jdbcType=VARCHAR},
            </if>
            <if test="deptManagerId != null">
                dept_manager_id = #{deptManagerId,jdbcType=VARCHAR},
            </if>
            <if test="managerName != null">
                manager_name = #{managerName,jdbcType=VARCHAR},
            </if>
            <if test="phone != null">
                phone = #{phone,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="deleted != null">
                deleted = #{deleted,jdbcType=TINYINT},
            </if>
            <if test="groupId != null">
                group_id = #{groupId},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.user.api.entity.SysDept">
        update sys_dept
        set dept_no         = #{deptNo,jdbcType=VARCHAR},
            `name`          = #{name,jdbcType=VARCHAR},
            pid             = #{pid,jdbcType=VARCHAR},
            `status`        = #{status,jdbcType=TINYINT},
            relation_code   = #{relationCode,jdbcType=VARCHAR},
            dept_manager_id = #{deptManagerId,jdbcType=VARCHAR},
            manager_name    = #{managerName,jdbcType=VARCHAR},
            phone           = #{phone,jdbcType=VARCHAR},
            create_time     = #{createTime,jdbcType=TIMESTAMP},
            update_time     = #{updateTime,jdbcType=TIMESTAMP},
            deleted         = #{deleted,jdbcType=TINYINT},
            group_id        = #{groupId}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <!-- @Description:     维护Relation_code 的层级关系-->
    <!-- @Author:          -->
    <!-- @CreateDate:      2019/9/19 14:57 -->
    <update id="updateRelationCode">
        UPDATE sys_dept
        set relation_code=replace(relation_code, #{oldStr}, #{newStr})
        where relation_code LIKE CONCAT(#{relationCode}, '%')
    </update>





</mapper>