package com.jeesharp.business.order.controller;

import com.jeesharp.business.modules.trade.api.TradeOrderMiniProgramApi;
import com.jeesharp.business.modules.trade.vo.TradeOrderQueryVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.query.PageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 java.util.Map;

/**
 * 小程序「我的订单」全业态聚合入口（{@code /mobile/tradeOrder}）。
 *
 * <p>数据源 {@code biz_trade_order}，由各垂直业务双写写入；列表/详情由
 * {@link com.jeesharp.business.modules.trade.application.TradeOrderApplicationService} 组装卡片，
 * 并经优惠券展示补全器写入抵扣信息。</p>
 *
 * <p>仅看商城垂直单请用 {@link com.jeesharp.business.mall.controller.MallOrderMobileController}（{@code /mallOrder/*}）。</p>
 *
 * @author jeesharp
 * @since 2026-05-19
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/tradeOrder")
@Tag(name = "小程序-统一订单中心")
@AllArgsConstructor
public class TradeOrderMobileController extends BaseMobileController {

    private final TradeOrderMiniProgramApi tradeOrderMiniProgramApi;

    @Operation(summary = "我的统一订单列表（全业态）")
    @PostMapping("/list")
    public AjaxResult<PageRespVO<Map<String, Object>>> list(@RequestBody(required = false) TradeOrderQueryVO body) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(tradeOrderMiniProgramApi.listUnifiedOrdersForMiniProgram(memberId, body));
    }

    @Operation(summary = "我的统一订单详情（全业态）")
    @GetMapping("/{id}")
    public AjaxResult<Map<String, Object>> detail(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        Map<String, Object> row = tradeOrderMiniProgramApi.getUnifiedOrderDetailForMiniProgram(memberId, id);
        if (row == null) {
            return AjaxResult.failMsg("订单不存在");
        }
        return AjaxResult.okData(row);
    }
}
