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

import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.bean.sys.homework.domain.HomeworkPictureCorrect;
import com.ssj.bean.sys.imlibuser.TbClassTeacher;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.dao.sys.homework.dao.HomeworkPictureDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.sys.homework.service.HomeworkPictureCorrectService;
import com.ssj.service.sys.homework.service.HomeworkPictureService;
import com.ssj.service.sys.imlibuser.service.TbClassTeacherService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author sh
 * @className HomeworkPictureServiceImpl
 * @description TODO
 * @date 2020/7/14
 */
@Service
@Transactional
public class HomeworkPictureServiceImpl extends BaseServiceImpl<HomeworkPicture, String> implements HomeworkPictureService {
    @Autowired
    private HomeworkPictureDao homeworkPictureDao;

    @Autowired
    private IBookManagerService managerService;

    @Autowired
    private TbClassTeacherService tbClassTeacherService;

    @Autowired
    private HomeworkPictureCorrectService homeworkPictureCorrectService;

    @Override
    public PagingAndSortingRepository<HomeworkPicture, String> getDao() {
        return homeworkPictureDao;
    }

    @Override
    public void allotLibHomework(String libId, String teacherId, String classId) {
        TbLibManager teacher = managerService.findByLibIdAndUserIdAndType(libId, teacherId, 15);
        //所有科目
        List<String> subjects;
        if (teacher != null) {
            subjects = Arrays.asList(teacher.getCourseName().split(","));
        } else {
            return;
        }
        //作业有效开始时间
        String beginTime = this.getBeginTime();
        //查找该老师下所有班级
        if (StringUtil.isBlank(classId)) {
            List<TbClassTeacher> classTeacherList = tbClassTeacherService.getTbLibImTeacherBylibId(teacher.getLibId(), teacherId);
            classTeacherList.forEach(e -> {
                //查找指定班级下的指定科目的未分配校区批改老师的作业列表
                List<HomeworkPicture> list = homeworkPictureDao.findUnallocatedHomeworkList(subjects, e.getClassId(), beginTime);
                list.forEach(homeworkPicture -> {
                    //分配老师
                    homeworkPicture.setTeacherId(teacherId);
                    HomeworkPictureCorrect homeworkPictureCorrect = homeworkPictureCorrectService.findByHomeworkPictureIdAndType(homeworkPicture.getId(),15);
                    if (homeworkPictureCorrect == null) {
                        //插入分配记录
                        homeworkPictureCorrect = new HomeworkPictureCorrect();
                        homeworkPictureCorrect.setType(managerService.findCorrectTeacherType(homeworkPicture.getTeacherId()));
                        homeworkPictureCorrect.setCreateTime(new Date());
                        homeworkPictureCorrect.setHomeworkPictureId(homeworkPicture.getId());
                        homeworkPictureCorrect.setHomeworkId(homeworkPicture.getHomeworkId());
                    }
                    homeworkPictureCorrect.setStartTime(null);
                    homeworkPictureCorrect.setEndTime(null);
                    homeworkPictureCorrect.setAllotTime(homeworkPicture.getAllotTime());
                    homeworkPictureCorrectService.save(homeworkPictureCorrect);
                });
                this.save(list);
            });
        } else {
            //查找指定班级下的指定科目的未分配校区批改老师的作业列表
            List<HomeworkPicture> list = homeworkPictureDao.findUnallocatedHomeworkList(subjects, classId, beginTime);
            list.forEach(homeworkPicture -> {
                //分配老师
                homeworkPicture.setTeacherId(teacherId);
                HomeworkPictureCorrect homeworkPictureCorrect = homeworkPictureCorrectService.findByHomeworkPictureIdAndType(homeworkPicture.getId(),15);
                if (homeworkPictureCorrect == null) {
                    //插入分配记录
                    homeworkPictureCorrect = new HomeworkPictureCorrect();
                    homeworkPictureCorrect.setType(managerService.findCorrectTeacherType(homeworkPicture.getTeacherId()));
                    homeworkPictureCorrect.setCreateTime(new Date());
                    homeworkPictureCorrect.setHomeworkPictureId(homeworkPicture.getId());
                    homeworkPictureCorrect.setHomeworkId(homeworkPicture.getHomeworkId());
                }
                homeworkPictureCorrect.setStartTime(null);
                homeworkPictureCorrect.setEndTime(null);
                homeworkPictureCorrect.setAllotTime(homeworkPicture.getAllotTime());
                homeworkPictureCorrectService.save(homeworkPictureCorrect);
            });
            this.save(list);
        }
    }

    @Override
    public List<HomeworkPicture> findNoFeedbackByHomeworkId(String homeworkId) {
        return homeworkPictureDao.findNoFeedbackByHomeworkId(homeworkId);
    }

    @Override
    public List<HomeworkPicture> findAllByHomeworkId(String homeworkId) {
        return homeworkPictureDao.queryHomeworkPictureByHomeworkId(homeworkId);
    }

    private String getBeginTime() {
        Date now = new Date();
        String nowString = DateUtil.format_yyyyMMddHHmmss(now);
        String today = DateUtil.format_yyyyMMdd(now);
        String today2pm = today + " 14:00:00";
        String beginTime;
        if (nowString.compareTo(today2pm) >= 0) {
            //超过下午2点获取今天的数据
            beginTime = today;
        } else {
            //获取昨天
            beginTime = DateUtil.format_yyyyMMdd(DateUtil.addDays(now, -1));
        }
        return beginTime;
    }
}
