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


import cc.mrbird.febs.common.redis.service.RedisService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.EduStudent;
import com.yida.data.school.classCall.mapper.EduClassCallStudentRecordMapper;
import com.yida.data.school.classCall.service.EduClassCallStudentRecordService;
import com.yida.data.school.dto.classCall.StudentDTO;
import com.yida.data.user.feign.RemoteStudentService;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
@Transactional
public class EduClassCallStudentRecordServiceImpl extends ServiceImpl
    <EduClassCallStudentRecordMapper, EduClassCallStudentRecord> implements EduClassCallStudentRecordService {

    private final RedisService redisService;

    private final RemoteStudentService remoteStudentService;

    @Override
    public void saveRecord(Long callId, List<StudentDTO> list) {
        List<EduClassCallStudentRecord> eduClassCallStudentRecordList = new ArrayList<>();
        for (StudentDTO studentDTO : list) {
            EduStudent student = (EduStudent) redisService
                .hget(CachePrefixConstant.STUDENT_DATA, studentDTO.getStudentId().toString());
            if (student == null) {
                student = remoteStudentService.getStudentNoPermission(studentDTO.getStudentId()).getData();
            }
            EduClassCallStudentRecord eduClassCallStudentRecord = new EduClassCallStudentRecord();
            eduClassCallStudentRecord.setStudentId(student.getId());
            eduClassCallStudentRecord.setStudentName(student.getStuName());
            eduClassCallStudentRecord.setSchoolId(student.getSchoolId());
            eduClassCallStudentRecord.setCampusId(student.getCampusId());
            eduClassCallStudentRecord.setCampusName(student.getCampusName());
            eduClassCallStudentRecord.setSectionId(student.getSectionId());
            eduClassCallStudentRecord.setSectionName(student.getSectionName());
            eduClassCallStudentRecord.setGradeId(student.getGradeId());
            eduClassCallStudentRecord.setGradeName(student.getGradeName());
            eduClassCallStudentRecord.setClassId(student.getClassId());
            eduClassCallStudentRecord.setClassName(student.getClassName());
            eduClassCallStudentRecord
                .setGender(ObjectUtil.isNull(student.getStuSex()) ? null : Integer.parseInt(student.getStuSex()));
            eduClassCallStudentRecord.setAvatar(student.getAvatar());
            eduClassCallStudentRecord.setStatus(studentDTO.getStatus());
            eduClassCallStudentRecord.setCallId(callId);
            eduClassCallStudentRecordList.add(eduClassCallStudentRecord);
        }
        saveBatch(eduClassCallStudentRecordList);
    }
}
