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

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.refundrecord.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.refundrecord.RefundRecordDO;
import cn.iocoder.yudao.module.pay.service.refundrecord.RefundRecordService;

@Tag(name = "管理后台 - 退款记录")
@RestController
@RequestMapping("/pay/refund-record")
@Validated
public class RefundRecordController {

    @Resource
    private RefundRecordService refundRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建退款记录")
    @PreAuthorize("@ss.hasPermission('pay:refund-record:create')")
    public CommonResult<Long> createRefundRecord(@Valid @RequestBody RefundRecordSaveReqVO createReqVO) {
        return success(refundRecordService.createRefundRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新退款记录")
    @PreAuthorize("@ss.hasPermission('pay:refund-record:update')")
    public CommonResult<Boolean> updateRefundRecord(@Valid @RequestBody RefundRecordSaveReqVO updateReqVO) {
        refundRecordService.updateRefundRecord(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得退款记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:refund-record:query')")
    public CommonResult<RefundRecordRespVO> getRefundRecord(@RequestParam("id") Long id) {
        RefundRecordDO refundRecord = refundRecordService.getRefundRecord(id);
        return success(BeanUtils.toBean(refundRecord, RefundRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得退款记录分页")
    @PreAuthorize("@ss.hasPermission('pay:refund-record:query')")
    public CommonResult<PageResult<RefundRecordRespVO>> getRefundRecordPage(@Valid RefundRecordPageReqVO pageReqVO) {
        PageResult<RefundRecordDO> pageResult = refundRecordService.getRefundRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RefundRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出退款记录 Excel")
    @PreAuthorize("@ss.hasPermission('pay:refund-record:export')")
    @OperateLog(type = EXPORT)
    public void exportRefundRecordExcel(@Valid RefundRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RefundRecordDO> list = refundRecordService.getRefundRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退款记录.xls", "数据", RefundRecordRespVO.class,
                        BeanUtils.toBean(list, RefundRecordRespVO.class));
    }

}