package com.yida.data.user.config;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.collection.CollUtil;
import com.yida.data.common.core.entity.school.EduDormitoryRoom;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.EduUserDept;
import com.yida.data.common.excel.DefaultExportExcelClient;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.dto.index.ListDormRoomDTO;
import com.yida.data.school.feign.facility.RemoteDormitoryService;
import com.yida.data.user.dto.StudentDormExportDTO;
import com.yida.data.user.excel.SelectStudentDormExportData;
import com.yida.data.user.service.EduStudentService;
import org.springframework.beans.BeanUtils;

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

/**
 * 学生住宿信息导出处理类
 *
 * @author ZYJ
 * @date 2023/12/13 15:00
 */
public class ExportStudentDormClient
        extends DefaultExportExcelClient<SelectStudentDormExportData, StudentDormExportDTO, EduStudent, EduStudentService> {

    private final RemoteDormitoryService remoteDormitoryService;

    private Map<Long, List<EduDormitoryRoom>> dormRoomGroup = new HashMap<>();

    public ExportStudentDormClient(EduStudentService eduStudentService, RedisService redisService, CommonService commonService,
                                   String exportCacheKey, String uploadUrl, RemoteDormitoryService remoteDormitoryService) {
        super(eduStudentService, redisService, commonService, exportCacheKey, uploadUrl);
        this.remoteDormitoryService = remoteDormitoryService;
    }

    @Override
    public List<EduStudent> selectExportData(SelectStudentDormExportData data) {
        List<Long> dormIds = new ArrayList<>();
        // 查询匹配的宿舍
        if (data.getDormRoomId() == null) {
            List<EduDormitoryRoom> dormRooms = remoteDormitoryService.listDormRoomNoAuth(ListDormRoomDTO.builder()
                    .schoolId(data.getSchoolId())
                    .floor(data.getFloor())
                    .dormIds(data.getDormId() != null ? Collections.singletonList(data.getDormId()) : null).build()).getData();
            if (CollUtil.isNotEmpty(dormRooms)) {
                dormRoomGroup = dormRooms.stream().collect(Collectors.groupingBy(EduDormitoryRoom::getDormId));
                dormIds = dormRooms.stream().map(EduDormitoryRoom::getId).collect(Collectors.toList());
            }
        } else {
            dormIds = Collections.singletonList(data.getDormRoomId());
        }
        // 宿舍为空 不查询数据
        if (CollUtil.isEmpty(dormIds)) {
            dormIds = new ArrayList<>();
            dormIds.add(-1L);
        }
        // 查询学生信息
        return this.baseService.listStudentDorm(dormIds, data);
    }

    @Override
    public StudentDormExportDTO handleSingleData(EduStudent o) {
        // 年级班级信息
        EduUserDept grade = this.commonService.getUserDept(o.getGradeId());
        EduUserDept clazz = this.commonService.getUserDept(o.getClassId());
        StringBuilder gradeClass = new StringBuilder();
        if (grade != null) {
            gradeClass.append(grade.getDeptName());
        }
        if (clazz != null) {
            gradeClass.append(clazz.getDeptName());
        }
        o.setClassName(gradeClass.toString());
        // 入住寝室信息
        EduDormitoryRoom dormRoom;
        if (CollUtil.isNotEmpty(dormRoomGroup) && dormRoomGroup.containsKey(o.getDormRoomId())) {
            dormRoom = dormRoomGroup.get(o.getDormRoomId()).get(0);
        } else {
            dormRoom = remoteDormitoryService.getDormRoomNoPermission(o.getDormRoomId()).getData();
            if (dormRoom != null) {
                dormRoomGroup.put(dormRoom.getId(), Collections.singletonList(dormRoom));
            }
        }
        // 查询到了寝室信息
        if (Objects.nonNull(dormRoom)) {
            String dormRoomName = dormRoom.getDormName() +
                    dormRoom.getFloor() + "层" +
                    dormRoom.getName();
            o.setDormRoomName(dormRoomName);
        }
        StudentDormExportDTO exportDTO = new StudentDormExportDTO();
        BeanUtils.copyProperties(o, exportDTO);
        exportDTO.setDeptName(o.getClassName());
        return exportDTO;
    }
}
