<?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.qryord.project.master.field.mapper.MFieldMapper">
    
    <resultMap type="MField" id="MFieldResult">
        <result property="id"    column="id"    />
        <result property="fieldKey"    column="field_key"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="sort"    column="sort"    />
        <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>

    <sql id="selectMFieldVo">
        select id, field_key, sort, del_flag, create_by, create_time, update_by, update_time from m_field
    </sql>

    <select id="selectMFieldList" parameterType="MField" resultMap="MFieldResult">
        <include refid="selectMFieldVo"/>
        <where>  
            <if test="fieldKey != null  and fieldKey != ''"> and field_key like concat('%', #{fieldKey}, '%')</if>
            <if test="delFlag != null and delFlag != ''"> and del_flag = #{delFlag}</if>
        </where>
        order by sort
    </select>
    
    <select id="selectMFieldById" parameterType="Long" resultMap="MFieldResult">
        <include refid="selectMFieldVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertMField" parameterType="MField" useGeneratedKeys="true" keyProperty="id">
        insert into m_field
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fieldKey != null">field_key,</if>
            <if test="sort != null">sort,</if>
            <if test="delFlag != null">del_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="fieldKey != null">#{fieldKey},</if>
            <if test="sort != null">#{sort},</if>
            <if test="delFlag != null">#{delFlag},</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="updateMField" parameterType="MField">
        update m_field
        <trim prefix="SET" suffixOverrides=",">
            <if test="fieldKey != null">field_key = #{fieldKey},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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>

    <update id="deleteMFieldById" parameterType="Long">
        update m_field
        set del_flag = '2'
        where id = #{id}
    </update>

    <update id="deleteMFieldByIds" parameterType="String">
        update m_field
        set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateList" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            update m_field
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.fieldKey != null  and item.fieldKey != ''  ">field_key = #{item.fieldKey},</if>
                <if test="item.sort != null  ">sort = #{item.sort},</if>
                <if test="item.delFlag != null  and item.delFlag != ''  ">del_flag = #{item.delFlag},</if>
                <if test="item.updateBy != null  and item.updateBy != ''  ">update_by = #{item.updateBy},</if>
                <if test="item.updateTime != null  ">update_time = #{item.updateTime},</if>
            </trim>
            where id = #{item.id}
        </foreach>
    </update>
</mapper>