package com.xjrsoft.module.student.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.enums.GenderDictionaryEnum;
import com.xjrsoft.common.enums.StudyStatusEnum;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.room.mapper.RoomBedMapper;
import com.xjrsoft.module.room.vo.RoomBedInfoVo;
import com.xjrsoft.module.student.dto.BaseStudentInfoDetailDto;
import com.xjrsoft.module.student.dto.BaseStudentInfoPageDto;
import com.xjrsoft.module.student.dto.PbVXsxxsfytbDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentInfoDto;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.mapper.BaseStudentSchoolRollMapper;
import com.xjrsoft.module.student.mapper.PbSemesterConfigMapper;
import com.xjrsoft.module.student.mapper.PbVXsxxsfytbMapper;
import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
import com.xjrsoft.module.student.vo.BaseStudentInfoCategoryVo;
import com.xjrsoft.module.student.vo.BaseStudentInfoDetailVo;
import com.xjrsoft.module.student.vo.BaseStudentInfoPageDataVo;
import com.xjrsoft.module.student.vo.BaseStudentInfoPageVo;
import com.xjrsoft.module.student.vo.MobileClassStatisticsVo;
import com.xjrsoft.module.student.vo.PbVXsxxsfytbVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
* @title: 助学金申请
* @Author dzx
* @Date: 2023-11-24
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BaseStudentSchoolRollServiceImpl extends MPJBaseServiceImpl<BaseStudentSchoolRollMapper, BaseStudentSchoolRoll> implements IBaseStudentSchoolRollService {

    private final BaseStudentSchoolRollMapper baseStudentSchoolRollMapper;
    private final RoomBedMapper roomBedMapper;
    private final BaseClassMapper baseClassMapper;
    private final PbSemesterConfigMapper pbSemesterConfigMapper;
    private final PbVXsxxsfytbMapper pbVXsxxsfytbMapper;


    @Override
    public Page<BaseStudentInfoPageVo> getMobilePage(Page<BaseStudentInfoPageVo> page, BaseStudentInfoPageDto dto) {

        List<String> roleList = StpUtil.getRoleList();

        long teacherId = StpUtil.getLoginIdAsLong();
        List<BaseClass> classList = baseClassMapper.selectList(
            new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getTeacherId, teacherId)
            .eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
        );
        if(roleList.size() == 1 && roleList.contains("CLASSTE")){
            if(classList != null && !classList.isEmpty()){
                dto.setTeacherId(teacherId);
            }
        }else{
            if(classList != null && !classList.isEmpty() &&  dto.getClassId() == null){
                dto.setClassId(classList.get(0).getId());
            }
        }

        String lastBeltcode = pbSemesterConfigMapper.getLastBeltcode();
        dto.setBeltcode(lastBeltcode);
        Page<BaseStudentInfoPageVo> mobilePage = baseStudentSchoolRollMapper.getMobilePage(page, dto);
        List<String> studentCodes = new ArrayList<>();
        for (BaseStudentInfoPageVo record : mobilePage.getRecords()) {
            studentCodes.add(record.getStudentId());
        }
        PbVXsxxsfytbDto dto2 = new PbVXsxxsfytbDto();
        dto2.setStudentCodes(studentCodes);
        dto2.setBeltcode(lastBeltcode);
        List<PbVXsxxsfytbVo> list = pbVXsxxsfytbMapper.getList(dto2);
        Map<String, String> jfztMaps = new HashMap<>();
        for (PbVXsxxsfytbVo pbVXsxxsfytb : list) {
            jfztMaps.put(pbVXsxxsfytb.getStudentcode() + pbVXsxxsfytb.getFeeitemcode(), pbVXsxxsfytb.getJfzt().replaceAll("未缴费", "欠费"));
        }
        for (BaseStudentInfoPageVo record : mobilePage.getRecords()) {
            record.setSsf(jfztMaps.get(record.getStudentId() + "103042766003001") == null?"欠费":jfztMaps.get(record.getStudentId() + "103042766003001"));
            record.setJxf(jfztMaps.get(record.getStudentId() + "999010604") == null?"欠费":jfztMaps.get(record.getStudentId() + "999010604"));
            record.setJcf(jfztMaps.get(record.getStudentId() + "999010603") == null?"欠费":jfztMaps.get(record.getStudentId() + "999010603"));
        }
        return mobilePage;
    }

    @Override
    public Boolean updateInfo(UpdateBaseStudentInfoDto dto) {
        for (Long id : dto.getUserIds()) {
            baseStudentSchoolRollMapper.updateInfoByUserId(dto, id);
        }
        return true;
    }

    @Override
    public BaseStudentInfoDetailVo getInfoById(BaseStudentInfoDetailDto dto) {
        String lastBeltcode = pbSemesterConfigMapper.getLastBeltcode();
        dto.setBeltcode(lastBeltcode);
        BaseStudentInfoDetailVo info = baseStudentSchoolRollMapper.getInfoById(dto);
        RoomBedInfoVo bedInfoByUserId = roomBedMapper.getBedInfoByUserId(info.getId());
        if(bedInfoByUserId != null){
            info.setBedInfo(bedInfoByUserId.getBuildName() + " " + bedInfoByUserId.getRoomName() + " " + bedInfoByUserId.getBedNumber());
        }
        return info;
    }

    @Override
    public BaseStudentInfoPageDataVo getMobilePageStatistics(BaseStudentInfoPageDto dto) {
        List<String> roleList = StpUtil.getRoleList();

        long teacherId = StpUtil.getLoginIdAsLong();
        List<BaseClass> classList = baseClassMapper.selectList(
                new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getTeacherId, teacherId)
        );
        if(roleList.size() == 1 && roleList.contains("CLASSTE")){
            if(classList != null && !classList.isEmpty()){
                dto.setTeacherId(teacherId);
            }
        }else{
            if(classList != null && !classList.isEmpty() &&  dto.getClassId() == null){
                dto.setClassId(classList.get(0).getId());
            }
        }

        List<BaseStudentInfoCategoryVo> genderCount = baseStudentSchoolRollMapper.getGenderCount(dto);
        int totalCount = 0;

        BaseStudentInfoPageDataVo result = new BaseStudentInfoPageDataVo();
        for (BaseStudentInfoCategoryVo categoryVo : genderCount) {
            totalCount = totalCount + categoryVo.getNumber();
            if ("男".equals(GenderDictionaryEnum.getValue(categoryVo.getCategory()))) {
                result.setMaleCount(categoryVo.getNumber());
            } else if ("女".equals(GenderDictionaryEnum.getValue(categoryVo.getCategory()))) {
                result.setFemaleCount(categoryVo.getNumber());
            }
        }
        result.setTotalCount(totalCount);
        List<BaseStudentInfoCategoryVo> studyStatusCount = baseStudentSchoolRollMapper.getStudyStatusCount(dto);
        int stayCount = 0;
        int notStayCount = 0;
        for (BaseStudentInfoCategoryVo categoryVo : studyStatusCount) {
            if (StudyStatusEnum.InResidence.getCode().equals(categoryVo.getCategory())) {
                stayCount = stayCount + categoryVo.getNumber();

                if ("男".equals(GenderDictionaryEnum.getValue(categoryVo.getGender()))) {
                    result.setMaleStayCount(categoryVo.getNumber());
                } else if ("女".equals(GenderDictionaryEnum.getValue(categoryVo.getGender()))) {
                    result.setFemaleStayCount(categoryVo.getNumber());
                }

            } else if (StudyStatusEnum.AttendDaySchool.getCode().equals(categoryVo.getCategory())) {
                notStayCount = notStayCount + categoryVo.getNumber();

                if ("男".equals(GenderDictionaryEnum.getValue(categoryVo.getGender()))) {
                    result.setMaleNotStayCount(categoryVo.getNumber());
                } else if ("女".equals(GenderDictionaryEnum.getValue(categoryVo.getGender()))) {
                    result.setFemaleNotStayCount(categoryVo.getNumber());
                }
            }
        }
        result.setStayCount(stayCount);
        result.setNotStayCount(notStayCount);

        return result;
    }

    @Override
    public MobileClassStatisticsVo getMobileClassStatistics(BaseStudentInfoPageDto dto) {
        //获取当前用户的班级
        LambdaQueryWrapper<BaseClass> baseClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        baseClassLambdaQueryWrapper
                .eq(BaseClass::getTeacherId, StpUtil.getLoginIdAsLong());
        List<BaseClass> baseClassList = baseClassMapper.selectList(baseClassLambdaQueryWrapper);
        //如果集合不为空，表示该登录用户是班主任
        if(baseClassList != null && !baseClassList.isEmpty()){
            for (BaseClass baseClass : baseClassList){

            }
        }else {

        }
        return null;
    }

    @Override
    public Boolean updateStudentClass(Long classId, Long userId) {
        return baseStudentSchoolRollMapper.updateStudentClass(classId, userId);
    }
}
