Utils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. public static string GetDateTimeNow()
  61. {
  62. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  63. }
  64. public static string ConvertLongToDate(string strValue)
  65. {
  66. DateTime dateTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(strValue)).DateTime;
  67. return dateTime.ToString("yyyy-MM-dd");
  68. }
  69. public static string ConvertShortDate(string strValue)
  70. {
  71. if (!strValue.Contains("-") && strValue.Length >= 8)
  72. {
  73. return strValue.Substring(0, 4) + "-" + strValue.Substring(4, 2) + "-" + strValue.Substring(6, 2);
  74. }
  75. return strValue.Length > 10 ? strValue.Substring(0, 10) : strValue;
  76. }
  77. public static string ConvertNoSplitDate(string strValue)
  78. {
  79. strValue = strValue.Replace("-", "");
  80. if (strValue.Length > 8)
  81. {
  82. strValue = strValue.Substring(0, 8);
  83. }
  84. return strValue;
  85. }
  86. #endregion
  87. #region BeanUtils
  88. /// <summary>
  89. /// 用反射对实体进行属性值合并,返回合并后的属性并集
  90. /// </summary>
  91. /// <typeparam name="T"></typeparam>
  92. /// <param name="entity">合并对象1</param>
  93. /// <param name="addtional">合并对象2</param>
  94. /// <param name="forceCover">是否强制覆盖</param>
  95. /// <returns></returns>
  96. public static T merge<T>(T entity, T addtional,bool forceCover)
  97. {
  98. if (entity == null) return addtional;
  99. if (addtional == null) return entity;
  100. // 进行合并
  101. Type mType = typeof(T);
  102. object result = Activator.CreateInstance(mType);
  103. PropertyInfo[] pi = mType.GetProperties();
  104. pi.ToList().ForEach((p) =>
  105. {
  106. object v1 = p.GetValue(entity);
  107. object v2 = p.GetValue(addtional);
  108. if (forceCover)
  109. {
  110. v1 = v2;
  111. }
  112. else {
  113. v1 = (v1 == null ? v2 : v1);
  114. }
  115. p.SetValue(result, v1);
  116. });
  117. return (T)result;
  118. }
  119. /// <summary>
  120. /// 对2个对象进行合并,以第一个对象为重点
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. /// <param name="entity"></param>
  124. /// <param name="addtional"></param>
  125. /// <returns></returns>
  126. public static T merge<T>(T entity, T addtional)
  127. {
  128. return merge(entity, addtional, false);
  129. }
  130. private static JObject merge(JObject to, JObject from)
  131. {
  132. to.Merge(from);
  133. return to;
  134. }
  135. /// <summary>
  136. /// 采用Json的方式进行属性合并(默认会覆盖)
  137. /// </summary>
  138. /// <typeparam name="T"></typeparam>
  139. /// <param name="to"></param>
  140. /// <param name="from"></param>
  141. /// <returns></returns>
  142. public static T mergeObject<T>(T to, T from)
  143. {
  144. JObject toObj = JObject.FromObject(to);
  145. JObject fromObj = JObject.FromObject(from);
  146. JObject result = merge(toObj, fromObj);
  147. return result.ToObject<T>();
  148. }
  149. #endregion
  150. #region 业务个性化
  151. /// <summary>
  152. /// 在JObject对象基础上进行包装
  153. /// </summary>
  154. /// <param name="jsonInParam"></param>
  155. /// <returns></returns>
  156. public static JObject Wrapper(JObject jsonInParam)
  157. {
  158. //dynamic request = new JObject();
  159. //request.arg0 = jsonInParam;
  160. //return request;
  161. return jsonInParam;
  162. }
  163. /// <summary>
  164. /// 删除JObject上的进行包装
  165. /// </summary>
  166. /// <param name="jsonInParam"></param>
  167. /// <returns></returns>
  168. public static JObject removeWrapper(JObject jsonInParam)
  169. {
  170. //if (jsonInParam.ContainsKey("arg0"))
  171. //{
  172. // return (JObject)jsonInParam.GetValue("arg0");
  173. //}
  174. return jsonInParam;
  175. }
  176. public static bool isOtherCityPatient()
  177. {
  178. // 重庆没有市内异地
  179. string areaCode = Global.pat.insuplc_admdvs;
  180. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "50")
  181. {
  182. return true;
  183. }
  184. return false;
  185. }
  186. public static string convertAdmDr(string admNo)
  187. {
  188. // 重庆异地(每次唯一)
  189. if (isOtherCityPatient())
  190. {
  191. // 住院号15位,控制总长度不超过20
  192. string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  193. // 保存到扩展信息字段中
  194. JObject exp = new JObject();
  195. exp.Add("admDr",ret);
  196. Global.pat.ExpContent = exp.ToString();
  197. return ret;
  198. }
  199. return admNo;
  200. }
  201. #endregion
  202. /// <summary>
  203. /// 保存信息到pat.Expand字段
  204. /// </summary>
  205. /// <param name="key"></param>
  206. /// <param name="value"></param>
  207. public static void ExpandData(string key, object value)
  208. {
  209. string expand = Global.pat.ExpContent;
  210. JObject obj = new JObject();
  211. if (!string.IsNullOrEmpty(expand))
  212. {
  213. obj = JObject.Parse(expand);
  214. }
  215. if (!obj.ContainsKey(key))
  216. {
  217. obj.Add(key, value?.ToString());
  218. }
  219. else
  220. {
  221. obj[key] = value?.ToString();
  222. }
  223. Global.pat.ExpContent = obj.ToString();
  224. }
  225. /// <summary>
  226. /// 获取参保地编码
  227. /// </summary>
  228. /// <returns></returns>
  229. public static string GetInsuCode()
  230. {
  231. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  232. {
  233. Global.pat.insuplc_admdvs = Global.inf.areaCode;
  234. }
  235. return Global.pat.insuplc_admdvs;
  236. }
  237. /// <summary>
  238. /// 获取医院编码(社保)
  239. /// </summary>
  240. /// <returns></returns>
  241. public static string GetInsuOrgCode()
  242. {
  243. return Global.inf.hospitalNO;
  244. }
  245. public static bool isOtherCity()
  246. {
  247. return isOtherCity(Global.pat.insuplc_admdvs);
  248. }
  249. /// <summary>
  250. /// 省内异地
  251. /// </summary>
  252. /// <returns></returns>
  253. public static bool isOtherCity(string insuplcCode)
  254. {
  255. if (!string.IsNullOrEmpty(insuplcCode) && insuplcCode.Length > 4 && insuplcCode.Substring(0, 4) !=Global.inf.areaCode.Substring(0,4))
  256. {
  257. return true;
  258. }
  259. return false;
  260. }
  261. }
  262. }