package com.czg.controller.user;

import com.czg.constants.SystemConstants;
import com.czg.product.param.ShopProductSkuParam;
import com.czg.product.service.ConsInfoService;
import com.czg.product.service.UProductService;
import com.czg.product.vo.*;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import jakarta.annotation.Resource;
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 Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-16
 */
@RestController
@RequestMapping("/user/product")
public class UProductController {
    @Resource
    private UProductService uProductService;
    @Resource
    private ConsInfoService consInfoService;

    /**
     * 小程序点餐-热销商品列表
     */
    @GetMapping("/miniApp/hots/query")
    public CzgResult<List<ShopProductVo>> queryHotsProductList() {
        Long shopId = StpKit.USER.getShopId(0L);
        List<ShopProductVo> list = uProductService.queryHotsProductList(shopId);
//        list.forEach(prod -> {
//            prod.setIsSaleTime(uProductService.calcIsSaleTime(prod.getDays(), prod.getStartTime(), prod.getEndTime()));
//        });
        return CzgResult.success(list);
    }

    /**
     * 小程序点餐-分组商品列表
     */
    @GetMapping("/miniApp/group/query")
    public CzgResult<List<ShopGroupProductVo>> queryGroupProductList() {
        Long shopId = StpKit.USER.getShopId(0L);
        List<ShopGroupProductVo> list = uProductService.queryGroupProductList(shopId);
        list.forEach(item -> {
            item.getProductList().forEach(prod -> {
                prod.setIsSaleTime(uProductService.calcIsSaleTime(prod.getDays(), prod.getStartTime(), prod.getEndTime()));
                prod.setIsSaleTime(uProductService.calcIsSaleTime(prod.getIsSaleTime(), item.getUseTime(), item.getSaleStartTime(), item.getSaleEndTime()));
                if (item.getUseTime() == SystemConstants.OneZero.ONE) {
                    prod.setStartTime(item.getSaleStartTime());
                    prod.setEndTime(item.getSaleEndTime());
                }
            });
        });
        return CzgResult.success(list);
    }

    /**
     * 列表
     */
    @GetMapping("consStock")
    public CzgResult<List<ConsStockRecord>> getConsStockList(Long shopId) {
        return CzgResult.success(consInfoService.getConsStockList(shopId));
    }

    /**
     * 小程序点餐-商品详情
     *
     * @param id 商品id
     */
    @GetMapping("/miniApp/info/{id}")
    public CzgResult<ShopProductInfoVo> getProductInfo(@PathVariable("id") Long id) {
        AssertUtil.isNull(id, "商品id不能为空");
        ShopProductInfoVo data = uProductService.getProductInfo(id);
        return CzgResult.success(data);
    }

    /**
     * 小程序点餐-商品SKU详情
     */
    @GetMapping("/miniApp/sku/info")
    public CzgResult<ShopProductSkuInfoVo> getProductSkuInfo(ShopProductSkuParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        ShopProductSkuInfoVo data = uProductService.getProductSkuInfo(param);
        return CzgResult.success(data);
    }

}