using Muchinfo.PC.Common.Helpers; using Muchinfo.MTPClient.Adapter.Utilities; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Infrastructure.LinkProxy; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.NetworkCore; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using ProtoBuf; using tas; using System.Reflection; namespace Muchinfo.MTPClient.Service.Utilities { /// /// 通用查询帮助类 /// public class QueryCommonHelper { /// /// 通用查询方法 /// /// 泛型,要求是可实例化的对象 /// 查询语句编号 /// 查询条件列表,不能转入null /// 成功回调,将返回查询结果泛型对象列表 /// 失败回调 /// The request function. public static void QueryCommon(string statement, List paramValues, Action> onSuccess, Action onFail, string requestFunc = null) where T : class, new() { QueryCommon(statement, paramValues, new Action>((pageInfo) => { if (null != pageInfo && pageInfo.QueryResults != null) { onSuccess(pageInfo.QueryResults); } else { onSuccess(new List()); ////返回成功,但数据有问题或没有数据。 } }), onFail, null, requestFunc); } /// /// 通用查询方法 /// /// 泛型,要求是可实例化的对象 /// 查询语句编号 /// 查询条件列表,不能转入null /// 成功回调,将返回查询结果泛型对象列表 /// 失败回调 /// The page information. /// The request function. public static void QueryCommon(string statement, List paramValues, Action> onSuccess, Action onFail, PageInfo pageInfo = null, string requestFunc = null) where T : class, new() { byte[] reqeust = null; // 判断当前数据类型,构建用于发送的数据 switch (LinkManager.Instance.Parameters.TradeDatagramType) { case Infrastructure.LinkProxy.Enum.DatagramType.Protobuf: // Protobuff reqeust = BuildRequestWithProtobuf(statement, paramValues, pageInfo); break; case Infrastructure.LinkProxy.Enum.DatagramType.Json: // Json break; default: break; } if (null == reqeust) { if (null != onFail) onFail(new ErrorEntity() { ReturnCode = -1, ReturnDesc = "通用查询请求错误", RequestFunc = requestFunc }); // 错误数据 return; } // 发送数据 var package = new TCPPackage() { Content = reqeust, FunCode = FuncCode.FID_QueryCommonReq }; LinkManager.Instance.TradeTcpLinkProxy.SendPackage(package, new Action((responsePackage) => { // 数据发送成功返回 if (null == responsePackage.Content) if (null != onSuccess) onSuccess(new PageInfo()); // warning:异常收到空数据内容的包体,目前先直接返回空列表 switch (LinkManager.Instance.Parameters.TradeDatagramType) { case Infrastructure.LinkProxy.Enum.DatagramType.Protobuf: // Protobuff AnalysisResponseWithProtobuf(responsePackage.Content, onSuccess, onFail); break; case Infrastructure.LinkProxy.Enum.DatagramType.Json: // Json break; default: break; } }), new Action((errorCode, errorDesc) => { // 数据发送失败 if (null != onFail) onFail(new ErrorEntity() { ReturnCode = errorCode, ReturnDesc = errorDesc }); })); } /// /// 生成Protobuf类型的通用查询请求内容体的方法 /// /// SELECT ID /// 条件对象列表 /// 通用查询请求内容体 private static byte[] BuildRequestWithProtobuf(string statement, List paramValues, PageInfo pageInfo = null) { // warning: 在这个工程里使用Protobuf需要引用dll,与邓确认此事 var queryCommonReq = new QueryCommonReq(); queryCommonReq.Statement = statement; if (pageInfo != null) { var queryPageInfo = new QueryReqPageInfo(); queryPageInfo.PageNumber = pageInfo.PageNumber; queryPageInfo.NeedTotalCount = 1; queryPageInfo.RecordPerPage = pageInfo.RecordPerPage; // queryPageInfo. = pageInfo.PageNumber; queryCommonReq.PageInfo = queryPageInfo; } foreach (QueryCommonParam param in paramValues) { var p = new ParamValue() { Key = param.ParamKey, Value = param.ParamValue }; queryCommonReq.ParamValues.Add(p); } return ProtoBufHelper.EntitySerialize(queryCommonReq); } /// /// 分析通用查询返回数据的方法,采用Protobuf结构体 /// /// 泛型 /// 数据内容 /// 成功回调 /// 失败回调 private static void AnalysisResponseWithProtobuf(byte[] content, Action> onSuccess, Action onFail) where T : class, new() { // 获取通用查询返回对象 var queryCommonRsp = ProtoBufHelper.EntityDeSerialize(content); // warning: 这里要处理queryCommonRsp的null异常 if (queryCommonRsp.RetCode == 0) { // 操作成功 List returnList = new List(); if (queryCommonRsp.Rsps.Any()) ////可能没有数据 { returnList = AnalysisPackage(queryCommonRsp); } // 调用成功回调 if (null != onSuccess) onSuccess(returnList); } else { // 操作失败 if (null != onFail) { onFail(new ErrorEntity() { ReturnCode = queryCommonRsp.RetCode, ReturnDesc = queryCommonRsp.RetDesc }); } } } /// /// 分析通用查询返回数据的方法,采用Protobuf结构体 /// /// 泛型 /// 数据内容 /// 成功回调 /// 失败回调 private static void AnalysisResponseWithProtobuf(byte[] content, Action> onSuccess, Action onFail) where T : class, new() { // 获取通用查询返回对象 var queryCommonRsp = ProtoBufHelper.EntityDeSerialize(content); // warning: 这里要处理queryCommonRsp的null异常 if (queryCommonRsp.RetCode == 0) { PageInfo pageInfo = new PageInfo(); if (queryCommonRsp.PageInfo != null) { pageInfo.PageNumber = queryCommonRsp.PageInfo.PageNumber; pageInfo.TotalCount = queryCommonRsp.PageInfo.TotalCount; pageInfo.RecordPerPage = queryCommonRsp.PageInfo.RecordPerPage; } // 操作成功 List returnList = new List(); if (queryCommonRsp.Rsps.Any()) ////可能没有数据 { returnList = AnalysisPackage(queryCommonRsp); #if DEBUG //获取通用查询返回字段及类型,便于生成对象 var strfff = new StringBuilder(); foreach (var field in queryCommonRsp.Rsps[0].FieldInfos) { strfff.Append(string.Format("[PropertyDisc('{1}')] public *{0}* {1} + get; set; -", field.FieldType.ToLower(), field.FieldName.ToLower())); } var aaa = strfff.ToString().Replace('+', '{').Replace('-', '}') .Replace("*bigint*", "long").Replace("*varchar*", "string") .Replace("*char*", "string").Replace("*decimal*", "decimal") .Replace("*timestamp*", "DateTime"); Debug.WriteLine(aaa); #endif pageInfo.QueryResults = returnList; } // 调用成功回调 if (null != onSuccess) onSuccess(pageInfo); } else { // 操作失败 if (null != onFail) { onFail(new ErrorEntity() { ReturnCode = queryCommonRsp.RetCode, ReturnDesc = queryCommonRsp.RetDesc }); } } } private static List AnalysisPackage(QueryCommonRsp queryCommonRsp) where T : class, new() { // 操作成功 List returnList = new List(); // 获取泛型对象所有属性字段 Type type = typeof(T); List properties = type.GetProperties().Where((item) => item.CanWrite).ToList(); bool isSeted = false; foreach (RowValue rowValue in queryCommonRsp.Rsps[0].RowValues) // 返回数据行循环 { T t = new T(); isSeted = false; foreach (var propertyInfo in properties) { var custemAttrs = Attribute.GetCustomAttributes(propertyInfo); // var custemAttrs = propertyInfo.GetCustomAttributes(typeof(PropertyDiscAttribute),true); if (null == custemAttrs || !custemAttrs.Any()) ////没有标记的属性不处理 { continue; } var propertyDisc = custemAttrs.FirstOrDefault((item) => item is PropertyDiscAttribute) as PropertyDiscAttribute; if (null == propertyDisc) ////没有标记为反射属性 不进行设置值 { continue; } if (string.IsNullOrEmpty(propertyDisc.PropertyName)) ////无属性名 { continue; } for (int i = 0; i < queryCommonRsp.Rsps[0].FieldInfos.Count; i++) { try { if (string.IsNullOrWhiteSpace(rowValue.RowValues[i]) || rowValue.RowValues[i].ToLower().Equals("null")) { continue; } tas.FieldInfo fieldInfo = queryCommonRsp.Rsps[0].FieldInfos[i]; // 查找泛型对象中相同的属性名称 if (propertyDisc.PropertyName.Trim().ToLower().Equals(fieldInfo.FieldName.Trim().ToLower())) { isSeted = true; // 判断类型赋值 if (propertyInfo.PropertyType == typeof(String)) { propertyInfo.SetValue(t, rowValue.RowValues[i], null); } else if (propertyInfo.PropertyType == typeof(Int32) || propertyInfo.PropertyType == typeof(int)) { propertyInfo.SetValue(t, Convert.ToInt32(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(UInt32) || propertyInfo.PropertyType == typeof(int)) { propertyInfo.SetValue(t, Convert.ToUInt32(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(DateTime)) { DateTime date; if (DateTime.TryParse(rowValue.RowValues[i], out date)) { propertyInfo.SetValue(t, date, null); } } else if (propertyInfo.PropertyType == typeof(Decimal)) { propertyInfo.SetValue(t, Convert.ToDecimal(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(Double)) { propertyInfo.SetValue(t, Convert.ToDouble(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(Boolean)) { propertyInfo.SetValue(t, Convert.ToBoolean(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(long) || propertyInfo.PropertyType == typeof(Int64)) { propertyInfo.SetValue(t, Convert.ToInt64(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType == typeof(ulong) || propertyInfo.PropertyType == typeof(UInt64)) { propertyInfo.SetValue(t, Convert.ToUInt64(rowValue.RowValues[i]), null); } else if (propertyInfo.PropertyType.IsValueType) { propertyInfo.SetValue(t, Convert.ToInt32(rowValue.RowValues[i]), null); } } } catch (Exception ex) { LogHelper.WriteError(typeof(QueryCommonHelper), propertyDisc.PropertyName + " value: " + rowValue.RowValues[i] + " ex:" + ex.ToString()); } } } if (isSeted) returnList.Add(t); } return returnList; } } }