package org.dromara.mall.product.controller.app;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.mall.product.domain.bo.MallProductBo;
import org.dromara.mall.product.domain.vo.MallProductVo;
import org.dromara.mall.product.domain.vo.MallSkuVo;
import org.dromara.mall.product.service.MallProductService;
import org.dromara.mall.product.service.MallSkuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品（移动端）
 *
 * @author pengles
 */
@Tag(name = "移动端-商品", description = "商品查询接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/mall/product")
public class AppMallProductController {

    private final MallProductService mallProductService;
    private final MallSkuService mallSkuService;

    /**
     * 查询商品列表（移动端）
     */
    @Operation(summary = "查询商品列表", description = "查询上架的商品列表")
    @GetMapping("/list")
    public R<TableDataInfo<MallProductVo>> list(
            @Parameter(description = "查询条件") MallProductBo bo,
            @Parameter(description = "分页参数") @Validated PageQuery pageQuery) {
        // 移动端只查询上架的商品
        if (bo == null) {
            bo = new MallProductBo();
        }
        bo.setStatus(0); // 0表示上架
        TableDataInfo<MallProductVo> result = mallProductService.queryPageList(bo, pageQuery);
        return R.ok(result);
    }

    /**
     * 查询商品详情（移动端）
     */
    @Operation(summary = "查询商品详情", description = "根据商品ID查询商品详情")
    @GetMapping("/{id}")
    public R<MallProductVo> getInfo(
            @Parameter(description = "商品ID", required = true) @PathVariable Long id) {
        MallProductVo product = mallProductService.queryById(id);
        if (product == null) {
            return R.fail("商品不存在");
        }
        // 只有上架的商品才能查看
        if (product.getStatus() != null && product.getStatus() != 0) {
            return R.fail("商品已下架");
        }
        return R.ok(product);
    }

    /**
     * 查询商品SKU列表（移动端）
     */
    @Operation(summary = "查询商品SKU列表", description = "根据商品ID查询SKU列表")
    @GetMapping("/{productId}/skus")
    public R<List<MallSkuVo>> getSkus(
            @Parameter(description = "商品ID", required = true) @PathVariable Long productId) {
        List<MallSkuVo> skus = mallSkuService.queryListByProductId(productId);
        return R.ok(skus);
    }
}