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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.distributed.print.business.bean.result.analyze.GradeInitResult;
import com.qmth.distributed.print.business.mapper.GradeInitializeMapper;
import com.qmth.distributed.print.business.service.GradeInitializeService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.GradeAnalyzePaperStatusEnum;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 教研分析对接-数据初始化服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-05-26
 */
@Service
public class GradeInitializeServiceImpl implements GradeInitializeService {
    @Resource
    GradeInitializeMapper gradeInitializeMapper;


    @Override
    public IPage<GradeInitResult> findNeedAnalyzedPaper(Long semesterId, Long examId, String courseCode, SysUser requestUser, int pageNumber, int pageSize) {
        Long schoolId = requestUser.getSchoolId();
        Long userId = requestUser.getId();
        List<String> finished = new ArrayList<>();
        finished.add(GradeAnalyzePaperStatusEnum.READY_TO_CALCULATE.name());
        finished.add(GradeAnalyzePaperStatusEnum.CALCULATING.name());
        finished.add(GradeAnalyzePaperStatusEnum.FINISH_CALCULATE.name());
        IPage<GradeInitResult> page = gradeInitializeMapper.findNeedAnalyzedPaper(new Page<>(pageNumber, pageSize), semesterId, examId, courseCode, userId, schoolId);
        for (GradeInitResult record : page.getRecords()) {
            String status = record.getStatus();
            if (SystemConstant.strNotNull(status)) {
                if (Arrays.stream(status.split(",")).anyMatch(finished::contains)) {
                    status = "已提交";
                } else {
                    status = "未提交";
                }
                record.setStatus(status);
            }
        }
        return page;
    }

    @Override
    public long countByPropositionTeacherId() {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        Long schoolId = requestUser.getSchoolId();
        Long userId = requestUser.getId();
        List<GradeInitResult> findNeedAnalyzedPaper = gradeInitializeMapper.listNeedAnalyzedPaperToWork(schoolId, userId);
        return findNeedAnalyzedPaper.isEmpty() ? 0 : findNeedAnalyzedPaper.size();
    }

    @Override
    public IPage<GradeInitResult> listNeedAnalyzedPaperToWork(SysUser requestUser, Integer pageNumber, Integer pageSize) {
        Long schoolId = requestUser.getSchoolId();
        Long userId = requestUser.getId();
        return gradeInitializeMapper.listNeedAnalyzedPaperToWork(new Page<>(pageNumber, pageSize),schoolId, userId);
    }
}
