package com.yida.data.attendance.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.dto.ListResultStatisticsDTO;
import com.yida.data.attendance.dto.StasticStaffExcepByDayDTO;
import com.yida.data.attendance.dto.StasticStaffResultDTO;
import com.yida.data.attendance.service.EduAttendanceStasticService;
import com.yida.data.attendance.vo.CountVO;
import com.yida.data.attendance.vo.RateRankVO;
import com.yida.data.attendance.vo.RateVO;
import com.yida.data.attendance.vo.StaRecordCountVO;
import com.yida.data.attendance.vo.StasticDormResultVO;
import com.yida.data.attendance.vo.StasticStaffExcepByDayVO;
import com.yida.data.attendance.vo.StasticStaffResultByDayVO;
import com.yida.data.attendance.vo.StasticStaffResultByMonthVO;
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.system.Dept;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.feign.RemoteDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "考勤-考勤统计")
@RestController
@RequestMapping("/stastic")
@RequiredArgsConstructor
public class EduAttendanceStasticController {

    private final EduAttendanceStasticService eduAttendanceStasticService;
    private final RemoteDeptService remoteDeptService;

    @ApiOperation("查询出勤率最高前五，最低前三")
    @GetMapping("/stasticRateRank")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "查询出勤率最高前五，最低前三")
    public ResultBean<RateRankVO> stasticRateRank(@ApiParam("日期 yyyy-MM-dd") String date,
                                                  @RequestParam(required = false) Long schoolId,
                                                  @ApiParam("年级id") Long gradeId,
                                                  @ApiParam("时段(校门考勤用)") Long timeId) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                schoolId = currentUser.getDeptId();
            }
        }
        return ResultBean.buildSuccess(eduAttendanceStasticService.stasticRateRank(date, schoolId, gradeId, timeId));
    }

    @ApiOperation("查询各宿舍楼归寝率排名")
    @GetMapping("/stasticDormRateRank")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "查询各宿舍楼归寝率排名")
    public ResultBean stasticDormRateRank(@ApiParam("日期 yyyy-MM-dd") String date,
                                          @RequestParam Long schoolId,
                                          @ApiParam("时段(宿舍考勤用),1-中午，2-晚上") Integer duration) {
        return ResultBean.buildSuccess(eduAttendanceStasticService.stasticDormRateRank(date, schoolId, duration));
    }

    @ApiOperation("出勤率统计表")
    @GetMapping("/listRate")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "出勤率统计表")
    public ResultBean<List<RateVO>> listRate(ListResultStatisticsDTO dto) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                dto.setSchoolId(currentUser.getDeptId());
            }
        }
        return ResultBean.buildSuccess(eduAttendanceStasticService.listRate(dto));
    }

    @ApiOperation("导出出勤率统计表")
    @PostMapping("/exportRateList")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "导出出勤率统计表")
    public void exportRateList(@RequestBody ListResultStatisticsDTO dto, HttpServletResponse response) {
        if (dto.getSchoolId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                dto.setSchoolId(currentUser.getDeptId());
            }
        }
        eduAttendanceStasticService.exportRateList(dto, response);
    }

    @ApiOperation("分宿舍楼统计当日考勤数据")
    @GetMapping("/stasticResultGroupByDorm")
    public ResultBean<List<StasticDormResultVO>> stasticResultGroupByDorm(@ApiParam("管理人员id(职工id)") Long staffId,
                                                                          @ApiParam("时间段，（1-中午，2-晚上）") Integer time,
                                                                          @ApiParam("日期（yyyy-MM-dd）") String date) {
        return ResultBean
                .buildSuccess(eduAttendanceStasticService.stasticResultGroupByDorm(FebsUtil.getTopDeptId(), staffId, time, date));
    }
