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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.TeacherAttendanceDao;
import com.yonge.cooleshow.biz.dal.entity.CourseSchedule;
import com.yonge.cooleshow.biz.dal.entity.TeacherAttendance;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import org.springframework.stereotype.Service;
import com.yonge.cooleshow.biz.dal.entity.StudentAttendance;
import com.yonge.cooleshow.biz.dal.vo.StudentAttendanceVo;
import com.yonge.cooleshow.biz.dal.dto.search.StudentAttendanceSearch;
import com.yonge.cooleshow.biz.dal.dao.StudentAttendanceDao;
import com.yonge.cooleshow.biz.dal.service.StudentAttendanceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
public class StudentAttendanceServiceImpl extends ServiceImpl<StudentAttendanceDao, StudentAttendance> implements StudentAttendanceService {

    @Override
    public StudentAttendanceDao getDao() {
        return this.baseMapper;
    }

	@Override
    public StudentAttendanceVo detail(Long id) {
        StudentAttendanceVo detail = baseMapper.detail(id);
        return detail;
    }
    
     @Override
    public IPage<StudentAttendanceVo> selectPage(IPage<StudentAttendanceVo> page, StudentAttendanceSearch query){
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    @Override
    public StudentAttendance getByCourseTypeAndScheduleId(CourseScheduleEnum courseType, Long courseScheduleId) {
        List<StudentAttendance> list = this.lambdaQuery()
                                           .eq(StudentAttendance::getCourseGroupType, courseType)
                                           .eq(StudentAttendance::getCourseScheduleId, courseScheduleId)
                                           .list();
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StudentAttendance signIn(Long userId, CourseSchedule courseSchedule) {
        //查询是否已经签到
        //如果存在则跳过，如果不存在则新增
        StudentAttendance studentAttendance = getDao().findByUserIdAndCourseId(userId, courseSchedule.getId());
        if (Objects.nonNull(studentAttendance)) {
            if(Objects.isNull(studentAttendance.getSignInTime())){
                studentAttendance.setSignInTime(new Date());
                getDao().updateById(studentAttendance);
            }
            return studentAttendance;
        }
        Date now = new Date();
        studentAttendance = createAttendance(userId, courseSchedule, now);
        getDao().insert(studentAttendance);
        return studentAttendance;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void signOut(Long userId, CourseSchedule courseSchedule) {
        //更新签到记录
        StudentAttendance studentAttendance = getDao().findByUserIdAndCourseId(userId, courseSchedule.getId());
        Date now = new Date();
        if (Objects.isNull(studentAttendance)) {
            studentAttendance = createAttendance(userId, courseSchedule,now);
            studentAttendance.setSignOutTime(now);
            studentAttendance.setSignInTime(courseSchedule.getStartTime());
            getDao().insert(studentAttendance);
        }else {
            studentAttendance.setSignOutTime(now);
            studentAttendance.setUpdateTime(now);
            getDao().updateById(studentAttendance);
        }
    }
    private StudentAttendance createAttendance(Long userId, CourseSchedule courseSchedule,Date now) {
        StudentAttendance studentAttendance = new StudentAttendance();
        studentAttendance.setCourseScheduleId(courseSchedule.getId());
        studentAttendance.setStudentId(userId);
        studentAttendance.setSignInTime(now);
        studentAttendance.setCourseGroupId(courseSchedule.getCourseGroupId());
        studentAttendance.setCourseGroupType(courseSchedule.getType());
        studentAttendance.setCreateTime(now);
        studentAttendance.setUpdateTime(now);
        return studentAttendance;
    }

}