package com.erp.report.export.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.erp.report.export.dto.ExportFormat;
import com.erp.report.export.dto.ExportStatus;
import com.erp.report.export.dto.ExportType;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

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

/**
 * 数据导出记录实体
 * 对应数据库表：data_exports
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("data_exports")
public class DataExportEntity {

    @TableId(type = IdType.AUTO)
    private Long id;

    private Long tenantId;

    private Long userId;

    private ExportType type;

    private ExportFormat format;

    private String filename;

    private String filePath;

    private Long fileSize;

    private Integer rowCount;

    private ExportStatus status;

    private String errorMsg;

    /**
     * 导出时的过滤条件JSON
     */
    private String filtersJson;

    /**
     * 定时任务ID（如果是定时任务触发的导出）
     */
    private String scheduledTaskId;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    /**
     * 标记导出完成
     */
    public void markCompleted(String filePath, long fileSize, int rowCount) {
        this.filePath = filePath;
        this.fileSize = fileSize;
        this.rowCount = rowCount;
        this.status = ExportStatus.COMPLETED;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 标记导出失败
     */
    public void markFailed(String errorMsg) {
        this.status = ExportStatus.FAILED;
        this.errorMsg = errorMsg;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 标记为处理中
     */
    public void markProcessing() {
        this.status = ExportStatus.PROCESSING;
        this.updatedAt = LocalDateTime.now();
    }
}
