package com.zt.plat.module.base.controller.admin.plandate.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 实时报表树形结构返回VO（包含执行量字段）
 */
@Data
@Schema(description = "管理后台 - 实时报表树形结构 Response VO")
public class RealTimeReportsTreeRespVO {

    // ========== 基础节点信息 ==========
    @Schema(description = "主键 ID", example = "10584")
    private Long id;

    @Schema(description = "计划名称", example = "铜精矿年度计划")
    private String planName;

    @Schema(description = "计划编码")
    private String planCoding;

    @Schema(description = "父级ID", example = "6059")
    private Long parentId;

    @Schema(description = "年份", example = "2025")
    private String year;

    @Schema(description = "初始值")
    private BigDecimal initialValue;

    @Schema(description = "平均值")
    private BigDecimal averageValue;

    @Schema(description = "合计值（计划）")
    private BigDecimal sumValue;

    // ========== 执行量核心字段 ==========
    @Schema(description = "合计执行量（所有月份执行量总和）")
    private BigDecimal sumExecuteValue;

    @Schema(description = "执行率（sumExecuteValue / sumValue * 100）")
    private BigDecimal executeRate;

    // ========== 12个月计划值（保留原字段） ==========
    @Schema(description = "一月计划值")
    private BigDecimal january;

    @Schema(description = "二月计划值")
    private BigDecimal february;

    @Schema(description = "三月计划值")
    private BigDecimal march;

    @Schema(description = "四月计划值")
    private BigDecimal april;

    @Schema(description = "五月计划值")
    private BigDecimal may;

    @Schema(description = "六月计划值")
    private BigDecimal june;

    @Schema(description = "七月计划值")
    private BigDecimal july;

    @Schema(description = "八月计划值")
    private BigDecimal august;

    @Schema(description = "九月计划值")
    private BigDecimal september;

    @Schema(description = "十月计划值")
    private BigDecimal october;

    @Schema(description = "十一月计划值")
    private BigDecimal november;

    @Schema(description = "十二月计划值")
    private BigDecimal december;

    // ========== 12个月执行量（新增核心字段） ==========
    @Schema(description = "一月执行量")
    private BigDecimal januaryExecute;

    @Schema(description = "二月执行量")
    private BigDecimal februaryExecute;

    @Schema(description = "三月执行量")
    private BigDecimal marchExecute;

    @Schema(description = "四月执行量")
    private BigDecimal aprilExecute;

    @Schema(description = "五月执行量")
    private BigDecimal mayExecute;

    @Schema(description = "六月执行量")
    private BigDecimal juneExecute;

    @Schema(description = "七月执行量")
    private BigDecimal julyExecute;

    @Schema(description = "八月执行量")
    private BigDecimal augustExecute;

    @Schema(description = "九月执行量")
    private BigDecimal septemberExecute;

    @Schema(description = "十月执行量")
    private BigDecimal octoberExecute;

    @Schema(description = "十一月执行量")
    private BigDecimal novemberExecute;

    @Schema(description = "十二月执行量")
    private BigDecimal decemberExecute;

    // ========== 其他基础字段 ==========
    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    // ========== 树形结构核心字段 ==========
    @Schema(description = "子节点列表")
    private List<RealTimeReportsTreeRespVO> children;

    @Schema(description = "是否为叶子节点", example = "true")
    private Boolean isLeaf;

    @Schema(description = "节点层级（根节点为0）", example = "0")
    private Integer level;

    // ========== 辅助方法：自动计算合计执行量和执行率 ==========
    /**
     * 计算当前节点的合计执行量（12个月执行量求和）
     */
    public void calculateSumExecuteValue() {
        this.sumExecuteValue = BigDecimal.ZERO;
        // 累加12个月执行量（空值处理为0）
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(januaryExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(februaryExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(marchExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(aprilExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(mayExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(juneExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(julyExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(augustExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(septemberExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(octoberExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(novemberExecute));
        this.sumExecuteValue = this.sumExecuteValue.add(getNotNullValue(decemberExecute));
    }

    /**
     * 计算执行率（执行量/计划值 * 100，保留2位小数）
     */
    public void calculateExecuteRate() {
        if (this.sumValue == null || this.sumValue.compareTo(BigDecimal.ZERO) == 0) {
            this.executeRate = BigDecimal.ZERO;
            return;
        }
        if (this.sumExecuteValue == null) {
            this.calculateSumExecuteValue();
        }
        // 执行率 = (执行量 / 计划值) * 100，保留2位小数
        this.executeRate = this.sumExecuteValue.divide(this.sumValue, 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal("100"))
                .setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 空值处理：null转为BigDecimal.ZERO
     */
    private BigDecimal getNotNullValue(BigDecimal value) {
        return value == null ? BigDecimal.ZERO : value;
    }
}