package com.yolo.keyboard.controller.admin.tenantwithdraworder;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.tenantwithdraworder.vo.*;
import com.yolo.keyboard.dal.dataobject.tenantwithdraworder.KeyboardTenantWithdrawOrderDO;
import com.yolo.keyboard.service.tenantwithdraworder.KeyboardTenantWithdrawOrderService;

@Tag(name = "管理后台 - 租户提现订单表（申请-审核-打款-完成/失败）")
@RestController
@RequestMapping("/keyboard/tenant-withdraw-order")
@Validated
public class KeyboardTenantWithdrawOrderController {

    @Resource
    private KeyboardTenantWithdrawOrderService tenantWithdrawOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建租户提现订单表（申请-审核-打款-完成/失败）")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:create')")
    public CommonResult<Long> createTenantWithdrawOrder(@Valid @RequestBody KeyboardTenantWithdrawOrderSaveReqVO createReqVO) {
        return success(tenantWithdrawOrderService.createTenantWithdrawOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户提现订单表（申请-审核-打款-完成/失败）")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:update')")
    public CommonResult<Boolean> updateTenantWithdrawOrder(@Valid @RequestBody KeyboardTenantWithdrawOrderSaveReqVO updateReqVO) {
        tenantWithdrawOrderService.updateTenantWithdrawOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户提现订单表（申请-审核-打款-完成/失败）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:delete')")
    public CommonResult<Boolean> deleteTenantWithdrawOrder(@RequestParam("id") Long id) {
        tenantWithdrawOrderService.deleteTenantWithdrawOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户提现订单表（申请-审核-打款-完成/失败）")
                @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:delete')")
    public CommonResult<Boolean> deleteTenantWithdrawOrderList(@RequestParam("ids") List<Long> ids) {
        tenantWithdrawOrderService.deleteTenantWithdrawOrderListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户提现订单表（申请-审核-打款-完成/失败）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:query')")
    public CommonResult<KeyboardTenantWithdrawOrderRespVO> getTenantWithdrawOrder(@RequestParam("id") Long id) {
        KeyboardTenantWithdrawOrderDO tenantWithdrawOrder = tenantWithdrawOrderService.getTenantWithdrawOrder(id);
        return success(BeanUtils.toBean(tenantWithdrawOrder, KeyboardTenantWithdrawOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户提现订单表（申请-审核-打款-完成/失败）分页")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:query')")
    public CommonResult<PageResult<KeyboardTenantWithdrawOrderRespVO>> getTenantWithdrawOrderPage(@Valid KeyboardTenantWithdrawOrderPageReqVO pageReqVO) {
        PageResult<KeyboardTenantWithdrawOrderRespVO> pageResult = tenantWithdrawOrderService.getTenantWithdrawOrderPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户提现订单表（申请-审核-打款-完成/失败） Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:tenant-withdraw-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantWithdrawOrderExcel(@Valid KeyboardTenantWithdrawOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardTenantWithdrawOrderRespVO> list = tenantWithdrawOrderService.getTenantWithdrawOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户提现订单表（申请-审核-打款-完成/失败）.xls", "数据", KeyboardTenantWithdrawOrderRespVO.class, list);
    }

}