package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.DailyUpdateRanking;
import com.ruoyi.system.mapper.DailyUpdateRankingMapper;
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 DailyUpdateRankingServiceImpl implements IDailyUpdateRankingService {


    @Autowired
    private DailyUpdateRankingMapper dailyUpdateRankingMapper;


    @Override
    public List<DailyUpdateRanking> selectDailyUpdateRankingList() {
        return dailyUpdateRankingMapper.selectDailyUpdateRankingList();
    }

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