#!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 *
import uuid


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 POST(real_data):
    """
    Post.
    :param real_data:
    :return:
    """

    # real_data = {
    #     "exactorId": "ddaa65cc-2658-4e35-b6ec-9deb046e0bfb",
    #     "exactorNo": "WJ001",
    #     "id": "d83f09cb-8df3-44d5-8e46-10dbaa317e19",
    #     "unloadAreaId": "10c75b23-4134-3ef1-9097-114bacac1982",
    #     "unloadAreaName": "卸载区4",
    #     "truckId": "de4080b3-fb89-460d-b511-c2ecd73c815c",
    #     "truckNo": "de4080b3-fb89-460d-b511-c2ecd73c815c",
    #     "isauto": "false",
    #     "tempChange": 1,
    #     "immediateEffect": 1
    #     }
    #
    # real_data = {
    #      'exactorId': 'a3d09975-82d7-4a30-8c54-fe4ed8ff5a29',
    #      'exactorNo': 'BI395-02',
    #      'id': '76cb19a8-6541-4245-8ef6-40682aca9635',
    #      'unloadAreaId': 'd4675d61-2134-8be7-fb36-fb9ef3c97f7c',
    #      'unloadAreaName': '哈卸4',
    #      'truckId': '7c196882-8850-4344-aff8-846a597f7792',
    #      'truckNo': '7c196882-8850-4344-aff8-846a597f7792',
    #      'isauto': 'true',
    #      'tempChange': 1,
    #      'immediateEffect': 1
    # }
    #

    # head = {"Content-Type":"application/json; charset=UTF-8", 'Connection': 'close'}
    url_d = "http://172.16.0.103:8020/ht/api/dispatch/update"
    real_data = json.dumps(real_data)
    print(real_data)
    res_d = requests.post(url=url_d, data=real_data)
    return res_d.text

#
# truck_id, excavator_id, unload_area_id = "9ce82957-b1e1-4dee-8dd1-3bbfa2a496b5", \
#                                          "a3d09975-82d7-4a30-8c54-fe4ed8ff5a29", \
#                                          "d4675d61-2134-8be7-fb36-fb9ef3c97f7c"
#
# res = redispatch_request(truck_id, excavator_id, unload_area_id)
#
# print(res)