
package com.tmerclub.cloud.product.controller.app;

import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.common.product.vo.app.CategorySearchAppVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 分类信息
 *
 * @author tmerclub
 * @date 2020-10-28 15:27:24
 */
@RestController("appCategoryController")
@RequestMapping("/ua/category")
@Tag(name = "app-分类信息")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/category_list")
    @Operation(summary = "获取指定分类下的分类列表（顶级分类的parentId为0,默认为一级分类）", description = "获取指定分类下的分类列表")
    @Parameters({
            @Parameter(name = "parentId", description = "分类ID"),
            @Parameter(name = "shopId", description = "店铺id")
    })
    public ServerResponseEntity<List<CategoryAppVO>> categoryList(@RequestParam(value = "parentId", defaultValue = "0") Long parentId, @RequestParam(value = "shopId", defaultValue = "0") Long shopId) {
        List<CategoryAppVO> categories;
        if (Objects.equals(parentId, 0L) && Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            categories = categoryService.listPlatformCategoryByShopIdAndParenId(shopId, parentId, I18nMessage.getLang());
        } else {
            categories = categoryService.listByShopIdAndParenId(shopId, parentId, I18nMessage.getLang());
        }
        return ServerResponseEntity.success(BeanUtil.mapAsList(categories, CategoryAppVO.class));
    }

    @GetMapping("/shop_category_list")
    @Operation(summary = "店铺/平台的全部分类列表接口", description = "店铺/平台分类列表接口")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<List<CategoryAppVO>> shopCategoryList(@RequestParam(value = "shopId", defaultValue = "0") Long shopId) {
        List<CategoryAppVO> categories = categoryService.shopCategoryList(shopId, I18nMessage.getLang());
        return ServerResponseEntity.success(categories);
    }


    @GetMapping("/category_search_item")
    @Operation(summary = "分类-搜索参数", description = "分类-搜索参数")
    @Parameter(name = "categoryId", description = "分类id", required = true)
    public ServerResponseEntity<CategorySearchAppVO> categorySearchItem(@RequestParam("categoryId") Long categoryId) {
        CategorySearchAppVO categorySearch = categoryService.categorySearchItem(categoryId);
        return ServerResponseEntity.success(categorySearch);
    }
}
