<?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.inkasso.factoring.dict.dao.DictionaryMapper">

    <resultMap type="com.inkasso.factoring.dict.entity.Dictionary" id="dictionaryResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="parentId" column="parentId"/>
        <result property="code" column="code"/>
        <result property="value" column="value"/>
        <result property="gradation" column="gradation"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
    </resultMap>

    <insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.inkasso.factoring.dict.entity.Dictionary">
        insert into t_dictionary
            (name, parentId, code, gradation)
        values (#{name}, #{parentId}, #{code}, #{gradation})
    </insert>

    <select id="get" parameterType="java.lang.Long" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where id = #{value}
    </select>

    <select id="getByCode" parameterType="java.lang.String" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where code = #{value}
        order by gradation
    </select>

    <select id="getByName" parameterType="java.lang.String" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where name = #{name}
        order by gradation
    </select>

    <select id="getByNameAndParentCode" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where name = #{name}
          and parentId = (select id from t_dictionary where code = #{parentCode})
        order by gradation
    </select>

    <select id="findByParentId" parameterType="java.lang.Long" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where parentId = #{value}
        order by gradation
    </select>

    <select id="findByParentIdIsNull" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where parentId is null
           or parentId = ''
        order by gradation
    </select>

    <select id="findByParentCode" parameterType="java.lang.String" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where parentId = (select id from t_dictionary where code = #{value})
        order by gradation
    </select>

    <select id="findByParentCodeAndCode" parameterType="java.lang.String" resultMap="dictionaryResult">
        select *
        from t_dictionary
        where parentId = (select id from t_dictionary where code = #{parentCode})
          and code like CONCAT('%', #{code}, '%')
        order by gradation
    </select>
    <select id="getByCodeAndParentCode" resultType="com.inkasso.factoring.dict.entity.Dictionary">
        select *
        from t_dictionary
        where code = #{code}
          and parentId = (select id from t_dictionary where code = #{parentCode})
        order by gradation
    </select>

</mapper>
