package com.xunwang.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xunwang.commonutils.R;
import com.xunwang.education.entity.TeacherCertifications;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.vo.TeacherViewCountVo;
import com.xunwang.education.service.TeacherCertificationsService;
import com.xunwang.education.service.TeacherCoursesService;
import io.swagger.annotations.ApiOperation;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author Guan
 * @date 2023/1/11
 * discription
 */
@RestController
@RequestMapping("/education/home")
@EnableScheduling
public class HomeController {

    @Autowired
    private TeacherCoursesService teacherCoursesService;

    @Autowired
    private TeacherCertificationsService teacherCertificationsService;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取课程id和对应的点击量
//    @Scheduled(cron = "*/5 * * * * ?")
    @GetMapping("/getTeacherCourseViewCount")
    public void getTeacherCourseViewCount(){
        List<TeacherCourses> list = teacherCoursesService.list(null);
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            String courseId = list.get(i).getCourseId().toString();
            redisTemplate.opsForZSet().add("viewCount",courseId,list.get(i).getViewedCount());
        }
    }

    @ApiOperation("获取每个老师的总点击量，并存入redis")
    @GetMapping("/showPopularTeacher")
    public void showPopularTeacher(){
        String keyModel = "viewCount";
        QueryWrapper<TeacherCertifications> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",0 );
        wrapper.eq("verify_status","已通过" );
        List<TeacherCertifications> list = teacherCertificationsService.list(wrapper);//老师列表
//        TreeMap<String, Double> idAndScore = new TreeMap<>();
        Double score = 0.0;
        for (int i = 0; i < list.size(); i++) {
            List<TeacherCourses> coursesList = teacherCoursesService.getTeacherCourseId(list.get(i).getUid());//根据一个老师的Uid查询出所有课程
            for (int j = 0; j < coursesList.size(); j++) {
                score = score + redisTemplate.opsForZSet().score(keyModel, coursesList.get(j).getCourseId()) ;
            }
//            idAndScore.put(list.get(i).getUid(),score);
            redisTemplate.opsForZSet().add("teacherAllCount",list.get(i).getUid(),score);
            score = 0.0;
        }
    }

    @GetMapping("/getHottestTeacher")
    public R getHottestTeacher(){

        Set teacherAllCount = redisTemplate.opsForZSet().reverseRange("teacherAllCount", 0, -1);
        String[] array = (String[])teacherAllCount.toArray(new String[teacherAllCount.size()]);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        List<TeacherCertifications> teacherDetail = teacherCertificationsService.getTeacherDetail(array);
        System.out.println(teacherDetail);
        return R.ok().data("list",teacherDetail);
    }



    @GetMapping("addTest")
    public void setOneValue(){
        String keyModel = "viewCount";
        String ss = "139009318069469189";
        String teacherUid = "";
        Double aDouble = redisTemplate.opsForZSet().incrementScore(keyModel, ss, 1);
        Double aDouble2 = redisTemplate.opsForZSet().incrementScore("teacherAllCount", teacherUid, 1);
        System.out.println(aDouble);
    }


//    @Scheduled(cron = "*/5 * * * * ?")
    @GetMapping("return")
    public void returnDb(){
        String keyModel = "viewCount";
        Long size = redisTemplate.opsForZSet().size(keyModel);
        Set range = redisTemplate.opsForZSet().range(keyModel, 0, size - 1);

        String[] array = (String[]) range.toArray(new String[range.size()]);

        TeacherCourses updateVo = new TeacherCourses();


        for (int i = 0; i < size; i++) {
            QueryWrapper<TeacherCourses> wrapper = new QueryWrapper<>();
            Double score = redisTemplate.opsForZSet().score(keyModel, array[i]);
            int intValue = score.intValue();
            updateVo.setViewedCount(intValue);
            updateVo.setCourseId(array[i]);
            wrapper.eq("course_id",array[i]);
            teacherCoursesService.update(updateVo,wrapper);

        }
        System.gc();
    }

//
//    //获取老师id和对应的点击量
////    @Scheduled(cron = "*/5 * * * * ?")
//    @GetMapping("/getTeacherCourseViewCount")
//    public void getTeacherCourseViewCount(){
//        List<TeacherCertifications> list = teacherCertificationsService.list(null);
//        for (int i = 0; i < list.size(); i++) {
//            Integer viewCount = teacherCoursesService.getTeacherViewCount(list.get(i).getUid());
//            redisTemplate.opsForZSet().add("viewCount",list.get(i).getUid(),viewCount);
//        }
//    }
//
//    @GetMapping("addTest")
//    public void setOneValue(){
//        String keyModel = "viewCount";
//        String ss = "1df782200000002";
//        Double aDouble = redisTemplate.opsForZSet().incrementScore(keyModel, ss, 1);
//        System.out.println(aDouble);
//    }
//
//    @GetMapping("return")
//    public void returnDb(){
//        String keyModel = "viewCount";
//        Long size = redisTemplate.opsForZSet().size(keyModel);
//        Set range = redisTemplate.opsForZSet().range(keyModel, 0, size - 1);
//
//        for (int i = 0; i < size; i++) {
//
//        }
//        System.out.println(size);
//    }
//    @Test
//    public void test8() {
//        //获得指定元素的分数
//        Double score = redisTemplate.opsForZSet().score("ranking-list", "p1");
//        System.out.println(score);
//    }

}
