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

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.invoicelog.vo.InvoiceLogPageReqVO;
import com.yc.ship.module.trade.controller.admin.invoicelog.vo.InvoiceLogRespVO;
import com.yc.ship.module.trade.dal.dataobject.invoice.InvoiceLogDO;
import com.yc.ship.module.trade.service.invoice.InvoiceLogService;
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-log")
@Validated
public class InvoiceLogController {

    @Resource
    private InvoiceLogService invoiceLogService;


    @GetMapping("/get")
    @Operation(summary = "获得发票记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:invoice-log:query')")
    @OperateLog(type = API,enable = false)
    public CommonResult<InvoiceLogRespVO> getInvoiceLog(@RequestParam("id") Long id) {
        InvoiceLogDO invoiceLog = invoiceLogService.getInvoiceLog(id);
        return success(BeanUtils.toBean(invoiceLog, InvoiceLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发票记录分页")
    @PreAuthorize("@ss.hasPermission('trade:invoice-log:query')")
    @OperateLog(type = API,enable = false)
    public CommonResult<PageResult<InvoiceLogRespVO>> getInvoiceLogPage(@Valid InvoiceLogPageReqVO pageReqVO) {
        PageResult<InvoiceLogDO> pageResult = invoiceLogService.getInvoiceLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发票记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:invoice-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportInvoiceLogExcel(@Valid InvoiceLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceLogDO> list = invoiceLogService.getInvoiceLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "发票记录.xls", "数据", InvoiceLogRespVO.class,
                        BeanUtils.toBean(list, InvoiceLogRespVO.class));
    }

}