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.account.entity.ShopInfo;
import com.czg.account.service.ShopInfoService;
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 lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

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

    @DubboReference
    private ShopInfoService shopInfoService;



    @Override
    public void statistic(DateTime dateTime) {
        // 获取前一天的开始时间（00:00:00）
        DateTime startOfDay = DateUtil.beginOfDay(dateTime);
        // 获取前一天的结束时间（23:59:59）
        DateTime endOfDay = DateUtil.endOfDay(dateTime);
        List<Long> shopIdList = dataSummaryService.getShopIdList();
        if (CollUtil.isEmpty(shopIdList)) {
            return;
        }
        List<List<Long>> split = CollUtil.split(shopIdList, 5);
        for (List<Long> splitIdList : split) {
            splitIdList.parallelStream().forEach(shopId -> {
                Long mainShopId = shopInfoService.getMainIdByShopId(shopId);
                DataSummaryTradeParam param = new DataSummaryTradeParam();
                param.setShopId(shopId);
                param.setMainShopId(mainShopId);
                param.setBeginDate(startOfDay.toStringDefaultTimeZone());
                param.setEndDate(endOfDay.toStringDefaultTimeZone());
                ShopOrderStatistic statistic = dataSummaryService.getRealTimeTradeData(param);
                statistic.setShopId(shopId);
                statistic.setCreateDay(dateTime.toLocalDateTime().toLocalDate());
                statistic.setUpdateTime(LocalDateTime.now());
                if (statistic.getNewMemberCount() != 0L) {
                    System.out.println("newMemberCount:" + statistic.getNewMemberCount());
                }
                // 如果没有订单和退款，则不更新数据，否则会产出很多数据
                if (statistic.getSaleCount() == 0
                        && statistic.getRefundCount() == 0
                        && statistic.getMemberPayCount() == 0
                        && statistic.getNewMemberCount() == 0
                ) {
                    log.info("店铺：{}，{}，没有要存档的订单统计数据", shopId, dateTime.toDateStr());
                } else {
                    ShopOrderStatistic entity =
                            getMapper()
                            .selectOneByQuery(query().eq("shop_id", shopId)
                                    .eq("create_day", dateTime.toDateStr()));
                    if (entity != null) {
                        statistic.setId(entity.getId());
                        updateById(statistic);
                    } else {
                        save(statistic);
                    }
                }
            });
        }
    }

}
