package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.page.StudentManageQueryInfo;
import com.ym.mec.biz.dal.page.StudentQueryInfo;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.page.StudentAttendanceQueryInfo;
import com.ym.mec.common.controller.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2020.09.14
 */
@RestController
public class EduStudentStudentController extends BaseController {

    @Autowired
    private StudentAttendanceService studentAttendanceService;
    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private StudentManageService studentManageService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "获取某节课学生签到列表")
    @GetMapping("eduStudentAttendance/findStudentAttendance")
    public Object findStudentAttendance(StudentAttendanceQueryInfo queryInfo){
        return succeed(studentAttendanceService.findStudentAttendance(queryInfo));
    }

    @ApiOperation(value = "获取学员详情页，包含所在乐团信息")
    @PostMapping("studentManage/detail")
    public Object getStudentDetail(Integer studentId){
        return succeed(studentRegistrationService.getStudentDetail(studentId));
    }

    @ApiOperation(value = "教务老师关联的学员列表")
    @GetMapping("studentManage/queryGroupStudents")
    public Object queryGroupStudents(StudentQueryInfo queryInfo){
        return succeed(studentManageService.queryGroupStudents(queryInfo));
    }

    @ApiOperation(value = "获取当前教务老师关联的课程组上该学员的所有剩余课程")
    @GetMapping("studentManage/queryStudentNoStartCourse")
    public Object queryStudentNoStartCourse(StudentQueryInfo queryInfo){
        return succeed(studentManageService.queryStudentNoStartCourse(queryInfo));
    }

    @ApiOperation(value = "获取学生列表")
    @PostMapping("eduStudentManage/queryStudentList")
    public Object queryStudentList(@RequestBody StudentManageQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(Objects.nonNull(queryInfo.getTeacherIdForOrgan())){
            Teacher teacher = teacherService.get(queryInfo.getTeacherIdForOrgan());
            if(Objects.isNull(teacher)){
                return failed("教师信息不存在");
            }
            if(Objects.nonNull(teacher.getTeacherOrganId())){
                queryInfo.setOrganId(teacher.getTeacherOrganId().toString());
            }
            if(StringUtils.isNotBlank(teacher.getFlowOrganRange())){
                queryInfo.setOrganId(queryInfo.getOrganId()+","+teacher.getFlowOrganRange());
            }
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())||StringUtils.isBlank(employee.getOrganIdList())) {
            return failed("用户所在分部异常");
        }
        Set<String> employeeOrganList = Arrays.stream(employee.getOrganIdList().split(",")).collect(Collectors.toSet());
        Set<String> teacherOrganList = Arrays.stream(queryInfo.getOrganId().split(",")).collect(Collectors.toSet());
        List<String> organIdList = teacherOrganList.stream().filter(id -> employeeOrganList.contains(id)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(organIdList)){
            return failed("用户所在分部异常");
        }
        queryInfo.setOrganId(StringUtils.join(organIdList, ","));

        queryInfo.setIsExport(false);
        return succeed(studentManageService.findStudentsByOrganId(queryInfo));
    }

    @ApiOperation(value = "获取学生列表")
    @PostMapping("eduStudentManage/queryStudentPage")
    public Object queryStudentPage(@RequestBody StudentManageQueryInfo queryInfo){
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        queryInfo.setIsExport(false);
        return succeed(studentManageService.findStudentsByOrganId(queryInfo));
    }

}
