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

    <!-- 检查表是否存在 -->
    <select id="checkTableExists" resultType="int">
        SELECT COUNT(*)
        FROM information_schema.TABLES
        WHERE TABLE_SCHEMA = (SELECT DATABASE())
        AND TABLE_NAME = #{tableName}
    </select>

    <!-- 创建表 -->
    <update id="createTable">
        ${sql}
    </update>

    <!-- 删除表 -->
    <update id="dropTable">
        DROP TABLE IF EXISTS `${tableName}`
    </update>

    <!-- 动态插入数据 -->
    <insert id="insertData" useGeneratedKeys="true" keyProperty="data.id">
        INSERT INTO `${tableName}`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <foreach collection="data.keys" item="key" separator=",">
                `${key}`
            </foreach>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <foreach collection="data.keys" item="key" separator=",">
                #{data[${key}]}
            </foreach>
        </trim>
    </insert>

    <!-- 动态查询数据列表 -->
    <select id="selectDataList" resultType="map">
        SELECT * FROM `${tableName}`
        <where>
            del_flag = '0'
            <if test="params.companyId != null">
                AND company_id = #{params.companyId}
            </if>
            <if test="params.versionId != null">
                AND version_id = #{params.versionId}
            </if>
            <if test="params.userId != null">
                AND user_id = #{params.userId}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND submit_time &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND submit_time &lt;= #{params.endTime}
            </if>
        </where>
        ORDER BY submit_time DESC
    </select>

    <!-- 查询数据详情 -->
    <select id="selectDataById" resultType="map">
        SELECT * FROM `${tableName}` WHERE id = #{id} AND del_flag = '0'
    </select>

    <!-- 动态更新数据 -->
    <update id="updateData">
        UPDATE `${tableName}`
        <set>
            <foreach collection="data.keys" item="key" separator=",">
                <if test="key != 'id'">
                    `${key}` = #{data[${key}]}
                </if>
            </foreach>
        </set>
        WHERE id = #{data.id}
    </update>

    <!-- 删除数据(逻辑删除) -->
    <update id="deleteData">
        UPDATE `${tableName}` SET del_flag = '1' WHERE id = #{id}
    </update>


    <insert id="companyDoctorApplyinsertData" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO `${tableName}`
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <foreach collection="data.keys" item="key" separator=",">
                `${key}`
            </foreach>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <foreach collection="data.keys" item="key" separator=",">
                #{data[${key}]}
            </foreach>
        </trim>
    </insert>

</mapper>
