package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.dto.UserOrderStats;
import com.gxwebsoft.shop.service.ShopOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户侧订单接口
 */
@Tag(name = "用户订单")
@RestController
@RequestMapping("/api/user/orders")
public class UserOrderController extends BaseController {

    @Resource
    private ShopOrderService shopOrderService;

    @Operation(summary = "获取当前用户订单状态统计（一次聚合查询，带 60s 缓存）")
    @GetMapping("/stats")
    public ApiResult<UserOrderStats> getUserOrderStats(@RequestParam(value = "type", required = false) Integer type) {
        Integer userId = getLoginUserId();
        if (userId == null) {
            return fail("用户未登录", null);
        }
        return success(shopOrderService.getUserOrderStats(userId, getTenantId(), type));
    }
}

