package com.gxwebsoft.shop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.gxwebsoft.cms.entity.CmsWebsite;
import com.gxwebsoft.cms.service.CmsWebsiteService;
import com.gxwebsoft.shop.service.ShopWebsiteService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.vo.ShopVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 商城主入口
 *
 * @author 科技小王子
 * @since 2024-09-10 20:36:14
 */
@Slf4j
@Tag(name = "商城")
@RestController
@RequestMapping("/api/shop")
public class ShopMainController extends BaseController {
  @Resource
  private ShopWebsiteService shopWebsiteService;

  @Operation(summary = "商城基本信息", description = "获取商城的基本信息，包括配置、导航、设置和过期状态等")
  @GetMapping("/getShopInfo")
  public ApiResult<ShopVo> getShopInfo() {
    Integer tenantId = getTenantId();

    if (ObjectUtil.isEmpty(tenantId)) {
      return fail("租户ID不能为空", null);
    }

    try {
      // 使用专门的商城信息获取方法
      ShopVo shopVo = shopWebsiteService.getShopInfo(tenantId);
//      log.debug("获取商城信息成功: {}", shopVo);
      return success(shopVo);
    } catch (IllegalArgumentException e) {
      return fail(e.getMessage(), null);
    } catch (RuntimeException e) {
      return fail(e.getMessage(), null);
    } catch (Exception e) {
      log.error("获取商城信息失败", e);
      return fail("获取商城信息失败", null);
    }
  }

}
