package com.boman.system.service.impl;

import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.redis.service.RedisService;
import com.boman.domain.QianshanGrid;
import com.boman.domain.SysDept;
import com.boman.domain.constant.CacheConstants;
import com.boman.domain.dto.AjaxResult;
import com.boman.system.api.model.LoginUser;
import com.boman.system.domain.ChinaArea;
import com.boman.system.domain.vo.TreeSelect;
import com.boman.system.mapper.ChinaAreaMapper;
import com.boman.system.service.IChinaAreaService;
import com.boman.web.core.api.RemoteQianshanGridService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author tjf
 * @Date: 2022/03/25/9:39
 */
@Service
@Slf4j
public class ChinaAreaServiceImpl implements IChinaAreaService {

    @Resource
    private RedisService redisService;

    @Resource
    private ChinaAreaMapper chinaAreaMapper;

    @Resource
    private RemoteQianshanGridService remoteQianshanGridService;
    /**
     * 获取行政区域规划下拉树列表
     * @param chinaArea
     * @return
     */
    @Override
    public AjaxResult selectChinaAreaList(ChinaArea chinaArea) {

        //先去从redis获取
        List<TreeSelect> treeSelects = redisService.getCacheList(CacheConstants.CHINA_AREA_DP+chinaArea.getPid());
        if (treeSelects != null && treeSelects.size() > 0){
            log.info("从缓存中获取到区域集合");
            return AjaxResult.success(treeSelects);
        }
        List<ChinaArea> chinaAreas = chinaAreaMapper.selectChinaAreaList(chinaArea);
        if (chinaAreas != null && chinaAreas.size() > 0){
            log.info("从数据库中获取到行政区域集合");
            redisService.setCacheList(CacheConstants.CHINA_AREA_DP+chinaArea.getPid(),chinaAreas);
        }else{
            List<QianshanGrid> QianshanGrids = remoteQianshanGridService.list(String.valueOf(chinaArea.getPid()));
            if(QianshanGrids != null && QianshanGrids.size() > 0){
                chinaAreas = new ArrayList<>();
                for (QianshanGrid grid : QianshanGrids) {
                    ChinaArea chinaAre = new ChinaArea();
                    chinaAre.setAreaId(String.valueOf(grid.getId()));
                    chinaAre.setName(grid.getGridName());
                    chinaAre.setPid(grid.getParentGridId());
                    chinaAreas.add(chinaAre);
                }

                redisService.setCacheList(CacheConstants.CHINA_AREA_DP+chinaArea.getPid(),chinaAreas);
            }
        }
        return AjaxResult.success(chinaAreas);
    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @param chinaAreaList 行政区域列表
     * @return 下拉树结构列表
     */
    public List<TreeSelect> buildChinaAreaTreeSelect(List<ChinaArea> chinaAreaList) {
        List<ChinaArea> chinaAreaTrees = buildChinaAreaTree(chinaAreaList);
        return chinaAreaTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param chinaAreaList 行政区域列表
     * @return 树结构列表
     */
    public List<ChinaArea> buildChinaAreaTree(List<ChinaArea> chinaAreaList) {
        List<ChinaArea> returnList = new ArrayList<ChinaArea>();
        List<String> tempList = new ArrayList<String>();
        for (ChinaArea chinaArea : chinaAreaList) {
            tempList.add(chinaArea.getAreaId());
        }
        for (Iterator<ChinaArea> iterator = chinaAreaList.iterator(); iterator.hasNext(); ) {
            ChinaArea chinaArea = (ChinaArea) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(chinaArea.getPid())) {
                recursionFn(chinaAreaList, chinaArea);
                returnList.add(chinaArea);
            }
        }
        if (returnList.isEmpty()) {
            returnList = chinaAreaList;
        }
        return returnList;
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<ChinaArea> chinaAreaList, ChinaArea c) {
        // 得到子节点列表
        List<ChinaArea> childList = getChildList(chinaAreaList, c);
        c.setChildren(childList);
        for (ChinaArea chinaArea : chinaAreaList) {
            if (hasChild(chinaAreaList, chinaArea)) {
                recursionFn(chinaAreaList, chinaArea);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ChinaArea> getChildList(List<ChinaArea> chinaAreaList, ChinaArea c) {
        List<ChinaArea> tlist = new ArrayList<ChinaArea>();
        Iterator<ChinaArea> it = chinaAreaList.iterator();
        while (it.hasNext()) {
            ChinaArea n = (ChinaArea) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().equals(c.getAreaId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ChinaArea> list, ChinaArea c) {
        return getChildList(list, c).size() > 0;
    }
}
