package com.ym.mec.student.controller;

import com.netflix.discovery.converters.Auto;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dao.ExtracurricularExercisesDao;
import com.ym.mec.biz.dal.dao.ExtracurricularExercisesReplyDao;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkDao;
import com.ym.mec.biz.dal.entity.*;
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 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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Author Joburgess
 * @Date 2019/9/19
 */

@RequestMapping("studentCourseHomeworkReply")
@Api(tags = "作业评论-学生端")
@RestController
public class StudentCourseHomeworkReplyController extends BaseController {

    @Autowired
    private StudentCourseHomeworkReplyService studentCourseHomeworkReplyService;
    @Autowired
    private StudentCourseHomeworkDao studentCourseHomeworkDao;
    @Autowired
    private ExtracurricularExercisesMessageService extracurricularExercisesMessageService;
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentServeService studentServeService;
    @Autowired
    private CourseScheduleDao courseScheduleDao;
    @Autowired
    private ExtracurricularExercisesReplyDao extracurricularExercisesReplyDao;
    @Autowired
    private ExtracurricularExercisesDao extracurricularExercisesDao;

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

    @ApiOperation(value = "新增回复-公用")
    @PostMapping("/addPublic")
    public Object addPublic(@RequestBody StudentCourseHomeworkReply studentCourseHomeworkReply) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        Integer teacherId = null;
        if(!studentCourseHomeworkReply.isExtra()){
            studentCourseHomeworkReply.setUserId(sysUser.getId());
            studentCourseHomeworkReplyService.insert(studentCourseHomeworkReply);
            StudentCourseHomework studentCourseHomework = studentCourseHomeworkDao.get(studentCourseHomeworkReply.getStudentCourseHomeworkId());
            CourseSchedule courseSchedule = courseScheduleDao.get(studentCourseHomework.getCourseScheduleId());
            teacherId=courseSchedule.getActualTeacherId();
            studentServeService.updateExercisesSituation(courseSchedule.getClassDate(), new ArrayList<>(Arrays.asList(sysUser.getId())), teacherId);
        }else{
            ExtracurricularExercisesMessage extracurricularExercisesMessage=new ExtracurricularExercisesMessage();
            extracurricularExercisesMessage.setExtracurricularExercisesReplyId(studentCourseHomeworkReply.getStudentCourseHomeworkId());
            extracurricularExercisesMessage.setContent(studentCourseHomeworkReply.getContent());
            extracurricularExercisesMessage.setUserId(sysUser.getId());
            extracurricularExercisesMessage.setMsgType(studentCourseHomeworkReply.getMsgType());
            extracurricularExercisesMessage.setRole("STUDENT");
            extracurricularExercisesMessageService.addStudentExtraExerciseComment(extracurricularExercisesMessage);
        }
        return succeed();
    }

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

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

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

}
