package com.hzs.system.base.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.base.BdArea;
import com.hzs.common.domain.system.base.ext.AreaCache;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdAreaService;
import com.hzs.system.base.vo.BdAreaVo;
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.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/1/30 16:26
 * @Classname: BdAreaController
 * @PackageName: com.hzs.system.base.controller.api
 */
@RestController("apiAreaController")
@RequestMapping("/api/area")
public class ApiAreaController {

    @Autowired
    private IBdAreaService areaService;

    /**
     * 查询树形图 省市区
     *
     * @return
     */
    @RequestMapping("/tree")
    public AjaxResult tree(Integer pkCountry) {
        if (pkCountry == null) {
            pkCountry = SecurityUtils.getPkCountry();
        }
        List<AreaCache> areaList = areaService.getAreaList(pkCountry);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setDeep(3);

        //0表示最顶层的id是0
        List<Tree<String>> build = TreeUtil.build(areaList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getPkId().toString());
                    tree.setParentId(treeNode.getParent().toString());
                    tree.setName(treeNode.getName());
                });
        return AjaxResult.success(build);
    }



    @RequestMapping("/two-layer-tree")
    public AjaxResult towTree(Integer pkCountry) {
        if (pkCountry == null) {
            pkCountry = SecurityUtils.getPkCountry();
        }
        List<AreaCache> areaList = areaService.getAreaList(pkCountry);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setDeep(2);
        //0表示最顶层的id是0
        List<Tree<String>> build = TreeUtil.build(areaList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getPkId().toString());
                    tree.setParentId(treeNode.getParent().toString());
                    tree.setName(treeNode.getName());
                });
        return AjaxResult.success(build);
    }

    /**
     * @description: 查询所有省市区
     * @author: yh
     * @date: 2022/11/24 16:26
     * @param: []
     * @return: com.hz.common.core.domain.AjaxResult
     **/
    @GetMapping("/provinceList")
    public AjaxResult provinceList(Integer pkCountry) {
        List<AreaCache> list = areaService.getAreaList(pkCountry);
        List<AreaCache> provinceList = new ArrayList<>();
        List<AreaCache> cityList = new ArrayList<>();
        List<AreaCache> countyList = new ArrayList<>();
        AreaCache areaCache = new AreaCache();
        if (CollectionUtil.isNotEmpty(list)) {
            //省
            for (AreaCache ac : list) {
                // 上级为0，则为省一级
                if (ac.getParent().equals(0)) {
                    provinceList.add(ac);
                }
            }
            areaCache.setProvinceList(provinceList);
        }
        //市
        for (AreaCache ac : provinceList) {
            for (AreaCache ct : list) {
                if (ct.getParent().intValue() == ac.getPkId()) {
                    cityList.add(ct);
                }
            }
        }
        areaCache.setCityList(cityList);
        Set<Integer> idSet = new HashSet<>();
        for (AreaCache cache : cityList) {
            for (AreaCache ac : list) {
                if (ac.getParent().intValue() == cache.getPkId().intValue()) {
                    idSet.add(ac.getPkId());
                }
            }
        }
        for (AreaCache cache : list) {
            for (Integer integer : idSet) {
                if (cache.getPkId().equals(integer)) {
                    countyList.add(cache);
                }
            }
        }
        areaCache.setCountyList(countyList);
        return AjaxResult.success(areaCache);
    }

    /**
     * 获取下级
     *
     * @param parent
     * @return
     */
    @GetMapping("get-level-list")
    public AjaxResult getLevelList(Integer parent) {
        List<BdArea> list = new ArrayList<>();
        if (parent == null) {
            return AjaxResult.success(list);
        }
        LambdaQueryWrapper<BdArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdArea::getParent, parent);
        queryWrapper.eq(BdArea::getPkCountry, SecurityUtils.getPkCountry());
        queryWrapper.eq(BdArea::getEnableState, EYesNo.YES.getIntValue());
        list = areaService.list(queryWrapper);
        List<BdAreaVo> resultList = new ArrayList<>();
        for (BdArea bdArea : list) {
            BdAreaVo bdAreaVo = BeanUtil.copyProperties(bdArea, BdAreaVo.class);
            resultList.add(bdAreaVo);
        }
        return AjaxResult.success(resultList);
    }

    @GetMapping("/get-area-by-parentId")
    public AjaxResult getArea(Integer parentId) {
        return AjaxResult.success(areaService.getAreaByParentId(parentId));
    }
}
