package com.sundot.airport.web.controller.attendance.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.sundot.airport.attendance.domain.AttendanceRecord;
import com.sundot.airport.attendance.service.IAttendanceRecordService;
import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.entity.SysUser;
import com.sundot.airport.common.dto.AttendanceRecordDTO;
import com.sundot.airport.common.dto.AttendanceRecordReq;
import com.sundot.airport.common.dto.UserInfo;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.system.service.ISysConfigService;
import com.sundot.airport.web.core.cache.UserCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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;

import java.util.Date;
import java.util.List;

/**
 * 考勤管理 h5 api
 *
 * @Author: wangchong
 * @Date: 2025/7/10 14:49
 **/
@RestController
@RequestMapping("/attendance")
public class AttendanceController extends BaseController {

    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    @Autowired
    private UserCache userCache;

    /**
     * 打卡签到
     *
     * @param dto 打卡信息
     * @return 今日打卡历史
     */
    @Log(title = "打卡签到", businessType = BusinessType.INSERT)
    @PostMapping("/v1/record")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult record(@RequestBody AttendanceRecordDTO dto) {
        UserInfo userInfo = userCache.getUserInfo(dto.getUserId());
        return success(attendanceRecordService.record(dto, userInfo));
    }

    /**
     * 打卡签到列表
     */
    @PostMapping("/v1/record-list")
    public AjaxResult recordList(@RequestBody AttendanceRecordReq dto) {
        return success(attendanceRecordService.recordList(dto));
    }

    /**
     * 查询可打卡类型:1=签到,2=签退
     */
    @GetMapping("/v1/getRecordType")
    public AjaxResult getRecordType() {
        return AjaxResult.success("操作成功", attendanceRecordService.getRecordType());
    }

    /**
     * 根据角色标识查询今日打卡用户列表
     */
    @PostMapping("/selectUserListByRoleKey")
    public AjaxResult selectUserListByRoleKey(@RequestBody List<String> roleTypeList) {
        // 计算当前时间对应的班次日期
        Date queryDate = DateUtils.getNowDate();
        List<SysUser> result = attendanceRecordService.selectUserListByRoleKey(queryDate, roleTypeList);
        return AjaxResult.success(result);
    }

}
