package com.ruoyi.web.modules.trading.support;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import com.ruoyi.web.modules.trading.domain.AppYakMarketQuoteRow;
import com.ruoyi.web.modules.trading.domain.vo.AppYakMarketStats7Vo;

/**
 * 近 7 日行情统计计算。
 */
public final class YakMarketStatsSupport
{
    private YakMarketStatsSupport()
    {
    }

    public static AppYakMarketStats7Vo build(List<AppYakMarketQuoteRow> rows)
    {
        AppYakMarketStats7Vo vo = new AppYakMarketStats7Vo();
        if (rows == null || rows.isEmpty())
        {
            return vo;
        }
        BigDecimal sum = BigDecimal.ZERO;
        int count = 0;
        AppYakMarketQuoteRow minRow = null;
        AppYakMarketQuoteRow maxRow = null;
        Date latestCreate = null;
        AppYakMarketQuoteRow latestByDate = null;
        for (AppYakMarketQuoteRow row : rows)
        {
            if (row.getAvgPrice() == null)
            {
                continue;
            }
            sum = sum.add(row.getAvgPrice());
            count++;
            if (minRow == null || row.getAvgPrice().compareTo(minRow.getAvgPrice()) < 0
                    || (row.getAvgPrice().compareTo(minRow.getAvgPrice()) == 0
                    && isLaterQuoteDate(row, minRow)))
            {
                minRow = row;
            }
            if (maxRow == null || row.getAvgPrice().compareTo(maxRow.getAvgPrice()) > 0
                    || (row.getAvgPrice().compareTo(maxRow.getAvgPrice()) == 0
                    && isLaterQuoteDate(row, maxRow)))
            {
                maxRow = row;
            }
            if (row.getCreateTime() != null
                    && (latestCreate == null || row.getCreateTime().after(latestCreate)))
            {
                latestCreate = row.getCreateTime();
            }
            if (latestByDate == null || isLaterQuoteDate(row, latestByDate))
            {
                latestByDate = row;
            }
        }
        if (count == 0)
        {
            return vo;
        }
        vo.setAvgPrice7(sum.divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP));
        if (minRow != null)
        {
            vo.setMinPrice7(minRow.getAvgPrice());
            vo.setMinQuoteDate(minRow.getQuoteDate());
            vo.setMinQuoteDateLabel(YakMarketDateSupport.shortMonthDayLabel(minRow.getQuoteDate()));
        }
        if (maxRow != null)
        {
            vo.setMaxPrice7(maxRow.getAvgPrice());
            vo.setMaxQuoteDate(maxRow.getQuoteDate());
            vo.setMaxQuoteDateLabel(YakMarketDateSupport.shortMonthDayLabel(maxRow.getQuoteDate()));
        }
        vo.setCreateTime(latestCreate);
        if (latestByDate != null)
        {
            vo.setPriceUnit(latestByDate.getPriceUnit());
            vo.setPriceUnitName(priceUnitName(latestByDate.getPriceUnit()));
        }
        return vo;
    }

    private static boolean isLaterQuoteDate(AppYakMarketQuoteRow a, AppYakMarketQuoteRow b)
    {
        if (a.getQuoteDate() == null)
        {
            return false;
        }
        if (b.getQuoteDate() == null)
        {
            return true;
        }
        return a.getQuoteDate().after(b.getQuoteDate());
    }

    private static String priceUnitName(Integer unit)
    {
        if (unit == null)
        {
            return "";
        }
        return unit == MarketDailyQuoteRules.PRICE_UNIT_PER_HEAD ? "元/头" : "元/斤";
    }
}
