package com.ym.mec.student.controller;

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.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @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 SysUserService sysUserService;
    @Autowired
    private StudentServeService studentServeService;

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

    @ApiOperation(value = "重新提交")
    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @AuditLogAnnotation(operateName = "重新提交")
    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)
    @AuditLogAnnotation(operateName = "重新提交-公用")
    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) {
        return succeed(studentCourseHomeworkService.findCourseHomeworkStudentDetail(courseScheduleID,sysUserService.getUserId().longValue()));
    }

    @ApiOperation(value = "获取学生作业界面详细信息-公用")
    @GetMapping(value = "/findCourseHomeworkStudentDetailPublic")
    public Object findCourseHomeworkStudentDetailPublic(Long courseScheduleID, boolean extra) {
        if(!extra){
            return succeed(studentCourseHomeworkService.findCourseHomeworkStudentDetail(courseScheduleID,sysUserService.getUserId().longValue()));
        }else{
            return succeed(extracurricularExercisesReplyService.findStudentExtraExerciseDetail(courseScheduleID));
        }
    }

    @ApiOperation(value = "获取学生作业记录")
    @GetMapping(value = "/findStudentHomeworkRecord")
    public HttpResponseResult findStudentHomeworkRecord(CourseHomeworkQueryInfo queryInfo){
        queryInfo.setUserId(sysUserService.getUserId().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);
    }

    @ApiOperation(value = "乐团作业情况")
    @GetMapping("/musicGroupHomeworkStat")
    public HttpResponseResult musicGroupHomeworkStat(String musicGroupId, String year, Integer term) {
        if(StringUtils.isEmpty(year)){
            throw new BizException("请录入年份");
        }
        if(term == null){
            throw new BizException("请录入学期");
        }
        String startDay = "";
        String endDay = "";
        if(term == 0){
            startDay = year + "-03-01";
            endDay = year + "-08-31";
        }else {
            startDay = year + "-09-01";
            endDay = (Integer.valueOf(year) + 1) + "-02-01";
        }

        return succeed(studentServeService.musicGroupHomeworkStatWithStu(musicGroupId, sysUserService.getUserId(), startDay, endDay));
    }

}
