package com.hz.pm.api.leave.controller;


import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.model.IdVo;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.log.annotation.WebLog;
import com.hz.pm.api.leave.entity.po.LeaveCreateReq;
import com.hz.pm.api.leave.entity.po.LeaveListByCreatorReq;
import com.hz.pm.api.leave.entity.vo.LeaveDetailVO;
import com.hz.pm.api.leave.entity.vo.LeaveListItemVO;
import com.hz.pm.api.leave.manage.LeaveManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WendyYang
 * @since 2022-08-11
 */
@Api(tags = "请假管理控制器")
@RestController
@AllArgsConstructor
@RequestMapping("/api/v1/leave/")
public class ExpertLeaveController {

    private final LeaveManage leaveManage;

    @ApiOperation("请假")
    @PostMapping("/save")
    @WebLog("请假")
    public IdVo<Long> askForLeave(@Valid @RequestBody LeaveCreateReq po) {
        return leaveManage.askForLeave(po);
    }

    @ApiOperation("请假详情")
    @GetMapping(value = {"/detail/{leaveId}", "/detail/{auditId}/byApply"})
    public LeaveDetailVO detail(@PathVariable(required = false) Long leaveId,
                                @PathVariable(required = false) Long auditId) {
        if (ObjectUtils.allNull(leaveId, auditId)) {
            throw BizException.wrap("请假ID或申请ID不能为空");
        }
        return leaveManage.leaveDetail(leaveId, auditId);
    }

    @ApiOperation("我的请假列表(发起人)")
    @WebLog("我的请假列表(发起人)")
    @GetMapping("/leaveListByCreator")
    public PageVo<LeaveListItemVO> leaveListByCreator(LeaveListByCreatorReq po) {
        return leaveManage.leaveListByCreator(po);
    }

    @ApiOperation("销假")
    @WebLog("销假")
    @GetMapping("/endLeave")
    public void endLeave(Long leaveId) {
        leaveManage.endLeave(leaveId);
    }

    @ApiOperation("撤销")
    @WebLog("撤销")
    @GetMapping("/cancel")
    public void cancelLeave(Long leaveId) {
        leaveManage.cancel(leaveId);
    }

}
