package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.invoice.BpmOAInvoiceCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.invoice.BpmOAInvoicePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.invoice.BpmOAInvoiceRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAInvoiceDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAInvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 开票申请 Controller
 *
 * @author 符溶馨

 */
@Tag(name = "管理后台 - OA 开票申请")
@RestController
@RequestMapping("/bpm/oa/invoice")
@Validated
public class BpmOAInvoiceController {

    @Resource
    private BpmOAInvoiceService invoiceService;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createInvoice(@Valid @RequestBody BpmOAInvoiceCreateReqVO createReqVO) {

        return success(invoiceService.createInvoice(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得开票申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAInvoiceRespVO> getInvoice(@RequestParam("id") Long id) {

        BpmOAInvoiceDO invoice = invoiceService.getInvoice(id);

        BpmOAInvoiceRespVO bpmOAInvoiceRespVO = BeanUtils.toBean(invoice, BpmOAInvoiceRespVO.class);
        return success(bpmOAInvoiceRespVO);
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得开票申请")
    @Parameter(name = "BpmOAInvoiceRespVO", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOAInvoiceRespVO> getByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOAInvoiceDO invoice = invoiceService.getByProcessInstanceId(processInstanceId);

        BpmOAInvoiceRespVO bpmOAInvoiceRespVO = BeanUtils.toBean(invoice, BpmOAInvoiceRespVO.class);
        return success(bpmOAInvoiceRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得开票申请分页列表")
    public CommonResult<PageResult<BpmOAInvoiceRespVO>> getPage(@Valid BpmOAInvoicePageReqVO pageReqVO) {

        return success(invoiceService.getInvoicePage(pageReqVO));
    }
}
