package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.CountyDailyTrafficRanking;
import com.ruoyi.system.domain.DailyTrafficRanking;
import com.ruoyi.system.domain.DailyUpdateRanking;
import com.ruoyi.system.mapper.CountyDailyTrafficRankingMapper;
import com.ruoyi.system.mapper.DailyUpdateRankingMapper;
import com.ruoyi.system.service.ICountyDailyTrafficRankingService;
import com.ruoyi.system.service.IDailyUpdateRankingService;
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.List;
import java.util.stream.Collectors;

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


@Service
public class CountyDailyTrafficRankingServiceImpl implements ICountyDailyTrafficRankingService {


    @Autowired
    private CountyDailyTrafficRankingMapper countyDailyTrafficRankingMapper;


    @Override
    public List<CountyDailyTrafficRanking> selectCountyDailyTrafficRankingList() {
        return countyDailyTrafficRankingMapper.selectCountyDailyTrafficRankingList();
    }

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