package cn.com.qmth.examcloud.core.questions.service.impl;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.base.Model;
import cn.com.qmth.examcloud.core.questions.dao.PaperDetailRepo;
import cn.com.qmth.examcloud.core.questions.dao.PaperDetailUnitRepo;
import cn.com.qmth.examcloud.core.questions.dao.PaperRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.service.PaperDetailService;
import cn.com.qmth.examcloud.core.questions.service.PaperDetailUnitService;
import cn.com.qmth.examcloud.core.questions.service.QuesTypeNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenken
 * @date 2017年9月13日 上午11:32:33
 * @company QMTH
 * @description PaperDetailServiceImpl.java
 */
@Service("paperDetailService")
public class PaperDetailServiceImpl implements PaperDetailService {

    @Autowired
    PaperDetailRepo paperDetailRepo;

    @Autowired
    PaperDetailUnitService unitService;

    @Autowired
    PaperRepo paperRepo;

    @Autowired
    QuesTypeNameService quesTypeNameService;

    @Autowired
    PaperDetailUnitRepo paperDetailUnitRepo;

    /**
     * 根据Id获得对应所有小题
     *
     * @param id
     * @return
     */
    public List<PaperDetailUnit> getUnitsByPaperDetailId(String id) {
        return unitService.getUnitsByPaperDetail(Model.of(paperDetailRepo.findById(id)));
    }

    /**
     * 按ID查询大题
     *
     * @param id
     * @return
     */
    public PaperDetail findById(String id) {
        return Model.of(paperDetailRepo.findById(id));
    }

    /**
     * 保存大题
     *
     * @param pd
     * @return
     */
    public PaperDetail savePaperDetail(PaperDetail pd, String paperId, User user) {
        Paper paper = Model.of(paperRepo.findById(paperId));
        paper.setLastModifyName(user.getDisplayName());
        paper = paperRepo.save(paper);
        pd.setPaper(paper);
        PaperDetail paperDetail = paperDetailRepo.save(pd);
        List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaperDetailOrderByNumber(paperDetail);
        quesTypeNameService.saveQuesTypeName(paperDetailUnits);
        return paperDetail;
    }

    /**
     * 删除大题
     *
     * @param id
     * @return
     */
    public void deletePaperDetail(String id) {
        paperDetailRepo.deleteById(id);
    }

    /**
     * 查询同一个试卷对象对应大题
     *
     * @param paper
     * @return
     */
    public List<PaperDetail> getPaperDetailsByPaper(Paper paper) {
        return paperDetailRepo.findByPaperOrderByNumber(paper);
    }

    /**
     * 删除试卷大题
     *
     * @param paper
     */
    public void deletePaperDetailsByPaper(Paper paper) {
        List<PaperDetail> details = getPaperDetailsByPaper(paper);
        unitService.deleteUnitsByPaperDetails(details);
        paperDetailRepo.deleteAll(details);
    }

    /**
     * 批量删除试卷大题
     *
     * @param papers
     */
    public void deletePaperDetailsByPapers(List<Paper> papers) {
        for (Paper paper : papers) {
            List<PaperDetail> details = getPaperDetailsByPaper(paper);
            unitService.deleteUnitsByPaperDetails(details);
            paperDetailRepo.deleteAll(details);
        }
    }

}