package com.yida.data.attendance.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.attendance.mapper.EduAttendanceManagePushResultMapper;
import com.yida.data.attendance.mapper.EduAttendanceResultMapper;
import com.yida.data.attendance.service.EduAttendanceManagePushResultService;
import com.yida.data.common.core.entity.attendance.EduAttendanceManagePushResult;
import com.yida.data.common.core.entity.attendance.EduAttendanceResult;
import com.yida.data.common.core.entity.constant.StringConstant;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.user.feign.RemoteStudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.stream.Stream;

/**
 * 管理考勤结果推送记录service
 *
 * @author ZYJ
 * @date 2022/7/15
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduAttendanceManagePushResultServiceImpl extends ServiceImpl<EduAttendanceManagePushResultMapper, EduAttendanceManagePushResult>
        implements EduAttendanceManagePushResultService {

    private final EduAttendanceResultMapper eduAttendanceResultMapper;

    private final RemoteStudentService remoteStudentService;

    @Override
    public List<EduAttendanceResult> selectPushList(Long pushId) {
        EduAttendanceManagePushResult result = getOne(Wrappers.lambdaQuery(new EduAttendanceManagePushResult())
                .eq(EduAttendanceManagePushResult::getId, pushId));

        if (Objects.isNull(result)) {
            return new ArrayList<>();
        }
        List<EduAttendanceResult> resultList = new ArrayList<>();
        // 考勤记录id
        List<Long> resultIds = Stream.of(result.getResultId().split(StringConstant.COMMA))
                .map(Long::valueOf).collect(Collectors.toList());
        resultIds.forEach(resultId -> {
            EduAttendanceResult eduAttendanceResult = eduAttendanceResultMapper.selectById(resultId);
            EduStudent student = remoteStudentService.getStudentNoPermission(eduAttendanceResult.getStudentId()).getData();
            if (Objects.nonNull(student)) {
                eduAttendanceResult.setStuName(student.getStuName());
                eduAttendanceResult.setPicUrl(student.getAvatar());
                eduAttendanceResult.setStuNo(student.getStuNumber());

                resultList.add(eduAttendanceResult);
            }
        });
        return resultList;
    }
}
