package org.dromara.pay.controller.app;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.pay.domain.bo.PayOrderBo;
import org.dromara.pay.domain.vo.PayOrderSubmitRespVO;
import org.dromara.pay.domain.vo.PayOrderVo;
import org.dromara.pay.service.PayOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 移动端支付订单Controller
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/pay/order")
public class AppPayOrderController extends BaseController {

    private final PayOrderService payOrderService;

    /**
     * 提交支付订单
     */
    @PostMapping("/submit")
    public R<PayOrderSubmitRespVO> submitOrder(@Validated @RequestBody PayOrderBo bo) {
        PayOrderSubmitRespVO resp = payOrderService.submitOrder(bo);
        return R.ok(resp);
    }

    /**
     * 查询支付订单列表
     */
    @GetMapping("/list")
    public TableDataInfo<PayOrderVo> list(PayOrderBo bo, PageQuery pageQuery) {
        // 可以根据业务需要过滤当前用户的订单
        return payOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询支付订单详情
     */
    @GetMapping("/{id}")
    public R<PayOrderVo> getInfo(@PathVariable Long id) {
        return R.ok(payOrderService.queryById(id));
    }

    /**
     * 根据商户订单号查询支付订单
     */
    @GetMapping("/query")
    public R<PayOrderVo> queryByMerchantOrderId(@RequestParam String merchantOrderId) {
        PayOrderBo bo = new PayOrderBo();
        bo.setMerchantOrderId(merchantOrderId);
        var list = payOrderService.queryList(bo);
        if (list.isEmpty()) {
            return R.fail("支付订单不存在");
        }
        return R.ok(list.get(0));
    }

}
