package com.xunwang.education.controller;

import com.xunwang.commonutils.R;
import com.xunwang.education.entity.dto.MyStuNeedsDto;
import com.xunwang.education.entity.dto.MyTeacherCoursesDto;
import com.xunwang.education.mapper.StudentRequirementsMapper;
import com.xunwang.education.mapper.TeacherCoursesMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

    @Resource
    private StudentRequirementsMapper studentRequirementsMapper;

    @Resource
    private TeacherCoursesMapper teacherCoursesMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/getHottestTeacher")
    public R getHottestTeacher(){
        String key = "viewCount:course";
        Set<String> strings = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 2);
        List<MyTeacherCoursesDto> teacherLists = null;
        if (strings != null) {
            teacherLists = teacherCoursesMapper.selectByBatch(new ArrayList<String>(strings));
        }
        return R.ok().data("teacher", teacherLists);
    }

    @GetMapping("/getHottestStuNeed")
    public R getHottestStuNeed(){
        String key = "viewCount:stuNeed";
        Set<String> strings = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 2);
        List<MyStuNeedsDto> stuNeedLists = null;
        if (strings != null) {
            stuNeedLists = studentRequirementsMapper.selectBatch(new ArrayList<String>(strings));
        }
        return R.ok().data("stuNeed", stuNeedLists);
    }

}
