package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.CensusElements;
import com.ruoyi.system.domain.DailyTrafficRanking;
import com.ruoyi.system.mapper.CensusElementsMapper;
import com.ruoyi.system.mapper.DailyTrafficRankingMapper;
import com.ruoyi.system.service.ICensusElementsService;
import com.ruoyi.system.service.IDailyTrafficRankingService;
import com.ruoyi.websocket.WebSocketUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ruoyi.common.core.domain.AjaxResult.success;


@Service
public class DailyTrafficRankingServiceImpl implements IDailyTrafficRankingService {


    @Autowired
    private DailyTrafficRankingMapper dailyTrafficRankingMapper;


    @Override
    public List<DailyTrafficRanking> selectDailyTrafficRankingList() {
        return dailyTrafficRankingMapper.selectDailyTrafficRankingList();
    }

    @Override
    public void insertBatch(List<DailyTrafficRanking> list) {
        if (!CollectionUtils.isEmpty(list)) {
            dailyTrafficRankingMapper.deleteDailyTrafficRankingAll();
        }
        dailyTrafficRankingMapper.insertBatch(list);
        WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(success(AjaxResult.Type.TYPE_FIVE,list.stream().sorted(Comparator.comparing(DailyTrafficRanking::getRanking)).collect(Collectors.toList()))));
    }
}
