package com.cf.imes.module.system.service.funds.statistics;

import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.module.system.controller.admin.statistics.vo.OrgStatusGroupStatisticsReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.statistics.FundsTradeTypeGroupStatisticsDO;
import com.cf.imes.module.system.dal.mysql.incomeandexpense.IncomeExpenseDetailsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.cf.imes.framework.common.constants.StatisticsConstants.DATELIST_FIELD_NAME;
import static com.cf.imes.framework.common.constants.StatisticsConstants.SERIES_FIELD_NAME;
import static com.cf.imes.framework.common.util.time.StatisticsChangeUtils.generateDateRangeAxis;
import static com.cf.imes.framework.common.util.time.StatisticsChangeUtils.getDateList;

/**
 * @author Gqr
 * @since 2025/9/30 16:07
 */
@Service
public class FundsStatisticsServiceImpl implements FundsStatisticsService {

    @Resource
    private IncomeExpenseDetailsMapper incomeExpenseDetailsMapper;

    @Resource
    private I18nUtils i18nUtils;

    @Override
    public Map<String, Object> getFunsTradeTypeGroupStatistics(OrgStatusGroupStatisticsReqVO reqVO) {
        Map<String, Object> resultMap = new LinkedHashMap<>();

        // 所有的日期集合
        List<String> dateList = getDateList(reqVO);

        reqVO.setCreateTimeForQry(LocalDateTimeUtils.generateTimeSection(reqVO.getCreateTime()));
        // 销售额Map
        LinkedHashMap<String, List<FundsTradeTypeGroupStatisticsDO>> purchaseMap = incomeExpenseDetailsMapper.selectTotalSalesTrendGroupByCreateTime(reqVO).stream()
                .sorted(Comparator.naturalOrder())
                .collect(Collectors.groupingBy(FundsTradeTypeGroupStatisticsDO::getDate, LinkedHashMap::new, Collectors.toList()));

        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> purchaseSeriesItem = new HashMap<>();
        BigDecimal[] purchaseCountArr = new BigDecimal[dateList.size()];
        // 遍历时间跨度列表，匹配数量存入数组，没有补0存入数组
        for (int i = 0; i < dateList.size(); i++) {
            String dateStr = dateList.get(i);

            // 软件购买金额
            BigDecimal purchaseAmount = Optional.ofNullable(purchaseMap.get(dateStr))
                    .map(list -> list.stream().findFirst().orElse(new FundsTradeTypeGroupStatisticsDO()))
                    .map(FundsTradeTypeGroupStatisticsDO::getAmount)
                    .orElse(BigDecimal.valueOf(0));
            purchaseCountArr[i] = purchaseAmount;
        }
        purchaseSeriesItem.put("data", purchaseCountArr);
        purchaseSeriesItem.put("name", i18nUtils.getMessage("statistics.series.name.saleAmount"));
        series.add(purchaseSeriesItem);

        // x轴时间区间字符串
        resultMap.put(DATELIST_FIELD_NAME, dateList.stream().map(date -> generateDateRangeAxis(date, reqVO.getUnit())).toList());
        // 类别：类别：单量数组
        resultMap.put(SERIES_FIELD_NAME, series);

        return resultMap;
    }
}
