package com.zhongshu.card.client.model.payment.statistic;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TRX
 * @date 2024/8/2
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BusinessMainStatisticModel {

    @Schema(description = "统计项")
    private List<StatisticItem> items = new ArrayList<>();

    @Schema(description = "当前金额,单位分")
    private BigDecimal amount = BigDecimal.ZERO;

    @Schema(description = "当前订单数量")
    private Long total = 0L;

    @Schema(description = "上一个金额,单位分")
    private BigDecimal preAmount = BigDecimal.ZERO;

    @Schema(description = "上一个订单数量")
    private Long preTotal = 0L;

    @Schema(description = "金额的增长")
    private BigDecimal percentageAmount = BigDecimal.ZERO;

    /**
     * 计算比例
     *
     * @return
     */
    public BigDecimal getPercentageAmount() {
        BigDecimal per = BigDecimal.ZERO;
        if (amount.compareTo(BigDecimal.ZERO) > 0 && preAmount.compareTo(BigDecimal.ZERO) > 0) {
            per = (amount.subtract(preAmount)).divide(preAmount, 4, BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return per;
    }

    @Schema(description = "订单的增长")
    private BigDecimal percentageTotal = BigDecimal.ZERO;

    public BigDecimal getPercentageTotal() {
        BigDecimal per = BigDecimal.ZERO;
        if (total != null && total > 0 && preTotal != null && preTotal > 0) {
            per = (BigDecimal.valueOf(total)).subtract(BigDecimal.valueOf(preTotal))
                    .divide(BigDecimal.valueOf(preTotal), 4, BigDecimal.ROUND_HALF_UP)
                    .multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return per;
    }
}
