package com.yida.data.attendance.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.dto.AttendanceResultPageDTO;
import com.yida.data.attendance.service.EduAttendanceResultService;
import com.yida.data.attendance.vo.AttendanceResultDormVO;
import com.yida.data.attendance.vo.RuleTimeVO;
import com.yida.data.attendance.vo.StayStudentResultVO;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.attendance.EduAttendanceVerifyDorm;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考勤结果 Controller
 *
 * @author wjm
 * @date 2021-09-15 09:56:24
 */
@Api("考勤-学生-考勤记录")
@Slf4j
@Validated
@RestController
@RequestMapping("/result")
@RequiredArgsConstructor
public class EduAttendanceResultController {

    private final EduAttendanceResultService eduAttendanceResultService;


    @ApiOperation("查询一栋宿舍楼的考勤数据")
    @GetMapping("/listResultByDorm")
    public ResultBean<List<StayStudentResultVO>> listResultByDorm(@ApiParam("宿舍楼id") Long dormId,
                                                                  @ApiParam("日期（yyyy-MM-dd）") String date,
                                                                  @ApiParam("1-午间，2-晚上") Integer time,
                                                                  @ApiParam("搜索关键词，寝室号/学生姓名") String keyword,
                                                                  @ApiParam("考勤结果，0-正常，4-晚归，5-异常晚归，6-旷宿") Integer status) {
        return ResultBean.buildSuccess(eduAttendanceResultService.listResultByDorm(dormId, date, time, keyword, status));
    }

    @ApiOperation("查询某个宿舍学生的考勤状况")
    @GetMapping("/listStudentResByDormRoom")
    public ResultBean<List<StayStudentResultVO>> listStudentResByDormRoom(@ApiParam("寝室id") Long roomId,
                                                                          @ApiParam("日期（yyyy-MM-dd）") String date,
                                                                          @ApiParam("1-午间，2-晚上") Integer time) {
        return ResultBean.buildSuccess(eduAttendanceResultService.listStudentResByDormRoom(roomId, date, time));
    }

    @ApiOperation("更改某个学生的在寝状态")
    @GetMapping("/changeStayStudentStatus")
    public ResultBean changeStayStudentStatus(@ApiParam("学生id") Long studentId,
                                              @ApiParam("日期（yyyy-MM-dd）") String date,
                                              @ApiParam("1-午间，2-晚上") Integer time,
                                              @ApiParam("将要修改成的状态,0-归寝，1-未归") Integer status) {
        eduAttendanceResultService.changeStayStudentStatus(studentId, date, time, status);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("完成某个寝室的查寝")
    @PostMapping("/verifyDormRoom")
    public ResultBean verifyDormRoom(@RequestBody EduAttendanceVerifyDorm verifyDorm) {
        eduAttendanceResultService.verifyDormRoom(verifyDorm);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("分页查询考勤记录")
    @GetMapping("/listResultPage")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "分页查询进出记录")
    public ResultBean<IPage<AttendanceResultDormVO>> listResultPage(AttendanceResultPageDTO dto) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                dto.setSchoolId(currentUser.getDeptId());
            }
        }
        return ResultBean.buildSuccess(eduAttendanceResultService.listAttendanceResultPage(dto));
    }


    @ApiOperation("导出考勤记录")
    @PostMapping("/exportAttendanceResult")
    public void exportAttendanceResult(@RequestBody AttendanceResultPageDTO dto, HttpServletResponse response) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                dto.setSchoolId(currentUser.getDeptId());
            }
        }
        eduAttendanceResultService.exportAttendanceResult(dto, response);
    }

    @ApiOperation("查询某年级某天校门考勤记录的规则时间列表")
    @GetMapping("/listGradeResultTime")
    public ResultBean<List<RuleTimeVO>> listGradeResultTime(@ApiParam("年级id") Long gradeId,
                                                            @ApiParam("天，yyyy-MM-dd") String date) {
        return ResultBean.buildSuccess(eduAttendanceResultService.listGradeResultTime(gradeId, date));
    }
}
