package com.qqchen.deploy.backend.workflow.entity;

import com.qqchen.deploy.backend.framework.annotation.LogicDelete;
import com.qqchen.deploy.backend.framework.domain.Entity;
import com.qqchen.deploy.backend.workflow.enums.FormBusinessTypeEnums;
import com.qqchen.deploy.backend.workflow.enums.FormDataStatusEnums;
import com.vladmihalcea.hibernate.type.json.JsonType;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Type;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 表单数据实体
 *
 * @author qqchen
 * @date 2025-10-24
 */
@Data
@Table(name = "form_data")
@jakarta.persistence.Entity
@EqualsAndHashCode(callSuper = true)
@LogicDelete
public class FormData extends Entity<Long> {

    /**
     * 表单定义ID
     */
    @Column(name = "form_definition_id", nullable = false)
    private Long formDefinitionId;

    /**
     * 表单标识（冗余存储，避免JOIN）
     */
    @Column(name = "form_key", nullable = false)
    private String formKey;

    /**
     * 表单版本（冗余存储，用于历史追溯）
     */
    @Column(name = "form_version", nullable = false)
    private Integer formVersion;

    /**
     * 表单分类ID（冗余存储，便于统计和查询）
     */
    @Column(name = "category_id")
    private Long categoryId;

    /**
     * 业务标识（如工作流实例ID、订单号等）
     */
    @Column(name = "business_key")
    private String businessKey;

    /**
     * 业务类型（WORKFLOW-工作流、ORDER-订单、STANDALONE-独立表单）
     */
    @Column(name = "business_type")
    @Enumerated(EnumType.STRING)
    private FormBusinessTypeEnums businessType;

    /**
     * 表单填写数据（用户提交的实际数据）
     */
    @Type(JsonType.class)
    @Column(name = "data", nullable = false, columnDefinition = "json")
    private Map<String, Object> data;

    /**
     * 表单Schema快照（用于历史追溯，确保数据可还原，原样存储）
     */
    @Type(JsonType.class)
    @Column(name = "schema_snapshot", nullable = false, columnDefinition = "json")
    private Map<String, Object> schemaSnapshot;

    /**
     * 提交人
     */
    @Column(name = "submitter")
    private String submitter;

    /**
     * 提交时间
     */
    @Column(name = "submit_time")
    private LocalDateTime submitTime;

    /**
     * 状态（DRAFT-草稿、SUBMITTED-已提交、COMPLETED-已完成、REJECTED-已拒绝）
     */
    @Column(name = "status", nullable = false)
    @Enumerated(EnumType.STRING)
    private FormDataStatusEnums status;
}

