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 { /// /// 服务提供者对象 /// private static IServiceProvider serviceProvider; /// /// httpClient请求类 /// private static HttpClient client; /// /// DES加密Key /// private readonly static string encryptKey = "estsh.mes.selectupdate"; /// /// 登录用户ID /// public int userId { get; set; } /// /// 登录用户账号 /// public string userNo { get; set; } /// /// 初始化服务和httpClient /// /// /// public static void InitService(IServiceProvider provider,string httpClientName) { ArgumentNullException.ThrowIfNull(provider, nameof(provider)); serviceProvider = provider; client = GetHttpClientFactory().CreateClient(httpClientName); } /// /// 得到IHttpClientFactory的对象 /// /// public static IHttpClientFactory GetHttpClientFactory() { return serviceProvider.GetService(); } /// /// 获得HttpClient /// /// public static HttpClient GetHttpClient() { return client; } #region 通用的sql执行方法 /// /// 执行sql /// /// /// /// public int Execute(string cmd, Dictionary? Params = null) { RequestBody requestBody = new RequestBody(); requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey); requestBody.Params = Params; string retust = Post("DataHandle/Execute", requestBody); return JsonConvert.DeserializeObject(retust); } /// /// 查询sql,返回列表 /// /// /// /// public List GetEntityList(string cmd, Dictionary? Params = null) { RequestBody requestBody = new RequestBody(); requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey); requestBody.Params = Params; string retust = Post("DataHandle/GetEntityList", requestBody); return JsonConvert.DeserializeObject>(retust) ; } /// /// 执行存储过程 /// /// /// /// 存储过程出参 /// public Hashtable ExecuteSotreProcedure(string cmd, Dictionary? Params = null, Dictionary? 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(retust); } /// /// 获取单个值 /// /// /// /// public object GetScalar(string cmd, Dictionary? Params = null) { RequestBody requestBody = new RequestBody(); requestBody.cmd = SecurityEncDecrypt.EncryptDES(cmd, encryptKey); requestBody.Params = Params; string retust = Post("DataHandle/GetScalar", requestBody); return JsonConvert.DeserializeObject(retust); } /// /// 获取服务器时间 /// /// public string GetServerTime() { string retust = Post("DataHandle/GetServerTime"); return JsonConvert.DeserializeObject(retust); } #endregion #region 常用的业务查询方法 /// /// 查询记录是否存在 /// /// /// public bool Exists(string SqlString) { if (this.GetScalar(SqlString) != null) return true; return false; } /// /// 获取工厂信息 /// /// public List GetFactoyData() { string sqlString = "select factory_id, factory_code, factory_name from sys_factory where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取部门信息 /// /// public List GetDeptData() { string sqlString = "select dept_id, dept_code, dept_name from sys_dept where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取班次信息 /// /// public List GetShiftData() { string sqlString = "select shift_id, shift_code, shift_name from sys_shift where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取工站信息 /// /// public List GetStageData() { string sqlString = "select stage_id, stage_code, stage_name from sys_stage where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取工序信息 /// /// public List GetProcessData() { string sqlString = "select process_id, process_code, process_name from dbo.sys_process where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取零件信息 /// /// public List GetPartNoData() { string sqlString = "select part_id, part_no, part_spec from dbo.sys_part where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取操作类型 /// /// public List GetOPTypeData() { string sqlString = "select type_id, type_name from sys_operate_type"; return this.GetEntityList(sqlString); } /// /// 获取产线信息 /// /// /// public List GetPDLineData(int factoryID) { string sqlString = "select pdline_id, pdline_name from sys_pdline where factory_id=@factory_id"; Dictionary hashtable = new Dictionary(1); hashtable.Add("@factory_id", factoryID); return this.GetEntityList(sqlString, hashtable); } /// /// 获取流程信息 /// /// /// public List GetRouteData(int factoryID) { string sqlString = "select route_id, route_name from sys_route where enabled='Y' and factory_id=@factory_id"; Dictionary hashtable = new Dictionary(1); hashtable.Add("@factory_id", factoryID); return this.GetEntityList(sqlString, hashtable); } /// /// 获取仓库信息 /// /// public List GetWarehouseData() { string sqlString = "select warehouse_id,warehouse_name from sys_warehouse where enabled='Y'"; return this.GetEntityList(sqlString); } /// /// 获取枚举信息 /// /// /// public List GetSysEnumData(string enumType) { string sqlString = "select enum_value,enum_desc from sys_enum where enum_type=@enum_type"; Dictionary hashtable = new Dictionary(1); hashtable.Add("@enum_type", enumType); return this.GetEntityList(sqlString, hashtable); } /// /// 通用的获取数据列表 /// /// /// /// /// public List GetListData(string key, string value, string table) { return this.GetEntityList(string.Format("select {0},{1} from {2} where enabled='Y'", key, value, table)); } /// /// 获取系统配置信息 /// /// /// public string GetSysBase(string paramName) { string sqlString = "select param_value from dbo.sys_base where param_name=@param_name"; Dictionary hashtable = new Dictionary(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; } /// /// 检查列表数据是否有内容 /// /// /// public bool CheckDataValid(List objList) { return objList != null && objList.Count != 0; } /// /// Inserts the specified table name. /// /// Name of the table. /// The cols. /// public bool Insert(string TableName, Dictionary 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)); } /// /// Updates the specified table name. /// /// Name of the table. /// The cols. /// The where. /// public bool Update(string TableName, Dictionary 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)); } /// /// 获取更新 SQL 语句,用于事务处理 /// /// 表名 /// 字段及值 /// Where 子语句(例如: where aa=11) /// 是否转换成小写 /// UPDATE SQL public string Update(string TableName, Dictionary 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请求方法 // // Post请求数据--同步方法 // // 例如/api/Files/UploadFile // 请求参数 // 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; } } // // Post请求数据-异步方法 // // 例如/api/Files/UploadFile // 请求参数 // public async Task 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 } }