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.dao.SysConfigDao;
import com.ym.mec.biz.dal.entity.CourseScheduleEvaluate;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.service.CourseScheduleEvaluateService;
import com.ym.mec.biz.service.SysConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.util.date.DateUtil;
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.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RequestMapping("studyReport")
@Api(tags = "陪练报告")
@RestController
public class StudyReportController extends BaseController {

    @Autowired
    private CourseScheduleEvaluateService courseScheduleEvaluateService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysConfigDao sysConfigDao;


    @ApiOperation("获取学生陪练报告信息")
    @GetMapping(value = "info")
    public Object info(Integer classGroupId) {
        return succeed(courseScheduleEvaluateService.getStudyReport(classGroupId));
    }

    @ApiOperation(value = "课程组报告列表")
    @GetMapping("getGroupReviews")
    public Object getGroupReviews(Integer groupId) {
        List<CourseScheduleEvaluate> courseScheduleEvaluates = courseScheduleEvaluateService.findByGroupId(groupId,null);
        String teacherBaseUrl = sysConfigDao.findConfigValue(SysConfigService.TEACHER_BASE_URL);
        for (CourseScheduleEvaluate courseScheduleEvaluate : courseScheduleEvaluates) {
            courseScheduleEvaluate.setReportLink(teacherBaseUrl + "/#/studyReportNew?id=" + courseScheduleEvaluate.getId() + "&classGroupId=" + courseScheduleEvaluate.getClassGroupId());
            if (courseScheduleEvaluate.getStatus().equals(1) && courseScheduleEvaluate.getVersion().equals(2)) {
                courseScheduleEvaluate.setReportLink(teacherBaseUrl + "/#/reportDetailNew?id=" + courseScheduleEvaluate.getId() + "&classGroupId=" + courseScheduleEvaluate.getClassGroupId());
            } else if ((courseScheduleEvaluate.getStatus().equals(1) && courseScheduleEvaluate.getVersion().equals(1))){
                courseScheduleEvaluate.setReportLink(teacherBaseUrl + "/#/reportDetail?classGroupId=" + courseScheduleEvaluate.getClassGroupId());
            }
        }
        return succeed(courseScheduleEvaluates);
    }

    @ApiOperation(value = "评论详情")
    @GetMapping("getReviewsInfo")
    public Object getReviewsInfo(Integer id) {
        return succeed(courseScheduleEvaluateService.findById(id));
    }

    @ApiOperation(value = "获取需评论的课程列表")
    @GetMapping("getNeedPost")
    public Object getNeedPost() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleEvaluateService.getNeedPostList(sysUser.getId()));
    }

    @ApiOperation(value = "预生成报告")
    @GetMapping("createEvaluate")
    public Object createEvaluate(Date date) {
        Date nowDate = new Date();
        if (date != null) {
            nowDate = date;
        }
        return succeed(courseScheduleEvaluateService.createEvaluate(nowDate));
    }

    @ApiOperation(value = "提交陪练报告")
    @PostMapping(value = "/addStudyReport")
    public HttpResponseResult addStudyReport(@RequestBody CourseScheduleEvaluate courseScheduleEvaluate) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (courseScheduleEvaluate.getId() == null || courseScheduleEvaluate.getId() < 0) {
            return failed(HttpStatus.BAD_REQUEST, "报告id不能为空");
        }
        if (courseScheduleEvaluate.getClassGroupId() == null || courseScheduleEvaluate.getClassGroupId() < 0) {
            return failed(HttpStatus.BAD_REQUEST, "班级id必须大于0");
        }
        if (courseScheduleEvaluate.getItem() == null || courseScheduleEvaluate.getItem().isEmpty()) {
            return failed(HttpStatus.BAD_REQUEST, "课程评价选项不能为空");
        }
        courseScheduleEvaluate.setTeacherId(sysUser.getId());
        return succeed(courseScheduleEvaluateService.updateStudyReport(courseScheduleEvaluate));
    }
}
