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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.entity.ClientStatus;
import com.qmth.distributed.print.business.entity.ExamDetailCourse;
import com.qmth.distributed.print.business.mapper.ClientStatusMapper;
import com.qmth.distributed.print.business.service.ClientStatusService;
import com.qmth.distributed.print.business.service.ExamDetailCourseService;
import com.qmth.distributed.print.business.service.ExamTaskService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Date: 2021/4/19.
 */
@Service
public class ClientStatusServiceImpl extends ServiceImpl<ClientStatusMapper, ClientStatus> implements ClientStatusService {

    @Autowired
    private ExamDetailCourseService examDetailCourseService;

    @Override
    public Boolean tagPass(Long schoolId, Long printPlanId, String courseCode, String courseName, String paperNumber, String machineCode, Boolean isPass, Long userId) {
        List<ExamDetailCourse> examDetailCourses = examDetailCourseService.listByPrintPlanIdAndCourseCodeAndPaperNumber(schoolId, printPlanId, courseCode, paperNumber);
        long currentTime = System.currentTimeMillis();
        for (ExamDetailCourse examDetailCours : examDetailCourses) {
            QueryWrapper<ClientStatus> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ClientStatus::getSchoolId, schoolId).eq(ClientStatus::getExamDetailId, examDetailCours.getExamDetailId()).eq(ClientStatus::getPrintPlanId, printPlanId).eq(ClientStatus::getCourseCode, courseCode).eq(ClientStatus::getPaperNumber, paperNumber).eq(ClientStatus::getMachineCode, machineCode);
            ClientStatus clientStatus = this.getOne(queryWrapper);
            if(clientStatus != null){
                clientStatus.setTry(true);
                clientStatus.setPass(isPass);
                clientStatus.setUpdateId(userId);
                clientStatus.setUpdateTime(currentTime);
                // 合格，更新打印时间
                if(isPass){
                    clientStatus.setTryTime(currentTime);
                }
                this.updateById(clientStatus);
            } else {
                clientStatus = new ClientStatus();
                clientStatus.setId(SystemConstant.getDbUuid());
                clientStatus.setSchoolId(schoolId);
                clientStatus.setExamDetailId(examDetailCours.getExamDetailId());
                clientStatus.setPrintPlanId(printPlanId);
                clientStatus.setMachineCode(machineCode);
                clientStatus.setCourseCode(courseCode);
                clientStatus.setCourseName(courseName);
                clientStatus.setPaperNumber(paperNumber);
                clientStatus.setTry(true);
                clientStatus.setTryTime(currentTime);
                clientStatus.setPass(isPass);
                clientStatus.setDownload(false);
                clientStatus.setCreateId(userId);
                clientStatus.setCreateTime(currentTime);
                this.save(clientStatus);
            }
        }
        return true;
    }
}
