package com.yc.ship.module.trade.controller.app.invoice;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.admin.invoice.vo.InvoicePageReqVO;
import com.yc.ship.module.trade.controller.app.invoice.vo.AppInvoiceRespVO;
import com.yc.ship.module.trade.dal.dataobject.invoice.InvoiceDO;
import com.yc.ship.module.trade.service.invoice.InvoiceService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;


@Tag(name = "用户 APP - 电子发票")
@RestController
@RequestMapping("/trade/invoice")
@Validated
public class AppInvoiceController {

    @Resource
    private InvoiceService invoiceService;

    //TODO
//    @PostMapping("/apply-invoicing")
//    @Operation(summary = "开票")
//    @OperateLog(type = API)
//    public CommonResult createInvoice(@Valid @RequestBody InvoicingReqDTO reqDTO) {
//        if(reqDTO.getTitleType()==null) {
//            reqDTO.setTitleType(0);
//        }
//        return  invoiceService.applyInvoicing(reqDTO);
//    }


    @GetMapping("/get")
    @Operation(summary = "获得电子发票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @OperateLog(type = API)
    public CommonResult<AppInvoiceRespVO> getInvoice(@RequestParam("id") Long id) {
        InvoiceDO invoice = invoiceService.getInvoice(id);
        return success(BeanUtils.toBean(invoice, AppInvoiceRespVO.class));
    }

    @GetMapping("/page/list")
    @Operation(summary = "获得电子发票分页列表")
    @OperateLog(type = API)
    public CommonResult<PageResult<AppInvoiceRespVO>> getInvoicePageList(@Valid InvoicePageReqVO pageReqVO) {
        PageResult<InvoiceDO> pageResult = invoiceService.getAppInvoicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppInvoiceRespVO.class));
    }
}