package cn.iocoder.yudao.module.system.controller.admin.attendance;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.attendance.dto.*;
import cn.iocoder.yudao.module.system.controller.admin.attendance.vo.*;
import cn.iocoder.yudao.module.system.service.attendance.AttendanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "考勤 App - 考勤管理")
@RestController
@RequestMapping("/system/attendance")
@Validated
public class AttendanceController {
    @Resource
    private AttendanceService attendanceService;

    @GetMapping("/getPunchPage")
    @Operation(summary = "获取考勤页面")
    public CommonResult<AttendancePunchPageVO> getPunchPage(@ModelAttribute AttendancePunchPageDTO dto) {
        AttendancePunchPageVO vo = attendanceService.getPunchPage(dto);
        return success(vo);
    }

    @PostMapping("/punch")
    @Operation(summary = "打卡")
    public CommonResult<AttendancePunchVO> punch(@RequestBody AttendancePunchDTO dto) {
        AttendancePunchVO vo = attendanceService.punch(dto);
        return success(vo);
    }

    @GetMapping("/getReplacementCardNum")
    @Operation(summary = "获取本月剩余补卡次数")
    public CommonResult<Integer> getReplacementCardNum() {
        Integer num = attendanceService.getReplacementCardNum();
        return success(num);
    }

    @PostMapping("/replacementCard")
    @Operation(summary = "补卡")
    public CommonResult replacementCard(@RequestBody AttendanceReplacementCardDTO dto) {
        attendanceService.replacementCard(dto);
        return success("补卡成功");
    }

    @GetMapping("/getRules/{groupId}")
    @Operation(summary = "获取考勤规则")
    public CommonResult<AttendanceRulesVO> getRules(@PathVariable Long groupId) {
        AttendanceRulesVO vo = attendanceService.getRules(groupId);
        return success(vo);
    }

    @GetMapping("/getRulesByUserId")
    @Operation(summary = "获取考勤规则根据用户id")
    public CommonResult<AttendanceRulesVO> getRulesByUserId() {
        AttendanceRulesVO vo = attendanceService.getRulesByUserId();
        return success(vo);
    }

    @GetMapping("/statisticsByDay")
    @Operation(summary = "统计按天")
    public CommonResult<Map<String, AttendanceStatusByDayVO>> statisticsByDay(@ModelAttribute AttendanceStatisticsByDayDTO dto) {
        Map<String, AttendanceStatusByDayVO> vo = attendanceService.statisticsByDay(dto);
        return success(vo);
    }

    @GetMapping("/statisticsByCycle")
    @Operation(summary = "统计按周期")
    public CommonResult<AttendanceStatisticsVO> statisticsByCycle(@ModelAttribute @Valid AttendanceStatisticsByCycleDTO dto) {
        AttendanceStatisticsVO vo = attendanceService.statisticsByCycle(dto);
        return success(vo);
    }

    @GetMapping("/teamStatisticsByDay")
    @Operation(summary = "团队统计按天")
    public CommonResult<Map<String, TeamAttendanceStatisticsByDayVO>> teamStatisticsByDay(@ModelAttribute @Valid TeamAttendanceStatisticsByDayDTO dto) {
        Map<String, TeamAttendanceStatisticsByDayVO> map = attendanceService.teamStatisticsByDay(dto);
        return success(map);
    }

    @GetMapping("/tesmStatisticsByCycle")
    @Operation(summary = "团队统计按周期")
    public CommonResult<TeamAttendanceStatisticsByCycleVO> tesmStatisticsByCycle(@ModelAttribute @Valid TeamAttendanceStatisticsByCycleDTO dto) {
        TeamAttendanceStatisticsByCycleVO map = attendanceService.tesmStatisticsByCycle(dto);
        return success(map);
    }

    @GetMapping("/getTheAttendanceGroupToWhichTheCurrentlyLoggedInUserBelongsAndWhetherTheyHaveAdministratorRights")
    @Operation(summary = "获取当前登陆用户所在考勤组并且是否有管理员权限")
    public CommonResult<List<AttendanceGroupSystemVO>> getTheAttendanceGroupToWhichTheCurrentlyLoggedInUserBelongsAndWhetherTheyHaveAdministratorRights(@RequestParam(name = "allFlag", required = false, defaultValue = "false") Boolean allFlag) {
        List<AttendanceGroupSystemVO> vo = attendanceService.getTheAttendanceGroupToWhichTheCurrentlyLoggedInUserBelongsAndWhetherTheyHaveAdministratorRights(allFlag);
        return success(vo);
    }


    @PostMapping("/export-excel")
    @Operation(summary = "导出考勤报表 Excel")
    public void exportAttendanceExcel(@RequestBody ExportAttendanceExcelDTO dto,
                                      HttpServletResponse response) {
        attendanceService.exportAttendanceExcel(response, dto);
    }

}
