package com.ym.mec.biz.service.impl;


import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.ClassGroupDao;
import com.ym.mec.biz.dal.dao.CourseScheduleEvaluateDao;
import com.ym.mec.biz.dal.dao.PracticeGroupDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.entity.ClassGroup;
import com.ym.mec.biz.dal.entity.CourseScheduleEvaluate;
import com.ym.mec.biz.dal.entity.PracticeGroup;
import com.ym.mec.biz.service.CourseScheduleEvaluateService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.redis.service.RedisCache;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CourseScheduleEvaluateServiceImpl extends BaseServiceImpl<Long, CourseScheduleEvaluate> implements CourseScheduleEvaluateService {

    @Autowired
    private CourseScheduleEvaluateDao courseScheduleEvaluateDao;
    @Autowired
    private ClassGroupDao classGroupDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private PracticeGroupDao practiceGroupDao;
    @Autowired
    private RedisCache<String, Object> redisCache;

    @Override
    public BaseDAO<Long, CourseScheduleEvaluate> getDAO() {
        return courseScheduleEvaluateDao;
    }


    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public boolean addStudyReport(CourseScheduleEvaluate courseScheduleEvaluate) {
        ClassGroup classGroup = classGroupDao.get(courseScheduleEvaluate.getClassGroupId());
        if (classGroup == null) {
            throw new BizException("课程不存在！");
        }
        String lockKey = "study_report_" + classGroup.getId();
        if (!redisCache.getLocked(lockKey, 1, 10)) {
            throw new BizException("报告提交中，请稍等");
        }
        try {
            CourseScheduleEvaluate hasOne = courseScheduleEvaluateDao.findByClassGroupId(courseScheduleEvaluate.getClassGroupId());
            if (hasOne != null) {
                throw new BizException("报告已添加，请勿重复提交");
            }
            courseScheduleEvaluate.setMusicGroupId(classGroup.getMusicGroupId());
            courseScheduleEvaluate.setCreateTime(new Date());
            courseScheduleEvaluate.setStatus(1);
            long num = courseScheduleEvaluateDao.insert(courseScheduleEvaluate);
            if (num <= 0) {
                throw new BizException("报告添加失败，请重试");
            }
        } finally {
            redisCache.releaseLocked(lockKey, 1);
        }
        return true;
    }

    @Override
    public CourseScheduleEvaluate getStudyReport(Integer classGroupId) {
        CourseScheduleEvaluate studyReport = courseScheduleEvaluateDao.findByClassGroupId(classGroupId);
        if (studyReport == null) {
            throw new BizException("报告不存在");
        }
        SysUser user = teacherDao.getUser(studyReport.getStudentId());
        BasicUserDto teacherInfo = teacherDao.findTeacherInfo(studyReport.getTeacherId());
        studyReport.setStudent(user);
        studyReport.setTeacher(teacherInfo);
        long practiceGroupId = Long.parseLong(studyReport.getMusicGroupId());
        PracticeGroup practiceGroup = practiceGroupDao.get(practiceGroupId);
        ClassGroup classGroup = classGroupDao.get(studyReport.getClassGroupId());
        studyReport.setTimes(classGroup.getTotalClassTimes());
        studyReport.setTotalMinutes(classGroup.getTotalClassTimes() * practiceGroup.getSingleClassMinutes());
        return studyReport;

    }

    @Override
    public List<CourseScheduleEvaluate> findByGroupId(Integer groupId) {
        return courseScheduleEvaluateDao.findByGroupId(groupId);
    }

    @Override
    public CourseScheduleEvaluate findById(Integer id) {
        return courseScheduleEvaluateDao.findById(id);
    }

    @Override
    public List<CourseScheduleEvaluate> getNeedPostList(Integer teacherId) {
        return courseScheduleEvaluateDao.getNeedPostReports(teacherId);
    }

    @Override
    public List<CourseScheduleEvaluate> createEvaluate(Date NowDate, Date afterDate) {
        List<CourseScheduleEvaluate> needPostReportPracticeGroups = practiceGroupDao.getNeedPostReportPracticeGroups(NowDate, afterDate);
        for (CourseScheduleEvaluate needPostReportPracticeGroup : needPostReportPracticeGroups) {
            needPostReportPracticeGroup.setCreateTime(NowDate);
            needPostReportPracticeGroup.setStatus(0);
            needPostReportPracticeGroup.setVersion(2);
        }
        if (needPostReportPracticeGroups.size() > 0) {
            courseScheduleEvaluateDao.batchAdd(needPostReportPracticeGroups);
        }
        return needPostReportPracticeGroups;
    }
}
