package com.admin.major.service.impl;

import com.admin.major.domain.ClassInfo;
import com.admin.major.domain.dto.ClassInfoDTO;
import com.admin.major.domain.dto.ClassResultDTO;
import com.admin.major.mapper.EnhanceClassInfoMapper;
import com.admin.major.mapper.EnhanceClassResultMapper;
import com.admin.major.service.EnhanceClassInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liangjunxing
 * @date 2022/5/18 0018
 */
@Service
public class EnhanceClassInfoServiceImpl implements EnhanceClassInfoService {
    @Autowired
    private EnhanceClassInfoMapper enhanceClassInfoMapper;
    @Autowired
    private EnhanceClassResultMapper enhanceClassResultMapper;
    @Override
    public List<ClassInfoDTO> viewClassInfo(ClassInfo classInfo) {
        return enhanceClassInfoMapper.viewClassInfo(classInfo);
    }

    @Override
    public ClassInfoDTO selectClassInfoById(long classId) {
        return enhanceClassInfoMapper.selectClassInfoById(classId);
    }

    @Override
    public List<ClassInfoDTO> viewSpareClassInfo() {
        List<ClassInfoDTO> classInfoDTOS = enhanceClassInfoMapper.viewClassInfo(new ClassInfo());
        List<ClassResultDTO> classResultDTOS = enhanceClassResultMapper.viewClassResult(new ClassResultDTO());
        //统计分班后的班级数量
        ArrayList<Long> classList = new ArrayList<>();
        for (int i = 0; i < classResultDTOS.size(); i++){
            classList.add(classResultDTOS.get(i).getClassId());
        }
        Map<Long,Integer> map = new HashMap<>();
        for(Long l:classList){
            Integer i = 1; //定义一个计数器，用来记录重复数据的个数
            if(map.get(l) != null){
                i=map.get(l)+1;
            }
            map.put(l,i);
        }
        //筛选空余班级
        for (int i = 0; i < classInfoDTOS.size(); i++){
            //已报满班级移除
            if (classInfoDTOS.get(i).getClassNumber() == map.get(classInfoDTOS.get(i).getClassId())){
                classInfoDTOS.remove(i);
            }
        }
        return classInfoDTOS;
    }
}
