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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.marking.GroupInfo;
import com.qmth.distributed.print.business.bean.marking.Marker;
import com.qmth.distributed.print.business.bean.marking.PictureConfig;
import com.qmth.distributed.print.business.bean.marking.Question;
import com.qmth.distributed.print.business.entity.ExamPaperGroup;
import com.qmth.distributed.print.business.entity.ExamPaperGroupMarker;
import com.qmth.distributed.print.business.mapper.ExamPaperGroupMapper;
import com.qmth.distributed.print.business.service.ExamPaperGroupMarkerService;
import com.qmth.distributed.print.business.service.ExamPaperGroupService;
import com.qmth.teachcloud.common.base.BaseEntity;
import com.qmth.teachcloud.common.enums.paperGroup.ScorePolicyEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 试卷分组服务类
 * @Author: CaoZixuan
 * @Date: 2022-04-13
 */
@Service
public class ExamPaperGroupServiceImpl extends ServiceImpl<ExamPaperGroupMapper, ExamPaperGroup> implements ExamPaperGroupService {
    @Resource
    private ExamPaperGroupMarkerService examPaperGroupMarkerService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveExamPaperGroupInfo(Long examPaperStructureId, List<GroupInfo> groupInfo) {
        // 1.删除原有分组
        this.deleteExamPaperGroupInfo(examPaperStructureId);
        // 2.新增新的试卷结构分组
        for (int i = 0; i < groupInfo.size(); i++) {
            GroupInfo group = groupInfo.get(i);
            // 题目信息
            List<Question> questionList = group.getQuestions();
            // 评卷员信息
            List<Marker> markerList = group.getMarkerList();
            // 是否开启双评
            Double doubleRate = group.getDoubleRate();
            // 仲裁阈值
            Double arbitrateThreshold = group.getArbitrateThreshold();
            // 图片配置
            List<PictureConfig> pictureConfigList = group.getPictureConfigList();

            ExamPaperGroup examPaperGroup = new ExamPaperGroup();
            examPaperGroup.setExamPaperStructureId(examPaperStructureId);
            examPaperGroup.setGroupNumber(i + 1);
            examPaperGroup.setDoubleRate(doubleRate);
            examPaperGroup.setArbitrateThreshold(arbitrateThreshold);
            examPaperGroup.setScorePolicy(ScorePolicyEnum.AVG);
            examPaperGroup.setTrialCount(0);
            examPaperGroup.setMarkMode(null);
            examPaperGroup.setQuestionInfo(JSON.toJSONString(questionList));
            examPaperGroup.setPictureConfig(JSON.toJSONString(pictureConfigList));
            this.save(examPaperGroup);
            // 新增后的分组id
            Long groupId = examPaperGroup.getId();
            examPaperGroupMarkerService.saveExamPaperGroupMarkerByGroupId(groupId,markerList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteExamPaperGroupInfo(Long examPaperStructureId) {
        // 要删除的分组id集合
        Set<Long> groupIdSet = this.list(new QueryWrapper<ExamPaperGroup>()
                .lambda()
                .eq(ExamPaperGroup::getExamPaperStructureId,examPaperStructureId))
                .stream()
                .map(BaseEntity::getId)
                .collect(Collectors.toSet());

        // 删除评卷员和分组的关系
        examPaperGroupMarkerService.deleteExamPaperGroupMarkerService(groupIdSet);

        // 删除分组信息
        this.removeByIds(groupIdSet);
    }

    @Override
    public List<ExamPaperGroup> listByExamPaperStructureId(Long id) {
        QueryWrapper<ExamPaperGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamPaperGroup::getExamPaperStructureId, id);
        return this.baseMapper.selectList(queryWrapper);
    }
}
