package com.ruoyi.web.modules.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.order.dto.CheckoutPreviewDTO;
import com.ruoyi.web.modules.order.dto.CheckoutSubmitDTO;
import com.ruoyi.web.modules.order.service.ICheckoutAppService;

/**
 * C 端确认订单（须会员 Token）
 */
@RestController
@RequestMapping("/api/checkout")
public class CheckoutAppController extends BaseController
{
    @Autowired
    private ICheckoutAppService checkoutAppService;

    @PostMapping("/preview")
    public AjaxResult preview(@RequestBody CheckoutPreviewDTO dto)
    {
        return success(checkoutAppService.preview(requireMemberId(), dto));
    }

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody CheckoutSubmitDTO dto)
    {
        return success(checkoutAppService.submit(requireMemberId(), dto));
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new ServiceException("请先登录", 401);
        }
        return memberId;
    }
}
