<?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.ximai.mes.md.mapper.MdSapDictDataMapper">

    <resultMap type="MdSapDictData" id="MdSapDictDataResult">
        <result property="id" column="id"/>
        <result property="dictId" column="dict_id"/>
        <result property="value" column="value"/>
        <result property="name" column="name"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="parentId" column="parent_id"/>
        <collection property="children" ofType="MdSapDictData" column="id" select="listByParentId"/>
    </resultMap>

    <select id="listByParentId" resultMap="MdSapDictDataResult">
        select t1.id, t1.dict_id, t2.name dict_name, t1.value, t1.name, t1.enable_flag, t1.parent_id, t1.create_by, t1.create_time, t1.update_by, t1.update_time
        from md_sap_dict_data t1
        left join md_sap_dict t2 on t1.dict_id = t2.id
        where parent_id = #{id}
    </select>

    <sql id="selectSapDictDataVo">
        select id, dict_id, value, name, enable_flag, parent_id, create_by, create_time, update_by, update_time
        from md_sap_dict_data
    </sql>

    <select id="selectList" resultMap="MdSapDictDataResult">
        <include refid="selectSapDictDataVo"/>
        <where>
            <if test="dictId != null "> and dict_id = #{dictId}</if>
            <if test="value != null  and value != ''"> and value = #{value}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="enableFlag != null "> and enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectById" resultMap="MdSapDictDataResult">
        <include refid="selectSapDictDataVo"/>
        where id = #{id}
    </select>

    <select id="pageByMap" resultMap="MdSapDictDataResult">
        select t1.id, t1.dict_id, t2.name dict_name, t1.value, t1.name, t1.enable_flag, t1.parent_id, t1.create_by, t1.create_time, t1.update_by, t1.update_time
        from md_sap_dict_data t1
        left join md_sap_dict t2 on t1.dict_id = t2.id
        <where>
            <if test="dictId != null"> and t1.dict_id = #{dictId}</if>
            <if test="parentId != null"> and t1.parent_id = #{parentId}</if>
            <if test="value != null and value != ''"> and t1.value = #{value}</if>
            <if test="name != null and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
        </where>
        order by t1.id desc
        limit #{offset}, #{limit}
    </select>

    <select id="countByMap" resultType="java.lang.Integer">
        select count(*)
        from md_sap_dict_data t1
        left join md_sap_dict t2 on t1.dict_id = t2.id
        <where>
            <if test="dictId != null"> and t1.dict_id = #{dictId}</if>
            <if test="parentId != null"> and t1.parent_id = #{parentId}</if>
            <if test="value != null and value != ''"> and t1.value = #{value}</if>
            <if test="name != null and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
        </where>
    </select>

    <insert id="insert" parameterType="MdSapDictData" useGeneratedKeys="true" keyProperty="id">
        insert into md_sap_dict_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dictId != null">dict_id,</if>
            <if test="value != null">value,</if>
            <if test="name != null">name,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dictId != null">#{dictId},</if>
            <if test="value != null">#{value},</if>
            <if test="name != null">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="update" parameterType="MdSapDictData">
        update md_sap_dict_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="dictId != null">dict_id = #{dictId},</if>
            <if test="value != null">value = #{value},</if>
            <if test="name != null">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById" parameterType="String">
        delete from md_sap_dict_data where id = #{id}
    </delete>

    <delete id="deleteByIds" parameterType="String">
        delete from md_sap_dict_data where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
