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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.czg.order.entity.ShopOrderStatistic;
import com.czg.order.param.DataSummaryTradeParam;
import com.czg.order.service.DataSummaryService;
import com.czg.order.service.ShopOrderStatisticService;
import com.czg.service.order.mapper.ShopOrderStatisticMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

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

/**
 * 服务层实现。
 *
 * @author zs
 * @since 2025-03-07
 */
@Service
public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisticMapper, ShopOrderStatistic> implements ShopOrderStatisticService {
    @Resource
    private DataSummaryService dataSummaryService;


    @Override
    public void statistic() {
        // 获取前一天
        DateTime yesterday = DateUtil.yesterday();
        // 获取前一天的开始时间（00:00:00）
        DateTime startOfDay = DateUtil.beginOfDay(yesterday);
        // 获取前一天的结束时间（23:59:59）
        DateTime endOfDay = DateUtil.endOfDay(yesterday);
        List<Long> shopIdList = dataSummaryService.getShopIdList();
        if (CollUtil.isEmpty(shopIdList)) {
            return;
        }
        shopIdList.parallelStream().forEach(shopId -> {
            DataSummaryTradeParam param = new DataSummaryTradeParam();
            param.setShopId(shopId);
            param.setBeginDate(startOfDay.toStringDefaultTimeZone());
            param.setEndDate(endOfDay.toStringDefaultTimeZone());
            ShopOrderStatistic statistic = dataSummaryService.getTradeData(param);
            statistic.setShopId(shopId);
            statistic.setCreateDay(LocalDate.now());
            statistic.setUpdateTime(LocalDateTime.now());
            saveOrUpdate(statistic);
        });
    }

}
