package jnpf.workflow.controller;

import jnpf.base.ActionResult;
import jnpf.constant.MsgCode;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.entity.workflow.AttendanceLeaveApprove;
import jnpf.model.workflow.dto.AttendanceLeaveApproveDto;
import jnpf.model.workflow.vo.AttendanceLeaveFlowApproveVo;
import jnpf.util.JsonUtil;
import jnpf.workflow.service.LeaveApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * describe
 *
 * @author HuangLinPan
 * @date 2023/12/11
 */
@Slf4j
@RestController
@RequestMapping(value = "/attendanceApprove/leave")
public class LeaveApproveController {

    @Resource
    private LeaveApproveService leaveApproveService;

    /**
     * 新建请假申请
     * @param attendanceLeaveApproveDto 表单对象
     * @return java.lang.Object
     */
    @PostMapping("/{id}")
    public Object create(@RequestBody @Valid AttendanceLeaveApproveDto attendanceLeaveApproveDto, @PathVariable("id") String id) {
        log.error("开始请假了................");
        log.error("开始请假了参数................: {}",attendanceLeaveApproveDto);
        AttendanceLeaveApprove entity = JsonUtil.getJsonToBean(attendanceLeaveApproveDto, AttendanceLeaveApprove.class);
        if (FlowStatusEnum.save.getMessage().equals(attendanceLeaveApproveDto.getSubmitStatus())) {
            leaveApproveService.save(id, entity, attendanceLeaveApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        leaveApproveService.submit(id, entity, attendanceLeaveApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }




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

        AttendanceLeaveFlowApproveVo vo = leaveApproveService.getAttendanceLeave(id);
        return ActionResult.success(vo);
    }


    /**
     * 修改请假申请
     * @param attendanceLeaveApproveDto 表单对象
     * @param id 主键
     * @return java.lang.Object
     */
    @PutMapping("/{id}")
    public Object update(@RequestBody @Valid AttendanceLeaveApproveDto attendanceLeaveApproveDto, @PathVariable("id") String id) {

        AttendanceLeaveApprove entity = JsonUtil.getJsonToBean(attendanceLeaveApproveDto, AttendanceLeaveApprove.class);
        entity.setId(id);
        if (FlowStatusEnum.save.getMessage().equals(attendanceLeaveApproveDto.getSubmitStatus())) {
            leaveApproveService.save(id, entity, attendanceLeaveApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        leaveApproveService.submit(id, entity, attendanceLeaveApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }

}
