package com.czg.service.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.czg.order.param.TableSummaryParam;
import com.czg.order.service.TableSummaryService;
import com.czg.order.vo.TableSummaryExportVo;
import com.czg.order.vo.TableSummaryInfoVo;
import com.czg.service.order.mapper.ShopTableOrderStatisticMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 台桌统计Service实现类
 *
 * @author tankaikai
 * @since 2025-03-07 15:32
 */
@Service
public class TableSummaryServiceImpl implements TableSummaryService {

    @Resource
    private ShopTableOrderStatisticMapper shopTableOrderStatisticMapper;

    @Override
    public List<TableSummaryInfoVo> summaryList(TableSummaryParam param) {
        return shopTableOrderStatisticMapper.findSummaryList(param);
    }

    @Override
    public List<TableSummaryExportVo> summaryExportList(TableSummaryParam param) {
        if (param.getBeginDate() == null && param.getEndDate() == null) {
            // 获取当前日期
            LocalDate currentDate = LocalDate.now();
            // 计算 30 天前的日期
            LocalDate startDate = currentDate.minusDays(30);

            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            // 格式化日期
            String formattedStartDate = startDate.format(formatter);
            String formattedEndDate = currentDate.format(formatter);

            param.setBeginDate(formattedStartDate + " 00:00:00");
            param.setEndDate(formattedEndDate + " 23:59:59");
        }
        List<TableSummaryExportVo> list = shopTableOrderStatisticMapper.findSummaryExportList(param);
        if (CollUtil.isEmpty(list)) {
            return List.of();
        }
        Map<String, BigDecimal> totalSalesAmountMap = list.stream().collect(
                Collectors.groupingBy(
                        TableSummaryExportVo::getTableName, Collectors.reducing(BigDecimal.ZERO,
                                TableSummaryExportVo::getAmount,
                                BigDecimal::add)
                )
        );
        Map<String, BigDecimal> salesAmountMap = list.stream().collect(
                Collectors.groupingBy(
                        TableSummaryExportVo::getTableConcatDate, Collectors.reducing(BigDecimal.ZERO,
                                TableSummaryExportVo::getAmount,
                                BigDecimal::add)
                )
        );
        list.parallelStream().forEach(data -> {
            data.setSalesAmount(salesAmountMap.get(data.getTableConcatDate()));
            data.setTotalSalesAmount(totalSalesAmountMap.get(data.getTableName()));
        });
        // 追加个空行用于处理表格样式
        TableSummaryExportVo nullVo = new TableSummaryExportVo();
        list.add(nullVo);
        return list;
    }

}
