package com.ym.mec.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ym.mec.education.base.BaseResponse;
import com.ym.mec.education.entity.ClassGroup;
import com.ym.mec.education.entity.ClassGroupTeacherMapper;
import com.ym.mec.education.enums.ReturnCodeEnum;
import com.ym.mec.education.mapper.ClassGroupMapper;
import com.ym.mec.education.req.ClassGroupReq;
import com.ym.mec.education.resp.ClassGroupResp;
import com.ym.mec.education.service.IClassGroupService;
import com.ym.mec.education.service.IClassGroupTeacherMapperService;
import com.ym.mec.education.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 班级 服务实现类
 * </p>
 *
 * @author lemeng
 * @since 2019-09-25
 */
@Service
public class ClassGroupServiceImpl extends ServiceImpl<ClassGroupMapper, ClassGroup> implements IClassGroupService {

    @Autowired
    private IClassGroupTeacherMapperService classGroupTeacherMapperService;
    @Autowired
    private ISysUserService userService;

    @Override
    public BaseResponse<ClassGroupResp> getInfo(ClassGroupReq classGroupReq) {
        BaseResponse<ClassGroupResp> baseResponse = new BaseResponse<>();
        ClassGroup classGroup = getById(classGroupReq.getClassGroupId());
        if (Objects.isNull(classGroup)) {
            baseResponse.setReturnCode(ReturnCodeEnum.CODE_207.getCode());
            baseResponse.setMessage(ReturnCodeEnum.CODE_207.getValue());
            return baseResponse;
        }
        QueryWrapper<ClassGroupTeacherMapper> teacherMapperQueryWrapper = new QueryWrapper<>();
        teacherMapperQueryWrapper.lambda().eq(true, ClassGroupTeacherMapper::getClassGroupId, classGroupReq.getClassGroupId());
        ClassGroupResp classGroupResp = new ClassGroupResp();
        List<ClassGroupTeacherMapper> teacherMapperList = classGroupTeacherMapperService.list(teacherMapperQueryWrapper);
        if (!CollectionUtils.isEmpty(teacherMapperList)) {
            teacherMapperList.stream().filter(item -> "TEACHING".equals(item.getTeacherRole())).findFirst().
                    ifPresent(classGroupTeacherMapper -> classGroupResp.
                            setTeacher(userService.getById(classGroupTeacherMapper.getUserId()).getUsername()));
            teacherMapperList.stream().filter(item -> "BISHOP".equals(item.getTeacherRole())).findFirst().
                    ifPresent(classGroupTeacherMapper -> classGroupResp.
                            setAssistant(userService.getById(classGroupTeacherMapper.getUserId()).getUsername()));
        }
        classGroupResp.setStudentNum(classGroup.getStudentNum());
        classGroupResp.setCourseProgress(classGroup.getCurrentClassTimes() + "/" + classGroup.getTotalClassTimes());
        classGroupResp.setClassGroupName(classGroup.getName());
        return BaseResponse.success(classGroupResp);
    }
}
