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

    <resultMap type="SurveyFieldConfig" id="SurveyFieldConfigResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="versionId"    column="version_id"    />
        <result property="questionnaireId"    column="questionnaire_id"    />
        <result property="fieldKey"    column="field_key"    />
        <result property="fieldLabel"    column="field_label"    />
        <result property="fieldTag"    column="field_tag"    />
        <result property="fieldTagIcon"    column="field_tag_icon"    />
        <result property="dbColumnType"    column="db_column_type"    />
        <result property="dbColumnLength"    column="db_column_length"    />
        <result property="isRequired"    column="is_required"    />
        <result property="isSearchable"    column="is_searchable"    />
        <result property="searchType"    column="search_type"    />
        <result property="isListShow"    column="is_list_show"    />
        <result property="listWidth"    column="list_width"    />
        <result property="isExport"    column="is_export"    />
        <result property="exportLabel"    column="export_label"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="defaultValue"    column="default_value"    />
        <result property="optionsJson"    column="options_json"    />
        <result property="regListJson"    column="reg_list_json"    />
        <result property="rawConfigJson"    column="raw_config_json"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectSurveyFieldConfigVo">
        select id, company_id, version_id, questionnaire_id, field_key, field_label,
               field_tag, field_tag_icon, db_column_type, db_column_length, is_required,
               is_searchable, search_type, is_list_show, list_width, is_export,
               export_label, sort_order, default_value, options_json, reg_list_json,
               raw_config_json, create_by, create_time
        from survey_field_config
    </sql>

    <select id="selectSurveyFieldConfigList" parameterType="SurveyFieldConfig" resultMap="SurveyFieldConfigResult">
        <include refid="selectSurveyFieldConfigVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="versionId != null "> and version_id = #{versionId}</if>
            <if test="questionnaireId != null "> and questionnaire_id = #{questionnaireId}</if>
            <if test="fieldKey != null  and fieldKey != ''"> and field_key = #{fieldKey}</if>
            <if test="fieldLabel != null  and fieldLabel != ''"> and field_label like concat('%', #{fieldLabel}, '%')</if>
            <if test="fieldTag != null  and fieldTag != ''"> and field_tag = #{fieldTag}</if>
        </where>
        order by sort_order asc
    </select>

    <select id="selectSurveyFieldConfigById" parameterType="Long" resultMap="SurveyFieldConfigResult">
        <include refid="selectSurveyFieldConfigVo"/>
        where id = #{id}
    </select>

    <select id="selectFieldConfigByVersionId" parameterType="Long" resultMap="SurveyFieldConfigResult">
        <include refid="selectSurveyFieldConfigVo"/>
        where version_id = #{versionId}
        order by sort_order asc
    </select>

    <insert id="insertSurveyFieldConfig" parameterType="SurveyFieldConfig" useGeneratedKeys="true" keyProperty="id">
        insert into survey_field_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="versionId != null">version_id,</if>
            <if test="questionnaireId != null">questionnaire_id,</if>
            <if test="fieldKey != null and fieldKey != ''">field_key,</if>
            <if test="fieldLabel != null and fieldLabel != ''">field_label,</if>
            <if test="fieldTag != null and fieldTag != ''">field_tag,</if>
            <if test="fieldTagIcon != null">field_tag_icon,</if>
            <if test="dbColumnType != null and dbColumnType != ''">db_column_type,</if>
            <if test="dbColumnLength != null">db_column_length,</if>
            <if test="isRequired != null">is_required,</if>
            <if test="isSearchable != null">is_searchable,</if>
            <if test="searchType != null">search_type,</if>
            <if test="isListShow != null">is_list_show,</if>
            <if test="listWidth != null">list_width,</if>
            <if test="isExport != null">is_export,</if>
            <if test="exportLabel != null">export_label,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="defaultValue != null">default_value,</if>
            <if test="optionsJson != null">options_json,</if>
            <if test="regListJson != null">reg_list_json,</if>
            <if test="rawConfigJson != null">raw_config_json,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="versionId != null">#{versionId},</if>
            <if test="questionnaireId != null">#{questionnaireId},</if>
            <if test="fieldKey != null and fieldKey != ''">#{fieldKey},</if>
            <if test="fieldLabel != null and fieldLabel != ''">#{fieldLabel},</if>
            <if test="fieldTag != null and fieldTag != ''">#{fieldTag},</if>
            <if test="fieldTagIcon != null">#{fieldTagIcon},</if>
            <if test="dbColumnType != null and dbColumnType != ''">#{dbColumnType},</if>
            <if test="dbColumnLength != null">#{dbColumnLength},</if>
            <if test="isRequired != null">#{isRequired},</if>
            <if test="isSearchable != null">#{isSearchable},</if>
            <if test="searchType != null">#{searchType},</if>
            <if test="isListShow != null">#{isListShow},</if>
            <if test="listWidth != null">#{listWidth},</if>
            <if test="isExport != null">#{isExport},</if>
            <if test="exportLabel != null">#{exportLabel},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="defaultValue != null">#{defaultValue},</if>
            <if test="optionsJson != null">#{optionsJson},</if>
            <if test="regListJson != null">#{regListJson},</if>
            <if test="rawConfigJson != null">#{rawConfigJson},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSurveyFieldConfig" parameterType="SurveyFieldConfig">
        update survey_field_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="fieldLabel != null and fieldLabel != ''">field_label = #{fieldLabel},</if>
            <if test="fieldTag != null and fieldTag != ''">field_tag = #{fieldTag},</if>
            <if test="fieldTagIcon != null">field_tag_icon = #{fieldTagIcon},</if>
            <if test="dbColumnType != null and dbColumnType != ''">db_column_type = #{dbColumnType},</if>
            <if test="dbColumnLength != null">db_column_length = #{dbColumnLength},</if>
            <if test="isRequired != null">is_required = #{isRequired},</if>
            <if test="isSearchable != null">is_searchable = #{isSearchable},</if>
            <if test="searchType != null">search_type = #{searchType},</if>
            <if test="isListShow != null">is_list_show = #{isListShow},</if>
            <if test="listWidth != null">list_width = #{listWidth},</if>
            <if test="isExport != null">is_export = #{isExport},</if>
            <if test="exportLabel != null">export_label = #{exportLabel},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="defaultValue != null">default_value = #{defaultValue},</if>
            <if test="optionsJson != null">options_json = #{optionsJson},</if>
            <if test="regListJson != null">reg_list_json = #{regListJson},</if>
            <if test="rawConfigJson != null">raw_config_json = #{rawConfigJson},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurveyFieldConfigById" parameterType="Long">
        delete from survey_field_config where id = #{id}
    </delete>

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

    <delete id="deleteSurveyFieldConfigByVersionId" parameterType="Long">
        delete from survey_field_config where version_id = #{versionId}
    </delete>
</mapper>
