package jnpf.workflow.controller;

import jnpf.base.ActionResult;
import jnpf.constant.MsgCode;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.entity.workflow.SelfApprove;
import jnpf.model.workflow.dto.SelfApproveDto;
import jnpf.model.workflow.vo.SelfApproveVo;
import jnpf.util.JsonUtil;
import jnpf.workflow.service.SelfApproveService;
import org.springframework.web.bind.annotation.*;

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

/**
 * describe
 * 借调审批控制器
 * @author HuangLinPan
 * @date 2023/12/13
 */
@RestController
@RequestMapping(value = "/attendanceApprove/selfApprove")
public class SelfApproveController {

    @Resource
    private SelfApproveService selfApproveService;

    /**
     * 新建借调审批
     * @param selfApproveDto 表单对象
     * @return java.lang.Object
     */
    @PostMapping("/{id}")
    public Object create(@RequestBody @Valid SelfApproveDto selfApproveDto, @PathVariable("id") String id) {
        if (selfApproveDto.getGroupId().equals(selfApproveDto.getSelfGroupId())){
            return ActionResult.fail("不能同组借调");
        }
        SelfApprove entity = JsonUtil.getJsonToBean(selfApproveDto, SelfApprove.class);
        if (FlowStatusEnum.save.getMessage().equals(selfApproveDto.getSubmitStatus())) {
            selfApproveService.save(id, entity, selfApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        selfApproveService.submit(id, entity, selfApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }




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

        SelfApproveVo vo = selfApproveService.getAttendanceLeave(id);
        return ActionResult.success(vo);
    }


    /**
     * 修改借调审批
     * @param selfApproveDto 表单对象
     * @param id 主键
     * @return java.lang.Object
     */
    @PutMapping("/{id}")
    public Object update(@RequestBody @Valid SelfApproveDto selfApproveDto, @PathVariable("id") String id) {
        if (selfApproveDto.getGroupId().equals(selfApproveDto.getSelfGroupId())){
            return ActionResult.fail("不能同组借调");
        }
        SelfApprove entity = JsonUtil.getJsonToBean(selfApproveDto, SelfApprove.class);
        entity.setId(id);
        if (FlowStatusEnum.save.getMessage().equals(selfApproveDto.getSubmitStatus())) {
            selfApproveService.save(id, entity, selfApproveDto);
            return ActionResult.success(MsgCode.SU002.get());
        }
        selfApproveService.submit(id, entity, selfApproveDto);
        return ActionResult.success(MsgCode.SU006.get());
    }
}
