package com.czg.controller.user;

import com.czg.enums.YesNoEnum;
import com.czg.product.param.ShopProductSkuParam;
import com.czg.product.service.UProductService;
import com.czg.product.vo.ShopGroupProductVo;
import com.czg.product.vo.ShopProductInfoVo;
import com.czg.product.vo.ShopProductSkuInfoVo;
import com.czg.product.vo.ShopProductVo;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.utils.AssertUtil;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import lombok.AllArgsConstructor;
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;
import java.util.Map;


/**
 * 店铺商品
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-16
 */
@AllArgsConstructor
@RestController
@RequestMapping("/user/product")
public class UProductController {
    private final UProductService uProductService;
    private final RedisService redisService;

    /**
     * 小程序点餐-热销商品列表
     */
    @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()));
        });
        uProductService.refreshProductStock(shopId, list);
        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);
        Map<Long, Integer> productStock = uProductService.findShopProductStock(shopId);
        list.forEach(item -> {
            uProductService.refreshProductStock(productStock, item.getProductList());
            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() == YesNoEnum.YES.value()) {
                    prod.setStartTime(item.getSaleStartTime());
                    prod.setEndTime(item.getSaleEndTime());
                }
            });
        });
        return CzgResult.success(list);
    }

    /**
     * 小程序点餐-商品详情
     *
     * @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);
    }

}