package com.zcxk;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class test {
    @Test
    public void getTree2(){

        List<CameraTreeDTO>treeDTOS=new ArrayList<>();
        List<Camera> cameras = JSONObject.parseArray("[" +
                "            {" +
                "                'id': 19," +
                "                'deviceSerial': 'E40848837'," +
                "                'appKey': 'af260c9cefdc4e8380fd8b0a008595d1'," +
                "                'appSecret': '685b64ecad937b994547f7e20a14b1c1'," +
                "                'manufacturer': null," +
                "                'platform': null," +
                "                'verificationCode': null," +
                "                'channelNo': 1," +
                "                'channelName': 'string5'," +
                "                'ipcSerial': null," +
                "                'dateCreate': '2020-12-09 09:44:46'," +
                "                'dateUpdate': null," +
                "                'createBy': null," +
                "                'updateBy': null," +
                "                'status': null," +
                "                'siteId': null," +
                "                'monitorPointId': null," +
                "                'monitorSceneId': null," +
                "                'picUrl': null," +
                "                'cameraStatus': 1," +
                "                'videoStatus': 1," +
                "                'monitorPointName': '测试point'," +
                "                'monitorSceneName': '来测试啊'," +
                "                'address': 'string'," +
                "                'token': null," +
                "                'playUrl': 'ezopen://open.ys7.com/E40848837/1.live'," +
                "                'replayUrl': null" +
                "            }" +
                "        ]",Camera.class);
        Iterator<Map.Entry<String, List<Camera>>> sceneIterator = cameras.stream()
                .collect(Collectors.groupingBy(d -> d.getMonitorSceneName())).entrySet().iterator();
        Iterator<Map.Entry<String, List<Camera>>> pointIterator = cameras.stream()
                .collect(Collectors.groupingBy(d -> d.getMonitorSceneName() + "_" + d.getMonitorPointName()))
                .entrySet().iterator();
        while (sceneIterator.hasNext()){
            CameraTreeDTO cameraTreeDTO=new CameraTreeDTO();
            Map.Entry<String, List<Camera>> next = sceneIterator.next();
            String key=next.getKey();
            cameraTreeDTO.setName(key);
            cameraTreeDTO.setUniqName(key);
            cameraTreeDTO.setParentName("first");
            cameraTreeDTO.setId(next.getValue().get(0).getMonitorSceneId());
            treeDTOS.add(cameraTreeDTO);
        }
        while (pointIterator.hasNext()){
            CameraTreeDTO cameraTreeDTO=new CameraTreeDTO();
            Map.Entry<String, List<Camera>> next = pointIterator.next();
            String key=next.getKey();
            String[] names = key.split("_");
            cameraTreeDTO.setName(names[1]);
            cameraTreeDTO.setParentName(names[0]);
            cameraTreeDTO.setUniqName(key);

            cameraTreeDTO.setId(next.getValue().get(0).getMonitorPointId());
            treeDTOS.add(cameraTreeDTO);
        }
        cameras.forEach(camera1 -> {
            CameraTreeDTO cameraTreeDTO=new CameraTreeDTO();
            cameraTreeDTO.setName(camera1.getChannelName());
            cameraTreeDTO.setUniqName(camera1.getMonitorSceneName() + "_" +
                    camera1.getMonitorPointName()+"_"+camera1.getChannelName());
            cameraTreeDTO.setParentName(camera1.getMonitorSceneName() + "_" + camera1.getMonitorPointName());
            cameraTreeDTO.setId(camera1.getId());
            treeDTOS.add(cameraTreeDTO);
        });

        List<CameraTreeDTO> first = getCameraTree(treeDTOS, "first", 1);
        System.out.println(JSONObject.toJSONString(first));
    }
    private List<CameraTreeDTO> getCameraTree(List<CameraTreeDTO> list, String uniqName, Integer leve) {
        List<CameraTreeDTO> temList = Lists.newArrayList();
        if (list != null) {
            for (CameraTreeDTO cameraTreeDTO : list) {
                if ( StringUtils.equals(uniqName,cameraTreeDTO.getParentName())) {
                    List<CameraTreeDTO> chidren = getCameraTree(list, cameraTreeDTO.getUniqName(), ++leve);
                    CameraTreeDTO tempTreeCameraTreeDTO = new CameraTreeDTO();
                    BeanUtils.copyProperties(cameraTreeDTO, tempTreeCameraTreeDTO);
                    tempTreeCameraTreeDTO.setChild(chidren);
                    temList.add(tempTreeCameraTreeDTO);
                    leve--;
                }
            }
        }
        if(temList.size() > 0){
            return temList;
        }else {
            return null;
        }
    }
}
