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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.entity.TSyncStmmsExam;
import com.qmth.distributed.print.business.mapper.TSyncStmmsExamMapper;
import com.qmth.distributed.print.business.service.TSyncStmmsExamService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.sync.CloudMarkingTaskUtils;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TSyncStmmsExamServiceImpl extends ServiceImpl<TSyncStmmsExamMapper, TSyncStmmsExam> implements TSyncStmmsExamService {

    @Resource
    private SysOrgService sysOrgService;
    @Resource
    CloudMarkingTaskUtils cloudMarkingTaskUtils;

    @Override
    public List<TSyncStmmsExam> listExam(String orgCode) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        if (cloudMarkingTaskUtils.isCollegeMode(schoolId) && StringUtils.isBlank(orgCode)) {
            throw ExceptionResultEnum.ERROR.exception("当前学校配置为按学院推送，请传入学院代码。若无法解决，请联系管理员。");
        }
        QueryWrapper<TSyncStmmsExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TSyncStmmsExam::getSchoolId, schoolId)
                .orderByDesc(TSyncStmmsExam::getExamId);
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.lambda().eq(TSyncStmmsExam::getOrgCode, orgCode);
        } else {
            queryWrapper.lambda().isNull(TSyncStmmsExam::getOrgCode);
        }
        return this.list(queryWrapper);
    }

    @Transactional
    @Override
    public List<TSyncStmmsExam> syncExam(String orgCode) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        if (cloudMarkingTaskUtils.isCollegeMode(schoolId)) {
            if (StringUtils.isBlank(orgCode)) {
                throw ExceptionResultEnum.ERROR.exception("当前学校配置为按学院推送，请传入学院代码。若无法解决，请联系管理员。");
            } else {
                SysOrg sysOrg = sysOrgService.findByCode(orgCode);
                cloudMarkingTaskUtils.syncSchool(schoolId, orgCode, sysOrg.getName());
            }
        }
        UpdateWrapper<TSyncStmmsExam> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(TSyncStmmsExam::getSchoolId, schoolId);
        this.remove(updateWrapper);

        String orgCodeTrim = StringUtils.isBlank(orgCode) ? null : orgCode.trim();

        List<JSONObject> objectList = cloudMarkingTaskUtils.getExam(schoolId, orgCodeTrim, SystemConstant.PAGE_NUMBER_MIN, SystemConstant.PAGE_SIZE);
        if (objectList.isEmpty()) {
            throw ExceptionResultEnum.ERROR.exception("未查询到云阅卷考试");
        }
        List<TSyncStmmsExam> markingExams = objectList.stream().map(m -> {
            TSyncStmmsExam cloudMarkingExam = new TSyncStmmsExam();
            cloudMarkingExam.setId(SystemConstant.getDbUuid());
            cloudMarkingExam.setSchoolId(schoolId);
            cloudMarkingExam.setOrgCode(orgCodeTrim);
            cloudMarkingExam.setExamId(m.getInteger("id"));
            cloudMarkingExam.setExamName(m.getString("name"));
            return cloudMarkingExam;
        }).collect(Collectors.toList());
        if (!markingExams.isEmpty()) {
            this.saveBatch(markingExams);
        }
        return markingExams;
    }

    @Override
    public TSyncStmmsExam getBySchoolIdAndExamId(Long schoolId, String orgCode, Long thirdRelateId) {
        QueryWrapper<TSyncStmmsExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TSyncStmmsExam::getSchoolId, schoolId)
                .eq(TSyncStmmsExam::getExamId, thirdRelateId);
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.lambda().eq(TSyncStmmsExam::getOrgCode, orgCode);
        }
        return this.getOne(queryWrapper);
    }

    @Override
    public TSyncStmmsExam getBySchoolIdAndOrgCodeAndExamName(Long schoolId, String orgCode, String thirdRelateName) {
        QueryWrapper<TSyncStmmsExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TSyncStmmsExam::getSchoolId, schoolId)
                .eq(TSyncStmmsExam::getExamName, thirdRelateName);
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.lambda().eq(TSyncStmmsExam::getOrgCode, orgCode);
        }
        return this.getOne(queryWrapper);
    }
}
