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

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.member.controller.admin.storerefund.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storerefund.StoreRefundDO;
import cn.iocoder.yudao.module.member.service.storerefund.StoreRefundService;

@Tag(name = "管理后台 - 超市订单退款审核")
@RestController
@RequestMapping("/member/store-refund")
@Validated
public class StoreRefundController {

    @Resource
    private StoreRefundService storeRefundService;

    @PostMapping("/create")
    @Operation(summary = "创建超市订单退款审核")
    @PreAuthorize("@ss.hasPermission('member:store-refund:create')")
    public CommonResult<Long> createStoreRefund(@Valid @RequestBody StoreRefundSaveReqVO createReqVO) {
        return success(storeRefundService.createStoreRefund(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新超市订单退款审核")
    @PreAuthorize("@ss.hasPermission('member:store-refund:update')")
    public CommonResult<Boolean> updateStoreRefund(@Valid @RequestBody StoreRefundSaveReqVO updateReqVO) {
        storeRefundService.updateStoreRefund(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除超市订单退款审核")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-refund:delete')")
    public CommonResult<Boolean> deleteStoreRefund(@RequestParam("id") Long id) {
        storeRefundService.deleteStoreRefund(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得超市订单退款审核")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-refund:query')")
    public CommonResult<StoreRefundRespVO> getStoreRefund(@RequestParam("id") Long id) {
        StoreRefundDO storeRefund = storeRefundService.getStoreRefund(id);
        return success(BeanUtils.toBean(storeRefund, StoreRefundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得超市订单退款审核分页")
    @PreAuthorize("@ss.hasPermission('member:store-refund:query')")
    public CommonResult<PageResult<StoreRefundRespVO>> getStoreRefundPage(@Valid StoreRefundPageReqVO pageReqVO) {
        PageResult<StoreRefundDO> pageResult = storeRefundService.getStoreRefundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreRefundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出超市订单退款审核 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-refund:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreRefundExcel(@Valid StoreRefundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreRefundDO> list = storeRefundService.getStoreRefundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "超市订单退款审核.xls", "数据", StoreRefundRespVO.class,
                        BeanUtils.toBean(list, StoreRefundRespVO.class));
    }

}