package com.kxs.stat.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.stat.api.vo.myUser.UserAddTrendVO;
import com.kxs.stat.biz.constant.enums.TotalTypeEnum;
import com.kxs.stat.biz.mapper.KxsUserNewTradeMapper;
import com.kxs.stat.api.model.KxsUserNewTrade;
import com.kxs.stat.biz.service.KxsUserNewTradeService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 创客新增统计表(KxsUserNewTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-16 11:07:42
 */
@Service("kxsUserNewTradeService")
public class KxsUserNewTradeServiceImpl extends ServiceImpl<KxsUserNewTradeMapper, KxsUserNewTrade> implements KxsUserNewTradeService {

    @Override
    public List<UserAddTrendVO> getAddUserTrend(Long userId, List<Integer> times, Integer dateType, Integer totalType) {

        List<UserAddTrendVO> userAddTrendVOList = baseMapper.getAddUserTrend(userId, times, dateType, TotalTypeEnum.TEAM.getType());

        Map<Integer, UserAddTrendVO> tradeTrendMap = userAddTrendVOList.stream().collect(Collectors.toMap(UserAddTrendVO::getTime, each -> each, (v1, v2) -> v2));
        LinkedHashMap<Integer, UserAddTrendVO> tradeTrend = new LinkedHashMap<>();
        for (Integer time : times) {
            if(tradeTrendMap.containsKey(time)){
                tradeTrend.put(time, tradeTrendMap.get(time));
            }else{
                //没有数据填充为0
                UserAddTrendVO userAddTrendVO = new UserAddTrendVO();
                userAddTrendVO.setNum(0);
                userAddTrendVO.setTime(time);
                tradeTrend.put(time, userAddTrendVO);
            }
        }
        List<UserAddTrendVO> list = new ArrayList<>(tradeTrend.values().stream().toList());
        Collections.reverse(list);
        return list;
    }
}

