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

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.wxrefund.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxrefund.WxRefundDO;
import cn.iocoder.yudao.module.pay.service.wxrefund.WxRefundService;

@Tag(name = "管理后台 - 微信退款")
@RestController
@RequestMapping("/pay/wx-refund")
@Validated
public class WxRefundController {

    @Resource
    private WxRefundService wxRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建微信退款")
    @PreAuthorize("@ss.hasPermission('pay:wx-refund:create')")
    public CommonResult<Long> createWxRefund(@Valid @RequestBody WxRefundSaveReqVO createReqVO) {
        return success(wxRefundService.createWxRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信退款")
    @PreAuthorize("@ss.hasPermission('pay:wx-refund:update')")
    public CommonResult<Boolean> updateWxRefund(@Valid @RequestBody WxRefundSaveReqVO updateReqVO) {
        wxRefundService.updateWxRefund(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得微信退款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:wx-refund:query')")
    public CommonResult<WxRefundRespVO> getWxRefund(@RequestParam("id") Long id) {
        WxRefundDO wxRefund = wxRefundService.getWxRefund(id);
        return success(BeanUtils.toBean(wxRefund, WxRefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信退款分页")
    @PreAuthorize("@ss.hasPermission('pay:wx-refund:query')")
    public CommonResult<PageResult<WxRefundRespVO>> getWxRefundPage(@Valid WxRefundPageReqVO pageReqVO) {
        PageResult<WxRefundDO> pageResult = wxRefundService.getWxRefundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxRefundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信退款 Excel")
    @PreAuthorize("@ss.hasPermission('pay:wx-refund:export')")
    @OperateLog(type = EXPORT)
    public void exportWxRefundExcel(@Valid WxRefundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxRefundDO> list = wxRefundService.getWxRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信退款.xls", "数据", WxRefundRespVO.class,
                        BeanUtils.toBean(list, WxRefundRespVO.class));
    }

}