package com.xunwang.education.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunwang.education.entity.StuNeeds;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.vo.ViewCountVo;
import com.xunwang.education.mapper.StudentRequirementsMapper;
import com.xunwang.education.mapper.TeacherCoursesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class EducationTask {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private TeacherCoursesMapper teacherCoursesMapper;

    @Resource
    private StudentRequirementsMapper studentRequirementsMapper;

    @Scheduled(cron = "0 0 12 * * ?")
    public void batchSaveViewCount() {
        log.info("batchSaveViewCount-course 被执行");
        String courseView = "viewCount:course";
        this.updateViewCount(courseView);

        log.info("batchSaveViewCount-stuNeed 被执行");
        String StuView = "viewCount:stuNeed";
        this.updateViewCount(StuView);
    }

    @Scheduled(cron = "0 0/30 * * * ?")
    public void unlockCourseOrStuNeed() {
        log.info("30分钟解锁课程或需求");
        LambdaQueryWrapper<TeacherCourses> courseWrapper = new LambdaQueryWrapper<>();
        courseWrapper.eq(TeacherCourses::getDisplay, "上架").eq(TeacherCourses::getLocked, "已锁");
        List<TeacherCourses> teacherCourses = teacherCoursesMapper.selectList(courseWrapper);
        for (TeacherCourses teacherCourse : teacherCourses) {
            teacherCourse.setLocked("未锁");
            teacherCoursesMapper.update(teacherCourse, new LambdaQueryWrapper<TeacherCourses>().eq(TeacherCourses::getCourseId, teacherCourse.getCourseId()));
        }

        LambdaQueryWrapper<StuNeeds> stuWrapper = new LambdaQueryWrapper<>();
        stuWrapper.eq(StuNeeds::getDisplay, "上架").eq(StuNeeds::getLocked, "已锁");
        List<StuNeeds> stuNeeds = studentRequirementsMapper.selectList(stuWrapper);
        for (StuNeeds stuNeed : stuNeeds) {
            stuNeed.setLocked("未锁");
            studentRequirementsMapper.update(stuNeed, new LambdaQueryWrapper<StuNeeds>().eq(StuNeeds::getRequireId, stuNeed.getRequireId()));
        }
    }

    private void updateViewCount(String key) {

        Set<ZSetOperations.TypedTuple<String>> tuples = stringRedisTemplate.opsForZSet().rangeWithScores(key, 0, -1);
        ArrayList<ViewCountVo> ViewCount = new ArrayList<>();
        if (tuples != null) {
            for (ZSetOperations.TypedTuple<String> tuple : tuples) {
                String id = tuple.getValue();
                Double count = tuple.getScore();
                ViewCountVo viewCountVo = new ViewCountVo();
                if (count != null) {
                    viewCountVo.setCount(count.intValue());
                }
                viewCountVo.setId(id);
                ViewCount.add(viewCountVo);
            }
            if ("viewCount:stuNeed".equals(key)) {
                studentRequirementsMapper.batchupdate(ViewCount);
            }
            if ("viewCount:course".equals(key)) {
                teacherCoursesMapper.batchupdate(ViewCount);
            }
        }
    }
}
