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


import static com.yida.data.common.core.entity.constant.CachePrefixConstant.STAFF_DATA;

import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.classCall.EduClassCall;
import com.yida.data.common.core.entity.classCall.EduClassCallStudentRecord;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.user.EduStaff;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.EduUserDept;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.school.classCall.mapper.EduClassCallMapper;
import com.yida.data.school.classCall.service.EduClassCallService;
import com.yida.data.school.classCall.service.EduClassCallStudentRecordService;
import com.yida.data.school.dto.classCall.ClassCallAddDTO;
import com.yida.data.school.dto.classCall.ClassCallPageDTO;
import com.yida.data.school.dto.classCall.StudentDTO;
import com.yida.data.system.feign.RemoteDeptService;
import com.yida.data.user.feign.RemoteStaffService;
import com.yida.data.user.feign.RemoteStudentService;
import com.yida.data.user.feign.RemoteTeacherService;
import com.yida.data.user.feign.RemoteUserDeptService;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课堂点名 Service实现
 */
@Service
@RequiredArgsConstructor
@Transactional
public class EduClassCallServiceImpl extends ServiceImpl
    <EduClassCallMapper, EduClassCall> implements EduClassCallService {

    private final RemoteUserDeptService remoteUserDeptService;

    private final RemoteStaffService remoteStaffService;

    private final RemoteStudentService remoteStudentService;

    private final RedisService redisService;

    private final CommonService commonService;

    private final EduClassCallStudentRecordService eduClassCallStudentRecordService;

    @Override
    public IPage<EduClassCall> listClassCallPage(ClassCallPageDTO dto) {
        return baseMapper.listClassCallPage(dto.toPage(), dto);
    }

    @Override
    public void save(ClassCallAddDTO dto) {
        EduClassCall classCall = new EduClassCall();
        BeanUtils.copyProperties(dto, classCall);
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        classCall.setSchoolId(currentUser.getDeptId());
        EduUserDept grade = remoteUserDeptService.getParentByDeptId(dto.getClassId()).getData();
        EduUserDept clazz = commonService.getUserDept(dto.getClassId());
        classCall.setClassName(String.join("/", grade.getDeptName(), clazz.getDeptName()));
        classCall
            .setAbnormalNumber(dto.getStudentList().stream().filter(s -> s.getStatus() == 0).collect(Collectors.toList()).size());
        classCall
            .setStudentNum(dto.getStudentList().size());

        EduStaff staff = (EduStaff) redisService.hget(STAFF_DATA, dto.getTeacherId().toString());
        if (ObjectUtil.isNull(staff)) {
            staff = remoteStaffService.getStaff(dto.getTeacherId()).getData();
        }
        classCall.setTeacherName(staff.getName());
        save(classCall);

        //保存记录
        eduClassCallStudentRecordService.saveRecord(classCall.getId(), dto.getStudentList());

        //TODO 推送考勤记录
    }

    @Override
    public EduClassCall getDetailById(Long id) {
        EduClassCall eduClassCall = getById(id);
        List<EduClassCallStudentRecord> list = eduClassCallStudentRecordService
            .list(Wrappers.lambdaQuery(new EduClassCallStudentRecord())
                .eq(EduClassCallStudentRecord::getCallId, id));
        eduClassCall.setStudentRecordList(list);
        return eduClassCall;
    }
}
