package com.yida.data.attendance.controller.H5;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.attendance.dto.AttendanceFaceMatchDTO;
import com.yida.data.attendance.dto.AttendanceResultByDayDTO;
import com.yida.data.attendance.dto.AttendanceResultByStartAndEndTimeDTO;
import com.yida.data.attendance.dto.AttendanceStatusByMonthDTO;
import com.yida.data.attendance.dto.AttendanceUserCheckInDTO;
import com.yida.data.attendance.dto.H5AttendanceRuleWifiDTO;
import com.yida.data.attendance.dto.ManageListUserDTO;
import com.yida.data.attendance.service.AttendanceCheckInService;
import com.yida.data.attendance.service.AttendanceResultService;
import com.yida.data.attendance.service.AttendanceRuleService;
import com.yida.data.attendance.vo.AttendanceFaceMatchVO;
import com.yida.data.attendance.vo.AttendanceRuleVO;
import com.yida.data.attendance.vo.AttendanceStatusByMonthVO;
import com.yida.data.attendance.vo.AttendanceUserCheckInVO;
import com.yida.data.attendance.vo.AttendanceWifiVO;
import com.yida.data.attendance.vo.DateStatusVO;
import com.yida.data.attendance.vo.DayReportDataVO;
import com.yida.data.attendance.vo.ManageListUserAttendanceDetailVO;
import com.yida.data.attendance.vo.ManageListUserVO;
import com.yida.data.attendance.vo.StaffIdentityVO;
import com.yida.data.attendance.vo.UserAttendanceResultByDayVO;
import com.yida.data.attendance.vo.UserAttendanceResultByTimeSlotVO;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.log.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 教职工考勤规则 Controller
 *
 * @date 2023-08-09 11:37:25
 */
@Slf4j
@RestController
@RequestMapping("H5/attendanceRule")
@RequiredArgsConstructor
@Api(tags = "移动端-考勤-打卡", value = "移动端-考勤-打卡")
public class H5AttendanceRuleController {

    private final AttendanceRuleService attendanceRuleService;
    private final AttendanceResultService attendanceResultService;
    private final AttendanceCheckInService attendanceCheckInService;

    @ApiOperation("查询用户当前时间的打卡规则")
    @PostMapping("/findRuleByUserAndDate")
    public ResultBean<AttendanceWifiVO> findRuleByUserAndDate(@RequestBody H5AttendanceRuleWifiDTO dto) {
        return ResultBean.buildSuccess(attendanceCheckInService.findByUserIdAndDateTime(dto));
    }

    @ApiOperation("签到打卡")
    @PostMapping("/userCheckIn")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "签到打卡", type = OperationLogTypeEnum.INSERT)
    public ResultBean<AttendanceUserCheckInVO> userCheckIn(@RequestBody AttendanceUserCheckInDTO dto) {
        return ResultBean.buildSuccess(attendanceCheckInService.userCheckIn(dto));
    }

    @ApiOperation("签到人脸比对")
    @PostMapping("/rosterFaceMatch")
    @OperationLog(module = ModuleName.ATTENDANCE, methods = "签到人脸比对", type = OperationLogTypeEnum.SELECT)
    public ResultBean<AttendanceFaceMatchVO> rosterFaceMatch(@RequestBody AttendanceFaceMatchDTO dto) {
        return ResultBean.buildSuccess(attendanceRuleService.AttendanceFaceMatch(dto));
    }


    @ApiOperation("家长端/教师我的考勤-获取当前月份用户考勤状态汇总")
    @PostMapping("/getUserAttendanceStatusByMonth")
    public ResultBean<List<DateStatusVO>> getUserAttendanceStatusByMonth(
        @Validated @RequestBody AttendanceStatusByMonthDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.getUserAttendanceStatusByMonth(dto));
    }

    @ApiOperation("家长端/教师我的考勤-获取某日用户考勤结果")
    @PostMapping("/getUserAttendanceResultByDay")
    public ResultBean<List<UserAttendanceResultByDayVO>> getUserAttendanceRecordByDay(
        @Validated @RequestBody AttendanceResultByDayDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.getUserAttendanceResultByDay(dto));
    }

    @ApiOperation("家长端/教师我的考勤（周和月）-获取某时间段内用户考勤结果统计")
    @PostMapping("/getUserAttendanceResultByTimeSlot")
    public ResultBean<UserAttendanceResultByTimeSlotVO> getUserAttendanceResultByTimeSlot(
        @Validated @RequestBody AttendanceResultByStartAndEndTimeDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.getUserAttendanceResultByTimeSlot(dto));
    }

    @ApiOperation("教师端-管理人员考勤状态-日历显示")
    @PostMapping("/findTotalUserStatus")
    public ResultBean<List<DateStatusVO>> findTotalUserStatus(
        @Validated @RequestBody ManageListUserDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.findTotalUserStatus(dto));
    }

    @ApiOperation("教师端-管理人员考勤统计")
    @PostMapping("/findManageListUserVO")
    public ResultBean<List<ManageListUserVO>> findManageListUserVO(
        @Validated @RequestBody ManageListUserDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.findManageListUserVO(dto));
    }

    @ApiOperation("教师端-查看明细")
    @PostMapping("/findManageListUserDetailVO")
    public ResultBean<JSONObject> findManageListUserDetailVO(
        @Validated @RequestBody ManageListUserDTO dto) {
        return ResultBean.buildSuccess(attendanceResultService.findManageListUserDetailVO(dto));
    }

    @ApiOperation("教师端-用户管理的家校部门树")
    @GetMapping("/findManageUserDeptTree")
    @Cacheable(value = CachePrefixConstant.ATTENDANCE_USER_MANAGE_TREE, key = "#userId+''+#searchUserType")
    public ResultBean<List> findManageUserDeptTree(@RequestParam Long userId, @RequestParam Integer searchUserType) {
        return ResultBean.buildSuccess(attendanceResultService.findManageUserDeptTree(userId, searchUserType));
    }

    @ApiOperation("教师端-职工身份")
    @GetMapping("/findStaffIdentity")
    @Cacheable(value = CachePrefixConstant.ATTENDANCE_STAFF_IDENTITY, key = "#userId")
    public ResultBean<StaffIdentityVO> findStaffIdentity(@RequestParam Long userId) {
        return ResultBean.buildSuccess(attendanceResultService.findStaffIdentity(userId));
    }

    @ApiOperation("教师端-查询用户管理规则")
    @GetMapping("/findUserManageRuleList")
    @Cacheable(value = CachePrefixConstant.ATTENDANCE_STAFF_MANAGE_RULE, key = "#userId + '' + #userType")
    public ResultBean<List<AttendanceRuleVO>> findUserManageRuleList(@RequestParam Long userId, @RequestParam Integer userType) {
        return ResultBean.buildSuccess(attendanceRuleService.findUserManageRuleList(userId, userType));
    }


    @ApiOperation("教师端-获取日报详情")
    @GetMapping("/getDayReport")
    public ResultBean<List<DayReportDataVO>> getDayReport(@RequestParam Long staffId, @RequestParam Integer searchUserType,
        @RequestParam Long ruleId, @RequestParam String attendanceDateStr) {
        return ResultBean.buildSuccess(attendanceRuleService.getDayReport(staffId, searchUserType, ruleId, attendanceDateStr));
    }
}
