<?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.xinelu.manage.mapper.labelfieldcontent.LabelFieldContentMapper">

    <resultMap type="LabelFieldContent" id="LabelFieldContentResult">
        <result property="id" column="id"/>
        <result property="fieldId" column="field_id"/>
        <result property="fieldName" column="field_name"/>
        <result property="contentName" column="content_name"/>
        <result property="contentCode" column="content_code"/>
        <result property="resultPreview" column="result_preview"/>
        <result property="contentSort" column="content_sort"/>
        <result property="contentRemark" column="content_remark"/>
        <result property="fieldMark" column="field_mark"/>
        <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="selectLabelFieldContentVo">
        select id,
               field_id,
               field_name,
               content_name,
               content_code,
               result_preview,
               content_sort,
               content_remark,
               field_mark,
               create_by,
               create_time,
               update_by,
               update_time
        from label_field_content
    </sql>

    <select id="selectLabelFieldContentList" parameterType="LabelFieldContent" resultMap="LabelFieldContentResult">
        <include refid="selectLabelFieldContentVo"/>
        <where>
            <if test="fieldId != null ">
                and field_id =
                #{fieldId}
            </if>
            <if test="fieldName != null  and fieldName != ''">
                and field_name like concat('%',
                #{fieldName},
                '%'
                )
            </if>
            <if test="contentName != null  and contentName != ''">
                and content_name like concat('%',
                #{contentName},
                '%'
                )
            </if>
            <if test="contentCode != null  and contentCode != ''">
                and content_code =
                #{contentCode}
            </if>
            <if test="resultPreview != null  and resultPreview != ''">
                and result_preview =
                #{resultPreview}
            </if>
            <if test="contentSort != null ">
                and content_sort =
                #{contentSort}
            </if>
            <if test="contentRemark != null  and contentRemark != ''">
                and content_remark =
                #{contentRemark}
            </if>
            <if test="fieldMark != null  and fieldMark != ''">
                and field_mark =
                #{fieldMark}
            </if>
        </where>
     order by create_time DESC
    </select>

    <select id="selectLabelFieldContentById" parameterType="Long"
            resultMap="LabelFieldContentResult">
        <include refid="selectLabelFieldContentVo"/>
        where id = #{id}
    </select>

    <select id="existCountByFieldIdAndContentName" resultType="java.lang.Integer">
        select count(1)
        from label_field_content
        where field_id = #{fieldId}
          and content_name = #{contentName}
    </select>

    <select id="existCountByContentNameExcludingId" resultType="java.lang.Integer">
        select count(1)
        from label_field_content
        where content_name = #{contentName}
          and field_id = #{fieldId}
          and id != #{id}
    </select>

    <insert id="insertLabelFieldContent" parameterType="LabelFieldContent" useGeneratedKeys="true"
            keyProperty="id">
        insert into label_field_content
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fieldId != null">field_id,
            </if>
            <if test="fieldName != null">field_name,
            </if>
            <if test="contentName != null">content_name,
            </if>
            <if test="contentCode != null">content_code,
            </if>
            <if test="resultPreview != null">result_preview,
            </if>
            <if test="contentSort != null">content_sort,
            </if>
            <if test="contentRemark != null">content_remark,
            </if>
            <if test="fieldMark != null">field_mark,
            </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="fieldId != null">#{fieldId},
            </if>
            <if test="fieldName != null">#{fieldName},
            </if>
            <if test="contentName != null">#{contentName},
            </if>
            <if test="contentCode != null">#{contentCode},
            </if>
            <if test="resultPreview != null">#{resultPreview},
            </if>
            <if test="contentSort != null">#{contentSort},
            </if>
            <if test="contentRemark != null">#{contentRemark},
            </if>
            <if test="fieldMark != null">#{fieldMark},
            </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="updateLabelFieldContent" parameterType="LabelFieldContent">
        update label_field_content
        <trim prefix="SET" suffixOverrides=",">
            <if test="fieldId != null">field_id =
            #{fieldId},
            </if>
            <if test="fieldName != null">field_name =
            #{fieldName},
            </if>
            <if test="contentName != null">content_name =
            #{contentName},
            </if>
            <if test="contentCode != null">content_code =
            #{contentCode},
            </if>
            <if test="resultPreview != null">result_preview =
            #{resultPreview},
            </if>
            <if test="contentSort != null">content_sort =
            #{contentSort},
            </if>
            <if test="contentRemark != null">content_remark =
            #{contentRemark},
            </if>
            <if test="fieldMark != null">field_mark =
            #{fieldMark},
            </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="deleteLabelFieldContentById" parameterType="Long">
        delete
        from label_field_content
        where id = #{id}
    </delete>

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

    <select id="selectLabelFieldContent"
            resultType="com.xinelu.manage.vo.labelfieldcontent.LabelFieldContentVO">
        select id LabelFieldContentId,
        field_id,
        field_name,
        content_name,
        content_code,
        result_preview,
        content_sort,
        content_remark,
        field_mark
        from label_field_content
        <where>
            <if test="labelFieldIds != null and labelFieldIds.size() > 0">
                field_id in
                <foreach item="labelFieldIds" collection="labelFieldIds" open="(" separator="," close=")">
                    #{labelFieldIds}
                </foreach>
            </if>
        </where>
    </select>
</mapper>