package com.hdkj.lt.mq.entity.dto;

import cn.hutool.core.bean.BeanUtil;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgInfoBreakerSvgAfter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description
 * @Author wcx
 * @Date 2025/6/16 9:07
 * @Version 1.0
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class SwitchTreeNode {
    // 方案ID
    private Long schemeId;
    // 设备PSRID
    private String id;
    // 父类设备PSRID
    private String pid;
    // 设备名称
    private String name;
    // 设备类型
    private String psrType;
    // 有功功率
    private BigDecimal afterP;
    // 无功功率
    private BigDecimal afterQ;
    // 电流
    private BigDecimal afterI;
    // 子节点信息
    private List<SwitchTreeNode> children;

    /**
     * 递归计算叶子结点的累加值并赋值
     */
    public static void calculateLeafSum(List<FhzgInfoBreakerSvgAfter> breakerSvgAfters, SwitchTreeNode treeNode, List<String> switchList) {
        // 节点不为空
        if (ObjectUtils.allNotNull(treeNode)) {
            // 如果是叶子节点
            if (ObjectUtils.isEmpty(treeNode.getChildren())) {
                SwitchTreeNode.setBreakerSvgAfterInfo(breakerSvgAfters, treeNode, switchList);
                return;
            }
            // 电流累加值
            AtomicReference<BigDecimal> sumAfterI = new AtomicReference<>();
            // 有功功率累加值
            AtomicReference<BigDecimal> sumAfterP = new AtomicReference<>();
            // 无功功率累加值
            AtomicReference<BigDecimal> sumAfterQ = new AtomicReference<>();
            // 非叶子节点
            for (SwitchTreeNode child : treeNode.getChildren()) {
                SwitchTreeNode.calculateLeafSum(breakerSvgAfters, child, switchList);// 递归获取子节点
                Optional.ofNullable(child.getAfterI()).ifPresent(node -> {
                    sumAfterI.set(Optional.ofNullable(sumAfterI).map(e -> e.get()).orElse(BigDecimal.ZERO));
                    sumAfterI.getAndAccumulate(node, BigDecimal::add);
                });
                Optional.ofNullable(child.getAfterP()).ifPresent(node -> {
                    sumAfterP.set(Optional.ofNullable(sumAfterP).map(e -> e.get()).orElse(BigDecimal.ZERO));
                    sumAfterP.getAndAccumulate(node, BigDecimal::add);
                });
                Optional.ofNullable(child.getAfterQ()).ifPresent(node -> {
                    sumAfterQ.set(Optional.ofNullable(sumAfterQ).map(e -> e.get()).orElse(BigDecimal.ZERO));
                    sumAfterQ.getAndAccumulate(node, BigDecimal::add);
                });
            }
            // 转供后累计IPQ值设置
            treeNode.setAfterI(sumAfterI.get()).setAfterP(sumAfterP.get()).setAfterQ(sumAfterQ.get());
            SwitchTreeNode.setBreakerSvgAfterInfo(breakerSvgAfters, treeNode, switchList);
        }
    }

    /**
     * 设置需要保存的转供后数据信息
     */
    private static void setBreakerSvgAfterInfo(List<FhzgInfoBreakerSvgAfter> breakerSvgAfters, SwitchTreeNode treeNode, List<String> switchList) {
        if (switchList.contains(treeNode.getPsrType())) {
            FhzgInfoBreakerSvgAfter fhzgInfoBreakerSvgAfter = BeanUtil.copyProperties(treeNode, FhzgInfoBreakerSvgAfter.class, "id");
            fhzgInfoBreakerSvgAfter.setStartSwitchId(treeNode.getId()).setCreateTime(LocalDateTime.now());
            breakerSvgAfters.add(fhzgInfoBreakerSvgAfter);
        }
    }
}
