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

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.wxstoreorder.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.wxstoreorder.WxStoreOrderDO;
import cn.iocoder.yudao.module.member.service.wxstoreorder.WxStoreOrderService;

@Tag(name = "管理后台 - 微信付款码订单")
@RestController
@RequestMapping("/member/wx-store-order")
@Validated
public class WxStoreOrderController {

    @Resource
    private WxStoreOrderService wxStoreOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建微信付款码订单")
    @PreAuthorize("@ss.hasPermission('member:wx-store-order:create')")
    public CommonResult<Long> createWxStoreOrder(@Valid @RequestBody WxStoreOrderSaveReqVO createReqVO) {
        return success(wxStoreOrderService.createWxStoreOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信付款码订单")
    @PreAuthorize("@ss.hasPermission('member:wx-store-order:update')")
    public CommonResult<Boolean> updateWxStoreOrder(@Valid @RequestBody WxStoreOrderSaveReqVO updateReqVO) {
        wxStoreOrderService.updateWxStoreOrder(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得微信付款码订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:wx-store-order:query')")
    public CommonResult<WxStoreOrderRespVO> getWxStoreOrder(@RequestParam("id") Long id) {
        WxStoreOrderDO wxStoreOrder = wxStoreOrderService.getWxStoreOrder(id);
        return success(BeanUtils.toBean(wxStoreOrder, WxStoreOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信付款码订单分页")
    @PreAuthorize("@ss.hasPermission('member:wx-store-order:query')")
    public CommonResult<PageResult<WxStoreOrderRespVO>> getWxStoreOrderPage(@Valid WxStoreOrderPageReqVO pageReqVO) {
        PageResult<WxStoreOrderDO> pageResult = wxStoreOrderService.getWxStoreOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxStoreOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信付款码订单 Excel")
    @PreAuthorize("@ss.hasPermission('member:wx-store-order:export')")
    @OperateLog(type = EXPORT)
    public void exportWxStoreOrderExcel(@Valid WxStoreOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxStoreOrderDO> list = wxStoreOrderService.getWxStoreOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信付款码订单.xls", "数据", WxStoreOrderRespVO.class,
                        BeanUtils.toBean(list, WxStoreOrderRespVO.class));
    }

}