package com.ssj.service.sys.homework.service.impl;

import com.ssj.bean.sys.homework.domain.NoHomeworkRecord;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.dao.sys.homework.dao.NoHomeworkRecordDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.sys.homework.service.NoHomeworkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class NoHomeworkServiceImpl extends BaseServiceImpl<NoHomeworkRecord, String> implements NoHomeworkRecordService {
    @Autowired
    private NoHomeworkRecordDao noHomeworkRecordDao;
    @Override
    public PagingAndSortingRepository<NoHomeworkRecord, String> getDao() {
        return noHomeworkRecordDao;
    }

    @Override
    public List<NoHomeworkRecord> findTodayRecord(String vipId, String courseName) {
        return noHomeworkRecordDao.findByVipIdAndCourseName(vipId, courseName);
    }

    @Override
    public NoHomeworkRecord findDateRecord(String vipId, String courseName, Date date) {
        String beginTime = DateUtil.format_yyyyMMdd(date);
        String endTime = DateUtil.format_yyyyMMdd(DateUtil.addDays(date, 1));
        return noHomeworkRecordDao.findDateRecordByVipIdAndCourseName(vipId, courseName, beginTime, endTime);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<NoHomeworkRecord> findTodayRecord(String vipId) {
        return noHomeworkRecordDao.findByVipId(vipId);
    }

    @Override
    public NoHomeworkRecord saveTodayNoHomeworkRecord(LibVip libVip, String courseName, int type, int deviceType, String homeworkId) {
        NoHomeworkRecord noHomeworkRecord = this.findRecordByVipIdAndSubjectAndDate(libVip.getId(), courseName, new Date());
        if (noHomeworkRecord != null){
            //理论上是唯一一条记录
            noHomeworkRecord.setType(type);
        } else {
            noHomeworkRecord = new NoHomeworkRecord();
            noHomeworkRecord.setVipId(libVip.getId());
            noHomeworkRecord.setType(type);
            noHomeworkRecord.setCourseName(courseName);
            noHomeworkRecord.setDeviceType(deviceType);
            noHomeworkRecord.setGrade(libVip.getGrade());
            noHomeworkRecord.setCreateTime(new Date());
        }
        noHomeworkRecord.setHomeworkId(homeworkId);
        noHomeworkRecord = this.save(noHomeworkRecord);
        return noHomeworkRecord;
    }

    @Override
    public List<NoHomeworkRecord> findDateRecordByVipId(String vipId, Date date) {
        String beginTime = DateUtil.format_yyyyMMdd(date);
        String endTime = DateUtil.format_yyyyMMdd(DateUtil.addDays(date, 1));
        return noHomeworkRecordDao.findDateRecordByVipId(vipId, beginTime, endTime);
    }

    @Override
    public NoHomeworkRecord findRecordByVipIdAndSubjectAndDate(String vipId, String courseName, Date dateTime) {
        String beginTime = DateUtil.format_yyyyMMdd(dateTime);
        return noHomeworkRecordDao.findDateRecordByVipIdAndCourseName(vipId, courseName, beginTime, beginTime + " 23:59:59");
    }

    @Override
    public List<NoHomeworkRecord> findRecordByVipIdAndSubjectAndBeginTimeAndEndTime(String vipId, String courseName, String beginTime, String endTime) {
        return noHomeworkRecordDao.findByVipIdAndCourseNameAndTime(vipId, courseName, beginTime, endTime);
    }
}
