<?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.MdSapDictMapper">

    <resultMap type="MdSapDict" id="MdSapDictResult">
        <result property="id" column="id"/>
        <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"/>
    </resultMap>

    <resultMap id="SapTypeRelationResult" type="MdSapDict" extends="MdSapDictResult">
        <collection property="dictList" ofType="MdSapDictData">
            <result column="data_id" property="id"/>
            <result column="value" property="value"/>
            <result column="data_name" property="name"/>
            <result column="data_enable_flag" property="enableFlag"/>
        </collection>
    </resultMap>

    <sql id="selectSapDictVo">
        select t1.id, t1.name, t1.enable_flag, t1.create_by, t1.create_time, t1.update_by, t1.update_time,
        t2.id data_id, t2.value, t2.name data_name, t2.enable_flag data_enable_flag
        from md_sap_dict t1
        left join md_sap_dict_data t2 on t1.id = t2.dict_id
    </sql>

    <select id="selectSapDictList" parameterType="MdSapDict" resultMap="SapTypeRelationResult">
        select t1.id, t1.name, t1.enable_flag, t1.create_by, t1.create_time, t1.update_by, t1.update_time
        from md_sap_dict t1
        <where>
            <if test="id != null"> and t1.id = #{id}</if>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
            <if test="enableFlag != null and enableFlag != ''"> and t1.enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectSapDictById" parameterType="String" resultMap="SapTypeRelationResult">
        <include refid="selectSapDictVo"/>
        where t1.id = #{id}
    </select>

    <insert id="insertSapDict" parameterType="MdSapDict" useGeneratedKeys="true" keyProperty="id">
        insert into md_sap_dict
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="enableFlag != null">enable_flag,</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="name != null">#{name},</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="updateSapDict" parameterType="MdSapDict">
        update md_sap_dict
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</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="deleteSapDictById" parameterType="String">
        update md_sap_dict set enable_flag = 'N' where id = #{id}
    </delete>

    <delete id="deleteSapDictByIds" parameterType="String">
        update md_sap_dict set enable_flag = 'N' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
