package com.ozs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozs.common.utils.AppendUtils;
import com.ozs.entity.BaseDeviceDynamicManagement;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.entity.CameraTree;
import com.ozs.entity.VehicleTree;
import com.ozs.entity.vo.BaseCameraManagementDto;
import com.ozs.entity.vo.BaseVehicleTerminalDto;
import com.ozs.entity.vo.CameraTreeVo;
import com.ozs.entity.vo.VehicleTreeVo;
import com.ozs.mapper.BaseDeviceDynamicManagementMapper;
import com.ozs.mapper.BaseRailwayManagementMapper;
import com.ozs.service.BaseDeviceDynamicManagementService;
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;
    @Autowired
    BaseRailwayManagementMapper railwayManagementMapper;

    @Override
    public CameraTree cameraTree(CameraTreeVo cameraTreeVo) {


        //1 父
        CameraTree cameraTree = CameraTree.builder()
                .name("相机树")
                .code("0")
                .flay(false)
                .children(new ArrayList<>())
                .build();
        Map<String, CameraTree> map = new HashMap<>();
        List<BaseCameraManagementDto> list = baseDeviceDynamicManagementMapper.list(cameraTreeVo);
        if (list.size() > 0) {
            for (BaseCameraManagementDto o : list) {
                // 线路
                CameraTree build = CameraTree.builder()
                        .code(o.getRailwayCode())
                        .name(o.getRailwayName())
                        .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.getRailwayCode(), build);
                }

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


                if (!ObjectUtils.isEmpty(map.get(o.getRailwayCode()))) {
                    CameraTree cameraTree1 = map.get(o.getInstallMile() + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"));
                    if (ObjectUtils.isEmpty(cameraTree1)) {
                        map.put(o.getInstallMile() + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"), build1);
                        map.get(o.getRailwayCode()).getChildren().add(build1);
                    }
                }

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


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

        return cameraTree;
    }

    @Override
    public VehicleTree vehicleTree(VehicleTreeVo vehicleTreeVo) {
        // 线路
        LambdaQueryWrapper<BaseRailwayManagement> rp = new LambdaQueryWrapper();
        List<BaseRailwayManagement> railwayList = railwayManagementMapper.selectList(rp);
        //1 父
        VehicleTree vehicleTree = VehicleTree.builder()
                .name("机车树")
                .code("0")
                .flay(false)
                .children(new ArrayList<>())
                .build();

        Map<String, VehicleTree> map = new HashMap<>();
        List<BaseVehicleTerminalDto> list = baseDeviceDynamicManagementMapper.vehicleTreelist(vehicleTreeVo);
        if (list.size() > 0) {
            for (BaseVehicleTerminalDto o : list) {

                VehicleTree build = VehicleTree.builder()
                        .code(o.getRailwayCode())
                        .flay(true)
                        .children(new ArrayList<>())
                        .build();
                for (BaseRailwayManagement railway : railwayList) {
                    if (railway.getRailwayCode().equals(build.getCode())) {
                        build.setName(railway.getRailwayName());
                        break;
                    }
                }
                if (!vehicleTree.getChildren().stream()
                        .map(VehicleTree::getCode)
                        .collect(Collectors.toList())
                        .contains(build.getCode())) {
                    vehicleTree.getChildren().add(build);
                    map.put(o.getRailwayCode(), build);
                }
                // 里程
                String mils = AppendUtils.stringAppend(o.getTravelMile());
                VehicleTree build1 = VehicleTree.builder()
                        .code(mils + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"))
                        .name(mils + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"))
                        .flay(true)
                        .mileage(o.getTravelMile())
                        .children(new ArrayList<>())
                        .build();


                if (!ObjectUtils.isEmpty(map.get(o.getRailwayCode()))) {
                    VehicleTree vehicleTree1 = map.get(o.getTravelMile() + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"));
                    if (ObjectUtils.isEmpty(vehicleTree1)) {
                        map.put(o.getTravelMile() + "-" + (o.getLineDir().equals("1") ? "上行" : "下行"), build1);
                        map.get(o.getRailwayCode()).getChildren().add(build1);
                    }
                }

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


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

        return vehicleTree;
    }
}
