#!E:\Pycharm Projects\Waytous
# -*- coding: utf-8 -*-
# @Time : 2022/9/8 9:42
# @Author : Opfer
# @Site :
# @File : util.py    
# @Software: PyCharm

import requests
from tables import *
with open(json_file) as f:
    mysql_config = json.load(f)["mssql"]


# def redispatch_request(truck_id, excavator_id, unload_area_id):
#     """
#     redispatch request.
#     :param truck_id:
#     :param excavator_id:
#     :param unload_area_id:
#     :return:
#     """
#
#     exactor_no = session_mysql.query(Equipment).filter_by(id=excavator_id).first().device_name
#     unload_area_name = session_postgre.query(DumpArea).filter_by(Id=unload_area_id).first().Name
#     truck_name = session_mysql.query(Equipment).filter_by(id=truck_id).first().device_name
#
#     real_data = {
#         "exactorId": excavator_id,
#         "exactorNo": exactor_no,
#         "id": str(uuid.uuid4()),
#         "unloadAreaId": unload_area_id,
#         "unloadAreaName": unload_area_name,
#         "truckId": truck_id,
#         "truckNo": truck_name,
#         "isauto": "true",
#         "tempChange": 1,
#         "immediateEffect": 1
#         }
#
#     return POST(real_data)


# def get_cross_next_lanes(truck_locate):
#     next_lane_list = []
#     startNodeId = session_postgre.query(Lane).filter_by(Id=truck_locate).first().EndNodeId
#     for item in session_postgre.query(Lane).filter_by(StartNodeId=startNodeId).all():
#         next_lane_list.append(str(item.Id))
#     return next_lane_list


def get_cross_next_lanes(truck_locate):
    next_lane_list = []
    startNodeId = session_postgre.query(Lane).filter_by(Id=truck_locate).first().EndNodeId
    for item in session_postgre.query(Lane).filter_by(StartNodeId=startNodeId).all():
        next_lane_list.append(str(item.Id))
        next_lane_end_node = item.EndNodeId
        for next_item in session_postgre.query(Lane).filter_by(StartNodeId=next_lane_end_node).all():
            next_lane_list.append(str(next_item.Id))
    print("next_lane_list")
    print(next_lane_list)
    return next_lane_list


def delete_congestion_load_area(congestion_lane_dict, load_area_dict, next_lane_load_area_dict):
    """

    :param congestion_lane_dict: 拥堵路段
    :param load_area_dict: 拓扑图规划得到的可达装载区
    :param next_lane_load_area_dict: 路段可达的装载区
    :return:
    """
    for lane_id in next_lane_load_area_dict:  # 下一装载区路段
        if lane_id in congestion_lane_dict:  #
            for load_area in next_lane_load_area_dict[lane_id]:
                if load_area in load_area_dict:
                    del load_area_dict[load_area]


def get_lane_reach_load_areas(load_area_lane_dict, next_lane_list):
    next_lane_load_area_dict = {}
    for lane_id in next_lane_list:
        for key, value in load_area_lane_dict.items():
            if lane_id in value:
                if lane_id not in next_lane_load_area_dict:
                    next_lane_load_area_dict[lane_id] = [key]
                else:
                    next_lane_load_area_dict[lane_id].append(key)

    print("next_lane_load_area_dict")
    print(next_lane_load_area_dict)
    return next_lane_load_area_dict


def POST(truck_id):
    """
    Post.
    :param real_data:
    :return:
    """
    url_d = "http://" + mysql_config["host"] + ":8020/ht/api/dispatch/updateTemp?truckId=" + truck_id
    res_d = requests.get(url_d)
    print(res_d.text)
    return res_d.text