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

import cn.hutool.core.collection.CollUtil;
import com.czg.exception.CzgException;
import com.czg.order.entity.ShopTableOrderStatistic;
import com.czg.order.service.ShopTableOrderStatisticService;
import com.czg.service.order.mapper.ShopTableOrderStatisticMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 台桌订单统计表 服务层实现。
 *
 * @author ww
 * @since 2025-11-21
 */
@Service
public class ShopTableOrderStatisticServiceImpl extends ServiceImpl<ShopTableOrderStatisticMapper, ShopTableOrderStatistic> implements ShopTableOrderStatisticService{

    @Override
    public List<ShopTableOrderStatistic>  getArchiveTradeData(Long shopId, String rangeType, LocalDate start, LocalDate end) {
        LocalDate currentDate = LocalDate.now();
        if ("today".equals(rangeType)) {
            return getRealTimeDataByDay(shopId, currentDate);
        } else if ("yesterday".equals(rangeType)) {
            return getStatSingleDate(shopId, currentDate.minusDays(1));
        }
        if (start.isAfter(currentDate)) {
            throw new CzgException("开始时间不能晚于当前时间");
        }
        if (start.equals(end)) {
            return getStatSingleDate(shopId, start);
        }
        //包括当前时间
        if (end.isBefore(currentDate)) {
            return getStatDateRange(shopId, start, end);
        } else {
            List<ShopTableOrderStatistic> realTimeDataByDay = getRealTimeDataByDay(shopId, currentDate);
            List<ShopTableOrderStatistic> statDateRange = getStatDateRange(shopId, start, end);
            return ShopTableOrderStatistic.mergeWithStream(realTimeDataByDay, statDateRange);
        }
    }



    @Override
    public void statisticAndInsert(Long shopId, LocalDate day) {
        List<ShopTableOrderStatistic> realTimeData = getRealTimeDataByDay(shopId, day);
        if (CollUtil.isNotEmpty(realTimeData)) {
            // 过滤掉没有有效数据的记录
            realTimeData = realTimeData.stream()
                    .filter(ShopTableOrderStatistic::hasValidData)
                    .toList();
            if (CollUtil.isNotEmpty(realTimeData)) {
                remove(QueryWrapper.create().eq(ShopTableOrderStatistic::getShopId, shopId).eq(ShopTableOrderStatistic::getCreateDay, day));
                 mapper.insertBatchSelective(realTimeData, 50);
            }
        }
    }

    @Override
    public List<ShopTableOrderStatistic> getRealTimeDataByDay(Long shopId, LocalDate day) {
        return mapper.getOnlineData(shopId, day);
    }

    @Override
    public List<ShopTableOrderStatistic> getStatSingleDate(Long shopId, LocalDate day) {
        return mapper.getStatSingleDate(shopId, day);
    }

    @Override
    public List<ShopTableOrderStatistic> getStatDateRange(Long shopId, LocalDate start, LocalDate end) {
        return mapper.getStatDateRange(shopId, start, end);
    }
}
