package com.ruoyi.voluntary.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.voluntary.domain.BusiCards;
import com.ruoyi.voluntary.domain.BusiGrade;
import com.ruoyi.voluntary.dto.ClassesDto;
import com.ruoyi.voluntary.dto.GradeClassDto;
import com.ruoyi.voluntary.service.IBusiCardsService;
import com.ruoyi.voluntary.service.IBusiGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.voluntary.mapper.BusiClassesMapper;
import com.ruoyi.voluntary.domain.BusiClasses;
import com.ruoyi.voluntary.service.IBusiClassesService;

/**
 * 班级Service业务层处理
 * 
 * @author voluntary
 * @date 2022-11-20
 */
@Service
public class BusiClassesServiceImpl implements IBusiClassesService 
{
    @Autowired
    private IBusiGradeService gradeService;
    @Autowired
    private BusiClassesMapper busiClassesMapper;
    @Autowired
    private IBusiCardsService cardsService;

    /**
     * 查询班级
     * 
     * @param classId 班级主键
     * @return 班级
     */
    @Override
    public BusiClasses selectBusiClassesByClassId(Long classId)
    {
        return busiClassesMapper.selectBusiClassesByClassId(classId);
    }

    /**
     * 查询班级列表
     * 
     * @param busiClasses 班级
     * @return 班级
     */
    @Override
    public List<BusiClasses> selectBusiClassesList(BusiClasses busiClasses)
    {
        return busiClassesMapper.selectBusiClassesList(busiClasses);
    }

    /**
     * 新增班级
     * 
     * @param busiClasses 班级
     * @return 结果
     */
    @Override
    public int insertBusiClasses(BusiClasses busiClasses)
    {
        busiClasses.setCreateTime(DateUtils.getNowDate());
        return busiClassesMapper.insertBusiClasses(busiClasses);
    }

    /**
     * 修改班级
     * 
     * @param busiClasses 班级
     * @return 结果
     */
    @Override
    public int updateBusiClasses(BusiClasses busiClasses)
    {
        busiClasses.setUpdateTime(DateUtils.getNowDate());
        return busiClassesMapper.updateBusiClasses(busiClasses);
    }

    /**
     * 批量删除班级
     * 
     * @param classIds 需要删除的班级主键
     * @return 结果
     */
    @Override
    public int deleteBusiClassesByClassIds(Long[] classIds)
    {
        return busiClassesMapper.deleteBusiClassesByClassIds(classIds);
    }

    /**
     * 删除班级信息
     * 
     * @param classId 班级主键
     * @return 结果
     */
    @Override
    public int deleteBusiClassesByClassId(Long classId)
    {
        return busiClassesMapper.deleteBusiClassesByClassId(classId);
    }

    @Override
    public List<GradeClassDto> getGradeClasses(Long schoolId) {
        if(StringUtils.isNull(schoolId)){
            //卡用户username默认为其卡号
            BusiCards busiCards = cardsService.selectBusiCardsByCardNo(SecurityUtils.getLoginUser().getUser().getUserName());
            schoolId = busiCards.getSchoolId();
        }
        if (StringUtils.isNull(schoolId)) {
            throw new BaseException("学校ID不能为空");
        }
        BusiClasses busiClasses = new BusiClasses();
        busiClasses.setSchoolId(schoolId);
        List<BusiClasses> busiClassesList = this.selectBusiClassesList(busiClasses);

        List<GradeClassDto> gradeClassDtoList = new ArrayList<>();
        Map<Long, GradeClassDto> gradeClassDtoMap = Maps.newHashMap();
        busiClassesList.forEach(ll->{
            // t1.id, t1.schoolId, t1.year, t1.name, t1.createTime, t1.status,t2.name schoolName, t1.greadId,t1.headteacherCode,t1.type,t1.subjectId, t3.gradeName
            GradeClassDto dto = gradeClassDtoMap.get(ll.getGradeId());
            if (null == dto) {
                dto = new GradeClassDto();
                BusiGrade grade = gradeService.selectBusiGradeById(ll.getGradeId());
                dto.setGradeId(grade.getId());
                dto.setGradeName(grade.getName());
                dto.setYear(ll.getYear());
                gradeClassDtoMap.put(ll.getGradeId(), dto);
                gradeClassDtoList.add(dto);
                dto.setClassList(Lists.newArrayList());
            }
            ClassesDto classesDto = new ClassesDto();
            classesDto.setClassId(ll.getClassId());
            classesDto.setClassName(ll.getName().trim());
            classesDto.setGradeId(ll.getGradeId());
            dto.getClassList().add(classesDto);
        });

        for (GradeClassDto dto : gradeClassDtoMap.values()) {
            Boolean classIsAllNumber = true;
            for (ClassesDto clazz : dto.getClassList()) {
                if (classIsAllNumber) {
                    classIsAllNumber = NumberUtils.isNumeric(clazz.getClassName());
                }
            }
            if (classIsAllNumber) {//班级全部为数字则升序排列
                Collections.sort(dto.getClassList(), new Comparator<ClassesDto>() {
                    @Override
                    public int compare(ClassesDto u1, ClassesDto u2) {
                        int diff = Integer.parseInt(u1.getClassName()) - Integer.parseInt(u2.getClassName());
                        if (diff > 0) {
                            return 1;
                        } else if (diff < 0) {
                            return -1;
                        }
                        return 0; //相等为0
                    }
                }); // 按班级排序
            }
        }
        gradeClassDtoList.stream().sorted(Comparator.comparing(GradeClassDto::getGradeName)).collect(Collectors.toList());
        return gradeClassDtoList;
    }
}
