<?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.design.IFormDao">

    <resultMap id="formPO" type="ink.wgink.module.form.pojo.pos.design.FormPO">
        <id column="id" property="id"/>
        <result column="form_id" property="formId"/>
        <result column="form_code" property="formCode"/>
        <result column="form_name" property="formName"/>
        <result column="form_summary" property="formSummary"/>
        <result column="form_type" property="formType"/>
        <result column="form_status" property="formStatus"/>
        <result column="form_source_data" property="formSourceData"/>
        <result column="save_page_code" property="savePageCode"/>
        <result column="update_page_code" property="updatePageCode"/>
        <result column="show_page_code" property="showPageCode"/>
        <result column="print_page_code" property="printPageCode"/>
        <result column="app_save_page_code" property="appSavePageCode"/>
        <result column="app_update_page_code" property="appUpdatePageCode"/>
        <result column="app_show_page_code" property="appShowPageCode"/>
        <result column="form_table_name" property="formTableName"/>
        <result column="form_version" property="formVersion"/>
        <result column="main_title_tpl" property="mainTitleTpl"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="formDTO" type="ink.wgink.module.form.pojo.dtos.design.FormDTO">
        <result column="form_id" property="formId"/>
        <result column="form_code" property="formCode"/>
        <result column="form_name" property="formName"/>
        <result column="form_summary" property="formSummary"/>
        <result column="form_type" property="formType"/>
        <result column="form_status" property="formStatus"/>
        <result column="form_source_data" property="formSourceData"/>
        <result column="save_page_code" property="savePageCode"/>
        <result column="update_page_code" property="updatePageCode"/>
        <result column="show_page_code" property="showPageCode"/>
        <result column="print_page_code" property="printPageCode"/>
        <result column="app_save_page_code" property="appSavePageCode"/>
        <result column="app_update_page_code" property="appUpdatePageCode"/>
        <result column="app_show_page_code" property="appShowPageCode"/>
        <result column="form_table_name" property="formTableName"/>
        <result column="form_version" property="formVersion"/>
        <result column="main_title_tpl" property="mainTitleTpl"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `form_form` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `form_id` char(36) DEFAULT NULL COMMENT '主键',
            `form_code` varchar(255) DEFAULT NULL COMMENT '表单编码',
            `form_name` varchar(255) DEFAULT NULL COMMENT '表单名称',
            `form_summary` text COMMENT '表单描述',
            `form_type` varchar(20) DEFAULT NULL COMMENT '表单类型：default、oa',
            `form_status` varchar(20) DEFAULT NULL COMMENT '表单状态：unactive、active',
            `form_source_data` longtext COMMENT '表单页面源数据',
            `save_page_code` longtext COMMENT '新增页面代码',
            `update_page_code` longtext COMMENT '更新页面代码',
            `show_page_code` longtext COMMENT '展示页面代码',
            `print_page_code` longtext COMMENT '打印页面代码',
            `app_save_page_code` longtext COMMENT 'APP新增页面代码',
            `app_update_page_code` longtext COMMENT 'APP更新页面代码',
            `app_show_page_code` longtext COMMENT 'APP展示页面代码',
            `form_table_name` varchar(255) DEFAULT NULL COMMENT '表单表名',
            `form_version` int(11) DEFAULT NULL COMMENT '表单版本',
            `main_title_tpl` varchar(255) DEFAULT NULL COMMENT '主标题模板',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `is_delete` int(1) DEFAULT '0' COMMENT '是否删除',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='表单';
    </update>

    <!-- 创建动态表单 -->
    <update id="createDynamicForm" parameterType="java.lang.String">
        ${_parameter}
    </update>

    <!-- 删除表单 -->
    <update id="deleteDynamicForm" parameterType="java.lang.String">
        ${_parameter}
    </update>

    <!-- 更新动态表单字段 -->
    <update id="updateDynamicFormField" parameterType="java.lang.String">
        ${_parameter}
    </update>

    <!-- 删除动态表单字段 -->
    <update id="deleteDynamicFormField" parameterType="java.lang.String">
        ${_parameter}
    </update>

    <!-- 查询表数量 -->
    <select id="countTable" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            information_schema.tables
        WHERE
            table_name = #{_parameter}
    </select>

    <!-- 新增表单 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO form_form(
            form_id,
            form_code,
            form_name,
            form_summary,
            form_type,
            form_status,
            form_source_data,
            save_page_code,
            update_page_code,
            show_page_code,
            print_page_code,
            app_save_page_code,
            app_update_page_code,
            app_show_page_code,
            form_table_name,
            form_version,
            main_title_tpl,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{formId},
            #{formCode},
            #{formName},
            #{formSummary},
            #{formType},
            #{formStatus},
            #{formSourceData},
            #{savePageCode},
            #{updatePageCode},
            #{showPageCode},
            #{printPageCode},
            #{appSavePageCode},
            #{appUpdatePageCode},
            #{appShowPageCode},
            #{formTableName},
            #{formVersion},
            #{mainTitleTpl},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除表单（物理删除） -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            form_form
        WHERE
            form_id IN
        <foreach collection="formIds" index="index" open="(" separator="," close=")">
            #{formIds[${index}]}
        </foreach>
    </delete>

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

    <!-- 更新表单 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            form_form
        SET
        <if test="formCode != null and formCode != ''">
            form_code = #{formCode},
        </if>
        <if test="formName != null and formName != ''">
            form_name = #{formName},
        </if>
        <if test="formSummary != null and formSummary != ''">
            form_summary = #{formSummary},
        </if>
        <if test="formType != null and formType != ''">
            form_type = #{formType},
        </if>
        <if test="formStatus != null and formStatus != ''">
            form_status = #{formStatus},
        </if>
        <if test="formSourceData != null and formSourceData != ''">
            form_source_data = #{formSourceData},
        </if>
        <if test="savePageCode != null and savePageCode != ''">
            save_page_code = #{savePageCode},
        </if>
        <if test="updatePageCode != null and updatePageCode != ''">
            update_page_code = #{updatePageCode},
        </if>
        <if test="showPageCode != null and showPageCode != ''">
            show_page_code = #{showPageCode},
        </if>
        <if test="printPageCode != null and printPageCode != ''">
            print_page_code = #{printPageCode},
        </if>
        <if test="appSavePageCode != null and appSavePageCode != ''">
            app_save_page_code = #{appSavePageCode},
        </if>
        <if test="appUpdatePageCode != null and appUpdatePageCode != ''">
            app_update_page_code = #{appUpdatePageCode},
        </if>
        <if test="appShowPageCode != null and appShowPageCode != ''">
            app_show_page_code = #{appShowPageCode},
        </if>
        <if test="formTableName != null and formTableName != ''">
            form_table_name = #{formTableName},
        </if>
        <if test="formVersion != null">
            form_version = #{formVersion},
        </if>
        <if test="mainTitleTpl != null">
            main_title_tpl = #{mainTitleTpl},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            form_id = #{formId}
    </update>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultMap="formDTO">
        SELECT
            form_id,
            form_code,
            form_name,
            form_summary,
            form_type,
            form_status,
            form_source_data,
            form_table_name,
            form_version,
            main_title_tpl
        FROM
            form_form
        WHERE
            is_delete = 0
        <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
        </if>
        <if test="formCode != null and formCode != ''">
        AND
            form_code = #{formCode}
        </if>
    </select>

    <!-- 详情 -->
    <select id="getPO" parameterType="map" resultMap="formPO">
        SELECT
            form_id,
            form_code,
            form_name,
            form_summary,
            form_type,
            form_status,
            form_source_data,
            save_page_code,
            update_page_code,
            show_page_code,
            print_page_code,
            app_save_page_code,
            app_update_page_code,
            app_show_page_code,
            form_table_name,
            form_version,
            main_title_tpl
        FROM
            form_form
        WHERE
            is_delete = 0
        <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
        </if>
        <if test="formCode != null and formCode != ''">
        AND
            form_code = #{formCode}
        </if>
        <if test="formVersion != null">
        AND
            form_version = #{formVersion}
        </if>
    </select>

    <!-- 最新的版本 -->
    <select id="getLatestVersionByCode" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
            form_version
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{_parameter}
        ORDER BY
            form_version DESC
        LIMIT 0, 1
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="formDTO">
        SELECT
            form_id,
            form_code,
            form_name,
            form_summary,
            form_type,
            form_status,
            form_table_name,
            form_version,
            main_title_tpl
        FROM
            form_form
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            form_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="formIds != null and formIds.size > 0">
        AND
            form_id IN
            <foreach collection="formIds" index="index" open="(" separator="," close=")">
                #{formIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="formPO">
        SELECT
            form_id,
            form_code,
            form_name,
            form_summary,
            form_type,
            form_status,
            form_table_name,
            form_version,
            main_title_tpl
        FROM
            form_form
        WHERE
            is_delete = 0
    </select>

    <!-- 代码 -->
    <select id="getSavePageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            save_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getUpdatePageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            update_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getShowPageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            show_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getPrintPageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            print_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getAppSavePageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            app_save_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getAppUpdatePageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            app_update_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

    <!-- 代码 -->
    <select id="getAppShowPageCodeByCodeAndVersion" parameterType="map" resultType="java.lang.String">
        SELECT
            app_show_page_code
        FROM
            form_form
        WHERE
            is_delete = 0
        AND
            form_code = #{formCode}
        AND
            form_version = #{formVersion}
    </select>

</mapper>