package cn.iocoder.yudao.module.member.controller.admin.invoiceinformation;

import cn.iocoder.yudao.module.member.controller.admin.invoiceinformation.vo.InvoiceInformationPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.invoiceinformation.vo.InvoiceInformationRespVO;
import cn.iocoder.yudao.module.member.controller.admin.invoiceinformation.vo.InvoiceInformationSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.invoiceinformation.InvoiceInformationDO;
import cn.iocoder.yudao.module.member.service.invoiceinformation.InvoiceInformationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 用户发票信息")
@RestController
@RequestMapping("/t/invoice-information")
@Validated
public class InvoiceInformationController {

    @Resource
    private InvoiceInformationService invoiceInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建用户发票信息")
    @PreAuthorize("@ss.hasPermission('t:invoice-information:create')")
    public CommonResult<Long> createInvoiceInformation(@Valid @RequestBody InvoiceInformationSaveReqVO createReqVO) {
        return success(invoiceInformationService.createInvoiceInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户发票信息")
    @PreAuthorize("@ss.hasPermission('t:invoice-information:update')")
    public CommonResult<Boolean> updateInvoiceInformation(@Valid @RequestBody InvoiceInformationSaveReqVO updateReqVO) {
        invoiceInformationService.updateInvoiceInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户发票信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:invoice-information:delete')")
    public CommonResult<Boolean> deleteInvoiceInformation(@RequestParam("id") Long id) {
        invoiceInformationService.deleteInvoiceInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户发票信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:invoice-information:query')")
    public CommonResult<InvoiceInformationRespVO> getInvoiceInformation(@RequestParam("id") Long id) {
        InvoiceInformationDO invoiceInformation = invoiceInformationService.getInvoiceInformation(id);
        return success(BeanUtils.toBean(invoiceInformation, InvoiceInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户发票信息分页")
    @PreAuthorize("@ss.hasPermission('t:invoice-information:query')")
    public CommonResult<PageResult<InvoiceInformationRespVO>> getInvoiceInformationPage(@Valid InvoiceInformationPageReqVO pageReqVO) {
        PageResult<InvoiceInformationDO> pageResult = invoiceInformationService.getInvoiceInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户发票信息 Excel")
    @PreAuthorize("@ss.hasPermission('t:invoice-information:export')")
    @OperateLog(type = EXPORT)
    public void exportInvoiceInformationExcel(@Valid InvoiceInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceInformationDO> list = invoiceInformationService.getInvoiceInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户发票信息.xls", "数据", InvoiceInformationRespVO.class,
                        BeanUtils.toBean(list, InvoiceInformationRespVO.class));
    }

}