HitsItem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Dynamic;
  6. using System.Reflection;
  7. using System.Text;
  8. namespace CoreEntity.ESEntity
  9. {
  10. public class JObjectAccessor : DynamicObject
  11. {
  12. JToken obj;
  13. public JObjectAccessor(JToken obj)
  14. {
  15. this.obj = obj;
  16. }
  17. public override bool TryGetMember(GetMemberBinder binder, out object result)
  18. {
  19. result = null;
  20. if (obj == null) return false;
  21. var val = obj[binder.Name];
  22. if (val == null) return false;
  23. result = Populate(val);
  24. return true;
  25. }
  26. public object Populate(JToken token)
  27. {
  28. var jval = token as JValue;
  29. if (jval != null)
  30. {
  31. return jval.Value;
  32. }
  33. else if (token.Type == JTokenType.Array)
  34. {
  35. var objectAccessors = new List<object>();
  36. foreach (var item in token as JArray)
  37. {
  38. objectAccessors.Add(Populate(item));
  39. }
  40. return objectAccessors;
  41. }
  42. else
  43. {
  44. return new JObjectAccessor(token);
  45. }
  46. }
  47. }
  48. public class HitsItem<T>
  49. {
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. public string _index { get; set; }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. public string _type { get; set; }
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. public string _id { get; set; }
  62. /// <summary>
  63. ///
  64. /// </summary>
  65. public string _score { get; set; }
  66. /// <summary>
  67. ///
  68. /// </summary>
  69. public string _routing { get; set; }
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. public dynamic _source { get; set; }
  74. /// <summary>
  75. ///
  76. /// </summary>
  77. public dynamic fields { get; set; }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. public T entity()
  82. {
  83. var a = this._source!=null? Mapper<T>(this._source): Activator.CreateInstance<T>();
  84. a = MapperFields<T>(this.fields,a);
  85. return a;
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. public T entityDynamic()
  91. {
  92. var a = this._source != null ? MapperDynamic<T>(this._source) : Activator.CreateInstance<T>();
  93. a = MapperFieldsDynamic<T>(this.fields, a);
  94. return a;
  95. }
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. public List<string> sort { get; set; }
  100. /// <summary>
  101. /// 反射实现两个类的对象之间相同属性的值的复制
  102. /// 适用于初始化新实体
  103. /// </summary>
  104. /// <typeparam name="D">返回的实体</typeparam>
  105. /// <typeparam name="S">数据源实体</typeparam>
  106. /// <param name="s">数据源实体</param>
  107. /// <returns>返回的新实体</returns>
  108. public static T Mapper<T>(dynamic s)
  109. {
  110. T d = Activator.CreateInstance<T>(); //构造新实例
  111. string fieldName = "";
  112. try
  113. {
  114. //获得类型
  115. var Typed = typeof(T);
  116. foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段
  117. {
  118. if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同
  119. {
  120. Object result = null;
  121. fieldName = dp.Name;
  122. if (s[dp.Name].GetType() == typeof(JObject))
  123. {
  124. JToken token = (JToken)s[dp.Name];
  125. result = token.ToObject(dp.PropertyType, JsonSerializer.Create());
  126. //JsonConvert.PopulateObject(d);
  127. }
  128. else
  129. {
  130. if (dp.PropertyType.ToString().IndexOf("Collections.Generic.IList`1") < 0)
  131. {
  132. if (dp.PropertyType == typeof(DateTime?)) {
  133. if (s[dp.Name].Value.GetType() == typeof(Double))
  134. result = ConvertIntDatetime((double)s[dp.Name].Value);
  135. if (s[dp.Name].Value.GetType() == typeof(String)) {
  136. var datestr = s[dp.Name].Value;
  137. try {
  138. result = Convert.ToDateTime(datestr);
  139. }
  140. catch (Exception ex1)
  141. {
  142. result = null;
  143. }
  144. }
  145. }
  146. else
  147. try
  148. {
  149. result = Convert.ChangeType(s[dp.Name], dp.PropertyType);
  150. }
  151. catch (Exception ex2)
  152. {
  153. Console.WriteLine(" dp.Name:"+ dp.Name +
  154. " dp.PropertyType: " + dp.PropertyType +
  155. " s[dp.Name].Value.GetType(): " + s[dp.Name].Value.GetType()+
  156. " s[dp.Name]:" + s[dp.Name]);
  157. result = null;
  158. }
  159. }
  160. }
  161. dp.SetValue(d, result, null);//获得s对象属性的值复制给d对象的属性
  162. }
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. throw new Exception(fieldName);
  168. }
  169. return d;
  170. }
  171. /// <summary>
  172. /// 反射实现两个类的对象之间相同属性的值的复制
  173. /// 适用于初始化新实体
  174. /// </summary>
  175. /// <typeparam name="D">返回的实体</typeparam>
  176. /// <typeparam name="S">数据源实体</typeparam>
  177. /// <param name="s">数据源实体</param>
  178. /// <returns>返回的新实体</returns>
  179. public static T MapperFields<T>(dynamic s,T d)
  180. {
  181. try
  182. {
  183. if (s == null) return d;
  184. //获得类型
  185. var Typed = typeof(T);
  186. foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段
  187. {
  188. if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同
  189. {
  190. Object result = null;
  191. if (s[dp.Name].GetType() == typeof(JArray))
  192. {
  193. JArray token = (JArray)s[dp.Name];
  194. result = token[0];
  195. if (dp.PropertyType == typeof(DateTime?))
  196. result = ConvertIntDatetime(Convert.ToDouble(result));
  197. else
  198. result = Convert.ChangeType(result, dp.PropertyType);
  199. //JsonConvert.PopulateObject(d);
  200. }
  201. else
  202. {
  203. if (dp.PropertyType == typeof(DateTime?))
  204. result = ConvertIntDatetime(Convert.ToDouble(s[dp.Name]));
  205. else
  206. result = Convert.ChangeType(s[dp.Name], dp.PropertyType);
  207. }
  208. dp.SetValue(d, result, null);//获得s对象属性的值复制给d对象的属性
  209. }
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. throw ex;
  215. }
  216. return d;
  217. }
  218. /// <summary>
  219. /// 反射实现两个类的对象之间相同属性的值的复制
  220. /// 适用于初始化新实体
  221. /// </summary>
  222. /// <typeparam name="D">返回的实体</typeparam>
  223. /// <typeparam name="S">数据源实体</typeparam>
  224. /// <param name="s">数据源实体</param>
  225. /// <returns>返回的新实体</returns>
  226. public static Dictionary<string, JToken> MapperDynamic<T>(Dictionary<string, JToken> s)
  227. {
  228. Dictionary<string, JToken> d = new Dictionary<string, JToken>(); //
  229. try
  230. {
  231. //获得类型
  232. var Typed = typeof(T);
  233. foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段
  234. {
  235. if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同
  236. {
  237. JToken result = null;
  238. if (s[dp.Name].GetType() == typeof(JObject))
  239. {
  240. //JToken token = (JToken)s[dp.Name];
  241. result = s[dp.Name];
  242. }
  243. else
  244. {
  245. if (dp.PropertyType == typeof(DateTime?))
  246. result = ConvertIntDatetime((double)s[dp.Name]);
  247. else
  248. result = s[dp.Name];
  249. }
  250. d.Add(dp.Name, result);//获得s对象属性的值复制给d对象的属性
  251. }
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. throw ex;
  257. }
  258. return d;
  259. }
  260. /// <summary>
  261. /// 反射实现两个类的对象之间相同属性的值的复制
  262. /// 适用于初始化新实体
  263. /// </summary>
  264. /// <typeparam name="D">返回的实体</typeparam>
  265. /// <typeparam name="S">数据源实体</typeparam>
  266. /// <param name="s">数据源实体</param>
  267. /// <returns>返回的新实体</returns>
  268. public static Dictionary<string, JToken> MapperFieldsDynamic<T>(Dictionary<string, JToken> s, Dictionary<string, JToken> d)
  269. {
  270. try
  271. {
  272. if (s == null) return d;
  273. //获得类型
  274. var Typed = typeof(T);
  275. foreach (PropertyInfo dp in Typed.GetProperties())//获得类型的属性字段
  276. {
  277. if (s[dp.Name] != null && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同
  278. {
  279. JToken result = null;
  280. if (s[dp.Name].GetType() == typeof(JArray))
  281. {
  282. JArray token = (JArray)s[dp.Name];
  283. result = token[0];
  284. if (dp.PropertyType == typeof(DateTime?))
  285. result = ConvertIntDatetime(Convert.ToDouble(result));
  286. //else
  287. // ;
  288. }
  289. else
  290. {
  291. if (dp.PropertyType == typeof(DateTime?))
  292. result = ConvertIntDatetime(Convert.ToDouble(s[dp.Name]));
  293. else
  294. result = s[dp.Name];
  295. }
  296. d.Add(dp.Name, result);//获得s对象属性的值复制给d对象的属性
  297. }
  298. }
  299. }
  300. catch (Exception ex)
  301. {
  302. throw ex;
  303. }
  304. return d;
  305. }
  306. public static DateTime ConvertIntDatetime(double utc)
  307. {
  308. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  309. startTime = startTime.AddMilliseconds(utc);
  310. //startTime = startTime.AddHours(8);//转化为北京时间(北京时间=UTC时间+8小时 )
  311. return startTime;
  312. }
  313. internal object MapperFields<T1>(object fields, object a)
  314. {
  315. throw new NotImplementedException();
  316. }
  317. }
  318. }