package com.yida.data.school.screen.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.screen.EduSchoolDashboardData;
import com.yida.data.common.core.entity.screen.EduSchoolDashboardDataBase;
import com.yida.data.common.core.entity.screen.EduSchoolDashboardDataTeacher;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.screen.SchoolDashboardBaseSaveDTO;
import com.yida.data.school.dto.screen.SchoolDashboardSaveDTO;
import com.yida.data.school.dto.screen.SchoolDashboardTeacherSaveDTO;
import com.yida.data.school.screen.mapper.EduSchoolDashboardDataMapper;
import com.yida.data.school.screen.service.EduSchoolDashboardDataBaseService;
import com.yida.data.school.screen.service.EduSchoolDashboardDataService;
import com.yida.data.school.screen.service.EduSchoolDashboardDataTeacherService;
import com.yida.data.school.vo.screen.SchoolDashboardInfoVO;
import com.yida.data.user.feign.RemoteStudentService;
import com.yida.data.user.feign.RemoteTeacherService;
import com.yida.data.user.feign.RemoteUserDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 校园大数据仪表盘数据中心Service实现
 *
 * @author ZYJ
 * @date 2023-04-19 16:42:57
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduSchoolDashboardDataServiceImpl extends ServiceImpl
        <EduSchoolDashboardDataMapper, EduSchoolDashboardData> implements EduSchoolDashboardDataService {

    private final EduSchoolDashboardDataBaseService eduSchoolDashboardDataBaseService;
    private final EduSchoolDashboardDataTeacherService eduSchoolDashboardDataTeacherService;

    private final RemoteStudentService remoteStudentService;

    @Override
    public SchoolDashboardInfoVO getSchoolDashboardInfo(Long deptId) {
        SchoolDashboardInfoVO vo = new SchoolDashboardInfoVO();
        // 查询校园大数据详情数据
        EduSchoolDashboardData dashboardData = getOne(Wrappers.lambdaQuery(new EduSchoolDashboardData())
                .eq(EduSchoolDashboardData::getSchoolId, deptId));
        if (Objects.nonNull(dashboardData)) {
            BeanUtils.copyProperties(dashboardData, vo);
            // 查询基础建设集合
            List<SchoolDashboardBaseSaveDTO> baseList = eduSchoolDashboardDataBaseService.list(Wrappers.lambdaQuery(new EduSchoolDashboardDataBase())
                    .eq(EduSchoolDashboardDataBase::getDataId, dashboardData.getId())).stream().map(base -> {
                SchoolDashboardBaseSaveDTO baseDTO = new SchoolDashboardBaseSaveDTO();
                BeanUtils.copyProperties(base, baseDTO);
                return baseDTO;
            }).collect(Collectors.toList());
            vo.setBaseList(baseList);
            // 查询师资力量集合
            List<SchoolDashboardTeacherSaveDTO> teacherList = eduSchoolDashboardDataTeacherService.list(Wrappers.lambdaQuery(new EduSchoolDashboardDataTeacher())
                    .eq(EduSchoolDashboardDataTeacher::getDataId, dashboardData.getId())).stream().map(teacher -> {
                SchoolDashboardTeacherSaveDTO teacherDTO = new SchoolDashboardTeacherSaveDTO();
                BeanUtils.copyProperties(teacher, teacherDTO);
                return teacherDTO;
            }).collect(Collectors.toList());
            vo.setTeacherList(teacherList);
        }
        return vo;
    }

    @Override
    public void saveSchoolDashboard(SchoolDashboardSaveDTO dto) {
        EduSchoolDashboardData dashboardData = new EduSchoolDashboardData();
        BeanUtils.copyProperties(dto, dashboardData);
        // 统计班级人数、学生人数、教师人数
        JSONObject jsonObject = remoteStudentService.countUserDashboardNum(FebsUtil.getTopDeptId()).getData();
        // 新增
        if (Objects.isNull(dto.getId())) {
            dashboardData.setSchoolId(FebsUtil.getTopDeptId());
        } else {
            // 编辑, 需要删除旧的关联数据
            eduSchoolDashboardDataBaseService.remove(Wrappers.lambdaQuery(new EduSchoolDashboardDataBase())
                    .eq(EduSchoolDashboardDataBase::getDataId, dto.getId()));
            eduSchoolDashboardDataTeacherService.remove(Wrappers.lambdaQuery(new EduSchoolDashboardDataTeacher())
                    .eq(EduSchoolDashboardDataTeacher::getDataId, dto.getId()));
        }
        dashboardData.setStudentNum(jsonObject.getInt("studentNum"));
        dashboardData.setClassNum(jsonObject.getInt("classNum"));
        dashboardData.setTeacherNum(jsonObject.getInt("teacherNum"));
        saveOrUpdate(dashboardData);
        // 保存基础设施
        if (CollUtil.isNotEmpty(dto.getBaseList())) {
            List<EduSchoolDashboardDataBase> baseList = new ArrayList<>();
            dto.getBaseList().forEach(baseDTO -> {
                EduSchoolDashboardDataBase dataBase = new EduSchoolDashboardDataBase();
                BeanUtils.copyProperties(baseDTO, dataBase);
                dataBase.setDataId(dashboardData.getId());
                baseList.add(dataBase);
            });
            eduSchoolDashboardDataBaseService.saveBatch(baseList);
        }
        // 保存师资力量
        if (CollUtil.isNotEmpty(dto.getTeacherList())) {
            List<EduSchoolDashboardDataTeacher> teacherList = new ArrayList<>();
            dto.getTeacherList().forEach(teacherDTO -> {
                EduSchoolDashboardDataTeacher dataTeacher = new EduSchoolDashboardDataTeacher();
                BeanUtils.copyProperties(teacherDTO, dataTeacher);
                dataTeacher.setDataId(dashboardData.getId());
                teacherList.add(dataTeacher);
            });
            eduSchoolDashboardDataTeacherService.saveBatch(teacherList);
        }
    }
}
