package edu.whut.domain.activity.service;
import edu.whut.domain.activity.adapter.repository.IActivityRepository;
import edu.whut.domain.activity.model.entity.MarketProductEntity;
import edu.whut.domain.activity.model.entity.TrialBalanceEntity;
import edu.whut.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import edu.whut.domain.activity.model.valobj.TeamStatisticVO;
import edu.whut.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import edu.whut.types.design.framework.tree.StrategyHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 首页营销服务
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class IndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService {

    private final DefaultActivityStrategyFactory defaultActivityStrategyFactory;

    private final IActivityRepository repository;

    /**
     * 首页营销试算入口
     * 传入userid和goodid ，可以获取商品的优惠以及当前用户是否可享优惠
     */
    @Override
    public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {

        // 获取执行策略
        StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> strategyHandler = defaultActivityStrategyFactory.strategyHandler();
        // 受理试算操作
        return strategyHandler.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
    }

    /**
     * 查询进行中的拼团订单
     * 查询当前用户参与的ownerCount个拼团 + randomCount个随机其他拼团
     */
    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, int randomCount) {
        return randomCount <= 0
                ? Collections.emptyList()
                : repository.listRandomDetails(activityId,randomCount);
    }


    /**
     * 获取xx活动的拼团统计数据
     */
    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }
}
