<?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.fs.survey.mapper.SurveyFieldTypeMappingMapper">

    <resultMap type="SurveyFieldTypeMapping" id="SurveyFieldTypeMappingResult">
        <result property="id"    column="id"    />
        <result property="tagName"    column="tag_name"    />
        <result property="tagIcon"    column="tag_icon"    />
        <result property="dbType"    column="db_type"    />
        <result property="dbLength"    column="db_length"    />
        <result property="searchType"    column="search_type"    />
        <result property="description"    column="description"    />
        <result property="status"    column="status"    />
        <result property="sortOrder"    column="sort_order"    />
    </resultMap>

    <sql id="selectSurveyFieldTypeMappingVo">
        select id, tag_name, tag_icon, db_type, db_length, search_type,
               description, status, sort_order
        from survey_field_type_mapping
    </sql>

    <select id="selectSurveyFieldTypeMappingList" parameterType="SurveyFieldTypeMapping" resultMap="SurveyFieldTypeMappingResult">
        <include refid="selectSurveyFieldTypeMappingVo"/>
        <where>
            <if test="tagName != null  and tagName != ''"> and tag_name = #{tagName}</if>
            <if test="tagIcon != null  and tagIcon != ''"> and tag_icon = #{tagIcon}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by sort_order asc
    </select>

    <select id="selectSurveyFieldTypeMappingById" parameterType="Long" resultMap="SurveyFieldTypeMappingResult">
        <include refid="selectSurveyFieldTypeMappingVo"/>
        where id = #{id}
    </select>

    <select id="selectByTagAndIcon" resultMap="SurveyFieldTypeMappingResult">
        <include refid="selectSurveyFieldTypeMappingVo"/>
        where tag_name = #{tagName}
        <if test="tagIcon != null and tagIcon != ''">
            and tag_icon = #{tagIcon}
        </if>
        and status = 1
        limit 1
    </select>

    <insert id="insertSurveyFieldTypeMapping" parameterType="SurveyFieldTypeMapping" useGeneratedKeys="true" keyProperty="id">
        insert into survey_field_type_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tagName != null and tagName != ''">tag_name,</if>
            <if test="tagIcon != null">tag_icon,</if>
            <if test="dbType != null and dbType != ''">db_type,</if>
            <if test="dbLength != null">db_length,</if>
            <if test="searchType != null">search_type,</if>
            <if test="description != null">description,</if>
            <if test="status != null">status,</if>
            <if test="sortOrder != null">sort_order,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tagName != null and tagName != ''">#{tagName},</if>
            <if test="tagIcon != null">#{tagIcon},</if>
            <if test="dbType != null and dbType != ''">#{dbType},</if>
            <if test="dbLength != null">#{dbLength},</if>
            <if test="searchType != null">#{searchType},</if>
            <if test="description != null">#{description},</if>
            <if test="status != null">#{status},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
        </trim>
    </insert>

    <update id="updateSurveyFieldTypeMapping" parameterType="SurveyFieldTypeMapping">
        update survey_field_type_mapping
        <trim prefix="SET" suffixOverrides=",">
            <if test="tagName != null and tagName != ''">tag_name = #{tagName},</if>
            <if test="tagIcon != null">tag_icon = #{tagIcon},</if>
            <if test="dbType != null and dbType != ''">db_type = #{dbType},</if>
            <if test="dbLength != null">db_length = #{dbLength},</if>
            <if test="searchType != null">search_type = #{searchType},</if>
            <if test="description != null">description = #{description},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurveyFieldTypeMappingById" parameterType="Long">
        delete from survey_field_type_mapping where id = #{id}
    </delete>

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

    <update id="truncateTable">
        truncate table survey_field_type_mapping
    </update>
</mapper>
