package com.boman.web.core.service.vaccineInfo.impl;

import com.boman.common.core.utils.ServletUtils;
import com.boman.common.core.utils.StringUtils;
import com.boman.common.datascope.annotation.DataScope;
import com.boman.common.security.service.TokenService;
import com.boman.domain.SysDept;
import com.boman.domain.SysUser;
import com.boman.system.api.model.LoginUser;
import com.boman.web.core.domain.AdministrativeInfo;
import com.boman.web.core.domain.TreeSelect;
import com.boman.web.core.mapper.AdministrativeInfoMapper;
import com.boman.web.core.service.vaccineInfo.AdministrativeInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tjf
 * @Date: 2021/09/05/13:23
 */
@Service
public class AdministrativeInfoServiceImpl implements AdministrativeInfoService {

    @Resource
    private AdministrativeInfoMapper administrativeInfoMapper;

    /**
     * 获取乡镇下拉树
     * @param administrativeInfo
     * @return
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<AdministrativeInfo> selectAdministrativeInfoList(AdministrativeInfo administrativeInfo) {
        List<AdministrativeInfo> administrativeInfos = administrativeInfoMapper.selectAdministrativeInfoList(administrativeInfo);
        return administrativeInfos;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param administrativeInfoList 乡镇列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildAdministrativeInfoTreeSelect(List<AdministrativeInfo> administrativeInfoList) {
        List<AdministrativeInfo> administrativeInfoTrees = buildAdministrativeInfoTree(administrativeInfoList);
        return administrativeInfoTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 构建前端所需要树结构
     *
     * @param administrativeInfoList 乡镇列表
     * @return 树结构列表
     */
    public List<AdministrativeInfo> buildAdministrativeInfoTree(List<AdministrativeInfo> administrativeInfoList)
    {
        List<AdministrativeInfo> returnList = new ArrayList<AdministrativeInfo>();
        List<Long> tempList = new ArrayList<Long>();
        for (AdministrativeInfo administrativeInfo : administrativeInfoList)
        {
            tempList.add(administrativeInfo.getId());
        }
        for (Iterator<AdministrativeInfo> iterator = administrativeInfoList.iterator(); iterator.hasNext();)
        {
            AdministrativeInfo administrativeInfo = (AdministrativeInfo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(administrativeInfo.getParentId()))
            {
                recursionFn(administrativeInfoList, administrativeInfo);
                returnList.add(administrativeInfo);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = administrativeInfoList;
        }
        return returnList;
    }


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

    /**
     * 得到子节点列表
     */
    private List<AdministrativeInfo> getChildList(List<AdministrativeInfo> list, AdministrativeInfo t)
    {
        List<AdministrativeInfo> tlist = new ArrayList<AdministrativeInfo>();
        Iterator<AdministrativeInfo> it = list.iterator();
        while (it.hasNext())
        {
            AdministrativeInfo n = (AdministrativeInfo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

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