package com.ozs.service.service.impl;

import com.ozs.service.entity.BaseDeviceDynamicManagement;
import com.ozs.service.entity.CameraTree;
import com.ozs.service.entity.vo.BaseCameraManagementDto;
import com.ozs.service.mapper.BaseDeviceDynamicManagementMapper;
import com.ozs.service.service.BaseDeviceDynamicManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * <p>
 * 台账管理表 服务实现类
 * </p>
 *
 * @author ozs
 * @since 2023-02-22
 */
@Service
public class BaseDeviceDynamicManagementServiceImpl extends ServiceImpl<BaseDeviceDynamicManagementMapper, BaseDeviceDynamicManagement> implements BaseDeviceDynamicManagementService {
    @Autowired
    BaseDeviceDynamicManagementMapper baseDeviceDynamicManagementMapper;

    @Override
    public CameraTree cameraTree(String name) {
        CameraTree cameraTree = CameraTree.builder()
                .name("相机树")
                .code("0")
                .flay(false)
                .children(new ArrayList<>())
                .build();
        Map<String, CameraTree> map = new HashMap<>();
        Map<String, CameraTree> map1 = new HashMap<>();

        List<BaseCameraManagementDto> list = baseDeviceDynamicManagementMapper.list(name);

        if (list.size() > 0) {
            for (BaseCameraManagementDto o : list) {
                // 线路
                CameraTree build = CameraTree.builder()
                        .code(o.getLineCode().toString())
                        .name(o.getLineName())
                        .flay(true)
                        .children(new ArrayList<>())
                        .build();

                if (!cameraTree.getChildren().stream()
                        .map(CameraTree::getCode)
                        .collect(Collectors.toList())
                        .contains(build.getCode())) {
                    cameraTree.getChildren().add(build);
                    map.put(o.getLineCode().toString(), build);
                }

                // 里程
                CameraTree build1 = CameraTree.builder()
                        .code(o.getInstallMile() + "-" + (o.getLineType().equals("1") ? "上行" : "下行"))
                        .name(o.getInstallMile() + "-" + (o.getLineType().equals("1") ? "上行" : "下行"))
                        .flay(true)
                        .children(new ArrayList<>())
                        .build();


                if (!ObjectUtils.isEmpty(map.get(o.getLineCode().toString()))) {
                    map.get(o.getLineCode().toString()).getChildren().add(build1);
                    map1.put(o.getInstallMile() + "-" + (o.getLineType().equals("1") ? "上行" : "下行"), build1);
                }

                // 摄像头
                CameraTree build2 = CameraTree.builder()
                        .code(o.getId())
                        .name(o.getCameraCode())
                        .flay(true)
                        .children(new ArrayList<>())
                        .build();


                if (!ObjectUtils.isEmpty(map1.get(o.getInstallMile() + "-" + (o.getLineType().equals("1") ? "上行" : "下行")))) {
                    map1.get(o.getInstallMile() + "-" + (o.getLineType().equals("1") ? "上行" : "下行")).getChildren().add(build2);
                }
            }
        }


        return cameraTree;
    }
}
