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

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
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.admin.invoice.vo.InvoiceRespVO;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 电子发票")
@RestController
@RequestMapping("/trade/invoice")
@Validated
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;


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

    @GetMapping("/page")
    @Operation(summary = "获得电子发票分页")
    @PreAuthorize("@ss.hasPermission('trade:invoice:query')")
    @OperateLog(type = API)
    public CommonResult<PageResult<InvoiceRespVO>> getInvoicePage(@Valid InvoicePageReqVO pageReqVO) {
        PageResult<InvoiceRespVO> pageResult = invoiceService.getInvoicePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电子发票 Excel")
    @PreAuthorize("@ss.hasPermission('trade:invoice:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportInvoiceExcel(@Valid InvoicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List list = invoiceService.getInvoicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电子发票.xls", "数据", InvoiceRespVO.class,
                        BeanUtils.toBean(list, InvoiceRespVO.class));
    }

    @GetMapping("/queryInvoice")
    @Operation(summary = "发票查询")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @OperateLog(type = API,enable = false)
    public CommonResult<InvoiceRespVO> queryInvoice(@RequestParam("id") Long id) {
        InvoiceDO invoice = invoiceService.queryInvoice(id);
        return success(BeanUtils.toBean(invoice, InvoiceRespVO.class));
    }

    @GetMapping("/redInvoice")
    @Operation(summary = "冲红")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @OperateLog(type = API)
    public CommonResult<InvoiceRespVO> openRedInvoice(@RequestParam("id") Long id) {
        InvoiceDO invoice = invoiceService.redInvoice(id);
        return success(BeanUtils.toBean(invoice, InvoiceRespVO.class));
    }

}
