package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ColumnPageView;
import com.ruoyi.system.domain.WebSiteTenDaysHits;
import com.ruoyi.system.mapper.ColumnPageViewMapper;
import com.ruoyi.system.mapper.WebSiteTenDaysHitsMapper;
import com.ruoyi.system.service.IColumnPageViewService;
import com.ruoyi.system.service.IWebSiteTenDaysHitsService;
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.*;
import java.util.stream.Collectors;

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


@Service
public class ColumnPageViewServiceImpl implements IColumnPageViewService {


    @Autowired
    private ColumnPageViewMapper columnPageViewMapper;


    @Override
    public Map<String, List<String>> selectColumnPageViewList() {
        Map<String, List<String>> returnMap = new HashMap<>();
        List<String> columnRankingList = new ArrayList<>();
//        List<String> rankingList = new ArrayList<>();
        List<String> browseList = new ArrayList<>();
        List<ColumnPageView> list = columnPageViewMapper.selectColumnPageViewList();
        for (ColumnPageView var : list) {
            columnRankingList.add(var.getColumnRanking() + "-" + var.getRanking());
//            rankingList.add(var.getRanking());
            browseList.add(var.getBrowse());
        }
        returnMap.put("columnRankingList",columnRankingList);
//        returnMap.put("rankingList",rankingList);
        returnMap.put("browseList",browseList);
        return returnMap;
    }

    @Override
    public void insertBatch(List<ColumnPageView> list) {
        Map<String, List<String>> returnMap = new HashMap<>();
        List<String> columnRankingList = new ArrayList<>();
        List<String> browseList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            columnPageViewMapper.deleteColumnPageViewAll();
        }
        columnPageViewMapper.insertBatch(list);
        for (ColumnPageView var : list) {
            columnRankingList.add(var.getColumnRanking() + "-" + var.getRanking());
            browseList.add(var.getBrowse());
        }
        returnMap.put("columnRankingList",columnRankingList);
        returnMap.put("browseList",browseList);
        WebSocketUsers.sendMessageToUsersByText(JSON.toJSONString(success(AjaxResult.Type.TYPE_EIGHTEEN,returnMap)));
    }
}
