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

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.bean.sys.homework.domain.HomeworkPictureCorrect;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.dao.sys.homework.dao.HomeworkPictureCorrectDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.Constant;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.teacher.service.TeacherService;
import com.ssj.service.sys.homework.service.HomeworkPictureCorrectService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 作业批改记录业务
 *
 * @author Mount Liu
 * @date 2019/7/22
 */
@Service(version = "1.0")
@Transactional
public class HomeworkPictureCorrectServiceImpl extends BaseServiceImpl<HomeworkPictureCorrect, String> implements HomeworkPictureCorrectService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HomeworkPictureCorrectDao dao;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private IBookManagerService bookManagerService;

    @Override
    public PagingAndSortingRepository<HomeworkPictureCorrect, String> getDao() {
        return dao;
    }

    @Override
    public HomeworkPictureCorrect findByHomeworkPictureIdAndType(String homeworkPictureId, int type) {
        return dao.findTopByHomeworkPictureIdAndType(homeworkPictureId, type);
    }

    @Override
    public HomeworkPictureCorrect findByHomeworkPictureIdAndType(String homeworkPictureId, List<Integer> types) {
        return dao.findTopByHomeworkPictureIdAndTypeIn(homeworkPictureId, types);
    }

    @Override
    public HomeworkPictureCorrect findNoCheckErrorByHomeworkPictureIdAndType(String homeworkPictureId, int type) {
        return dao.findTopByHomeworkPictureIdAndTypeAndIsCheckError(homeworkPictureId, type, 0);
    }

    @Override
    public long saveCorrectionEndTime4Record(HomeworkPicture homeworkPicture, int type) {
        HomeworkPictureCorrect homeworkPictureCorrect = this.findByHomeworkPictureIdAndType(homeworkPicture.getId(), type);
        Date now = new Date();
        long useTime = 0;
        if (homeworkPictureCorrect == null) {
            homeworkPictureCorrect = new HomeworkPictureCorrect();
            homeworkPictureCorrect.setHomeworkId(homeworkPicture.getHomeworkId());
            homeworkPictureCorrect.setHomeworkPictureId(homeworkPicture.getId());
            homeworkPictureCorrect.setCreateTime(now);
            homeworkPictureCorrect.setStartTime(now);
            homeworkPictureCorrect.setType(type);
        } else if (homeworkPictureCorrect.getStartTime() == null) {
            homeworkPictureCorrect.setStartTime(now);
        }

        //二次批改状态记录
        if (homeworkPicture.getIsFeedback() == 1 && type == Constant.KMT_122_TEACHER_TYPE){
            homeworkPictureCorrect.setSecondCorrectType(1);
            homeworkPicture.setIsKnowFeedback(0);
            HomeworkPictureCorrect knowCorrect = this.findByHomeworkPictureIdAndType(homeworkPicture.getId(), Constant.KNOW_MATCH_TYPE);
            if (knowCorrect != null){
                knowCorrect.setEndTime(null);
                this.save(knowCorrect);
            }
            TbLibManager knowTeacher = bookManagerService.findByUserId(homeworkPicture.getKnowTeacherId(), Constant.KNOW_MATCH_TYPE);
            if (knowTeacher != null){
                teacherService.pushWxToKnowTeacher4SecondCorrect(knowTeacher, homeworkPicture.getVipId(), homeworkPicture.getSubject());
            }
        }
        if (type == Constant.KNOW_MATCH_TYPE){
            homeworkPictureCorrect.setCheckEndTime(null);
            HomeworkPictureCorrect homeworkPictureCorrectTmp = this.findByHomeworkPictureIdAndType(homeworkPicture.getId(), Constant.KMT_122_TEACHER_TYPE);
            homeworkPictureCorrectTmp.setSecondCorrectType(0);
            if (homeworkPicture.getIsKnowFeedback() == 1){
                homeworkPictureCorrect.setSecondCorrectType(1);
            }
            this.save(homeworkPictureCorrectTmp);
        }

        homeworkPictureCorrect.setEndTime(now);
        if (homeworkPictureCorrect.getOperateNums() == 0){
            homeworkPictureCorrect.setOperateNums(1);
        }
        //累加纠正次数, 批改质检后不能再次批改，所以纠正次数不设置
        if (homeworkPictureCorrect.getIsCheckError() == 1) {
            homeworkPictureCorrect.setOperateNums(homeworkPictureCorrect.getOperateNums() + 1);
            homeworkPictureCorrect.setCorrectNums(homeworkPictureCorrect.getCorrectNums() + 1);
        }
        useTime = (DateUtil.getMillis(now) - DateUtil.getMillis(homeworkPictureCorrect.getStartTime())) / 1000L;
        homeworkPictureCorrect.setCorrectionTime((int)useTime);
        homeworkPictureCorrect.setIsCheckError(0);
        homeworkPictureCorrect.setCheckDate(null);
        homeworkPictureCorrect.setChecker(null);
        homeworkPictureCorrect.setErrorReason(null);
        this.save(homeworkPictureCorrect);
        return useTime;
    }

    @Override
    public void clearCorrectionTime(String homeworkPictureId, int type) {
        dao.updateCorrectTime(homeworkPictureId, type);
    }

    @Override
    public void clearCorrectionTime(String id) {
        dao.updateCorrectTime(id);
    }

    @Override
    public void clearRecoveryCorrectionTime(String beginTime){

        dao.clearRecoveryCorrectionTimeV8(beginTime);
        dao.clearRecoveryCorrectionTimeV13(beginTime);
    }

    @Override
    public List<HomeworkPictureCorrect> findByTypeAndHomeworkPictureIdIn(int type,List<String>list){
      return dao.findByTypeAndHomeworkPictureIdIn(type,list);
    }


    @Override
    public List<HomeworkPictureCorrect> findByHomeworkId(String homeworkId){
        return dao.findByHomeworkId(homeworkId);
    }

    @Override
    public List<HomeworkPictureCorrect> findByHomeworkIdAndTeacherIdType(String homeworkId, String teacherId, int type) {
        if ( 8 == type) {
            return dao.findByHomeworkIdAndCorrectTeacherIdType(homeworkId, teacherId);
        } else {
            return dao.findByHomeworkIdAndKnowTeacherIdType(homeworkId, teacherId);
        }
    }
}