<?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.willalp.level.mapper.HsSysLevelMapper">

    <resultMap type="HsSysLevel" id="HsSysLevelResult">
                    <result property="id"    column="id"    />
                    <result property="levelcode"    column="levelcode"    />
                    <result property="levelname"    column="levelname"    />
                    <result property="parentId"    column="parent_id"    />
                    <result property="ancestors"    column="ancestors"    />
                    <result property="xh"    column="xh"    />
                    <result property="jgid"    column="jgid"    />
                    <result property="sfzx"    column="sfzx"    />
                    <result property="createTime"    column="create_time"    />
                    <result property="createBy"    column="create_by"    />
                    <result property="updateTime"    column="update_time"    />
                    <result property="updateBy"    column="update_by"    />
                    <result property="remark"    column="remark"    />
            </resultMap>

    <sql id="Base_Column_List">
        a.*,
        (select count(*) from hs_sys_level s
        WHERE s.parent_id=a.id) as
        hasChildren
    </sql>
    <sql id="tableJoins">

    </sql>

    <select
            id="selectByTreeId"
            resultType="com.willalp.level.domain.HsSysLevel">
        select
        <include refid="Base_Column_List"/>
        from hs_sys_level a
        <include refid="tableJoins"/>
        where a.id = #{id,jdbcType=VARCHAR}
    </select>

    
    <sql id="selectHsSysLevelVo">
        select id, levelcode, levelname, ancestors, parent_id,xh, jgid, sfzx, create_time, update_time, remark from hs_sys_level
    </sql>

    <select id="selectHsSysLevelList" parameterType="HsSysLevel" resultMap="HsSysLevelResult">
        <include refid="selectHsSysLevelVo"/>
        <where>
            0 = 0
            <if test="levelcode != null  and levelcode != ''"> and levelcode = #{levelcode}</if>
            <if test="levelname != null  and levelname != ''"> and levelname like concat('%', #{levelname}, '%')</if>
            <if test="parentId != null  and parentId != ''"> and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
            <if test="xh != null  and xh != ''"> and xh = #{xh}</if>
            <if test="jgid != null  and jgid != ''"> and jgid = #{jgid}</if>
             <if test="sfzx != null  and sfzx != ''"> and sfzx = #{sfzx}</if>
              <if test="createTime != null "> and create_time = #{create_time}</if>
              <if test="updateTime != null "> and update_time = #{update_time}</if>
        </where>
    </select>

    <select id="getByMaxId" resultType="java.lang.Long">
        Select ifnull(max(id),1)+1  from hs_sys_level
            </select>

    <select id="getParent" resultType="java.lang.Long">
        Select  count(id)  from hs_sys_level where parent_id = #{id}
            </select>

    <select id="getCode" resultType="java.lang.Long">
        Select count(id)  from hs_sys_code where levelid = #{id}
            </select>

    <!-- 更新子树 -->
    <update id="updateSunTreeParentIds">
        update hs_sys_level set ancestors= CONCAT(#{newParentIds},substring(ancestors, length(#{oldParentIds})+1,length(ancestors)+1))
        where ancestors like concat(#{oldParentIds}, '%')
    </update>

    <delete
            id="deleteSunTree"
            parameterType="java.lang.String">
        delete from hs_sys_level
        where ancestors like concat(#{parentIds}, '%')
    </delete>

</mapper>
