Utils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Variables;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace PTMedicalInsurance.Common
  12. {
  13. public class Utils
  14. {
  15. #region 工具
  16. /// <summary>
  17. /// 获取交易号
  18. /// </summary>
  19. /// <returns></returns>
  20. public static string GetTradeNo()
  21. {
  22. return Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
  23. }
  24. /// <summary>
  25. /// 日期转换为时间戳Timestamp
  26. /// </summary>
  27. /// <param name="dateTime">要转换的日期</param>
  28. /// <returns></returns>
  29. public static long GetTimeStamp(DateTime dateTime)
  30. {
  31. DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
  32. //10位的时间戳
  33. long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
  34. //13位的时间戳
  35. //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
  36. return timeStamp;
  37. }
  38. /// <summary>
  39. /// UTC时间戳Timestamp转换为北京时间
  40. /// </summary>
  41. /// <param name="timestamp">要转换的时间戳</param>
  42. /// <returns></returns>
  43. public static DateTime GetDateTime(long timestamp)
  44. {
  45. //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  46. //使用上面的方式会显示TimeZone已过时
  47. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
  48. DateTime targetDt = dtStart.AddMilliseconds(timestamp).AddHours(8);
  49. return targetDt;
  50. }
  51. public static string GetDateTimeNow()
  52. {
  53. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  54. }
  55. public static string ConvertLongToDate(string strValue)
  56. {
  57. DateTime dateTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(strValue)).DateTime;
  58. return dateTime.ToString("yyyy-MM-dd");
  59. }
  60. public static string ConvertShortDate(string strValue)
  61. {
  62. if (!strValue.Contains("-") && strValue.Length >= 8)
  63. {
  64. return strValue.Substring(0, 4) + "-" + strValue.Substring(4, 2) + "-" + strValue.Substring(6, 2);
  65. }
  66. return strValue.Length > 10 ? strValue.Substring(0, 10) : strValue;
  67. }
  68. public static string ConvertNoSplitDate(string strValue)
  69. {
  70. strValue = strValue.Replace("-", "");
  71. if (strValue.Length > 8)
  72. {
  73. strValue = strValue.Substring(0, 8);
  74. }
  75. return strValue;
  76. }
  77. /// <summary>
  78. /// 将JObject对象中的timestamp数据转为yyyy-MM-dd格式
  79. /// </summary>
  80. /// <param name="joObject"></param>
  81. /// <param name="key"></param>
  82. public static void convertTimestamp(JObject joObject,string key)
  83. {
  84. if (joObject.ContainsKey(key))
  85. {
  86. Global.writeLog(string.Format("当前对象[{0}]的类型为:{1}", key, joObject.GetValue(key).Type.GetType()));
  87. }
  88. if (joObject.ContainsKey(key) && joObject.GetValue(key).Type.GetType() != typeof(string))
  89. {
  90. long timestamp = long.Parse(joObject.GetValue(key).ToString());
  91. string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd");
  92. joObject[key] = datetime;
  93. Global.writeLog(string.Format("转换后的日期为:{0}",datetime));
  94. }
  95. }
  96. public static string convertTimestamp(string key)
  97. {
  98. long timestamp = long.Parse(key);
  99. string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd HH:mm:ss");
  100. return datetime;
  101. }
  102. #endregion
  103. #region BeanUtils
  104. /// <summary>
  105. /// 用反射对实体进行属性值合并,返回合并后的属性并集
  106. /// </summary>
  107. /// <typeparam name="T"></typeparam>
  108. /// <param name="entity">合并对象1</param>
  109. /// <param name="addtional">合并对象2</param>
  110. /// <param name="forceCover">是否强制覆盖</param>
  111. /// <returns></returns>
  112. public static T merge<T>(T entity, T addtional,bool forceCover)
  113. {
  114. if (entity == null) return addtional;
  115. if (addtional == null) return entity;
  116. // 进行合并
  117. Type mType = typeof(T);
  118. object result = Activator.CreateInstance(mType);
  119. PropertyInfo[] pi = mType.GetProperties();
  120. pi.ToList().ForEach((p) =>
  121. {
  122. object v1 = p.GetValue(entity);
  123. object v2 = p.GetValue(addtional);
  124. if (forceCover)
  125. {
  126. v1 = v2;
  127. }
  128. else {
  129. v1 = (v1 == null ? v2 : v1);
  130. }
  131. p.SetValue(result, v1);
  132. });
  133. return (T)result;
  134. }
  135. /// <summary>
  136. /// 对2个对象进行合并,以第一个对象为重点
  137. /// </summary>
  138. /// <typeparam name="T"></typeparam>
  139. /// <param name="entity"></param>
  140. /// <param name="addtional"></param>
  141. /// <returns></returns>
  142. public static T merge<T>(T entity, T addtional)
  143. {
  144. return merge(entity, addtional, false);
  145. }
  146. private static JObject merge(JObject to, JObject from)
  147. {
  148. to.Merge(from);
  149. return to;
  150. }
  151. /// <summary>
  152. /// 采用Json的方式进行属性合并(默认会覆盖)
  153. /// </summary>
  154. /// <typeparam name="T"></typeparam>
  155. /// <param name="to"></param>
  156. /// <param name="from"></param>
  157. /// <returns></returns>
  158. public static T mergeObject<T>(T to, T from)
  159. {
  160. JObject toObj = JObject.FromObject(to);
  161. JObject fromObj = JObject.FromObject(from);
  162. JObject result = merge(toObj, fromObj);
  163. return result.ToObject<T>();
  164. }
  165. #endregion
  166. #region 业务个性化
  167. /// <summary>
  168. /// 在JObject对象基础上进行包装
  169. /// </summary>
  170. /// <param name="jsonInParam"></param>
  171. /// <returns></returns>
  172. public static JObject Wrapper(JObject jsonInParam)
  173. {
  174. dynamic request = new JObject();
  175. request.arg0 = jsonInParam;
  176. return request;
  177. }
  178. /// <summary>
  179. /// 删除JObject上的进行包装
  180. /// </summary>
  181. /// <param name="jsonInParam"></param>
  182. /// <returns></returns>
  183. public static JObject removeWrapper(JObject jsonInParam)
  184. {
  185. if (jsonInParam.ContainsKey("arg0"))
  186. {
  187. return (JObject)jsonInParam.GetValue("arg0");
  188. }
  189. return jsonInParam;
  190. }
  191. public static bool isOtherCityPatient()
  192. {
  193. // 重庆没有市内异地
  194. string areaCode = Global.pat.insuplc_admdvs;
  195. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "50")
  196. {
  197. return true;
  198. }
  199. return false;
  200. }
  201. public static string convertAdmDr(string admNo)
  202. {
  203. // 重庆异地(每次唯一)
  204. if (isOtherCityPatient())
  205. {
  206. // 住院号15位,控制总长度不超过20
  207. string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  208. // 保存到扩展信息字段中
  209. JObject exp = new JObject();
  210. exp.Add("admDr",ret);
  211. Global.pat.ExpContent = exp.ToString();
  212. return ret;
  213. }
  214. return admNo;
  215. }
  216. #endregion
  217. public static string MockData(bool requestFlag, string tradeNo)
  218. {
  219. string dataFile = Global.curEvt.path + "/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  220. // 加载字段映射配置
  221. if (File.Exists(dataFile))
  222. {
  223. return File.ReadAllText(dataFile);
  224. }
  225. else
  226. {
  227. Global.writeLog("配置文件不存在:" + dataFile);
  228. }
  229. return "";
  230. }
  231. /// <summary>
  232. /// 保存信息到pat.Expand字段
  233. /// </summary>
  234. /// <param name="key"></param>
  235. /// <param name="value"></param>
  236. public static void ExpandData(string key, object value)
  237. {
  238. string expand = Global.pat.ExpContent;
  239. JObject obj = new JObject();
  240. if (!string.IsNullOrEmpty(expand))
  241. {
  242. obj = JObject.Parse(expand);
  243. }
  244. if (!obj.ContainsKey(key))
  245. {
  246. obj.Add(key, value?.ToString());
  247. }
  248. else
  249. {
  250. obj[key] = value?.ToString();
  251. }
  252. Global.pat.ExpContent = obj.ToString();
  253. }
  254. /// <summary>
  255. /// 获取参保地编码
  256. /// </summary>
  257. /// <returns></returns>
  258. public static string GetInsuCode()
  259. {
  260. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  261. {
  262. Global.pat.insuplc_admdvs = Global.inf.areaCode;
  263. }
  264. return Global.pat.insuplc_admdvs;
  265. }
  266. /// <summary>
  267. /// 获取医院编码(社保)
  268. /// </summary>
  269. /// <returns></returns>
  270. public static string GetInsuOrgCode()
  271. {
  272. return Global.inf.hospitalNO;
  273. }
  274. public static bool Confirm(string title)
  275. {
  276. if (MessageBox.Show(title, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  277. {
  278. return false;
  279. }
  280. return true;
  281. }
  282. }
  283. }