Utils.cs 9.3 KB

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