GQ_Communicate/GQ_TongXin/Assets/script/接口/查询/LineQuery.cs

265 lines
6.7 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineQuery : MonoBehaviour
{
public static LineQuery Inst;
[System.Serializable]
public struct Dic
{
public string lineContentJson;
public string id;
}
public List<Dic> dic;//创建结构体对应的数组
/// <summary>
/// key:lineContentJson Value:id
/// </summary>
public Dictionary<string, string> keyValues = new Dictionary<string, string>();
private void Awake()
{
if (Inst != this && Inst != null)
{
Destroy(this.gameObject);
}
else
{
Inst = this;
DontDestroyOnLoad(this.gameObject);
}
}
void Start()
{
//StartCoroutine(getJsonCoroutine());
}
void Update()
{
}
/// <summary>
/// 查询线缆组
/// </summary>
public IEnumerator getJsonCoroutine()
{
Root root = new Root();
yield return StartCoroutine(getJson(root, (x) => { root = x; }));
if (root != null && root.message == "操作成功")
{
if (root.data != null && root.data.Count > 0)
{
for (int i = 0; i < root.data.Count; i++)
{
if (!keyValues.ContainsKey(root.data[i].lineContentJson))
{
keyValues.Add(root.data[i].lineContentJson, root.data[i].id);
var d = new Dic();
d.id = root.data[i].id;
d.lineContentJson = root.data[i].lineContentJson;
dic.Add(d);
}
}
}
else
{
string lineContentJson = "";
yield return StartCoroutine(SaveJsonCoroutine(lineContentJson));
}
}
else
{
}
}
/// <summary>
/// 删除线缆组
/// </summary>
public IEnumerator deleteJsonCoroutine(string id)
{
Root root = new Root();
yield return StartCoroutine(deleteJson(root, id, (x) => { root = x; }));
if (root != null && root.message == "操作成功")
{
foreach (var item in keyValues.Keys)
{
if (keyValues[item] == id)
{
keyValues.Remove(item);
for (int i = 0; i < dic.Count; i++)
{
if (dic[i].id == id)
dic.Remove(dic[i]);
}
break;
}
}
}
else
{
}
}
/// <summary>
/// 新增线缆组
/// </summary>
public IEnumerator SaveJsonCoroutine(string lineContentJson)
{
Root root = new Root();
yield return StartCoroutine(saveJson(root, lineContentJson, (x) => { root = x; }));
if (root != null && root.message == "操作成功")
{
yield return StartCoroutine(getJsonCoroutine());
}
else
{
}
}
/// <summary>
/// 新增
/// </summary>
public IEnumerator saveJson(Root root, string lineContentJson, Action<Root> callback)
{
var C = new Root_lineContentJson();
C.lineContentJson = lineContentJson;
string newData = JsonConvert.SerializeObject(C);
yield return
StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xlz_xz, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("新增线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
/// <summary>
/// 查询
/// </summary>
public IEnumerator getJson(Root root, Action<Root> callback)
{
yield return
StartCoroutine(
CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.xlz_cx, GameManager.Inst.arguments.token, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("查询线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public IEnumerator deleteJson(Root root, string id, Action<Root> callback)
{
var C = new Root_id();
C.id = id;
string newData = JsonConvert.SerializeObject(C);
yield return
StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xlz_sc, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("删除线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
#region JSON
[System.Serializable]
public class LineList
{
/// <summary>
///
/// </summary>
public string id;
/// <summary>
///
/// </summary>
public string lineContentJson;
/// <summary>
///
/// </summary>
public string createTime;
}
[System.Serializable]
public class Root
{
/// <summary>
///
/// </summary>
public string code;
/// <summary>
/// 反馈结果
/// </summary>
public string message;
/// <summary>
///
/// </summary>
public List<LineList> data;
/// <summary>
///
/// </summary>
public string serverTime;
}
public class Root_lineContentJson
{
/// <summary>
///
/// </summary>
public string lineContentJson;
}
public class Root_id
{
/// <summary>
///
/// </summary>
public string id;
}
#endregion
}