<?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="ink.wgink.module.form.dao.formreport.IFormReportDao">

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO df_${formCode}_v${formVersion}(
        <foreach collection="inserts" index="index" open="" separator="," close="">
            ${inserts[index]}
        </foreach>
        ) VALUES(
        <foreach collection="values" index="index" open="" separator="," close="">
            #{values[${index}]}
        </foreach>
        )
    </insert>

    <!-- 删除表单 -->
    <update id="remove" parameterType="map">
        UPDATE
            df_${formCode}_v${formVersion}
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            uid IN
        <foreach collection="uids" index="index" open="(" separator="," close=")">
            #{uids[${index}]}
        </foreach>
    </update>

    <!-- 更新表单 -->
    <update id="update" parameterType="map">
        UPDATE
            df_${formCode}_v${formVersion}
        SET
        <foreach collection="kvList" item="item" index="index" open="" separator="," close="">
            ${item[0]} = '${item[1]}'
        </foreach>
        WHERE
            is_delete = 0
        AND
            uid = #{uid}
        <if test="creator != null and creator != ''">
        AND
            creator = #{creator}
        </if>
    </update>

    <update id="updateProcessInstanceId" parameterType="map">
        UPDATE
            df_${formCode}_v${formVersion}
        SET
            process_instance_id = #{processInstanceId}
        WHERE
            uid = #{uid}
    </update>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultType="map">
        SELECT
            *
        FROM
            df_${formCode}_v${formVersion}
        WHERE
            is_delete = 0
        AND
            uid = #{uid}
        <if test="creator != null and creator != ''">
        AND
            creator = #{creator}
        </if>
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultType="map">
        SELECT
            <choose>
                <when test="listShowField != null and listShowField.size > 0">
                    <foreach collection="listShowField" index="item" separator=",">
            ${item}
                    </foreach>
            uid
                </when>
                <otherwise>
            *
                </otherwise>
            </choose>
        FROM
            df_${formCode}_v${formVersion}
        WHERE
            is_delete = 0
        <if test="creator != null and creator != ''">
        AND
            creator = #{creator}
        </if>
        <if test="uids != null and uids.size > 0">
        AND
            uid IN
            <foreach collection="uids" index="index" open="(" separator="," close=")">
                #{uids[${index}]}
            </foreach>
        </if>
        <trim prefix="AND">
            <foreach collection="queryList" item="item" index="index" open="" separator="AND" close="">
                ${item}
            </foreach>
        </trim>
    </select>

    <!-- 统计 -->
    <select id="countByTableName" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            ${_parameter}
    </select>

</mapper>