| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- 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
- {
- /// <summary>
- /// 通用查询帮助类
- /// </summary>
- public class QueryCommonHelper
- {
- /// <summary>
- /// 通用查询方法
- /// </summary>
- /// <typeparam name="T">泛型,要求是可实例化的对象</typeparam>
- /// <param name="statement">查询语句编号</param>
- /// <param name="paramValues">查询条件列表,不能转入null</param>
- /// <param name="onSuccess">成功回调,将返回查询结果泛型对象列表</param>
- /// <param name="onFail">失败回调</param>
- /// <param name="requestFunc">The request function.</param>
- public static void QueryCommon<T>(string statement, List<QueryCommonParam> paramValues,
- Action<List<T>> onSuccess, Action<ErrorEntity> onFail, string requestFunc = null) where T : class, new()
- {
- QueryCommon<T>(statement, paramValues, new Action<PageInfo<T>>((pageInfo) =>
- {
- if (null != pageInfo && pageInfo.QueryResults != null)
- {
- onSuccess(pageInfo.QueryResults);
- }
- else
- {
- onSuccess(new List<T>()); ////返回成功,但数据有问题或没有数据。
- }
- }), onFail, null, requestFunc);
- }
- /// <summary>
- /// 通用查询方法
- /// </summary>
- /// <typeparam name="T">泛型,要求是可实例化的对象</typeparam>
- /// <param name="statement">查询语句编号</param>
- /// <param name="paramValues">查询条件列表,不能转入null</param>
- /// <param name="onSuccess">成功回调,将返回查询结果泛型对象列表</param>
- /// <param name="onFail">失败回调</param>
- /// <param name="pageInfo">The page information.</param>
- /// <param name="requestFunc">The request function.</param>
- public static void QueryCommon<T>(string statement, List<QueryCommonParam> paramValues, Action<PageInfo<T>> onSuccess, Action<ErrorEntity> onFail, PageInfo<object> 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<TCPPackage>((responsePackage) =>
- {
- // 数据发送成功返回
- if (null == responsePackage.Content)
- if (null != onSuccess)
- onSuccess(new PageInfo<T>()); // warning:异常收到空数据内容的包体,目前先直接返回空列表
- switch (LinkManager.Instance.Parameters.TradeDatagramType)
- {
- case Infrastructure.LinkProxy.Enum.DatagramType.Protobuf: // Protobuff
- AnalysisResponseWithProtobuf<T>(responsePackage.Content, onSuccess, onFail);
- break;
- case Infrastructure.LinkProxy.Enum.DatagramType.Json: // Json
- break;
- default:
- break;
- }
- }), new Action<int, string>((errorCode, errorDesc) =>
- {
- // 数据发送失败
- if (null != onFail)
- onFail(new ErrorEntity() { ReturnCode = errorCode, ReturnDesc = errorDesc });
- }));
- }
- /// <summary>
- /// 生成Protobuf类型的通用查询请求内容体的方法
- /// </summary>
- /// <param name="statement">SELECT ID</param>
- /// <param name="paramValues">条件对象列表</param>
- /// <returns>通用查询请求内容体</returns>
- private static byte[] BuildRequestWithProtobuf(string statement, List<QueryCommonParam> paramValues, PageInfo<object> 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);
- }
- /// <summary>
- /// 分析通用查询返回数据的方法,采用Protobuf结构体
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="content">数据内容</param>
- /// <param name="onSuccess">成功回调</param>
- /// <param name="onFail">失败回调</param>
- private static void AnalysisResponseWithProtobuf<T>(byte[] content, Action<List<T>> onSuccess, Action<ErrorEntity> onFail) where T : class, new()
- {
- // 获取通用查询返回对象
- var queryCommonRsp = ProtoBufHelper.EntityDeSerialize<QueryCommonRsp>(content);
- // warning: 这里要处理queryCommonRsp的null异常
- if (queryCommonRsp.RetCode == 0)
- {
- // 操作成功
- List<T> returnList = new List<T>();
- if (queryCommonRsp.Rsps.Any()) ////可能没有数据
- {
- returnList = AnalysisPackage<T>(queryCommonRsp);
- }
- // 调用成功回调
- if (null != onSuccess)
- onSuccess(returnList);
- }
- else
- {
- // 操作失败
- if (null != onFail)
- {
- onFail(new ErrorEntity() { ReturnCode = queryCommonRsp.RetCode, ReturnDesc = queryCommonRsp.RetDesc });
- }
- }
- }
- /// <summary>
- /// 分析通用查询返回数据的方法,采用Protobuf结构体
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="content">数据内容</param>
- /// <param name="onSuccess">成功回调</param>
- /// <param name="onFail">失败回调</param>
- private static void AnalysisResponseWithProtobuf<T>(byte[] content, Action<PageInfo<T>> onSuccess, Action<ErrorEntity> onFail) where T : class, new()
- {
- // 获取通用查询返回对象
- var queryCommonRsp = ProtoBufHelper.EntityDeSerialize<QueryCommonRsp>(content);
- // warning: 这里要处理queryCommonRsp的null异常
- if (queryCommonRsp.RetCode == 0)
- {
- PageInfo<T> pageInfo = new PageInfo<T>();
- if (queryCommonRsp.PageInfo != null)
- {
- pageInfo.PageNumber = queryCommonRsp.PageInfo.PageNumber;
- pageInfo.TotalCount = queryCommonRsp.PageInfo.TotalCount;
- pageInfo.RecordPerPage = queryCommonRsp.PageInfo.RecordPerPage;
- }
- // 操作成功
- List<T> returnList = new List<T>();
- if (queryCommonRsp.Rsps.Any()) ////可能没有数据
- {
- returnList = AnalysisPackage<T>(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<T> AnalysisPackage<T>(QueryCommonRsp queryCommonRsp) where T : class, new()
- {
- // 操作成功
- List<T> returnList = new List<T>();
- // 获取泛型对象所有属性字段
- Type type = typeof(T);
- List<PropertyInfo> 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;
- }
- }
- }
|