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.entity.CourseHomework;
import com.ym.mec.biz.dal.entity.ExtracurricularExercises;
import com.ym.mec.biz.dal.page.StudentCourseHomeWorkQueryInfo;
import com.ym.mec.biz.service.ExtracurricularExercisesReplyService;
import com.ym.mec.biz.service.ExtracurricularExercisesService;
import com.ym.mec.biz.service.StudentCourseHomeworkService;
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.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.page.CourseHomeworkQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.common.controller.BaseController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/18
 */
@RequestMapping("courseHomework")
@Api(tags = "课后作业")
@RestController
public class CourseHomeworkController extends BaseController {

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

    @ApiOperation(value = "分页查询作业列表")
    @GetMapping(value = "/queryPage")
    public Object queryPage(CourseHomeworkQueryInfo queryInfo){
        return succeed(courseHomeworkService.queryPage(queryInfo));
    }

    @ApiOperation(value = "作业列表")
    @GetMapping(value = "getEduCourseHomeworkList")
    public HttpResponseResult getEduCourseHomeworkList(StudentCourseHomeWorkQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTeacherId(sysUser.getId());
        queryInfo.setTransactionTeacherId(sysUser.getId());
        if(!queryInfo.getExtra()){
            return succeed(studentCourseHomeworkService.getEduCourseHomeworkList(queryInfo));
        }else{
            return succeed(extracurricularExercisesReplyService.getEduHomeworkList(queryInfo));
        }
    }

    @ApiOperation(value = "根据课程计划获取需要交作业的学生-公用")
    @GetMapping("/findHomeworkStudents")
    public HttpResponseResult findHomeworkStudents(Long homeworkId,String userName, boolean extra){
        if(Objects.isNull(extra)){
            extra = false;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("content", "");
        if(!extra){
            CourseHomework courseHomework = courseHomeworkService.get(homeworkId);
            if(Objects.nonNull(courseHomework)){
                result.put("title", courseHomework.getTitle());
                result.put("content", courseHomework.getContent());
            }
            result.put("data", studentCourseHomeworkService.findStudentCourseHomeworkByCourse(null, homeworkId,userName));
        }else{
            ExtracurricularExercises extracurricularExercises = extracurricularExercisesService.get(homeworkId);
            if(Objects.nonNull(extracurricularExercises)){
                result.put("title", extracurricularExercises.getTitle());
                result.put("content", extracurricularExercises.getContent());
            }
            result.put("data", extracurricularExercisesReplyService.findExtraExerciseStudents(homeworkId,userName));
        }
        return succeed(result);
    }

    @ApiOperation(value = "作业统计")
    @GetMapping("/queryEduHomeworkStatList")
    public HttpResponseResult queryEduHomeworkStatList(StudentCourseHomeWorkQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTransactionTeacherId(sysUser.getId());
        return succeed(studentCourseHomeworkService.queryEduHomeworkStatList(queryInfo));
    }

    @ApiOperation(value = "作业统计详情")
    @GetMapping("/queryEduHomeworkStatDetail")
    public HttpResponseResult queryEduHomeworkStatDetail(StudentCourseHomeWorkQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        queryInfo.setTransactionTeacherId(sysUser.getId());
        return succeed(studentCourseHomeworkService.queryEduHomeworkStatDetail(queryInfo));
    }
}
