<?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.activiti.dao.oa.INodeFieldDao">

    <resultMap id="nodeFieldDTO" type="ink.wgink.module.activiti.pojo.dtos.oa.NodeFieldDTO">
        <id column="node_field_id" property="nodeFieldId"/>
        <result column="deployment_id" property="deploymentId"/>
        <result column="form_id" property="formId"/>
        <result column="node_index" property="nodeIndex"/>
        <result column="node_id" property="nodeId"/>
        <result column="field_name" property="fieldName"/>
        <result column="is_visible" property="isVisible"/>
        <result column="is_editable" property="isEditable"/>
        <result column="edit_history" property="editHistory"/>
        <result column="auto_back_fill" property="autoBackFill"/>
    </resultMap>

    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `oa_node_field` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `node_field_id` char(36) DEFAULT NULL COMMENT '主键',
            `model_id` char(36) DEFAULT NULL COMMENT '模型ID',
            `model_version` int(11) DEFAULT NULL COMMENT '模型版本',
            `deployment_id` char(36) DEFAULT NULL COMMENT '部署ID',
            `form_id` char(36) DEFAULT NULL COMMENT '表单ID',
            `node_id` char(60) DEFAULT NULL COMMENT '节点ID',
            `node_index` int(11) DEFAULT NULL COMMENT '节点下标',
            `field_id` char(36) DEFAULT NULL COMMENT '字段ID',
            `field_name` varchar(255) DEFAULT NULL COMMENT '字段名',
            `field_explain` varchar(255) DEFAULT NULL COMMENT '字段描述',
            `is_visible` int(1) DEFAULT '1' COMMENT '是否可见',
            `is_editable` int(1) DEFAULT '1' COMMENT '是否可编辑',
            `editHistory` varchar(255) DEFAULT 'noRecord' COMMENT '编辑历史',
            `auto_back_fill` varchar(255) DEFAULT 'noBackFill' COMMENT '自动回填',
            PRIMARY KEY (`id`),
            UNIQUE KEY `node_field_id` (`node_field_id`),
            KEY `deployment_id` (`deployment_id`),
            KEY `deployment_id_2` (`deployment_id`),
            KEY `form_id` (`form_id`),
            KEY `node_id` (`node_id`),
            KEY `model_id` (`model_id`,`model_version`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='节点字段绑定';
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO oa_node_field(
            node_field_id,
            model_id,
            model_version,
            form_id,
            node_id,
            node_index,
            field_id,
            field_name,
            field_explain,
            is_visible,
            is_editable,
            edit_history,
            auto_back_fill
        ) VALUES(
            #{nodeFieldId},
            #{modelId},
            #{modelVersion},
            #{formId},
            #{nodeId},
            #{nodeIndex},
            #{fieldId},
            #{fieldName},
            #{fieldExplain},
            #{isVisible},
            #{isEditable},
            #{editHistory},
            #{autoBackFill}
        )
    </insert>

    <!-- 删除 -->
    <delete id="delete" parameterType="map">
        DELETE FROM
            oa_node_field
        WHERE
            model_id = #{modelId}
        AND
            model_version = #{modelVersion}
        <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
        </if>
        <if test="nodeId != null and nodeId != ''">
        AND
            node_id = #{nodeId}
        </if>
    </delete>

    <!-- 更新 -->
    <update id="update" parameterType="map">
        UPDATE
            oa_node_field
        SET
        <if test="isVisible != null">
            is_visible = #{isVisible},
        </if>
        <if test="isEditable != null">
            is_editable = #{isEditable},
        </if>
        <if test="editHistory != null and editHistory != ''">
            edit_history = #{editHistory},
        </if>
        <if test="autoBackFill != null and autoBackFill != ''">
            auto_back_fill = #{autoBackFill},
        </if>
            node_field_id = #{nodeFieldId}
        WHERE
            node_field_id = #{nodeFieldId}
    </update>

    <!-- 更新部署ID -->
    <update id="updateDeploymentId" parameterType="map">
        UPDATE
            oa_node_field
        SET
            deployment_id = #{deploymentId}
        WHERE
            model_id = #{modelId}
        AND
            model_version = #{modelVersion}
    </update>

    <!-- 删除部署ID -->
    <update id="deleteDeploymentId" parameterType="java.lang.String">
        UPDATE
            oa_node_field
        SET
            deployment_id = NULL
        WHERE
            deployment_id = #{_parameter}
    </update>

    <!-- 节点列表 -->
    <select id="listNode" parameterType="map" resultMap="nodeFieldDTO">
        SELECT
            deployment_id,
            form_id,
            flow_node_index,
            flow_node_id,
            flow_node_name
        FROM
            oa_node_field
        <where>
            <if test="deploymentId != null and deploymentId != ''">
        AND
            deployment_id = #{deploymentId}
            </if>
            <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
            </if>
        </where>
        GROUP BY
            deployment_id,
            form_id,
            flow_node_index,
            flow_node_id,
            flow_node_name
        ORDER BY
            flow_node_index
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="nodeFieldDTO">
        SELECT
            node_field_id,
            deployment_id,
            form_id,
            node_index,
            node_id,
            field_name,
            is_visible,
            is_editable,
            edit_history,
            auto_back_fill
        FROM
            oa_node_field
        <where>
            <if test="deploymentId != null and deploymentId != ''">
        AND
            deployment_id = #{deploymentId}
            </if>
            <if test="formId != null and formId != ''">
        AND
            form_id = #{formId}
            </if>
            <if test="nodeId != null and nodeId != ''">
        AND
            node_id = #{nodeId}
            </if>
        </where>
        ORDER BY
            node_index
    </select>

</mapper>