package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ss.vipOrder.domain.VipOrderQuery;
import com.ruoyi.ss.vipOrder.domain.VipOrderVO;
import com.ruoyi.ss.vipOrder.domain.bo.AddPayVipOrderBO;
import com.ruoyi.ss.vipOrder.domain.dto.AddPayVipOrderDTO;
import com.ruoyi.ss.vipOrder.service.VipOrderConverter;
import com.ruoyi.ss.vipOrder.service.VipOrderService;
import com.ruoyi.ss.vipOrder.service.VipOrderValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author wjh
 * 2025/1/17
 */
@RestController
@RequestMapping("/app/vipOrder")
public class AppVipOrderController extends BaseController {

    @Autowired
    private VipOrderService vipOrderService;

    @Autowired
    private VipOrderConverter vipOrderConverter;

    @Autowired
    private VipOrderValidator vipOrderValidator;

    @ApiOperation("下单并发起支付")
    @PostMapping
    public AjaxResult addAndPay(@RequestBody @Validated AddPayVipOrderDTO dto) {
        AddPayVipOrderBO bo = vipOrderConverter.toBO(dto);
        return success(vipOrderService.addAndPay(bo));
    }

    @ApiOperation("刷新支付结果")
    @PutMapping("/refreshPayResult")
    public AjaxResult refreshPayResult(String orderNo) {
        return toAjax(vipOrderService.refreshPayResult(orderNo));
    }

    @ApiOperation("查询本人订单列表")
    @GetMapping("/list")
    public TableDataInfo list(VipOrderQuery query) {
        startPage();
        query.setUserId(getUserId());
        return getDataTable(vipOrderService.selectVipOrderList(query));
    }

    @ApiOperation("通过ID查询订单详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        VipOrderVO order = vipOrderService.selectVipOrderById(id);
        if (!vipOrderValidator.canView(order, getUserId())) {
            return error("您没有权限查看该订单");
        }
        return success(order);
    }

}
