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

    <resultMap id="formFieldPO" type="ink.wgink.module.form.pojo.pos.design.FormFieldPO">
        <id column="id" property="id"/>
        <result column="form_id" property="formId"/>
        <result column="field_id" property="fieldId"/>
        <result column="field_name" property="fieldName"/>
        <result column="field_explain" property="fieldExplain"/>
        <result column="field_type" property="fieldType"/>
        <result column="field_tag" property="fieldTag"/>
        <result column="field_default" property="fieldDefault"/>
        <result column="field_default" property="fieldDefault"/>
        <result column="list_show" property="listShow"/>
        <result column="field_required" property="fieldRequired"/>
    </resultMap>

    <resultMap id="formFieldDTO" type="ink.wgink.module.form.pojo.dtos.design.FormFieldDTO">
        <result column="form_id" property="formId"/>
        <result column="field_id" property="fieldId"/>
        <result column="field_name" property="fieldName"/>
        <result column="field_explain" property="fieldExplain"/>
        <result column="field_type" property="fieldType"/>
        <result column="field_tag" property="fieldTag"/>
        <result column="field_default" property="fieldDefault"/>
        <result column="list_show" property="listShow"/>
        <result column="field_required" property="fieldRequired"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `form_field` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `field_id` char(36) DEFAULT NULL COMMENT '主键',
            `form_id` char(36) DEFAULT NULL COMMENT '字段ID',
            `field_name` varchar(255) DEFAULT NULL COMMENT '字段名',
            `field_explain` varchar(255) DEFAULT NULL COMMENT '字段说明',
            `field_type` varchar(255) DEFAULT NULL COMMENT '字段类型',
            `field_tag` varchar(255) DEFAULT NULL COMMENT '标签类型',
            `field_default` varchar(255) DEFAULT NULL COMMENT '字段默认',
            `field_required` varchar(255) DEFAULT 'none' COMMENT '字段校验',
            `list_show` varchar(10) DEFAULT 'false' COMMENT '是否列表显示',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='字段字段';
    </update>

    <!-- 新增字段 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO form_field(
            field_id,
            form_id,
            field_name,
            field_explain,
            field_type,
            field_tag,
            field_default,
            field_required,
            list_show
        ) VALUES(
            #{fieldId},
            #{formId},
            #{fieldName},
            #{fieldExplain},
            #{fieldType},
            #{fieldTag},
            #{fieldDefault},
            #{fieldRequired},
            #{listShow}
        )
    </insert>

    <!-- 删除字段（物理删除） -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            form_field
        <where>
        <if test="fieldIds != null and fieldIds.size > 0">
            field_id IN
            <foreach collection="fieldIds" index="index" open="(" separator="," close=")">
                #{fieldIds[${index}]}
            </foreach>
        </if>
        <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
        </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>
        </where>
    </delete>

    <!-- 更新字段 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            form_field
        SET
            field_name = #{fieldName},
            field_explain = #{fieldExplain},
            field_type = #{fieldType},
            field_tag = #{fieldTag},
            field_default = #{fieldDefault},
            field_required = #{fieldRequired},
            list_show = #{listShow}
        WHERE
            field_id = #{fieldId}
    </update>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultMap="formFieldDTO" useCache="true">
        SELECT
            field_id,
            form_id,
            field_name,
            field_explain,
            field_type,
            field_tag,
            field_default,
            field_required,
            list_show
        FROM
            form_field
        WHERE
            field_id = #{fieldId}
    </select>

    <!-- 详情PO -->
    <select id="getPO" parameterType="map" resultMap="formFieldPO" useCache="true">
        SELECT
            field_id,
            form_id,
            field_name,
            field_explain,
            field_type,
            field_tag,
            field_default,
            field_required,
            list_show
        FROM
            form_field
        WHERE
            field_id = #{fieldId}
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="formFieldDTO" useCache="true">
        SELECT
            field_id,
            form_id,
            field_name,
            field_explain,
            field_type,
            field_tag,
            field_default,
            field_required,
            list_show
        FROM
            form_field
        <where>
            <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
            </if>
            <if test="formCode != null and formCode != '' and formVersion != ''">
        AND
            form_id = (
                SELECT
                    form_id
                FROM
                    form_form
                WHERE
                    is_delete = 0
                AND
                    form_code = #{formCode}
                AND
                    form_version = #{formVersion}
            )
            </if>
        </where>
    </select>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="formFieldPO" useCache="true">
        SELECT
            field_id,
            form_id,
            field_name,
            field_explain,
            field_type,
            field_tag,
            field_default,
            field_required,
            list_show
        FROM
            form_field
        <where>
            <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
            </if>
        </where>
    </select>

    <!-- 字段名列表 -->
    <select id="listFieldName" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            field_name
        FROM
            form_field
        <where>
            <if test="formId != null and formId != ''">
            form_id = #{formId}
            </if>
            <if test="listShow != null and listShow != ''">
        AND
            list_show = #{listShow}
            </if>
        </where>
    </select>

</mapper>