//    @ApiOperation("按班级统计当日宿舍考勤数据")
//    @GetMapping("/stasticResultGroupByDorm")
//    public ResultBean<List<StasticDormResultVO>> stasticResultGroupByDorm(@ApiParam("管理人员id(职工id)") Long staffId,
//                                                                          @ApiParam("时间段，（1-中午，2-晚上）") Integer time,
//                                                                          @ApiParam("日期（yyyy-MM-dd）") String date) {
//        return ResultBean
//                .buildSuccess(eduAttendanceStasticService.stasticResultGroupByDorm(FebsUtil.getTopDeptId(), staffId, time, date));
//    }


    @ApiOperation("分寝室查询当日考勤数据")
    @GetMapping("/stasticResultByDormRoom")
    public ResultBean<List<StasticDormResultVO>> stasticResultByDormRoom(
            @ApiParam("宿舍楼id，不选择时传全部宿舍楼id") @RequestParam(required = false) Long dormId,
            @ApiParam("查寝状态,0-未确认，1-已确认") @RequestParam(required = false) Integer verify,
            @ApiParam("归寝状态,0-未归寝，1-已归寝") @RequestParam(required = false) Integer status,
            @ApiParam("时间段，（1-中午，2-晚上）") Integer time,
            @ApiParam("日期（yyyy-MM-dd）") String date,
            @ApiParam("寝室号排序，0-顺序，1-倒序,默认顺序") @RequestParam(defaultValue =
                    "0") Integer sort,
            @ApiParam("搜索关键词，寝室名/学生名") String keyword) {
        return ResultBean
                .buildSuccess(eduAttendanceStasticService.stasticResultByDormRoom(dormId, verify, status, time, date, sort,
                        keyword));
    }

    @ApiOperation("按日统计教师考勤数据")
    @GetMapping("/stasticStaffResultByDay")
    public ResultBean<IPage<StasticStaffResultByDayVO>> stasticStaffResultByDay(StasticStaffResultDTO dto) {
        return ResultBean.buildSuccess(eduAttendanceStasticService.stasticStaffResultByDay(FebsUtil.getCurrentUser(), dto));
    }

    @ApiOperation("导出按日统计教师考勤数据")
    @PostMapping("/exportStasticStaffResultByDay")
    public void exportStasticStaffResultByDay(@RequestBody StasticStaffResultDTO dto,
                                              HttpServletResponse response) {
        eduAttendanceStasticService.exportStasticStaffResultByDay(FebsUtil.getCurrentUser(), dto, response);
    }

    @ApiOperation("按月统计教师考勤数据")
    @GetMapping("/stasticStaffResultByMonth")
    public ResultBean<IPage<StasticStaffResultByMonthVO>> stasticStaffResultByMonth(StasticStaffResultDTO dto) {
        return ResultBean.buildSuccess(eduAttendanceStasticService.stasticStaffResultByMonth(FebsUtil.getCurrentUser(), dto));
    }

    @ApiOperation("导出按日统计教师考勤数据")
    @PostMapping("/exportStasticStaffResultByMonth")
    public void exportStasticStaffResultByMonth(@RequestBody StasticStaffResultDTO dto,
                                                HttpServletResponse response) {
        eduAttendanceStasticService.exportStasticStaffResultByMonth(FebsUtil.getCurrentUser(), dto, response);
    }

    @ApiOperation("按日期段查询具体教职工异常信息")
    @GetMapping("/stasticStaffExcepByDay")
    public ResultBean<StasticStaffExcepByDayVO> stasticStaffExcepByDay(StasticStaffExcepByDayDTO dto) {
        return ResultBean.buildSuccess(eduAttendanceStasticService.stasticStaffExcepByDay(dto));
    }

    @ApiOperation("查询管理范围内各异常类型人数")
    @GetMapping("/countRangeExcepStaff")
    public ResultBean<CountVO> countRangeExcepStaff(StasticStaffExcepByDayDTO dto) {
        return ResultBean.buildSuccess(eduAttendanceStasticService.countRangeExcepStaff(dto));
    }

    @ApiOperation("dashboard-考勤概况")
    @GetMapping("/staRecordCount")
    public ResultBean<StaRecordCountVO> staRecordCount(@ApiParam("yyyy-MM-dd") String startDate,
                                                       @ApiParam("yyyy-MM-dd") String endDate) {
        List<Long> schoolIds = remoteDeptService.getSchoolByArea(null, null).getData().stream().map(Dept::getDeptId)
                .collect(Collectors.toList());
        return ResultBean.buildSuccess(eduAttendanceStasticService.staRecordCount(LocalDate.parse(startDate), LocalDate.parse(endDate), schoolIds));
    }
}
