package jnpf.workflow.controller;

import jnpf.base.ActionResult;
import jnpf.constant.MsgCode;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.entity.workflow.AttendanceWorkOvertimeApprove;
import jnpf.model.workflow.dto.AttendanceWorkOvertimeApproveDto;
import jnpf.model.workflow.vo.AttendanceWorkOvertimeApproveVo;
import jnpf.util.JsonUtil;
import jnpf.util.UserProvider;
import jnpf.workflow.service.WorkOvertimeApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * describe
 * 加班审批控制器
 * @author HuangLinPan
 * @date 2023/12/12
 */

@Slf4j
@RestController
@RequestMapping(value = "/attendanceApprove/workOvertime")
public class WorkOvertimeApproveController {

    @Resource
    private WorkOvertimeApproveService workOvertimeApproveService;
    @Autowired
    private UserProvider userProvider;

    /**
     * 新建加班申请
     * @param attendanceWorkOvertimeApproveDto 表单对象
     * @return java.lang.Object
     */
    @PostMapping("/{id}")
    public Object create(@RequestBody @Valid AttendanceWorkOvertimeApproveDto attendanceWorkOvertimeApproveDto, @PathVariable("id") String id) {
        log.error("新建加班申请 参数 : {}",attendanceWorkOvertimeApproveDto);
        String userId = userProvider.get().getUserId();
        attendanceWorkOvertimeApproveDto.setUserId(userId);
        AttendanceWorkOvertimeApprove entity = JsonUtil.getJsonToBean(attendanceWorkOvertimeApproveDto, AttendanceWorkOvertimeApprove.class);
        if (FlowStatusEnum.save.getMessage().equals(attendanceWorkOvertimeApproveDto.getSubmitStatus())) {
            workOvertimeApproveService.save(id, entity, attendanceWorkOvertimeApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        workOvertimeApproveService.submit(id, entity, attendanceWorkOvertimeApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }




    /**
     * 查询加班审批记录
     * @param id 加班审批id
     * @return jnpf.base.ActionResult<jnpf.model.workflow.vo.ApplyAttendanceChangeVo>
     */
    @GetMapping(value = "/{id}")
    public ActionResult<AttendanceWorkOvertimeApproveVo> getAttendanceLeave(@PathVariable(value = "id") String id) {

        AttendanceWorkOvertimeApproveVo vo = workOvertimeApproveService.getAttendanceLeave(id);
        return ActionResult.success(vo);
    }


    /**
     * 修改加班申请
     * @param attendanceWorkOvertimeApproveDto 表单对象
     * @param id 主键
     * @return java.lang.Object
     */
    @PutMapping("/{id}")
    public Object update(@RequestBody @Valid AttendanceWorkOvertimeApproveDto attendanceWorkOvertimeApproveDto, @PathVariable("id") String id) {
        log.error("修改加班申请 参数 : {}",attendanceWorkOvertimeApproveDto);
//        String userId = userProvider.get().getUserId();
//        attendanceWorkOvertimeApproveDto.setUserId(userId);
        AttendanceWorkOvertimeApprove entity = JsonUtil.getJsonToBean(attendanceWorkOvertimeApproveDto, AttendanceWorkOvertimeApprove.class);
        entity.setId(id);
        if (FlowStatusEnum.save.getMessage().equals(attendanceWorkOvertimeApproveDto.getSubmitStatus())) {
            workOvertimeApproveService.save(id, entity, attendanceWorkOvertimeApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        workOvertimeApproveService.submit(id, entity, attendanceWorkOvertimeApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }
}
