<?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.tuoheng.admin.mapper.DeptMapper">
    
    <resultMap id="DeptResult" type="com.tuoheng.admin.entity.Dept" >
        <result property="id"    column="id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="name"    column="name"    />
        <result property="code"    column="code"    />
        <result property="fullname"    column="fullname"    />
        <result property="type"    column="type"    />
        <result property="pid"    column="pid"    />
        <result property="sort"    column="sort"    />
        <result property="note"    column="note"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="mark"    column="mark"    />
    </resultMap>

    <sql id="Base_Column_List">
        id, tenant_id, name, code, fullname, type, pid, sort, note, create_user, create_time, update_user, update_time, mark
    </sql>

    <select id="selectAllChildListById" parameterType="String" resultType="java.lang.String">
        select id
        from (
                select @ids AS p_ids, (select @ids := group_concat(id) from th_dept where find_in_set(pid, convert(@ids using utf8) collate utf8_unicode_ci)) AS c_ids, @l := @l+1 AS level
                from th_dept, (select @ids := #{id}, @l := 0 ) b
                having @ids is not null
            ) u1
        join th_dept u2 on find_in_set(u2.id, convert(u1.p_ids using utf8) collate utf8_unicode_ci)
    </select>

    <select id="selectListByIdList" parameterType="list" resultMap="DeptResult">
        select <include refid="Base_Column_List"/>
        from th_dept
        where mark = 1 and id in
        <foreach collection="list" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </select>

    <update id="update" parameterType="com.tuoheng.admin.entity.Dept">
        update th_dept
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="code != null">code = #{code},</if>
            <if test="fullname != null">fullname = #{fullname},</if>
            <if test="type != null">type = #{type},</if>
            <if test="pid != null and pid != ''">pid = #{pid},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="note != null">note = #{note},</if>
            <if test="createUser != null and createUser != ''">create_user = #{createUser},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateUser != null">update_user = #{updateUser},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="mark != null">mark = #{mark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById" parameterType="String">
        update th_dept
        set mark = 0
        where id = #{id}
    </delete>

</mapper>