package cn.iocoder.yudao.module.pay.controller.admin.wxorder;

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.constraints.*;
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.*;

import cn.iocoder.yudao.module.pay.controller.admin.wxorder.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxorder.WxOrderDO;
import cn.iocoder.yudao.module.pay.service.wxorder.WxOrderService;

@Tag(name = "管理后台 - 微信订单")
@RestController
@RequestMapping("/pay/wx-order")
@Validated
public class WxOrderController {

    @Resource
    private WxOrderService wxOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建微信订单")
    @PreAuthorize("@ss.hasPermission('pay:wx-order:create')")
    public CommonResult<Long> createWxOrder(@Valid @RequestBody WxOrderSaveReqVO createReqVO) {
        return success(wxOrderService.createWxOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信订单")
    @PreAuthorize("@ss.hasPermission('pay:wx-order:update')")
    public CommonResult<Boolean> updateWxOrder(@Valid @RequestBody WxOrderSaveReqVO updateReqVO) {
        wxOrderService.updateWxOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:wx-order:delete')")
    public CommonResult<Boolean> deleteWxOrder(@RequestParam("id") Long id) {
        wxOrderService.deleteWxOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wx-order:query')")
    public CommonResult<WxOrderRespVO> getWxOrder(@RequestParam("id") Long id) {
        WxOrderDO wxOrder = wxOrderService.getWxOrder(id);
        return success(BeanUtils.toBean(wxOrder, WxOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信订单分页")
    @PreAuthorize("@ss.hasPermission('pay:wx-order:query')")
    public CommonResult<PageResult<WxOrderRespVO>> getWxOrderPage(@Valid WxOrderPageReqVO pageReqVO) {
        PageResult<WxOrderDO> pageResult = wxOrderService.getWxOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信订单 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wx-order:export')")
    @OperateLog(type = EXPORT)
    public void exportWxOrderExcel(@Valid WxOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxOrderDO> list = wxOrderService.getWxOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信订单.xls", "数据", WxOrderRespVO.class,
                        BeanUtils.toBean(list, WxOrderRespVO.class));
    }

}