package cn.superdesk.uniorder.openapi.api._thirdpart.qchjc.invoice;

import cn.superdesk.uniorder.common.exception.OpenApiException;
import cn.superdesk.uniorder.openapi.annotation.QchjcRequestLog;
import cn.superdesk.uniorder.openapi.api._thirdpart.qchjc._dto.invoice.QchjcSupplierInvoiceDto;
import cn.superdesk.uniorder.openapi.api.base.O;
import cn.superdesk.uniorder.openapi.config.exception.ParameterNotValidException;
import cn.superdesk.uniorder.openapi.service.qchjc.invoice.SupplierInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xc
 * @description
 * @date 2022/3/31 15:03
 */
@Api(value = "thirdpart-qchjc-invoice-supplier", tags = {"thirdpart-qchjc-invoice-supplier"})
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/v0/thirdpart/qchjc/invoice/supplier")
public class SupplierInvoiceRestApi {

    private final SupplierInvoiceService supplierInvoiceService;

    @ApiOperation(value = "供应商发票单接收")
    @RequestMapping(value = "/receive", method = RequestMethod.POST)
    @ResponseBody
    @QchjcRequestLog(value = "供应商发票单接收")
    public O<QchjcSupplierInvoiceDto> invoiceReceive(@RequestBody @Validated QchjcSupplierInvoiceDto dto, BindingResult result) throws OpenApiException {

        if (result.hasErrors()) {
            throw new ParameterNotValidException(result);
        }

        //log.info("receive qchjc supplier invoice apply, param : {}", JSON.toJSONString(dto));
        supplierInvoiceService.receive(dto);
        return new O<QchjcSupplierInvoiceDto>()
                .setResultCode(200)
                .setResultMessage("同步成功")
                .setSuccess(true);
    }
}
