<?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.labelfieldinfo.LabelFieldInfoMapper">

    <resultMap type="LabelFieldInfo" id="LabelFieldInfoResult">
        <result property="id" column="id"/>
        <result property="fieldName" column="field_name"/>
        <result property="fieldCode" column="field_code"/>
        <result property="fieldType" column="field_type"/>
        <result property="fieldSort" column="field_sort"/>
        <result property="fieldRemark" column="field_remark"/>
        <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="selectLabelFieldInfoVo">
        select id,
               field_name,
               field_code,
               field_type,
               field_sort,
               field_remark,
               create_by,
               create_time,
               update_by,
               update_time
        from label_field_info
    </sql>

    <select id="selectLabelFieldInfoList" parameterType="LabelFieldInfo" resultMap="LabelFieldInfoResult">
        <include refid="selectLabelFieldInfoVo"/>
        <where>
            <if test="fieldName != null  and fieldName != ''">
                and field_name like concat('%',
                #{fieldName},
                '%'
                )
            </if>
            <if test="fieldCode != null  and fieldCode != ''">
                and field_code =
                #{fieldCode}
            </if>
            <if test="fieldType != null  and fieldType != ''">
                and field_type =
                #{fieldType}
            </if>
            <if test="fieldSort != null ">
                and field_sort =
                #{fieldSort}
            </if>
            <if test="fieldRemark != null  and fieldRemark != ''">
                and field_remark =
                #{fieldRemark}
            </if>
        </where>
        order by create_time DESC
    </select>


    <select id="selectLabelFieldList" resultType="com.xinelu.manage.vo.labelfieldinfo.LabelFieldVO">
        select id LabelFieldId,
               field_name,
               field_code,
               field_type,
               field_sort,
               field_remark
        from label_field_info
        where field_type = #{fieldType}
    </select>

    <select id="selectLabelFieldInfoById" parameterType="Long"
            resultMap="LabelFieldInfoResult">
        <include refid="selectLabelFieldInfoVo"/>
        where id = #{id}
    </select>

    <select id="countDuplicateFieldNamesByType" resultType="java.lang.Integer">
        select count(1)
        from label_field_info
        where field_type = #{fieldType} and field_name in
        <foreach collection="fieldNames" item="fieldName" open="(" separator="," close=")">
            #{fieldName}
        </foreach>
    </select>

    <select id="countByFieldNameExcludingId" resultType="java.lang.Integer">
        select count(1)
        from label_field_info
        where field_name = #{fieldName}
          and field_type = #{fieldType}
          and id != #{id}
    </select>

    <insert id="insertLabelFieldInfo" parameterType="LabelFieldInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into label_field_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fieldName != null">field_name,
            </if>
            <if test="fieldCode != null">field_code,
            </if>
            <if test="fieldType != null">field_type,
            </if>
            <if test="fieldSort != null">field_sort,
            </if>
            <if test="fieldRemark != null">field_remark,
            </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="fieldName != null">#{fieldName},
            </if>
            <if test="fieldCode != null">#{fieldCode},
            </if>
            <if test="fieldType != null">#{fieldType},
            </if>
            <if test="fieldSort != null">#{fieldSort},
            </if>
            <if test="fieldRemark != null">#{fieldRemark},
            </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="updateLabelFieldInfo" parameterType="LabelFieldInfo">
        update label_field_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="fieldName != null">field_name =
                #{fieldName},
            </if>
            <if test="fieldCode != null">field_code =
                #{fieldCode},
            </if>
            <if test="fieldType != null">field_type =
                #{fieldType},
            </if>
            <if test="fieldSort != null">field_sort =
                #{fieldSort},
            </if>
            <if test="fieldRemark != null">field_remark =
                #{fieldRemark},
            </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="deleteLabelFieldInfoById" parameterType="Long">
        delete
        from label_field_info
        where id = #{id}
    </delete>

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