package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
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.ExtracurricularExercisesReplyService;
import com.ym.mec.biz.service.StudentCourseHomeworkService;
import com.ym.mec.biz.service.StudentServeService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/18
 */
@RequestMapping("studentCourseHomework")
@Api(tags = "学生作业")
@RestController
public class StudentCourseHomeworkController extends BaseController {

    @Autowired
    private StudentCourseHomeworkService studentCourseHomeworkService;
    @Autowired
    private ExtracurricularExercisesReplyService extracurricularExercisesReplyService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentServeService studentServeService;

    @ApiOperation(value = "提交作业")
    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object add(@RequestBody StudentCourseHomework studentCourseHomework){
        studentCourseHomeworkService.add(studentCourseHomework);
        return succeed();
    }

    @ApiOperation(value = "重新提交")
    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object update(@RequestBody StudentCourseHomework studentCourseHomework){
        studentCourseHomework.setStatus(YesOrNoEnum.YES);
        studentCourseHomework.setId(studentCourseHomework.getCourseHomeworkId());
        studentCourseHomework.setCourseHomeworkId(null);
        studentCourseHomeworkService.submitHomework(studentCourseHomework);
        return succeed();
    }

    @ApiOperation(value = "重新提交-公用")
    @PostMapping(value = "/updatePublic",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object updatePublic(@RequestBody StudentCourseHomework studentCourseHomework){
        if(!studentCourseHomework.isExtra()){
            studentCourseHomework.setStatus(YesOrNoEnum.YES);
            studentCourseHomework.setId(studentCourseHomework.getCourseHomeworkId());
            studentCourseHomework.setCourseHomeworkId(null);
            studentCourseHomeworkService.submitHomework(studentCourseHomework);
        }else{
            ExtracurricularExercisesReply extracurricularExercisesReply = new ExtracurricularExercisesReply();
            extracurricularExercisesReply.setId(studentCourseHomework.getCourseHomeworkId());
            extracurricularExercisesReply.setAttachments(studentCourseHomework.getAttachments());
            extracurricularExercisesReplyService.submitExtraExercise(extracurricularExercisesReply);
        }
        return succeed();
    }

    @ApiOperation(value = "获取学生作业界面详细信息")
    @GetMapping(value = "/findCourseHomeworkStudentDetail")
    public Object findCourseHomeworkStudentDetail(Long courseScheduleID) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(studentCourseHomeworkService.findCourseHomeworkStudentDetail(courseScheduleID,user.getId().longValue()));
    }

    @ApiOperation(value = "获取学生作业界面详细信息-公用")
    @GetMapping(value = "/findCourseHomeworkStudentDetailPublic")
    public Object findCourseHomeworkStudentDetailPublic(Long courseScheduleID, boolean extra) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        if(!extra){
            return succeed(studentCourseHomeworkService.findCourseHomeworkStudentDetail(courseScheduleID,user.getId().longValue()));
        }else{
            return succeed(extracurricularExercisesReplyService.findStudentExtraExerciseDetail(courseScheduleID));
        }
    }

    @ApiOperation(value = "获取学生作业记录")
    @GetMapping(value = "/findStudentHomeworkRecord")
    public HttpResponseResult findStudentHomeworkRecord(CourseHomeworkQueryInfo queryInfo){
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        queryInfo.setUserId(user.getId().longValue());
        return succeed(studentCourseHomeworkService.findStudentHomeworkRecord(queryInfo));
    }

    @ApiOperation(value = "分页查询作业列表")
    @PostMapping(value = "/queryPage")
    public Object queryPage(@RequestBody(required = false) CourseHomeworkQueryInfo queryInfo){
        if(queryInfo == null){
            queryInfo = new CourseHomeworkQueryInfo();
        }
        PageInfo<StudentCourseHomework> studentCourseHomeworkPageInfo = studentCourseHomeworkService.queryPage(queryInfo);
        return succeed(studentCourseHomeworkPageInfo);
    }

}
