package com.sonic.lion.service.impl;

import com.sonic.lion.domain.entity.PayCharge;
import com.sonic.lion.domain.enums.ProductRewardType;
import com.sonic.lion.domain.output.ChargeProductConfigListOutput;
import com.sonic.lion.domain.output.ChargeProductConfigOutput;
import com.sonic.lion.service.ActivityService;
import com.sonic.lion.service.ChargeProductConfigService;
import com.sonic.lion.service.PayChargeService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Author code
 * @Description 充值商品配置
 * @Date 2024/5/27 16:57
 * @Version 1.0
 */
@Slf4j
@Service
public class ChargeProductConfigServiceImpl implements ChargeProductConfigService {

    @Autowired
    private PayChargeService payChargeService;
    @Autowired
    private ActivityService activityService;

    @Override
    public ChargeProductConfigOutput getRechargeLevelConfig(Long currentUserId, String platform, String version) {
        ChargeProductConfigOutput output = new ChargeProductConfigOutput();
        //从数据库查询出配置的充值档位列表数据
        List<PayCharge> list = payChargeService.list(platform, version);
        List<ChargeProductConfigListOutput> productOutputList = Lists.newArrayList();
        List<String> largeProductIdList = list.stream()
                .filter(e -> ProductRewardType.LARGE_PRODUCT.name().equals(e.getBizType()))
                .map(e -> e.getProductId()).collect(Collectors.toList());
        //获取已得到赠送的大额充送的类型数据
        List<String> rewardProductIdList = activityService.getRewardTypeByUserIdV2(currentUserId, LocalDateTime.now(), largeProductIdList, version);

        //循环数据中的配置基础数据
        for (PayCharge payCharge : list) {
            ChargeProductConfigListOutput productOutput = new ChargeProductConfigListOutput();
            productOutput.setProductId(payCharge.getProductId());
            productOutput.setChargeAmount(payCharge.getChargeAmount());
            productOutput.setPayAmount(payCharge.getPayAmount());
            //当前业务类型为大额充送档位 且 本月没有享受过该档位的充送
            if(ProductRewardType.LARGE_PRODUCT.name().equals(payCharge.getBizType()) && !rewardProductIdList.contains(payCharge.getProductId())) {
                //设置赠送金额
                productOutput.setGiftAmount(payCharge.getGiftAmount());
            } else if(ProductRewardType.HOT.name().equals(payCharge.getBizType())) {
                //标记hot
                productOutput.setHot(true);
            }
            productOutputList.add(productOutput);
        }
        output.setProductList(productOutputList);
        return output;
    }

}
