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

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtHomeworkWrongQuestion;
import com.ssj.bean.sys.homework.domain.HomeworkHowDetail;
import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetails;
import com.ssj.dao.sys.homework.dao.HomeworkHowDetailsDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.exception.CommonException;
import com.ssj.service.conch.parents.v2.dto.KnowHowDetailDTO;
import com.ssj.service.kmt.service.KmtExerciseQuestionService;
import com.ssj.service.mapper.HomeworkMapper;
import com.ssj.service.sys.homework.service.HomeworkHowDetailsService;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;

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

@Service(version="1.0")
public class HomeworkHowDetailServiceImpl extends BaseServiceImpl<HomeworkHowDetail, String> implements HomeworkHowDetailsService {

    @Autowired
    private HomeworkHowDetailsDao dao;
    @Autowired
    private ZuoybKnowHowDetailsService zuoybKnowHowDetailsService;
    @Autowired
    private KmtExerciseQuestionService kmtExerciseQuestionService;
    @Autowired
    private HomeworkMapper homeworkMapper;

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

    @Override
    public HomeworkHowDetail createHomeworkHowDetail(Date now, String homeworkId, String detailId, Integer knowledgeVersion) {
        ZuoybKnowHowDetails zuoybKnowHowDetails = zuoybKnowHowDetailsService.getById(detailId);

        if (zuoybKnowHowDetails == null) {
            throw new RuntimeException("知识点不存在");
        }

        HomeworkHowDetail homeworkHowDetail = new HomeworkHowDetail();
        homeworkHowDetail.setHomeworkId(homeworkId);
        homeworkHowDetail.setHowDetailId(zuoybKnowHowDetails.getId());
        homeworkHowDetail.setModule(zuoybKnowHowDetails.getModule());
        homeworkHowDetail.setWorkDetailType(1);
        homeworkHowDetail.setDetailCount(1);
        homeworkHowDetail.setKnowledgeVersion(knowledgeVersion);
        homeworkHowDetail.setCreateTime(now);

        return homeworkHowDetail;
    }

    @Override
    public HomeworkHowDetail createHomeworkHowDetail(Date now, KmtHomeworkWrongQuestion kmtHomeworkWrongQuestion, String knowledgeId) {
        ZuoybKnowHowDetails zuoybKnowHowDetails = zuoybKnowHowDetailsService.getById(knowledgeId);

        if (zuoybKnowHowDetails == null) {
            throw new CommonException("知识点不存在");
        }

        HomeworkHowDetail homeworkHowDetail = new HomeworkHowDetail();
        homeworkHowDetail.setWrongQuestionId(kmtHomeworkWrongQuestion.getId());
        homeworkHowDetail.setHomeworkId(kmtHomeworkWrongQuestion.getHomeworkId());
        homeworkHowDetail.setHomeworkPictureId(kmtHomeworkWrongQuestion.getHomeworkPictureId());
        homeworkHowDetail.setHowDetailId(zuoybKnowHowDetails.getId());
        homeworkHowDetail.setModule(zuoybKnowHowDetails.getModule());
        homeworkHowDetail.setWorkDetailType(1);
        homeworkHowDetail.setDetailCount(1);
        homeworkHowDetail.setCreateTime(now);

        Integer knowledgeVersion = kmtExerciseQuestionService.findKnowledgeVersionById(kmtHomeworkWrongQuestion.getQuestionId());
        if (knowledgeVersion == null) {//如果没有版本，则默认北师大版
            knowledgeVersion = 1;
        }
        homeworkHowDetail.setKnowledgeVersion(knowledgeVersion);

        return homeworkHowDetail;
    }

    @Override
    public void deleteByHomeworkPictureIdIn(List<String> homeworkPictureIdList) {
        dao.deleteByHomeworkPictureIdIn(homeworkPictureIdList);
    }

    @Override
    public void deleteByHomeworkPictureId(String homeworkPictureId) {
        dao.deleteByHomeworkPictureId(homeworkPictureId);
    }

    @Override
    public void deleteExerciseByHomeworkPictureId(String homeworkPictureId) {
        dao.deleteExerciseByHomeworkPictureId(homeworkPictureId);
    }

    @Override
    public void deleteWrongQuesByHomeworkId(String homeworkId) {
        dao.deleteWrongQuesByHomeworkId(homeworkId);
    }

    @Override
    public void deleteWrongBookQuestion(List<KnowHowDetailDTO> knowHowDetailDTOList, String homeworkId) {
        knowHowDetailDTOList.forEach(knowHowDetailDTO -> {
            dao.deleteWrongBookQuestion(knowHowDetailDTO.getKnowHowDetailId(), knowHowDetailDTO.getKnowledgeVersion(), homeworkId);
            homeworkMapper.deleteWrongQuestionKnowledgeCount(homeworkId, knowHowDetailDTO.getKnowHowDetailId(), knowHowDetailDTO.getKnowledgeVersion());
        });
    }

    @Override
    public List<HomeworkHowDetail> findHowDetailsByHomeworkPictureId(String homeworkPictureId) {
        return dao.findByHomeworkPictureId(homeworkPictureId);
    }

    @Override
    public List<HomeworkHowDetail> findAllByHomeworkId(String homeworkId) {
        return dao.findByHomeworkIdAndWrongQuestionIdIsNull(homeworkId);
    }
}
