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

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.service.TeacherAttendanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 老师考勤表(TeacherAttendance)表服务实现类
 *
 * @author hgw
 * @since 2022-05-05 13:51:24
 */
@Service("teacherAttendanceService")
public class TeacherAttendanceServiceImpl extends ServiceImpl<TeacherAttendanceDao, TeacherAttendance> implements TeacherAttendanceService {

    private final static Logger log = LoggerFactory.getLogger(TeacherAttendanceServiceImpl.class);

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void signOut(Long userId, CourseSchedule courseSchedule) {
        //更新签到记录
        TeacherAttendance teacherAttendance = getDao().findByUserIdAndCourseId(userId, courseSchedule.getId());
        Date now = new Date();
        if (Objects.isNull(teacherAttendance)) {
            teacherAttendance = createAttendance(userId, courseSchedule, now);
            teacherAttendance.setSignOutTime(teacherAttendance.getSignInTime());
            teacherAttendance.setSignInTime(courseSchedule.getStartTime());
            getDao().insert(teacherAttendance);
        }else {
            teacherAttendance.setSignOutTime(now);
            teacherAttendance.setUpdateTime(now);
            getDao().updateById(teacherAttendance);
        }
    }

    private TeacherAttendance createAttendance(Long userId, CourseSchedule courseSchedule,Date now) {
        TeacherAttendance teacherAttendance = new TeacherAttendance();
        teacherAttendance.setCourseScheduleId(courseSchedule.getId());
        teacherAttendance.setTeacherId(userId);
        teacherAttendance.setSignInTime(now);
        teacherAttendance.setCourseGroupId(courseSchedule.getCourseGroupId());
        teacherAttendance.setCourseGroupType(courseSchedule.getType());
        teacherAttendance.setCreateTime(now);
        teacherAttendance.setUpdateTime(now);
        return teacherAttendance;
    }
}

