package jnpf.attendance.controller;

import jnpf.attendance.service.AttendanceChangeService;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.enums.personnel.FtbPersonnelsCheckStatusCodeEnum;
import jnpf.model.attendance.dto.NoApprovalDto;
import jnpf.model.attendance.vo.ChangeInfoVo;
import jnpf.util.UserProvider;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import javax.validation.Valid;

/**
 * 考勤变更控制器
 *
 * @author yanwenfu
 * @create 2024-11-08
 */
@RestController
@RequestMapping(value = "/change")
public class AttendanceChangeController {

    @Resource
    private AttendanceChangeService attendanceChangeService;

    /**
     * 被变更记录的信息
     * @param clockInResultId 结果id
     * @return java.lang.Object
     */
    @GetMapping(value = "/info/{clockInResultId}")
    public Object getChangeInfo(@PathVariable(value = "clockInResultId") String clockInResultId) throws Exception {

        ChangeInfoVo info = attendanceChangeService.getChangeInfo(clockInResultId);
        return ActionResult.success(info);
    }

    /**
     * 根据考勤组id及用户id查询过去一年内的变更涉及打卡记录
     * @param groupId 考勤组id
     * @param userId 用户id
     * @return jnpf.base.ActionResult
     */
    @GetMapping(value = "/getChangeInfoList")
    public ActionResult<List<ChangeInfoVo>> getChangeInfoList(@RequestParam String groupId,@RequestParam String userId) {
        List<ChangeInfoVo> infos = attendanceChangeService.getChangeInfoList(groupId, userId);
        return ActionResult.success(infos);
    }

    /**
     * 出勤变更(无需审批)
     * @param noApprovalDto 出勤变更参数
     * @return java.lang.Object
     */
    @PostMapping(value = "/noApproval")
    public Object attendanceChangeNoApproval(@RequestBody @Valid NoApprovalDto noApprovalDto) throws Exception {

        attendanceChangeService.attendanceChangeNoApproval(noApprovalDto);

        return ActionResult.success();
    }

    /**
     * 出勤变更(需审批)
     * @param taskId 审批id
     * @param passed 是否通过(0: 否, 1: 是, 2: 撤回)
     * @return java.lang.Object
     */
    @GetMapping(value = "/approval")
    public Object attendanceChangeApproval(@RequestParam(value = "taskId") String taskId, @RequestParam(value = "passed") String passed) {

        UserInfo user = UserProvider.getUser();
        ActionResult<Void> actionResult = new ActionResult<>();
        try {
            attendanceChangeService.attendanceChangeApproval(taskId, passed, user);
        } catch (Exception e) {
            actionResult.setCode(FtbPersonnelsCheckStatusCodeEnum.CHECK_REFUSE.getCode());
            actionResult.setMsg(e.getMessage());
            return actionResult;
        }
        actionResult.setCode(FtbPersonnelsCheckStatusCodeEnum.CHECK_PASS.getCode());
        actionResult.setMsg(FtbPersonnelsCheckStatusCodeEnum.CHECK_PASS.getMsg());
        return actionResult;
    }

    /**
     * 出勤变更撤回
     * @param clockInResultId 打卡结果id
     * @return java.lang.Object
     */
    @PutMapping(value = "/rollback/{clockInResultId}")
    public Object rollbackChange(@PathVariable(value = "clockInResultId") String clockInResultId) throws Exception {

        attendanceChangeService.rollbackChange(clockInResultId);
        return ActionResult.success();
    }

    /**
     * 查询能否撤回变更
     * @param clockInResultId 打卡结果id
     * @return java.lang.Object
     */
    @GetMapping(value = "/rollback/status")
    public Object getRollbackStatus(@RequestParam(value = "clockInResultId") String clockInResultId) {

        Integer status = attendanceChangeService.getRollbackStatus(clockInResultId);
        return ActionResult.success(status);
    }
}
