package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.CourseHomeworkStudentDetailDto;
import com.ym.mec.biz.dal.entity.CourseHomework;
import com.ym.mec.biz.dal.entity.ExtracurricularExercisesReply;
import com.ym.mec.biz.dal.entity.StudentCourseHomework;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.CourseHomeworkQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.biz.service.ExtracurricularExercisesReplyService;
import com.ym.mec.biz.service.StudentCourseHomeworkService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/10/20
 */
@Api(tags = "作业服务")
@RequestMapping("courseHomework")
@RestController
public class CourseHomeworkController extends BaseController {

    @Autowired
    private CourseHomeworkService courseHomeworkService;
    @Autowired
    private StudentCourseHomeworkService studentCourseHomeworkService;
    @Autowired
    private ExtracurricularExercisesReplyService extracurricularExercisesReplyService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "布置课堂作业")
    @GetMapping("/addCourseHomework")
    public HttpResponseResult addCourseHomework(CourseHomework courseHomework){
        courseHomeworkService.addCourseHomework(courseHomework);
        return succeed();
    }

    @ApiOperation(value = "根据班级获取教师布置的作业")
    @GetMapping("/findTeacherCourseHomeworkByClassGroup")
    public Object findTeacherCourseHomeworkByClassGroup(CourseHomeworkQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        queryInfo.setUserId(sysUser.getId().longValue());
        return succeed(courseHomeworkService.findTeacherCourseHomeworkByClassGroup(queryInfo));
    }

    @ApiOperation(value = "根据课程计划获取需要交作业的学生")
    @GetMapping("/findCourseStudents")
    public Object findCourseStudents(Long courseScheduleId,String userName){
        return succeed(studentCourseHomeworkService.findStudentCourseHomeworkByCourse(courseScheduleId,userName));
    }

    @ApiOperation(value = "根据课程计划获取需要交作业的学生-公用")
    @GetMapping("/findCourseStudentsPublic")
    public Object findCourseStudentsPublic(Long courseScheduleId,String userName, boolean extra){
        if(!extra){
            return succeed(studentCourseHomeworkService.findStudentCourseHomeworkByCourse(courseScheduleId,userName));
        }else{
            return succeed(extracurricularExercisesReplyService.findExtraExerciseStudents(courseScheduleId,userName));
        }
    }

    @ApiOperation(value = "获取学生作业界面详细信息")
    @GetMapping(value = "/findCourseHomeworkStudentDetail")
    public Object findCourseHomeworkStudentDetail(Long courseScheduleID,Long userId){
        StudentCourseHomework studentCourseHomework1 = studentCourseHomeworkService.get(courseScheduleID);
        if(Objects.isNull(studentCourseHomework1)){
            return failed("作业不存在");
        }
        CourseHomeworkStudentDetailDto courseHomeworkStudentDetail = studentCourseHomeworkService.findCourseHomeworkStudentDetail(studentCourseHomework1.getCourseScheduleId(), userId);
        if(Objects.nonNull(courseHomeworkStudentDetail)){
            StudentCourseHomework studentCourseHomework=new StudentCourseHomework();
            studentCourseHomework.setId(studentCourseHomework1.getId());
            studentCourseHomework.setIsView(YesOrNoEnum.YES);
            studentCourseHomeworkService.update(studentCourseHomework);
        }
        return succeed(courseHomeworkStudentDetail);
    }

    @ApiOperation(value = "获取学生作业界面详细信息-公用")
    @GetMapping(value = "/findCourseHomeworkStudentDetailPublic")
    public Object findCourseHomeworkStudentDetailPublic(Long courseScheduleID,Long userId, boolean extra){
        if(!extra){
            StudentCourseHomework studentCourseHomework1 = studentCourseHomeworkService.get(courseScheduleID);
            if(Objects.isNull(studentCourseHomework1)){
                return failed("作业不存在");
            }
            CourseHomeworkStudentDetailDto courseHomeworkStudentDetail = studentCourseHomeworkService.findCourseHomeworkStudentDetail(studentCourseHomework1.getCourseScheduleId(), userId);
            if(Objects.nonNull(courseHomeworkStudentDetail)&&Objects.nonNull(courseHomeworkStudentDetail.getAttachments())){
                StudentCourseHomework studentCourseHomework=new StudentCourseHomework();
                studentCourseHomework.setId(studentCourseHomework1.getId());
                studentCourseHomework.setIsView(YesOrNoEnum.YES);
                studentCourseHomeworkService.update(studentCourseHomework);
            }
            return succeed(courseHomeworkStudentDetail);
        }else{
            CourseHomeworkStudentDetailDto studentExtraExerciseDetail = extracurricularExercisesReplyService.findStudentExtraExerciseDetail(courseScheduleID);
            if(Objects.isNull(studentExtraExerciseDetail)){
                return failed("作业不存在");
            }
            if(Objects.nonNull(studentExtraExerciseDetail.getAttachments())&&studentExtraExerciseDetail.getStatus().equals(YesOrNoEnum.YES)){
                ExtracurricularExercisesReply extracurricularExercisesReply=new ExtracurricularExercisesReply();
                extracurricularExercisesReply.setId(studentExtraExerciseDetail.getCourseHomeworkId());
                extracurricularExercisesReply.setIsView(1);
                extracurricularExercisesReplyService.update(extracurricularExercisesReply);
            }
            return succeed(studentExtraExerciseDetail);
        }
    }

}
