package com.ruoyi.database.controller;

import com.ruoyi.database.domain.Order;
import com.ruoyi.database.domain.PayFees;
import com.ruoyi.database.service.OrderService;
import com.ruoyi.database.service.WechatMiniProgramPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private WechatMiniProgramPayService wechatPayService;

    @Autowired
    private OrderService orderService;

    /**
     * 创建支付订单
     */
    @PostMapping("/create")
    public Map<String, Object> createPayment(@RequestBody PayFees payFees,
                                             HttpServletRequest request) {
        try {
            // 1. 创建业务订单
            orderService.createOrder(payFees);

            // 2. 获取客户端IP
            String clientIp = wechatPayService.getClientIpAddress(request);

            // 3. 创建支付订单
//            Map<String, Object> result = wechatPayService.createJsapiOrder(
//                    openid, orderId, amount, description, clientIp);

//            return result;


        } catch (Exception e) {
//            System.err.println("创建支付订单异常: " + e.getMessage());
//            e.printStackTrace();
//
//            Map<String, Object> result = new HashMap<>();
//            result.put("success", false);
//            result.put("message", "系统异常: " + e.getMessage());
//            return result;
        }
        return null;
    }
}
