package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.DateUtils;
import com.boman.domain.dto.AjaxResult;
import com.boman.domain.dto.Base64Dto;
import com.boman.domain.dto.ClockOnDto;
import com.boman.domain.dto.FormDataDto;
import com.boman.web.core.service.attendance.AttendanceService;
import com.boman.web.core.service.attendance.rules.AttendanceRulesService;
import com.boman.web.core.utils.LeaveFromUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import static com.boman.web.core.utils.LeaveFromUtils.packTimes;

/**
 * @author shiqian
 * @date 2021年06月03日 15:41
 **/
@RequestMapping("/attendance")
@RestController
public class AttendanceController {

    @Resource
    private AttendanceService service;
    @Resource
    private AttendanceRulesService rulesService;

    /**
     * 功能描述: 打卡
     *
     * @param dto tableName
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("/clockOn")
    public AjaxResult clockOn(@RequestBody ClockOnDto dto) {
        return AjaxResult.success(service.clockOn(dto));
    }

    /**
     * 功能描述: H5查看考勤记录接口 根据tableName和userId查找迟到、早退等
     *
     * @param tableName tableName
     * @param userId  userId
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/listByUserId/{tableName}/{userId}")
    public AjaxResult listByUserId(@PathVariable("tableName") String tableName, @PathVariable("userId") Long userId) {
        return service.attendanceRecord(tableName, userId);
    }


    /**
     * 功能描述: 显示是上班打卡还是下班打卡
     *
     * @param tableName tableName
     * @param userName  userName
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/showAttendanceType/{tableName}/{userName}")
    public AjaxResult showAttendanceType(@PathVariable("tableName") String tableName, @PathVariable("userName") String userName) {
        return AjaxResult.success(service.showAttendanceType(tableName, userName));
    }

    /**
     * 功能描述: 查询该人的一天打卡时间
     *
     * @param tableName tableName
     * @param userName  userName
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/attendanceTime/{tableName}/{userName}")
    public AjaxResult attendanceTime(@PathVariable("tableName") String tableName, @PathVariable("userName") String userName) {
        return AjaxResult.success(service.attendanceTime(tableName, userName));
    }


    /**
     * 功能描述: 根据tableName和userId查找当月考勤详细记录
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @PostMapping("/detailedRecord")
    public AjaxResult detailedRecord(@RequestBody FormDataDto dto) {
        return service.detailedRecord(dto);
    }

    /**
     * 按月统计人员考勤信息
     *
     * @param map 传入时间 年-月
     * @return
     */
    @PostMapping("/statistics")
    public AjaxResult statisticsByMonth(@RequestBody Map<String, Object> map) {
        return service.statisticsByMonth(map);
    }


    /**
     * 查询用户某月的上班打卡时间
     *
     * @param date   查询的年-月
     * @param userId 用户id
     * @return
     */
    @GetMapping("/findDate/{date}/{userId}")
    public AjaxResult findDate(@PathVariable("date") String date, @PathVariable("userId") Long userId) {
        return service.findDate(date, userId);
    }

    @PostMapping("/updateAttendance")
    public AjaxResult updateAttendance(@RequestBody FormDataDto dto) {
        return service.updateAttendance(dto);
    }

    @PostMapping("/calcTime")
    public AjaxResult test(@RequestBody JSONObject dto) {
        Date startTime = dto.getDate("leavefrom_start_time");
        Date endTime = dto.getDate("leavefrom_end_time");
        Long deptId = dto.getLong("dept_id");
        JSONObject rule = rulesService.listByDeptId(deptId);
        long minutes = LeaveFromUtils.calcTimes(startTime, endTime, null, rule);
        return AjaxResult.success("成功", packTimes(minutes));
    }
    
}
