<?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.xhpc.general.mapper.XhpcDictBizMapper">

    <resultMap id="BaseResultMap" type="com.xhpc.general.domain.XhpcDictionary">
        <result property="dictBizId" column="dict_biz_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="code" column="code"/>
        <result property="dictKey" column="dict_key"/>
        <result property="dictValue" column="dict_value"/>
        <result property="sort" column="sort"/>
        <result property="delFlag" column="del_flag"/>
        <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>
    <!-- 通用查询映射结果 -->

    <select id="getCode" resultMap="BaseResultMap">
        select code, dict_key, dict_value, sort, remark from xhpc_dict_biz where code = #{code} and parent_id > 0 and del_flag = 0
    </select>

    <select id="countParentNameColumns" resultType="java.lang.Integer">
        select count(dict_value) from xhpc_dict_biz where dict_value=#{name} and dict_biz_id=#{id} and del_flag=0
    </select>

    <select id="countSameParentChildNameColumns" resultType="java.lang.Integer">
        select count(dict_value) from xhpc_dict_biz where parent_id=#{id} and dict_value=#{name} and del_flag=0
    </select>

    <insert id="insertAParentItemToTable">
        insert into xhpc_dict_biz(dict_value,code,sort) values(#{name},#{code},#{sort})
    </insert>

    <select id="selectADictionaryItem" resultMap="BaseResultMap">
        select * from xhpc_dict_biz where dict_biz_id=#{id}
    </select>

    <insert id="insertAChildItemToTable">
        insert into xhpc_dict_biz(parent_id,code,dict_key,dict_value,sort) values (#{id},#{code},#{dictKey},#{dictValue},#{sore})
    </insert>

    <update id="deleteChildDictionaryItemById">
        update xhpc_dict_biz set del_flag=1 where dict_biz_id=#{id}
    </update>

    <update id="deleteChildItemsFollowParent">
        update xhpc_dict_biz set del_flag=1 where parent_id=#{id}
    </update>

    <select id="selectParentDictionaryItemsByCodeAndName" resultMap="BaseResultMap">
        select * from xhpc_dict_biz
        where parent_id=0 and del_flag=0
        <if test="code!=null and code!=''">
            and code like concat('%',#{code},'%')
        </if>
        <if test="name!=null and name!=''">
            and dict_value like concat('%',#{name},'%')
        </if>
        order by sort asc
    </select>

    <select id="selectChildDictionaryItemsByCodeAndName" resultMap="BaseResultMap">
        select * from xhpc_dict_biz
        where parent_id!=0 and parent_id=#{id} and del_flag=0
        <if test="code!=null and code!=''">
            and code like concat('%',#{code},'%')
        </if>
        <if test="name!=null and name!=''">
            and dict_value like concat('%',#{name},'%')
        </if>
        order by sort asc
    </select>

    <update id="updateParentItemByCodeDictValueSort">
        update xhpc_dict_biz set code=#{code},dict_value=#{dictValue},sort=#{sort} where dict_biz_id=#{id}
    </update>

    <update id="updateChildValue">
        update xhpc_dict_biz set code=#{code} where parent_id=#{id}
    </update>

    <update id="updateChildItem">
        update xhpc_dict_biz set dict_key=#{key},dict_value=#{value},sort=#{sort} where dict_biz_id=#{id}
    </update>

    <update id="deleteChildItemByIds">
        update xhpc_dict_biz set del_flag=1
        where
        dict_biz_id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="countParentNameQuantity" resultType="java.lang.Integer">
        select count(dict_biz_id) from xhpc_dict_biz where dict_value=#{name} and del_flag=0
    </select>

    <update id="updateChildCodeAfterParentChanged">
        update xhpc_dict_biz set code=#{code} where parent_id=#{id}
    </update>

    <update id="deleteChildItemsAfterTheirParentDied">
        update xhpc_dict_biz set del_flag=1
        where
        parent_id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
</mapper>
