package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.CourseScheduleDao;
import com.yonge.cooleshow.biz.dal.dao.StudentStarDao;
import com.yonge.cooleshow.biz.dal.dto.search.StudentSearch;
import com.yonge.cooleshow.biz.dal.entity.StudentTotal;
import com.yonge.cooleshow.biz.dal.enums.CacheNameEnum;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.vo.*;
import com.yonge.toolset.utils.date.DateUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.dao.StudentDao;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public StudentVo detail(Long userId) {
        return baseMapper.detail(userId);
    }

    @Override
    public IPage<StudentVo> selectPage(IPage<StudentVo> page, StudentSearch studentSearch) {
        return page.setRecords(baseMapper.selectPage(page, studentSearch));
    }

    @Override
    public StudentHomeVo queryUserInfo(SysUser user) {
        StudentVo detail = detail(user.getId());
        StudentHomeVo studentHomeVo = new StudentHomeVo();
        try {
            BeanUtils.copyProperties(studentHomeVo, detail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        studentHomeVo.setHeardUrl(user.getAvatar());
        studentHomeVo.setUsername(user.getUsername());
        studentHomeVo.setBirthdate(user.getBirthdate());
        int num = DateUtil.daysBetween(new Date(), detail.getMembershipEndTime());
        studentHomeVo.setMembershipDays(num < 0 ? 0 : num);
        StudentTotal total = (StudentTotal) redissonClient.getBucket(CacheNameEnum.STUDENT_TOTAL.getRedisKey(user.getId()))
                .get();
        if (null == total) {
            total = new StudentTotal();
        }
        studentHomeVo.setExerciseDays(null == total.getExerciseDays() ? 0 : total.getExerciseDays());
        studentHomeVo.setExerciseHours(null == total.getExerciseHours() ? 0 : total.getExerciseHours());
        studentHomeVo.setTestingNum(null == total.getTestingNum() ? 0 : total.getTestingNum());
        studentHomeVo.setFinshClassHours(null == total.getFinshHours() ? 0 : total.getFinshHours());
        studentHomeVo.setUnfinshClassHours(null == total.getUnfinshHours() ? 0 : total.getUnfinshHours());
        studentHomeVo.setStarTeacherNum(null == total.getStarTeacherNum() ? 0 : total.getStarTeacherNum());
        return studentHomeVo;
    }

    @Override
    public Integer setSubject(Long id, String subjectIds) {
        return baseMapper.setSubject(id, subjectIds);
    }
}