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

import cn.iocoder.yudao.module.member.controller.admin.billing.vo.BillingPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.billing.vo.BillingRespVO;
import cn.iocoder.yudao.module.member.controller.admin.billing.vo.BillingSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.billing.BillingDO;
import cn.iocoder.yudao.module.member.service.billing.BillingService;
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/billing")
@Validated
public class BillingController {

    @Resource
    private BillingService billingService;

    @PostMapping("/create")
    @Operation(summary = "创建开票记录")
    @PreAuthorize("@ss.hasPermission('t:billing:create')")
    public CommonResult<String> createBilling(@Valid @RequestBody BillingSaveReqVO createReqVO) {
        return success(billingService.createBillingUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新开票记录")
    @PreAuthorize("@ss.hasPermission('t:billing:update')")
    public CommonResult<Boolean> updateBilling(@Valid @RequestBody BillingSaveReqVO updateReqVO) {
        billingService.updateBillingUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除开票记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:billing:delete')")
    public CommonResult<Boolean> deleteBilling(@RequestParam("id") String id) {
        billingService.deleteBilling(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得开票记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:billing:query')")
    public CommonResult<BillingRespVO> getBilling(@RequestParam("id") String id) {
        BillingDO billing = billingService.getBilling(id);
        return success(BeanUtils.toBean(billing, BillingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得开票记录分页")
    @PreAuthorize("@ss.hasPermission('t:billing:query')")
    public CommonResult<PageResult<BillingRespVO>> getBillingPage(@Valid BillingPageReqVO pageReqVO) {
        PageResult<BillingDO> pageResult = billingService.getBillingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出开票记录 Excel")
    @PreAuthorize("@ss.hasPermission('t:billing:export')")
    @OperateLog(type = EXPORT)
    public void exportBillingExcel(@Valid BillingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillingDO> list = billingService.getBillingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "开票记录.xls", "数据", BillingRespVO.class,
                        BeanUtils.toBean(list, BillingRespVO.class));
    }

}