package com.ruoyi.system.user.service.impl;

import com.ruoyi.bst.lightingNum.domain.LightingNumQuery;
import com.ruoyi.bst.lightingNum.service.LightingNumService;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.order.service.OrderService;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.domain.vo.LongDecimalVO;
import com.ruoyi.common.domain.vo.LongIntegerVO;
import com.ruoyi.common.domain.vo.LongLocalDateTimeVO;
import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.system.user.domain.vo.UserConsumeVO;
import com.ruoyi.system.user.domain.vo.UserStatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.user.service.UserAssembler;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserAssemblerImpl implements UserAssembler{

    @Autowired
    private LightingNumService lightingNumService;
    @Autowired
    private OrderService orderService;

    @Override
    public void assembleConsume(List<UserConsumeVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        // 拼接用户充值金额
        this.assembleRechargeAmount(list);
        // 拼接用户最近消费时间
        this.assembleLastConsumeTime(list);
    }


    @Override
    public void assembleRechargeAmount(List<UserConsumeVO> list){
        // 查询用户充值总额
        OrderQuery query = new OrderQuery();
        query.setUserIds(CollectionUtils.map(list,UserConsumeVO::getUserId));
        query.setStatusList(OrderStatus.payed());
        Map<Long, BigDecimal> rechargeMap = orderService.selectSumOfAmountGroupByUserId(query)
                .stream().collect(Collectors.toMap(LongDecimalVO::getKey, LongDecimalVO::getValue));
        for (UserConsumeVO userConsume : list) {
            BigDecimal totalRecharge = rechargeMap.get(userConsume.getUserId());
            userConsume.setTotalRecharge(totalRecharge);
        }
    }

    @Override
    public void assembleLastConsumeTime(List<UserConsumeVO> list) {
        // 查询最近消费时间
        OrderQuery query = new OrderQuery();
        query.setUserIds(CollectionUtils.map(list,UserConsumeVO::getUserId));
        query.setStatusList(OrderStatus.consumed());
        Map<Long, LocalDateTime> lastConsumeTimeMap = orderService.assembleLastConsumeTime(query)
                .stream().collect(Collectors.toMap(LongLocalDateTimeVO::getKey, LongLocalDateTimeVO::getValue));
        for (UserConsumeVO userConsume : list) {
            userConsume.setLastConsumeTime(lastConsumeTimeMap.get(userConsume.getUserId()));
        }
    }

    @Override
    public void assembleLightingNum(List<UserVO> list) {
        // 查询爆灯次数
        LightingNumQuery query = new LightingNumQuery();
        query.setUserIds(CollectionUtils.map(list,UserVO::getUserId));
        Map<Long, Integer> lastConsumeTimeMap = lightingNumService.assembleLightingNum(query)
                .stream().collect(Collectors.toMap(LongIntegerVO::getKey, LongIntegerVO::getValue));
        for (UserVO user : list) {
            user.setLightingNum(lastConsumeTimeMap.get(user.getUserId()));
        }
    }


}
