package com.zcxk.rmcp.web.util;

import com.zcxk.rmcp.api.dto.install.InstallPlanDataDto;
import com.zcxk.rmcp.api.vo.AreaVo;
import org.springframework.beans.BeanUtils;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @author linqingwei
 * @date 2021-07-21 16:56
 */
public class TreeUtil {

    public static List<AreaVo> getAreaTree(List<AreaVo> list, Integer id, Integer leve) {
        List<AreaVo> temList = newArrayList();
        if (list != null) {
            for (AreaVo areaDto : list) {
                if (id.equals(areaDto.getPid())) {
                    List<AreaVo> chidren = getAreaTree(list, areaDto.getCode(), ++leve);
                    AreaVo tempArea = new AreaVo();
                    BeanUtils.copyProperties(areaDto, tempArea);
                    tempArea.setChildren(chidren);
                    temList.add(tempArea);
                    leve--;
                }
            }
        }
        if(temList.size() > 0){
            return temList;
        }else {
            return null;
        }
    }

    public static List<InstallPlanDataDto> getInstallPlanDataTree(List<InstallPlanDataDto> list, Long id, Integer leve) {
        List<InstallPlanDataDto> temList = newArrayList();
        if (list != null) {
            for (InstallPlanDataDto installPlanDataDto : list) {
                if (id.equals(installPlanDataDto.getParentId())) {
                    List<InstallPlanDataDto> chidren = getInstallPlanDataTree(list, installPlanDataDto.getId(), ++leve);
                    InstallPlanDataDto temInstallPlanDataDTO = new InstallPlanDataDto();
                    BeanUtils.copyProperties(installPlanDataDto, temInstallPlanDataDTO);
                    temInstallPlanDataDTO.setChildren(chidren);
                    temList.add(temInstallPlanDataDTO);
                    leve--;
                }

            }
        }
        if(temList.size() > 0){
            return temList;
        }else {
            return null;
        }
    }

    /**
    * 递归后续遍历多叉树
    * @date 15:10 2021/8/3
    * @param list:
    * @return void
    **/
    public static void  postorderTraversal(List<InstallPlanDataDto> list){
        if (list != null && list.size()>0) {
            for (InstallPlanDataDto installPlanDataDto : list) {
                postorderTraversal(installPlanDataDto.getChildren());

                if(installPlanDataDto.getChildren() != null && installPlanDataDto.getChildren().size() > 0){
                    //计算安装数和总数
                    int installedNumber = 0;
                    int totalNumber = 0;
                    for (InstallPlanDataDto child : installPlanDataDto.getChildren()) {
                        if(installPlanDataDto.getType()==3){
                            if(child.getIsInstalled()==1){
                                installedNumber++;
                            }
                            totalNumber++;
                        }else {
                            installedNumber+=child.getInstalledNumber();
                            totalNumber+=child.getTotalNumber();
                        }

                    }
                    installPlanDataDto.setInstalledNumber(installedNumber);
                    installPlanDataDto.setTotalNumber(totalNumber);
                }
            }
        }
    }
}
