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.entity.ExtracurricularExercisesMessage;
import com.ym.mec.biz.dal.entity.StudentCourseHomeworkReply;
import com.ym.mec.biz.dal.page.StudentCourseHomeworkReplyQueryInfo;
import com.ym.mec.biz.service.ExtracurricularExercisesMessageService;
import com.ym.mec.biz.service.StudentCourseHomeworkReplyService;
import com.ym.mec.biz.service.StudentServeService;
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 io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/19
 */
@RequestMapping("teacherCourseHomeworkReply")
@Api(tags = "作业评论-教师端")
@RestController
public class TeacherCourseHomeworkReplyController extends BaseController {

    @Autowired
    private ExtracurricularExercisesMessageService extracurricularExercisesMessageService;
    @Autowired
    private StudentCourseHomeworkReplyService studentCourseHomeworkReplyService;
    @Autowired
    private StudentServeService studentServeService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "新增回复")
    @PostMapping("/add")
    public Object add(@RequestBody StudentCourseHomeworkReply studentCourseHomeworkReply) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        studentCourseHomeworkReply.setUserId(user.getId());
        studentCourseHomeworkReplyService.insertTeacherReply(studentCourseHomeworkReply);
        return succeed();
    }

    @ApiOperation(value = "新增回复-公用")
    @PostMapping("/addPublic")
    public Object addPublic(@RequestBody StudentCourseHomeworkReply studentCourseHomeworkReply) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        if(!studentCourseHomeworkReply.isExtra()){
            studentCourseHomeworkReply.setUserId(user.getId());
            studentCourseHomeworkReplyService.insertTeacherReply(studentCourseHomeworkReply);
        }else{
            ExtracurricularExercisesMessage extracurricularExercisesMessage=new ExtracurricularExercisesMessage();
            extracurricularExercisesMessage.setExtracurricularExercisesReplyId(studentCourseHomeworkReply.getStudentCourseHomeworkId());
            extracurricularExercisesMessage.setContent(studentCourseHomeworkReply.getContent());
            extracurricularExercisesMessage.setMsgType(studentCourseHomeworkReply.getMsgType());
            extracurricularExercisesMessage.setUserId(user.getId());
            extracurricularExercisesMessage.setRole("TEACHER");
            extracurricularExercisesMessageService.addStudentExtraExerciseComment(extracurricularExercisesMessage);
        }
        return succeed();
    }

    @ApiOperation(value = "回复查询")
    @GetMapping("/queryPage")
    public Object queryPage(StudentCourseHomeworkReplyQueryInfo queryInfo){
        return succeed(studentCourseHomeworkReplyService.queryPage(queryInfo));
    }

    @ApiOperation(value = "回复查询-公用")
    @GetMapping("/queryPagePublic")
    public Object queryPagePublic(StudentCourseHomeworkReplyQueryInfo queryInfo){
        if(!queryInfo.isExtra()){
            return succeed(studentCourseHomeworkReplyService.queryPage(queryInfo));
        }else{
            return succeed(extracurricularExercisesMessageService.findStudentExtraExerciseComments(queryInfo));
        }
    }

    @ApiOperation(value = "删除回复")
    @PostMapping("/del/{id}")
    public Object del(@ApiParam(value = "回复ID", required = true) @PathVariable("id") Long id){
        studentCourseHomeworkReplyService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "检测是否都布置了作业")
    @GetMapping("/checkeIsAssignHomework")
    public HttpResponseResult checkeIsAssignHomework(Long courseScheduleId, String studentIds){
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(studentServeService.checkeIsAssignHomework(courseScheduleId, studentIds, user.getId()));
    }

}
