<?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.cm.common.plugin.dao.dynamic.config.form.IDynamicConfigFormDao">

    <resultMap id="dynamicConfigFormDTO" type="com.cm.common.plugin.pojo.dtos.dynamic.config.form.DynamicConfigFormDTO">
        <id property="id" column="id"/>
        <result property="tableName" column="table_name"/>
        <result property="fieldName" column="field_name"/>
        <result property="fieldExplain" column="field_explain"/>
        <result property="fieldType" column="field_type"/>
        <result property="fieldDefault" column="field_default"/>
        <result property="dictionaryId" column="dictionary_id"/>
        <result property="dictionaryName" column="dictionary_name"/>
        <result property="verifyType" column="verify_type"/>
        <result property="verifyRegular" column="verify_regular"/>
        <result property="fieldSort" column="field_sort"/>
        <result property="listShow" column="list_show"/>
        <result property="listShowWidth" column="list_show_width"/>
        <result property="formShow" column="form_show"/>
        <result property="joinTable" column="join_table"/>
        <result property="joinTableField" column="join_table_field"/>
        <result property="joinTableSort" column="join_table_sort"/>
        <result property="joinTableListShow" column="join_table_list_show"/>
        <result property="joinTableFormShow" column="join_table_form_show"/>
    </resultMap>

    <!-- 表单新增 -->
    <insert id="saveForm" parameterType="map">
        INSERT INTO dynamic_config_form(
            id,
            table_name,
            field_name,
            field_explain,
            field_type,
            field_default,
            dictionary_id,
            verify_type,
            verify_regular,
            field_sort,
            list_show,
            list_show_width,
            form_show,
            join_table,
            join_table_field,
            join_table_sort,
            join_table_list_show,
            join_table_form_show,
            gmt_create,
            creator,
            gmt_modified,
            modifier,
            is_delete
        ) VALUES(
            #{id},
            #{tableName},
            #{fieldName},
            #{fieldExplain},
            #{fieldType},
            #{fieldDefault},
            #{dictionaryId},
            #{verifyType},
            #{verifyRegular},
            #{fieldSort},
            #{listShow},
            #{listShowWidth},
            #{formShow},
            #{joinTable},
            #{joinTableField},
            #{joinTableSort},
            #{joinTableListShow},
            #{joinTableFormShow},
            #{gmtCreate},
            #{creator},
            #{gmtModified},
            #{modifier},
            #{isDelete}
        )
    </insert>

    <!-- 表单删除 -->
    <update id="removeForm" parameterType="map">
        UPDATE
            dynamic_config_form
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            id IN
        <foreach collection="ids" index="index" open="(" separator="," close=")">
            #{ids[${index}]}
        </foreach>
    </update>

    <!-- 表单修改 -->
    <update id="updateForm" parameterType="map">
        UPDATE
            dynamic_config_form
        SET
        <if test="tableName != null">
            table_name = #{tableName},
        </if>
        <if test="tableName != null">
            field_name = #{fieldName},
        </if>
        <if test="fieldExplain != null">
            field_explain = #{fieldExplain},
        </if>
        <if test="fieldType != null">
            field_type = #{fieldType},
        </if>
        <if test="fieldDefault != null">
            field_default = #{fieldDefault},
        </if>
        <if test="dictionaryId != null">
            dictionary_id = #{dictionaryId},
        </if>
        <if test="verifyType != null">
            verify_type = #{verifyType},
        </if>
        <if test="verifyRegular != null">
            verify_regular = #{verifyRegular},
        </if>
        <if test="fieldSort != null">
            field_sort = #{fieldSort},
        </if>
        <if test="listShow != null">
            list_show = #{listShow},
        </if>
        <if test="listShowWidth != null">
            list_show_width = #{listShowWidth},
        </if>
        <if test="formShow != null">
            form_show = #{formShow},
        </if>
        <if test="joinTable != null">
            join_table = #{joinTable},
        </if>
        <if test="joinTableField != null">
            join_table_field = #{joinTableField},
        </if>
        <if test="joinTableSort != null">
            join_table_sort = #{joinTableSort},
        </if>
        <if test="joinTableListShow != null">
            join_table_list_show = #{joinTableListShow},
        </if>
        <if test="joinTableFormShow != null">
            join_table_form_show = #{joinTableFormShow},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            id = #{id}
    </update>

    <!-- 修改表单中的表名 -->
    <update id="updateFormTableName" parameterType="map">
        UPDATE
            dynamic_config_form
        SET
            table_name = #{newTableName},
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            table_name = #{oldTableName}
    </update>

    <!-- 表单详情 -->
    <select id="getFormById" parameterType="map" resultMap="dynamicConfigFormDTO">
        SELECT
            t1.*,
            t2.dictionary_name
        FROM
            dynamic_config_form t1
        LEFT JOIN
            data_dictionary t2
        ON
            (t1.dictionary_id = t2.dictionary_id AND t2.is_delete = 0)
        WHERE
            t1.is_delete = 0
        AND
            t1.id = #{id}
    </select>

    <!-- 表单全部列表单 -->
    <select id="listForm" parameterType="map" resultMap="dynamicConfigFormDTO">
        SELECT
            t1.*,
            t2.dictionary_name
        FROM
            dynamic_config_form t1
        LEFT JOIN
            data_dictionary t2
        ON
            (t1.dictionary_id = t2.dictionary_id AND t2.is_delete = 0)
        WHERE
            t1.is_delete = 0
        <if test="tableName != null">
        AND
            t1.table_name = #{tableName}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            t1.table_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.field_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY
            t1.field_sort
    </select>

</mapper>