package com.qmth.teachcloud.mark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.qmth.teachcloud.mark.bean.archivescore.QuestionVo;
import com.qmth.teachcloud.mark.entity.MarkSubjectiveScore;
import com.qmth.teachcloud.mark.mapper.MarkSubjectiveScoreMapper;
import com.qmth.teachcloud.mark.service.MarkSubjectiveScoreService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 主观题得分明细表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2023-09-22
 */
@Service
public class MarkSubjectiveScoreServiceImpl extends MppServiceImpl<MarkSubjectiveScoreMapper, MarkSubjectiveScore> implements MarkSubjectiveScoreService {

    @Override
    public List<MarkSubjectiveScore> listByStudentIdAndGroupNumber(Long studentId, Integer groupNumber) {
        QueryWrapper<MarkSubjectiveScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MarkSubjectiveScore::getStudentId, studentId)
        .orderByAsc(MarkSubjectiveScore::getMainNumber)
        .orderByAsc(MarkSubjectiveScore::getSubNumber);
        if (groupNumber != null) {
            queryWrapper.lambda().eq(MarkSubjectiveScore::getGroupNumber, groupNumber);
        }

        List<MarkSubjectiveScore> list = this.list(queryWrapper);
//        sort(list);
        return list;
    }

    @Override
    public Set<Integer> listMainNumberByStudentIdAndUncalculate(Long studentId, boolean uncalculate) {
        Set<Integer> mainNumbers = new HashSet<Integer>();
        List<MarkSubjectiveScore> list = this.listByStudentIdAndUncalculate(studentId, uncalculate);
        for (MarkSubjectiveScore subjectiveScore : list) {
            mainNumbers.add(subjectiveScore.getMainNumber());
        }
        return mainNumbers;
    }

    @Override
    public List<MarkSubjectiveScore> listByStudentIdAndUncalculate(Long studentId, boolean uncalculate) {
        QueryWrapper<MarkSubjectiveScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MarkSubjectiveScore::getStudentId, studentId)
                .eq(MarkSubjectiveScore::getUncalculate, uncalculate)
                .orderByAsc(MarkSubjectiveScore::getMainNumber)
                .orderByAsc(MarkSubjectiveScore::getSubNumber);
        return this.list(queryWrapper);
    }

    @Override
    public void deleteByStudentId(Long studentId) {
        UpdateWrapper<MarkSubjectiveScore> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MarkSubjectiveScore::getStudentId, studentId);
        this.remove(updateWrapper);
    }

    @Override
    public void deleteByExamIdAndPaperNumberAndGroupNumber(Long examId, String paperNumber, Integer groupNumber) {
        UpdateWrapper<MarkSubjectiveScore> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MarkSubjectiveScore::getExamId, examId)
                .eq(MarkSubjectiveScore::getPaperNumber, paperNumber)
                .eq(MarkSubjectiveScore::getGroupNumber, groupNumber);
        this.remove(updateWrapper);
    }

//    private void sort(List<MarkSubjectiveScore> list) {
//        list.sort((o1, o2) -> {
//            int i = o1.getMainNumber() - o2.getMainNumber();
//            if (i == 0) {
//                if (o1.getSubNumber().contains("-")) {
//                    String[] o1s = o1.getSubNumber().split("-");
//                    String[] o2s = o2.getSubNumber().split("-");
//                    int j = Integer.parseUnsignedInt(o1s[0]) - Integer.parseUnsignedInt(o2s[0]);
//                    if (j == 0) {
//                        return Integer.parseUnsignedInt(o1s[1]) - Integer.parseUnsignedInt(o2s[1]);
//                    } else {
//                        return j;
//                    }
//                } else {
//                    return Integer.parseUnsignedInt(o1.getSubNumber()) - Integer.parseUnsignedInt(o2.getSubNumber());
//                }
//            }
//            return i;
//        });
//    }

	@Override
	public List<QuestionVo> getSubjectiveVo(List<Long> studentIds) {
        if(CollectionUtils.isEmpty(studentIds)){
            return null;
        }
		return this.baseMapper.getSubjectiveVo(studentIds);
	}

	@Override
	public List<MarkSubjectiveScore> listByStudentId(Long studentId) {
		 QueryWrapper<MarkSubjectiveScore> queryWrapper = new QueryWrapper<>();
	        queryWrapper.lambda().eq(MarkSubjectiveScore::getStudentId, studentId).orderByAsc(MarkSubjectiveScore::getMainNumber)
            .orderByAsc(MarkSubjectiveScore::getSubNumber);;
	        List<MarkSubjectiveScore> list = this.list(queryWrapper);
	        return list;
	}

}
