package com.qmth.distributed.print.business.service.impl;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.qmth.boot.core.concurrent.annotation.Lockable;
import com.qmth.distributed.print.business.entity.*;
import com.qmth.distributed.print.business.enums.AssignModeEnum;
import com.qmth.distributed.print.business.mapper.ExamTaskAssignPaperTypeMapper;
import com.qmth.distributed.print.business.service.ExamDetailService;
import com.qmth.distributed.print.business.service.ExamTaskAssignPaperTypeService;
import com.qmth.distributed.print.business.util.pdf.CreatePdfNewUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-02-07
 */
@Service
public class ExamTaskAssignPaperTypeServiceImpl extends MppServiceImpl<ExamTaskAssignPaperTypeMapper, ExamTaskAssignPaperType> implements ExamTaskAssignPaperTypeService {

    @Resource
    ExamDetailService examDetailService;
    @Resource
    CreatePdfNewUtil createPdfNewUtil;

    @Lockable(name = "#p0.examId + '-' + #p1.paperNumber")
    @Override
    public ExamTaskAssignPaperType extractPaperType(TBTaskPdf tbTaskPdf, ExamDetailCourse examDetailCourse) {
        ExamDetail examDetail = examDetailService.getById(tbTaskPdf.getId());
        if (examDetail.getExamStartTime() == null) {
            examDetail.setExamStartTime(-1L);
        }
        if (examDetail.getExamEndTime() == null) {
            examDetail.setExamEndTime(-1L);
        }
        ExamTaskAssignPaperType examTaskAssignPaperType = this.selectByMultiId(new ExamTaskAssignPaperType(tbTaskPdf.getExamId(), examDetailCourse.getPaperNumber(), examDetail.getExamStartTime(), examDetail.getExamEndTime()));
        if (examTaskAssignPaperType == null) {
            String paperType = createPdfNewUtil.getPaperType(examDetail.getPrintPlanId(), examDetail.getExamId(), examDetailCourse.getPaperNumber());
            examTaskAssignPaperType = new ExamTaskAssignPaperType(tbTaskPdf.getExamId(), examDetailCourse.getPaperNumber(), examDetail.getExamStartTime(), examDetail.getExamEndTime(), paperType, AssignModeEnum.AUTO);
            this.saveOrUpdateByMultiId(examTaskAssignPaperType);
        }
        return examTaskAssignPaperType;
    }
}
