Utils.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Variables;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PTMedicalInsurance.Common
  10. {
  11. public class Utils
  12. {
  13. #region 工具
  14. /// <summary>
  15. /// 日期转换为时间戳Timestamp
  16. /// </summary>
  17. /// <param name="dateTime">要转换的日期</param>
  18. /// <returns></returns>
  19. public static long GetTimeStamp(DateTime dateTime)
  20. {
  21. DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
  22. //10位的时间戳
  23. long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
  24. //13位的时间戳
  25. //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
  26. return timeStamp;
  27. }
  28. /// <summary>
  29. /// UTC时间戳Timestamp转换为北京时间
  30. /// </summary>
  31. /// <param name="timestamp">要转换的时间戳</param>
  32. /// <returns></returns>
  33. public static DateTime GetDateTime(long timestamp)
  34. {
  35. //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  36. //使用上面的方式会显示TimeZone已过时
  37. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
  38. DateTime targetDt = dtStart.AddMilliseconds(timestamp).AddHours(8);
  39. return targetDt;
  40. }
  41. /// <summary>
  42. /// 将JObject对象中的timestamp数据转为yyyy-MM-dd格式
  43. /// </summary>
  44. /// <param name="joObject"></param>
  45. /// <param name="key"></param>
  46. public static void convertTimestamp(JObject joObject,string key)
  47. {
  48. if (joObject.ContainsKey(key))
  49. {
  50. Global.writeLog(string.Format("当前对象[{0}]的类型为:{1}", key, joObject.GetValue(key).Type.GetType()));
  51. }
  52. if (joObject.ContainsKey(key) && joObject.GetValue(key).Type.GetType() != typeof(string))
  53. {
  54. long timestamp = long.Parse(joObject.GetValue(key).ToString());
  55. string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd");
  56. joObject[key] = datetime;
  57. Global.writeLog(string.Format("转换后的日期为:{0}",datetime));
  58. }
  59. }
  60. /// <summary>
  61. /// 跨省异地
  62. /// </summary>
  63. /// <returns></returns>
  64. public static bool isOtherProvince(string areaCode)
  65. {
  66. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length > 2 && areaCode.Substring(0, 2) != Global.inf.areaCode.Substring(0,2))
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. public static bool isOtherProvince()
  73. {
  74. return isOtherProvince(Global.pat.insuplc_admdvs);
  75. }
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. /// <returns></returns>
  80. public static bool isOtherCity()
  81. {
  82. return isOtherCity(Global.pat.insuplc_admdvs);
  83. }
  84. /// <summary>
  85. /// 省内异地
  86. /// </summary>
  87. /// <returns></returns>
  88. public static bool isOtherCity(string areaCode)
  89. {
  90. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length > 4 && areaCode.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4) && areaCode.Substring(0, 2) == Global.inf.areaCode.Substring(0, 2))
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. #endregion
  97. #region BeanUtils
  98. /// <summary>
  99. /// 用反射对实体进行属性值合并,返回合并后的属性并集
  100. /// </summary>
  101. /// <typeparam name="T"></typeparam>
  102. /// <param name="entity">合并对象1</param>
  103. /// <param name="addtional">合并对象2</param>
  104. /// <param name="forceCover">是否强制覆盖</param>
  105. /// <returns></returns>
  106. public static T merge<T>(T entity, T addtional,bool forceCover)
  107. {
  108. if (entity == null) return addtional;
  109. if (addtional == null) return entity;
  110. // 进行合并
  111. Type mType = typeof(T);
  112. object result = Activator.CreateInstance(mType);
  113. PropertyInfo[] pi = mType.GetProperties();
  114. pi.ToList().ForEach((p) =>
  115. {
  116. object v1 = p.GetValue(entity);
  117. object v2 = p.GetValue(addtional);
  118. if (forceCover)
  119. {
  120. v1 = v2;
  121. }
  122. else {
  123. v1 = (v1 == null ? v2 : v1);
  124. }
  125. p.SetValue(result, v1);
  126. });
  127. return (T)result;
  128. }
  129. /// <summary>
  130. /// 对2个对象进行合并,以第一个对象为重点
  131. /// </summary>
  132. /// <typeparam name="T"></typeparam>
  133. /// <param name="entity"></param>
  134. /// <param name="addtional"></param>
  135. /// <returns></returns>
  136. public static T merge<T>(T entity, T addtional)
  137. {
  138. return merge(entity, addtional, false);
  139. }
  140. private static JObject merge(JObject to, JObject from)
  141. {
  142. to.Merge(from);
  143. return to;
  144. }
  145. /// <summary>
  146. /// 采用Json的方式进行属性合并(默认会覆盖)
  147. /// </summary>
  148. /// <typeparam name="T"></typeparam>
  149. /// <param name="to"></param>
  150. /// <param name="from"></param>
  151. /// <returns></returns>
  152. public static T mergeObject<T>(T to, T from)
  153. {
  154. JObject toObj = JObject.FromObject(to);
  155. JObject fromObj = JObject.FromObject(from);
  156. JObject result = merge(toObj, fromObj);
  157. return result.ToObject<T>();
  158. }
  159. #endregion
  160. #region 业务个性化
  161. /// <summary>
  162. /// 在JObject对象基础上进行包装
  163. /// </summary>
  164. /// <param name="jsonInParam"></param>
  165. /// <returns></returns>
  166. public static JObject Wrapper(JObject jsonInParam)
  167. {
  168. //dynamic request = new JObject();
  169. //request.arg0 = jsonInParam;
  170. //return request;
  171. return jsonInParam;
  172. }
  173. /// <summary>
  174. /// 删除JObject上的进行包装
  175. /// </summary>
  176. /// <param name="jsonInParam"></param>
  177. /// <returns></returns>
  178. public static JObject removeWrapper(JObject jsonInParam)
  179. {
  180. //if (jsonInParam.ContainsKey("arg0"))
  181. //{
  182. // return (JObject)jsonInParam.GetValue("arg0");
  183. //}
  184. return jsonInParam;
  185. }
  186. public static bool isOtherCityPatient()
  187. {
  188. // 重庆没有市内异地
  189. string areaCode = Global.pat.insuplc_admdvs;
  190. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "50")
  191. {
  192. return true;
  193. }
  194. return false;
  195. }
  196. public static string convertAdmDr(string admNo)
  197. {
  198. // 重庆异地(每次唯一)
  199. if (isOtherCityPatient())
  200. {
  201. // 住院号15位,控制总长度不超过20
  202. string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  203. // 保存到扩展信息字段中
  204. JObject exp = new JObject();
  205. exp.Add("admDr",ret);
  206. Global.pat.ExpContent = exp.ToString();
  207. return ret;
  208. }
  209. return admNo;
  210. }
  211. #endregion
  212. }
  213. }