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

    <resultMap id="oaNodeAssigneePO" type="ink.wgink.module.activiti.pojo.pos.oa.OaNodeAssigneePO">
        <id column="id" property="id"/>
        <result column="node_assignee_id" property="nodeAssigneeId"/>
        <result column="model_id" property="modelId"/>
        <result column="model_version" property="modelVersion"/>
        <result column="deployment_id" property="deploymentId"/>
        <result column="node_id" property="nodeId"/>
        <result column="node_index" property="nodeIndex"/>
        <result column="node_type" property="nodeType"/>
        <result column="assignee_count" property="assigneeCount"/>
        <result column="assignee" property="assignee"/>
        <result column="assignee_name" property="assigneeName"/>
        <result column="candidates" property="candidates"/>
        <result column="candidate_names" property="candidateNames"/>
        <result column="multiple_type" property="multipleType"/>
        <result column="complete_condition" property="completeCondition"/>
        <result column="complete_count" property="completeCount"/>
        <result column="multiple_assignees" property="multipleAssignees"/>
        <result column="multiple_assignee_names" property="multipleAssigneeNames"/>
        <result column="assignee_type" property="assigneeType"/>
        <result column="auto_assign_type" property="autoAssignType"/>
        <result column="department_type" property="departmentType"/>
        <result column="departments" property="departments"/>
        <result column="department_names" property="departmentNames"/>
        <result column="roles" property="roles"/>
        <result column="role_names" property="roleNames"/>
        <result column="positions" property="positions"/>
        <result column="position_names" property="positionNames"/>
        <result column="quick_assignee" property="quickAssignee"/>
        <result column="previous_n_step" property="previousNStep"/>
    </resultMap>

    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `oa_node_assignee` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `node_assignee_id` char(36) DEFAULT NULL,
            `model_id` char(36) DEFAULT NULL COMMENT '模型ID',
            `model_version` int(11) DEFAULT NULL COMMENT '模型版本',
            `deployment_id` char(36) DEFAULT NULL COMMENT '部署ID',
            `node_id` char(60) DEFAULT NULL COMMENT '节点ID',
            `node_index` int(11) DEFAULT NULL COMMENT '节点下标',
            `node_type` varchar(255) DEFAULT NULL COMMENT '节点类型',
            `assignee_count` varchar(255) DEFAULT NULL COMMENT '代理人数量',
            `assignee` varchar(255) DEFAULT NULL COMMENT '代理人',
            `assignee_name` varchar(255) DEFAULT NULL COMMENT '代理人名称',
            `candidates` varchar(255) DEFAULT NULL COMMENT '候选人',
            `candidate_names` varchar(255) DEFAULT NULL COMMENT '候选人名称',
            `multiple_type` varchar(255) DEFAULT NULL COMMENT '多实例类型',
            `complete_condition` varchar(255) DEFAULT NULL COMMENT '完成条件',
            `complete_count` int(11) DEFAULT NULL COMMENT '完成人数量',
            `multiple_assignees` varchar(255) DEFAULT NULL COMMENT '多实例候选人',
            `multiple_assignee_names` varchar(255) DEFAULT NULL COMMENT '多实例候选人名称',
            `assignee_type` varchar(255) DEFAULT NULL COMMENT '代理人类型',
            `auto_assign_type` varchar(255) DEFAULT NULL COMMENT '自动方式',
            `department_type` varchar(255) DEFAULT NULL COMMENT '部门类型',
            `departments` varchar(255) DEFAULT NULL COMMENT '部门',
            `department_names` varchar(255) DEFAULT NULL COMMENT '部门名称',
            `roles` varchar(255) DEFAULT NULL COMMENT '角色',
            `role_names` varchar(255) DEFAULT NULL COMMENT '角色名称',
            `positions` varchar(255) DEFAULT NULL COMMENT '职位',
            `position_names` varchar(255) DEFAULT NULL COMMENT '职位名称',
            `quick_assignee` varchar(255) DEFAULT NULL COMMENT '快速代理人',
            `previous_n_step` int(11) DEFAULT NULL COMMENT '前N步',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='节点代理人';
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO oa_node_assignee(
            node_assignee_id,
            model_id,
            model_version,
            node_id,
            node_index,
            node_type,
            assignee_count,
            assignee,
            assignee_name,
            candidates,
            candidate_names,
            multiple_type,
            complete_condition,
            complete_count,
            multiple_assignees,
            multiple_assignee_names,
            assignee_type,
            auto_assign_type,
            department_type,
            departments,
            department_names,
            roles,
            role_names,
            positions,
            position_names,
            quick_assignee,
            previous_n_step
        ) VALUES(
            #{nodeFieldId},
            #{modelId},
            #{modelVersion},
            #{nodeId},
            #{nodeIndex},
            #{nodeType},
            #{assigneeCount},
            #{assignee},
            #{assigneeName},
            #{candidates},
            #{candidateNames},
            #{multipleType},
            #{completeCondition},
            #{completeCount},
            #{multipleAssignees},
            #{multipleAssigneeNames},
            #{assigneeType},
            #{autoAssignType},
            #{departmentType},
            #{departments},
            #{departmentNames},
            #{roles},
            #{roleNames},
            #{positions},
            #{positionNames},
            #{quickAssignee},
            #{previousNStep}
        )
    </insert>

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

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

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

    <!-- 详情 -->
    <select id="getPO" parameterType="map" resultMap="oaNodeAssigneePO">
        SELECT
            id,
            node_assignee_id,
            model_id,
            model_version,
            deployment_id,
            node_id,
            node_index,
            node_type,
            assignee_count,
            assignee,
            assignee_name,
            candidates,
            candidate_names,
            multiple_type,
            complete_condition,
            complete_count,
            multiple_assignees,
            multiple_assignee_names,
            assignee_type,
            auto_assign_type,
            department_type,
            departments,
            department_names,
            roles,
            role_names,
            positions,
            position_names,
            quick_assignee,
            previous_n_step
        FROM
            oa_node_assignee
        <where>
            <if test="nodeAssigneeId != null and nodeAssigneeId != ''">
            node_assignee_id = #{nodeAssigneeId}
            </if>
            <if test="modelId != null and modelId != ''">
        AND
            model_id = #{modelId}
            </if>
            <if test="modelVersion != null">
        AND
            model_version = #{modelVersion}
            </if>
            <if test="deploymentId != null and deploymentId != ''">
        AND
            deployment_id = #{deploymentId}
            </if>
             <if test="nodeId != null and nodeId != ''">
        AND
            node_id = #{nodeId}
            </if>
        </where>
    </select>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="oaNodeAssigneePO">
        SELECT
            id,
            node_assignee_id,
            model_id,
            model_version,
            deployment_id,
            node_id,
            node_index,
            node_type,
            assignee_count,
            assignee,
            assignee_name,
            candidates,
            candidate_names,
            multiple_type,
            complete_condition,
            complete_count,
            multiple_assignees,
            multiple_assignee_names,
            assignee_type,
            auto_assign_type,
            department_type,
            departments,
            department_names,
            roles,
            role_names,
            positions,
            position_names,
            quick_assignee,
            previous_n_step
        FROM
            oa_node_assignee
        <where>
            <if test="modelId != null and modelId != ''">
            model_id = #{modelId}
            </if>
            <if test="modelVersion != null">
        AND
            model_version = #{modelVersion}
            </if>
            <if test="deploymentId != null and deploymentId != ''">
        AND
            deployment_id = #{deploymentId}
            </if>
        </where>
    </select>

</mapper>