package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.jkserver.core.exception.ApiException;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dao.ExamStudentDao;
import com.qmth.jkserver.enums.ExamFinishStatusEnum;
import com.qmth.jkserver.model.ExamStage;
import com.qmth.jkserver.model.ExamStudent;
import com.qmth.jkserver.service.ExamStudentService;
import com.qmth.jkserver.service.JkExamStageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 考务考生服务
 *
 * @author tianjun
 */
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStudent> implements ExamStudentService {

//    @Autowired
//    private MongoOperations mongoTemplate;

    @Resource
    private ExamStudentDao examStudentDao;

    @Resource
    @Lazy
    ExamStudentService examStudentService;

    @Resource
    JkExamStageService jkExamStageService;

    @Override
    public ExamStudent findStudent(String examCode, String ticketNo, String courseCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("ticketNo").is(ticketNo));
//        query.addCriteria(Criteria.where("courseCode").is(courseCode));
//        return mongoTemplate.findOne(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getTicketNo, ticketNo)
                .eq(ExamStudent::getCourseCode, courseCode);
        return examStudentService.getOne(examStudentQueryWrapper);
    }

    @Override
    public List<ExamStudent> findByExamCodeAndTicketNo(String examCode, String ticketNo) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("ticketNo").is(ticketNo));
//        return mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getTicketNo, ticketNo);
        return examStudentService.list(examStudentQueryWrapper);
    }

    /**
     * 根据身份证号查找考生信息
     *
     * @param idcard
     * @return
     */
    @Override
    public List<ExamStudent> findByIdcard(String idcard) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("idcard").is(idcard));
//        return mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getIdcard, idcard);
        return examStudentService.list(examStudentQueryWrapper);
    }

    //监考端考务数据获取
    @Override
    public List<ExamStudent> findJkDeviceKwStudents(String examCode, String placeCode, String roomCode) {
        if (StringUtils.isEmpty(examCode) || StringUtils.isEmpty(placeCode) || StringUtils.isEmpty(roomCode)) {
            throw new ApiException(ApiException.SYSTEM_EXCEPTION.getCode(), "考试识别码、考点代码、考场代码都不能够为空");
        }
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("placeCode").is(placeCode));
//        query.addCriteria(Criteria.where("roomCode").is(roomCode));
//        return mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getPlaceCode, placeCode)
                .eq(ExamStudent::getRoomCode, roomCode);
        return examStudentService.list(examStudentQueryWrapper);
    }

    @Override
    public List<ExamStage> findJkDeviceBatchStages(String examCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        return mongoTemplate.find(query, ExamStage.class);
        QueryWrapper<ExamStage> examStageQueryWrapper = new QueryWrapper<>();
        examStageQueryWrapper.lambda().eq(ExamStage::getExamCode, examCode);
        return jkExamStageService.list(examStageQueryWrapper);
    }

    @Override
    public ExamStudent saveService(ExamStudent examStudent) {
//        return examStudentDao.save(examStudent);
        examStudentService.saveOrUpdate(examStudent);
        return examStudent;
    }

    @Transactional
    @Override
    public boolean updateExamStudentExamStatus(String examCode, String courseCode, String ticketNumber, ExamFinishStatusEnum examStatus) {
        ExamStudent examStudent = this.findStudent(examCode, ticketNumber, courseCode);
        if (Objects.isNull(examStudent)) {
            throw new JkServerException("未找到考生数据");
        }
        examStudent.setExamFinishStatus(examStatus);
        return Objects.nonNull(this.saveOrUpdate(examStudent));
    }

    @Override
    public List<ExamStudent> findByExamCode(String examCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        return mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode);
        return examStudentService.list(examStudentQueryWrapper);
    }

    @Override
    public ExamStudent findSolfegeStudent(String examCode, String ticketNo) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("ticketNo").is(ticketNo));
//        List<ExamStudent> examStudents = mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getTicketNo, ticketNo);
        List<ExamStudent> examStudents = examStudentService.list(examStudentQueryWrapper);
        return examStudents.size() > 0 ? examStudents.get(0) : null;
    }

    //计算视唱考生某一个考场的考生人数
    @Override
    public Integer countSolfegeStudent(String examCode, String placeCode, String roomCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("placeCode").is(placeCode));
//        query.addCriteria(Criteria.where("roomCode").is(roomCode));
//        long count = mongoTemplate.count(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getPlaceCode, placeCode)
                .eq(ExamStudent::getRoomCode, roomCode);
        long count = examStudentService.count(examStudentQueryWrapper);
        return (int) count;
    }

    @Override
    public ExamStudent findById(String id) {
        return examStudentDao.findById(Long.parseLong(id));
    }

    @Override
    public List<ExamStudent> findExamStudentByQuery(String examCode, String idCard, String ticketNo, String batchId, String stageId, String placeCode, String roomCode, String courseCode, String courseName) {
//        Query query = new Query();
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        // 考试识别码
        if (StringUtils.isNotEmpty(examCode)) {
//            query.addCriteria(Criteria.where("examCode").is(examCode));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode);
        }
        // 身份证号
        if (StringUtils.isNotEmpty(idCard)) {
//            query.addCriteria(Criteria.where("idcard").is(idCard));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getIdcard, idCard);
        }
        // 准考证号
        if (StringUtils.isNotEmpty(ticketNo)) {
//            query.addCriteria(Criteria.where("ticketNo").is(ticketNo));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getTicketNo, ticketNo);
        }
        // 批次条件
        if (StringUtils.isNotEmpty(batchId)) {
//            query.addCriteria(Criteria.where("batchId").is(batchId));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getBatchId, batchId);
        }
        // 场次条件
        if (StringUtils.isNotEmpty(stageId)) {
//            query.addCriteria(Criteria.where("stageId").is(stageId));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getStageId, stageId);
        }
        // 考点条件
        if (StringUtils.isNotEmpty(placeCode)) {
//            query.addCriteria(Criteria.where("placeCode").is(placeCode));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getPlaceCode, placeCode);
        }
        // 考场条件
        if (StringUtils.isNotEmpty(roomCode)) {
//            query.addCriteria(Criteria.where("roomCode").is(roomCode));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getRoomCode, roomCode);
        }
        // 科目编号
        if (StringUtils.isNotEmpty(courseCode)) {
//            query.addCriteria(Criteria.where("courseCode").is(courseCode));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getCourseCode, courseCode);
        }
        // 科目名称
        if (StringUtils.isNotEmpty(courseName)) {
//            query.addCriteria(Criteria.where("courseName").is(courseName));
            examStudentQueryWrapper.lambda().eq(ExamStudent::getCourseName, courseName);
        }
//        Sort.Order order1 = new Sort.Order(Sort.Direction.DESC, "createTime");
//        Sort sort = new Sort(order1);
//        query.with(sort);
        examStudentQueryWrapper.lambda().orderByDesc(ExamStudent::getCreateTime);
//        return mongoTemplate.find(query, ExamStudent.class);
        return examStudentService.list(examStudentQueryWrapper);
    }
}
