<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.sys.dao.DataDictionaryDAO">
    <resultMap id="DataDictionaryMap" type="com.jpsoft.shinestar.modules.sys.entity.DataDictionary">
        <id property="id" column="id_"/>
        <result property="name" column="name_"/>
        <result property="value" column="value_"/>
        <result property="sortNo" column="sort_no"/>
        <result property="parentId" column="parent_id"/>
        <result property="parentName" column="parent_name"/>
        <result property="dataType" column="data_type"/>
        <result property="createBy" column="create_by"/>
        <result property="createDate" column="create_date"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateDate" column="update_date"/>
        <result property="activated" column="activated_"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.sys.entity.DataDictionary">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into sys_data_dictionary
	    (id_,name_,value_,sort_no,parent_id,data_type,create_by,create_date,update_by,update_date,activated_,del_flag)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{name,jdbcType=VARCHAR}
            ,#{value,jdbcType=VARCHAR}
            ,#{sortNo,jdbcType= NUMERIC }
            ,#{parentId,jdbcType=VARCHAR}
            ,#{dataType,jdbcType= NUMERIC }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{createDate,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{updateDate,jdbcType= TIMESTAMP }
            ,#{activated,jdbcType= NUMERIC }
            ,#{delFlag,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from sys_data_dictionary where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.sys.entity.DataDictionary">
        update sys_data_dictionary
        <set>
            <if test="name!=null">
                name_=#{name,jdbcType=VARCHAR},
            </if>
            <if test="value!=null">
                value_=#{value,jdbcType=VARCHAR},
            </if>
            <if test="sortNo!=null">
                sort_no=#{sortNo,jdbcType= NUMERIC },
            </if>
            <if test="parentId!=null">
                parent_id=#{parentId,jdbcType=VARCHAR},
            </if>
            <if test="dataType!=null">
                data_type=#{dataType,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createDate!=null">
                create_date=#{createDate,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateDate!=null">
                update_date=#{updateDate,jdbcType= TIMESTAMP },
            </if>
            <if test="activated!=null">
                activated_=#{activated,jdbcType= NUMERIC },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="DataDictionaryMap">
        select a.*,b.name_ as parent_name
        from sys_data_dictionary a left join sys_data_dictionary b on a.parent_id = b.id_
        where a.id_=#{0} and a.del_flag = 0
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from sys_data_dictionary where id_=#{0} and del_flag = 0
    </select>
    <select id="list" resultMap="DataDictionaryMap">
        select * from sys_data_dictionary where del_flag = 0
    </select>
    <select id="findParentId" parameterType="string" resultType="string">
        select a.name_ as name
        from sys_data_dictionary a
        where a.parent_id =#{parentId} and a.value_=#{value} and a.del_flag = 0
        order by a.sort_no asc
    </select>
    <select id="search" parameterType="hashmap" resultMap="DataDictionaryMap">
        <![CDATA[
			SELECT
				a.*,b.name_ AS parent_name
			FROM
				sys_data_dictionary a
			LEFT JOIN sys_data_dictionary b ON a.parent_id = b.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.id != null">
                and a.ID_ like #{searchParams.id}
            </if>
            <if test="searchParams.name != null">
                and a.name_ like #{searchParams.name}
            </if>
            <if test="searchParams.excludeId != null">
                <![CDATA[
				and a.ID_ <> #{searchParams.excludeId}
				]]>
            </if>
            <if test="searchParams.dataType != null">
                and a.data_type = #{searchParams.dataType}
            </if>
            <if test="searchParams.parentId != null">
                <![CDATA[
				and a.parent_id = #{searchParams.parentId}
				]]>
            </if>

        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="queryChildren" resultType="java.util.HashMap" parameterType="string">
        SELECT
        a.id_ as id,a.name_ as name,a.value_ as value
        FROM
        sys_data_dictionary a where a.del_flag = 0
        and data_type = 2
        and a.parent_id = #{0}
    </select>

    <select id="getName" parameterType="string" resultType="string">
        select a.name_ as name
        from sys_data_dictionary a
        where a.id_=#{0} and a.del_flag = 0
    </select>
    <select id="getValue" parameterType="string" resultType="string">
        select a.value_ as value
        from sys_data_dictionary a
        where a.id_=#{0} and a.del_flag = 0
    </select>
    <select id="findByCatalogName" resultMap="DataDictionaryMap">
        select a.*
        from sys_data_dictionary a,sys_data_dictionary b
        where a.parent_id =b.id_ and b.name_=#{0}
        and a.del_flag = 0
        order by a.sort_no asc
    </select>
    <select id="findNameByCatalogNameAndValue" parameterType="map" resultType="string">
        select a.name_
        from sys_data_dictionary a,sys_data_dictionary b
        where a.parent_id =b.id_ and b.name_=#{catalogName}
        and a.del_flag = 0
        and a.value_=#{value}
        order by a.sort_no asc
        limit 1
    </select>
</mapper>