package com.qmth.teachcloud.report.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicSemester;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.report.business.bean.params.TBExamParam;
import com.qmth.teachcloud.report.business.entity.TBCloudExam;
import com.qmth.teachcloud.report.business.entity.TBExam;
import com.qmth.teachcloud.report.business.mapper.TBExamMapper;
import com.qmth.teachcloud.report.business.service.TBCloudExamService;
import com.qmth.teachcloud.report.business.service.TBExamService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 教研分析基础考试信息表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2021-06-01
 */
@Service
public class TBExamServiceImpl extends ServiceImpl<TBExamMapper, TBExam> implements TBExamService {
    @Resource
    private TBCloudExamService tbCloudExamService;
    @Resource
    private BasicSemesterService basicSemesterService;

    @Override
    public TBExam findByCloudExamId(Long cloudExamId) {
        TBCloudExam tbCloudExam = tbCloudExamService.getById(cloudExamId);
        Long examId = tbCloudExam.getExamId();
        TBExam tbExam = this.getById(examId);
        if (Objects.isNull(tbExam)) {
            throw ExceptionResultEnum.ERROR.exception("未找到教研分析基础考试信息");
        }
        return tbExam;
    }

    @Override
    public Long findSchoolIdByExamId(Long examId) {
        TBExam tbExam = this.getById(examId);
        if (Objects.isNull(tbExam)) {
            throw ExceptionResultEnum.ERROR.exception("未找到考试id为 ： " + examId + " 的考试");
        }
        return tbExam.getSchoolId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long editTBExam(TBExamParam tbExamParam) {
        Long id = tbExamParam.getId();
        Long schoolId = tbExamParam.getSchoolId();
        if (!SystemConstant.longNotNull(schoolId)) {
            throw ExceptionResultEnum.ERROR.exception("缺少参数学校id");
        }

        String examName = tbExamParam.getExamName();
        Long examTime = tbExamParam.getExamTime();
        Long semesterId = tbExamParam.getSemesterId();
        if (basicSemesterService.count(new QueryWrapper<BasicSemester>().lambda().eq(BasicSemester::getId, semesterId)) == 0) {
            throw ExceptionResultEnum.ERROR.exception("系统中不存在id为 : " + semesterId + "的学期");
        }

        TBExam examCheck = this.getOne(new QueryWrapper<TBExam>().lambda().eq(TBExam::getSchoolId, schoolId).eq(TBExam::getExamName, examName));
        if (Objects.nonNull(examCheck) && !Objects.equals(examCheck.getId(), id)) {
            throw ExceptionResultEnum.ERROR.exception("系统中考试名称需要唯一");
        }

        TBExam tbExam = new TBExam();
        tbExam.setSchoolId(schoolId);
        tbExam.setExamName(examName);
        tbExam.setExamCode(DateFormatUtils.format(examTime, "yyyyMMdd"));
        tbExam.setSemesterId(semesterId);
        tbExam.setExamTime(examTime);
        if (SystemConstant.longNotNull(id)) {
            // 编辑
            if (Objects.isNull(this.getById(id))) {
                throw ExceptionResultEnum.ERROR.exception("考试id不存在");
            }
            tbExam.setId(id);
            this.updateById(tbExam);
        } else {
            // 新增
            id = SystemConstant.getDbUuid();
            tbExam.setId(id);
            this.save(tbExam);
        }
        return id;
    }
}
