package org.linlinjava.jz.admin.web;

import io.swagger.annotations.Api;
import org.apache.shiro.util.CollectionUtils;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.db.domain.JzGoodsCategory;
import org.linlinjava.jz.db.service.JzGoodsCategoryService;
import org.springframework.validation.annotation.Validated;
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;
import java.util.List;

@Api(value = "Admin-GoodsCategory-Controller", description = "商品分类")
@RestController
@RequestMapping("/admin/goodsCategory")
@Validated
public class AdminGoodsCategoryController {
    @Resource
    private JzGoodsCategoryService jzGoodsCategoryService;

    @GetMapping("/list")
    public Object list() {

        List<JzGoodsCategory> jzGoodsCategories = jzGoodsCategoryService.selectByPidAndLevel(0, "L1");
        for (JzGoodsCategory jzGoodsCategory : jzGoodsCategories) {
            //获取下级
            List<JzGoodsCategory> jzGoodsCategories1 = jzGoodsCategoryService.selectByPidAndLevel(jzGoodsCategory.getId(), "L2");
            if (!CollectionUtils.isEmpty(jzGoodsCategories1)) {
                jzGoodsCategory.setChildren(jzGoodsCategories1);
            }

        }
        return ResponseUtil.ok(jzGoodsCategories);
    }
}
