package com.ylx.web.controller.massage;

import com.alibaba.fastjson.JSON;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.massage.domain.dto.ProductSpecSetupDTO;
import com.ylx.massage.domain.vo.SpecComboVO;
import com.ylx.massage.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = {"商品规格管理"})
@RestController
@RequestMapping("product")
public class ProductSpecController extends BaseController {

    @Resource
    private ProductService productService;


    /**
     * 新增（更新）商品规格
     *
     * @param dto 规格设置请求DTO
     * @return R 操作结果
     */
    @PostMapping("spec/setup")
    @ApiOperation("新增商品规格")
    public R setupSpec(@Valid @RequestBody ProductSpecSetupDTO dto) {
        try {
            log.info("新增商品规格参数的值：{}", JSON.toJSONString(dto));
            productService.setupSpec(dto);
            return R.ok("新增规格成功");
        } catch (Exception e) {
            log.error("新增规格失败", e);
            return R.fail("新增规格失败：" + e.getMessage());
        }
    }

    /**
     * 获取商品规格组合（笛卡尔积）
     *
     * @param productId 商品ID
     * @return R<List < SpecComboVO>> 规格组合列表
     */
    @GetMapping("spec/combinations")
    @ApiOperation("获取商品规格组合")
    public R<List<SpecComboVO>> getSpecCombinations(@RequestParam String productNo) {
        try {
            return R.ok(productService.getSpecCombinations(productNo));
        } catch (Exception e) {
            log.error("获取商品规格组合失败", e);
            return R.fail("获取商品规格组合失败：" + e.getMessage());
        }
    }
}
