package com.hzs.sale.product.controller.api;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.controller.manage.params.AreaClassifyParam;
import com.hzs.sale.product.service.IBdAreaClassifyService;
import com.hzs.sale.product.vo.AreaClassifyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 商品分类列表
 * @Author: yuhui
 * @Time: 2022/9/5 10:57
 * @Classname: BdAreaClassifyController
 * @PackageName: com.hzs.sale.product.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/api/area-classify")
public class ApiAreaClassifyController extends BaseController {

    @Autowired
    private IBdAreaClassifyService areaClassifyService;

    /**
     * 查询分类列表
     *
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(AreaClassifyParam areaClassifyParam) {
        areaClassifyParam.setEnableState(EYesNo.YES.getIntValue());
        Integer pkCountry;
        if (areaClassifyParam.getPkCountry() != null) {
            pkCountry = areaClassifyParam.getPkCountry();
        } else {
            pkCountry = SecurityUtils.getPkCountry();
        }
        areaClassifyParam.setPkCountry(pkCountry);
        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        return AjaxResult.success(list);
    }


    /**
     * 查询第一层分类
     */
    @GetMapping("get-first-classify")
    public AjaxResult getFirstClassify() {
        AreaClassifyParam areaClassifyParam = new AreaClassifyParam();
        areaClassifyParam.setHierarchy(0);
        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        return AjaxResult.success(list);
    }

    /**
     * 查询
     *
     * @param areaClassifyParam
     * @return
     */
    @GetMapping("get-hierarchy-list")
    public AjaxResult getLevelList(AreaClassifyParam areaClassifyParam) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setDeep(4);
        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        List<Tree<String>> build = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getPkId().toString());
                    tree.setParentId(treeNode.getPkParent().toString());
                    tree.setName(treeNode.getClassifyName());
                    tree.put("value", treeNode.getPkId().toString());
                    tree.put("label", treeNode.getClassifyName());
                });
        return AjaxResult.success(build);
    }
}
