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.dto.StudentAttendanceDto;
import com.ym.mec.biz.dal.dto.TeacherSignOutDto;
import com.ym.mec.biz.dal.page.TeacherAttendanceQueryInfo;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.StudentAttendanceService;
import com.ym.mec.biz.service.TeacherAttendanceService;
import com.ym.mec.common.controller.BaseController;
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.Objects;

@RequestMapping("teacherAttendance")
@Api(tags = "教师签到服务")
@RestController
public class TeacherAttendanceController extends BaseController {

    @Autowired
    private TeacherAttendanceService teacherAttendanceService;

    @Autowired
    private StudentAttendanceService studentAttendanceService;

    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "新增教师签到")
    @PostMapping("/add")
    public Object add(@RequestBody TeacherSignOutDto teacherSignOutDto) {
        return succeed(teacherAttendanceService.addTeacherAttendanceRecord(teacherSignOutDto));
    }

    @ApiOperation(value = "分页查询教师签到列表")
    @GetMapping("/queryPage")
    public Object queryPage(TeacherAttendanceQueryInfo queryInfo) {
        return succeed(teacherAttendanceService.queryPage(queryInfo));
    }
    
    @ApiOperation(value = "根据班级ID获取当前课程的学生")
    @GetMapping("/getCurrentCourseStudents")
    public Object getCurrentCourseStudents(Long courseScheduleId){
        return succeed(classGroupService.findAttendanceStudentByCourse(courseScheduleId));
    }

    @ApiOperation(value = "点名")
    @PostMapping("/addStudentAttendances")
    public Object addStudentAttendances(@RequestBody StudentAttendanceDto studentAttendanceInfo){
        return succeed(studentAttendanceService.addStudentAttendances(studentAttendanceInfo));
    }

    @ApiOperation(value = "获取签退详情")
    @GetMapping("/getTeacherSignDetail")
    public Object getTeacherSignDetail(Integer courseScheduleId){
        SysUser user = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(user)){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        return succeed(teacherAttendanceService.getTeacherSignDetail(courseScheduleId, user.getId()));
    }

    @ApiOperation(value = "提交考勤申述")
    @PostMapping("/addComplaints")
    public Object addComplaints(Long courseScheduleId,String content,String url){
        teacherAttendanceService.addComplaints(courseScheduleId,content,url);
        return succeed();
    }

    @ApiOperation(value = "撤销考勤申述")
    @PostMapping("/repealComplaints")
    public Object repealComplaints(Long courseScheduleId){
        teacherAttendanceService.repealComplaints(courseScheduleId);
        return succeed();
    }
}
