package com.zt.plat.module.base.service.masterdatasync.dto;

import lombok.Data;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 主数据同步的执行结果统计。
 */
@Data
public class MasterDataSyncReport {

    private long processedRecords;
    private long insertedMaterials;
    private long updatedMaterials;
    private long insertedClasses;
    private long updatedClasses;
    private long createdClassRelations;
    private long updatedClassRelations;
    private long insertedPropertyDefinitions;
    private long updatedPropertyDefinitions;
    private long insertedPropertyValues;
    private long updatedPropertyValues;
    private LocalDateTime startedAt;
    private LocalDateTime finishedAt;
    private boolean success;
    private String message;
    private Integer batchSize;
    private LocalDateTime since;
    private List<String> materialCodes = Collections.emptyList();
    private Long recordLimit;

    public static MasterDataSyncReport start(MasterDataSyncCommand command) {
        MasterDataSyncReport report = new MasterDataSyncReport();
        report.setStartedAt(LocalDateTime.now());
        report.setBatchSize(command.getBatchSize());
        report.setSince(command.getSince());
        report.setMaterialCodes(command.getMaterialCodes());
        report.setRecordLimit(command.getRecordLimit());
        report.setSuccess(false);
        report.setMessage("执行中");
        return report;
    }

    public void finish() {
        this.finishedAt = LocalDateTime.now();
    }

    public void markSuccess() {
        this.success = true;
        this.message = "执行成功";
    }

    public void markFailure(String message) {
        this.success = false;
        this.message = message;
    }

    public void incrementProcessedRecords(long delta) {
        this.processedRecords += delta;
    }

    public void incrementInsertedMaterials() {
        this.insertedMaterials++;
    }

    public void incrementUpdatedMaterials() {
        this.updatedMaterials++;
    }

    public void incrementInsertedClasses() {
        this.insertedClasses++;
    }

    public void incrementUpdatedClasses() {
        this.updatedClasses++;
    }

    public void incrementCreatedClassRelations() {
        this.createdClassRelations++;
    }

    public void incrementUpdatedClassRelations() {
        this.updatedClassRelations++;
    }

    public void incrementInsertedPropertyDefinitions() {
        this.insertedPropertyDefinitions++;
    }

    public void incrementUpdatedPropertyDefinitions() {
        this.updatedPropertyDefinitions++;
    }

    public void incrementInsertedPropertyValues() {
        this.insertedPropertyValues++;
    }

    public void incrementUpdatedPropertyValues() {
        this.updatedPropertyValues++;
    }
}
