|
|
using Estsh.Client.Common.Util;
|
|
|
using Estsh.Core.Model.Result;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Newtonsoft.Json;
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Estsh.Client.Common.HttpClientUtil
|
|
|
{
|
|
|
public class HttpClientHelper
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 服务提供者对象
|
|
|
/// </summary>
|
|
|
private static IServiceProvider serviceProvider;
|
|
|
/// <summary>
|
|
|
/// httpClient请求类
|
|
|
/// </summary>
|
|
|
private static HttpClient client;
|
|
|
/// <summary>
|
|
|
/// DES加密Key
|
|
|
/// </summary>
|
|
|
private readonly static string encryptKey = "estsh.mes.selectupdate";
|
|
|
|
|
|
/// <summary>
|
|
|
/// 登录用户ID
|
|
|
/// </summary>
|
|
|
public int userId { get; set; }
|
|
|
/// <summary>
|
|
|
/// 登录用户账号
|
|
|
/// </summary>
|
|
|
public string userNo { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 初始化服务和httpClient
|
|
|
/// </summary>
|
|
|
/// <param name="provider"></param>
|
|
|
/// <param name="httpClientName"></param>
|
|
|
public static void InitService(IServiceProvider provider,string httpClientName)
|
|
|
{
|
|
|
ArgumentNullException.ThrowIfNull(provider, nameof(provider));
|
|
|
serviceProvider = provider;
|
|
|
client = GetHttpClientFactory().CreateClient(httpClientName);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 得到IHttpClientFactory的对象
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static IHttpClientFactory GetHttpClientFactory()
|
|
|
{
|
|
|
return serviceProvider.GetService<IHttpClientFactory>();
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获得HttpClient
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public static HttpClient GetHttpClient()
|
|
|
{
|
|
|
return client;
|
|
|
|
|
|
}
|
|
|
#region 通用的sql执行方法
|
|
|
|
|
|
/// <summary>
|
|
|
/// 执行sql
|
|
|
/// </summary>
|
|
|
/// <param name="cmd"></param>
|
|
|
/// <param name="Params"></param>
|
|
|
/// <returns></returns>
|
|
|
public int Execute(string cmd, Dictionary<string, object>? Params = null)
|
|
|
{
|
|
|
RequestBody requestBody = new RequestBody();
|
|
|
requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
|
requestBody.Params = Params;
|
|
|
string retust = Post("DataHandle/Execute", requestBody);
|
|
|
return JsonConvert.DeserializeObject<int>(retust);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 查询sql,返回列表
|
|
|
/// </summary>
|
|
|
/// <param name="cmd"></param>
|
|
|
/// <param name="Params"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetEntityList(string cmd, Dictionary<string, object>? Params = null)
|
|
|
{
|
|
|
RequestBody requestBody = new RequestBody();
|
|
|
requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
|
requestBody.Params = Params;
|
|
|
string retust = Post("DataHandle/GetEntityList", requestBody);
|
|
|
return JsonConvert.DeserializeObject<List<dynamic>>(retust) ;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 执行存储过程
|
|
|
/// </summary>
|
|
|
/// <param name="cmd"></param>
|
|
|
/// <param name="Params"></param>
|
|
|
/// <param name="OutParams">存储过程出参</param>
|
|
|
/// <returns></returns>
|
|
|
public Hashtable ExecuteSotreProcedure(string cmd, Dictionary<string, object>? Params = null, Dictionary<string, object>? OutParams = null)
|
|
|
{
|
|
|
RequestBody requestBody = new RequestBody();
|
|
|
requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
|
requestBody.Params = Params;
|
|
|
requestBody.OutParams = OutParams;
|
|
|
|
|
|
string retust = Post("DataHandle/ExecuteSotreProcedure", requestBody);
|
|
|
return JsonConvert.DeserializeObject<Hashtable>(retust);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取单个值
|
|
|
/// </summary>
|
|
|
/// <param name="cmd"></param>
|
|
|
/// <param name="Params"></param>
|
|
|
/// <returns></returns>
|
|
|
public object GetScalar(string cmd, Dictionary<string, object>? Params = null)
|
|
|
{
|
|
|
RequestBody requestBody = new RequestBody();
|
|
|
requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey);
|
|
|
requestBody.Params = Params;
|
|
|
string retust = Post("DataHandle/GetScalar", requestBody);
|
|
|
return JsonConvert.DeserializeObject<object>(retust);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取服务器时间
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public string GetServerTime()
|
|
|
{
|
|
|
string retust = Post("DataHandle/GetServerTime");
|
|
|
return JsonConvert.DeserializeObject<string>(retust);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 常用的业务查询方法
|
|
|
/// <summary>
|
|
|
/// 查询记录是否存在
|
|
|
/// </summary>
|
|
|
/// <param name="SqlString"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool Exists(string SqlString)
|
|
|
{
|
|
|
if (this.GetScalar(SqlString) != null)
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取工厂信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetFactoyData()
|
|
|
{
|
|
|
string sqlString = "select factory_id, factory_code, factory_name from sys_factory where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取部门信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetDeptData()
|
|
|
{
|
|
|
string sqlString = "select dept_id, dept_code, dept_name from sys_dept where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取班次信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetShiftData()
|
|
|
{
|
|
|
string sqlString = "select shift_id, shift_code, shift_name from sys_shift where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取工站信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetStageData()
|
|
|
{
|
|
|
string sqlString = "select stage_id, stage_code, stage_name from sys_stage where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取工序信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetProcessData()
|
|
|
{
|
|
|
string sqlString = "select process_id, process_code, process_name from dbo.sys_process where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取零件信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetPartNoData()
|
|
|
{
|
|
|
string sqlString = "select part_id, part_no, part_spec from dbo.sys_part where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取操作类型
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetOPTypeData()
|
|
|
{
|
|
|
string sqlString = "select type_id, type_name from sys_operate_type";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取产线信息
|
|
|
/// </summary>
|
|
|
/// <param name="factoryID"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetPDLineData(int factoryID)
|
|
|
{
|
|
|
string sqlString = "select pdline_id, pdline_name from sys_pdline where factory_id=@factory_id";
|
|
|
Dictionary<string, object> hashtable = new Dictionary<string, object>(1);
|
|
|
hashtable.Add("@factory_id", factoryID);
|
|
|
return this.GetEntityList(sqlString, hashtable);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取流程信息
|
|
|
/// </summary>
|
|
|
/// <param name="factoryID"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetRouteData(int factoryID)
|
|
|
{
|
|
|
string sqlString = "select route_id, route_name from sys_route where enabled='Y' and factory_id=@factory_id";
|
|
|
Dictionary<string, object> hashtable = new Dictionary<string, object>(1);
|
|
|
hashtable.Add("@factory_id", factoryID);
|
|
|
return this.GetEntityList(sqlString, hashtable);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取仓库信息
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetWarehouseData()
|
|
|
{
|
|
|
string sqlString = "select warehouse_id,warehouse_name from sys_warehouse where enabled='Y'";
|
|
|
return this.GetEntityList(sqlString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取枚举信息
|
|
|
/// </summary>
|
|
|
/// <param name="enumType"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetSysEnumData(string enumType)
|
|
|
{
|
|
|
string sqlString = "select enum_value,enum_desc from sys_enum where enum_type=@enum_type";
|
|
|
Dictionary<string, object> hashtable = new Dictionary<string, object>(1);
|
|
|
hashtable.Add("@enum_type", enumType);
|
|
|
return this.GetEntityList(sqlString, hashtable);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 通用的获取数据列表
|
|
|
/// </summary>
|
|
|
/// <param name="key"></param>
|
|
|
/// <param name="value"></param>
|
|
|
/// <param name="table"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<dynamic> GetListData(string key, string value, string table)
|
|
|
{
|
|
|
return this.GetEntityList(string.Format("select {0},{1} from {2} where enabled='Y'", key, value, table));
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取系统配置信息
|
|
|
/// </summary>
|
|
|
/// <param name="paramName"></param>
|
|
|
/// <returns></returns>
|
|
|
public string GetSysBase(string paramName)
|
|
|
{
|
|
|
string sqlString = "select param_value from dbo.sys_base where param_name=@param_name";
|
|
|
Dictionary<string, object> hashtable = new Dictionary<string, object>(1);
|
|
|
hashtable.Add("@param_name", paramName);
|
|
|
object scalar = this.GetScalar(sqlString, hashtable);
|
|
|
string result;
|
|
|
if (scalar == null)
|
|
|
{
|
|
|
result = string.Empty;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
result = scalar.ToString();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 检查列表数据是否有内容
|
|
|
/// </summary>
|
|
|
/// <param name="objList"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool CheckDataValid(List<dynamic> objList)
|
|
|
{
|
|
|
return objList != null && objList.Count != 0;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Inserts the specified table name.
|
|
|
/// </summary>
|
|
|
/// <param name="TableName">Name of the table.</param>
|
|
|
/// <param name="Cols">The cols.</param>
|
|
|
/// <returns></returns>
|
|
|
public bool Insert(string TableName, Dictionary<string, object> Cols)
|
|
|
{
|
|
|
int Count = 0;
|
|
|
|
|
|
if (Cols.Count <= 0)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
string Fields = " (";
|
|
|
string Values = " Values(";
|
|
|
|
|
|
foreach (var item in Cols)
|
|
|
{
|
|
|
if (Count != 0)
|
|
|
{
|
|
|
Fields += ",";
|
|
|
Values += ",";
|
|
|
}
|
|
|
|
|
|
Fields += item.Key.ToString();
|
|
|
|
|
|
if (item.Value.GetType().ToString() == "System.String")
|
|
|
Values += "'" + item.Value.ToString() + "'";
|
|
|
else
|
|
|
Values += item.Value.ToString();
|
|
|
|
|
|
Count++;
|
|
|
}
|
|
|
|
|
|
Fields += ")";
|
|
|
Values += ")";
|
|
|
|
|
|
string SqlString = "Insert Into " + TableName + Fields + Values;
|
|
|
|
|
|
return Convert.ToBoolean(this.Execute(SqlString));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// Updates the specified table name.
|
|
|
/// </summary>
|
|
|
/// <param name="TableName">Name of the table.</param>
|
|
|
/// <param name="Cols">The cols.</param>
|
|
|
/// <param name="Where">The where.</param>
|
|
|
/// <returns></returns>
|
|
|
public bool Update(string TableName, Dictionary<string, object> Cols, string Where)
|
|
|
{
|
|
|
int Count = 0;
|
|
|
|
|
|
if (Cols.Count <= 0)
|
|
|
return true;
|
|
|
|
|
|
string Fields = " ";
|
|
|
|
|
|
foreach (var item in Cols)
|
|
|
{
|
|
|
if (Count != 0)
|
|
|
Fields += ",";
|
|
|
|
|
|
Fields += item.Key.ToString();
|
|
|
Fields += "=";
|
|
|
if (item.Value.GetType().ToString() == "System.String")
|
|
|
Fields += "'" + item.Value.ToString() + "'";
|
|
|
else
|
|
|
Fields += item.Value.ToString();
|
|
|
Count++;
|
|
|
}
|
|
|
|
|
|
Fields += " ";
|
|
|
|
|
|
string SqlString = "Update " + TableName + " Set " + Fields + " " + Where;
|
|
|
return Convert.ToBoolean(this.Execute(SqlString));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取更新 SQL 语句,用于事务处理
|
|
|
/// </summary>
|
|
|
/// <param name="TableName">表名</param>
|
|
|
/// <param name="Cols">字段及值</param>
|
|
|
/// <param name="Where">Where 子语句(例如: where aa=11)</param>
|
|
|
/// <param name="ToLower">是否转换成小写</param>
|
|
|
/// <returns>UPDATE SQL</returns>
|
|
|
public string Update(string TableName, Dictionary<string, object> Cols, string Where, bool ToLower)
|
|
|
{
|
|
|
int Count = 0;
|
|
|
|
|
|
if (Cols.Count <= 0)
|
|
|
return string.Empty;
|
|
|
|
|
|
string Fields = " ";
|
|
|
|
|
|
foreach (var item in Cols)
|
|
|
{
|
|
|
if (Count != 0)
|
|
|
Fields += ",";
|
|
|
|
|
|
Fields += item.Key.ToString();
|
|
|
Fields += "=";
|
|
|
if (item.Value.GetType().ToString() == "System.String")
|
|
|
Fields += "'" + item.Value.ToString() + "'";
|
|
|
else
|
|
|
Fields += item.Value.ToString();
|
|
|
Count++;
|
|
|
}
|
|
|
|
|
|
Fields += " ";
|
|
|
|
|
|
string SqlString = "Update " + TableName + " Set " + Fields + " " + Where;
|
|
|
|
|
|
if (ToLower)
|
|
|
return SqlString.ToLower();
|
|
|
|
|
|
return SqlString;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region WebApi请求方法
|
|
|
// <summary>
|
|
|
// Post请求数据--同步方法
|
|
|
// </summary>
|
|
|
// <param name="actionUri">例如/api/Files/UploadFile</param>
|
|
|
// <param name="body">请求参数</param>
|
|
|
// <returns></returns>
|
|
|
public string Post(string actionUri, RequestBody? body = null)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string requestUri = client.BaseAddress + actionUri;
|
|
|
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
|
|
var result = client.PostAsync(requestUri, content).GetAwaiter().GetResult();
|
|
|
|
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|
|
{
|
|
|
string str = result.Content.ReadAsStringAsync().Result;
|
|
|
return str;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return string.Empty;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error("调用API异常", ex);
|
|
|
throw ex;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// <summary>
|
|
|
// Post请求数据-异步方法
|
|
|
// </summary>
|
|
|
// <param name="actionUri">例如/api/Files/UploadFile</param>
|
|
|
// <param name="body">请求参数</param>
|
|
|
// <returns></returns>
|
|
|
public async Task<string> PostAsync(string actionUri, RequestBody? body = null)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string requestUri = client.BaseAddress + actionUri;
|
|
|
|
|
|
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
|
|
|
|
|
|
var result = await client.PostAsync(requestUri, content);
|
|
|
|
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|
|
{
|
|
|
string str = result.Content.ReadAsStringAsync().Result;
|
|
|
return str;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return string.Empty;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error("调用API异常", ex);
|
|
|
throw ex;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|