Utils.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #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. }
  172. /// <summary>
  173. /// 删除JObject上的进行包装
  174. /// </summary>
  175. /// <param name="jsonInParam"></param>
  176. /// <returns></returns>
  177. public static JObject removeWrapper(JObject jsonInParam)
  178. {
  179. if (jsonInParam.ContainsKey("arg0"))
  180. {
  181. return (JObject)jsonInParam.GetValue("arg0");
  182. }
  183. return jsonInParam;
  184. }
  185. public static bool isOtherCityPatient()
  186. {
  187. // 重庆没有市内异地
  188. string areaCode = Global.pat.insuplc_admdvs;
  189. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "50")
  190. {
  191. return true;
  192. }
  193. return false;
  194. }
  195. public static string convertAdmDr(string admNo)
  196. {
  197. // 重庆异地(每次唯一)
  198. if (isOtherCityPatient())
  199. {
  200. // 住院号15位,控制总长度不超过20
  201. string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  202. // 保存到扩展信息字段中
  203. JObject exp = new JObject();
  204. exp.Add("admDr",ret);
  205. Global.pat.ExpContent = exp.ToString();
  206. return ret;
  207. }
  208. return admNo;
  209. }
  210. #endregion
  211. public static string MockData(bool requestFlag, string tradeNo)
  212. {
  213. string dataFile = Global.curEvt.path + "/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  214. // 加载字段映射配置
  215. if (File.Exists(dataFile))
  216. {
  217. return File.ReadAllText(dataFile);
  218. }
  219. else
  220. {
  221. Global.writeLog("配置文件不存在:" + dataFile);
  222. }
  223. return "";
  224. }
  225. /// <summary>
  226. /// 保存信息到pat.Expand字段
  227. /// </summary>
  228. /// <param name="key"></param>
  229. /// <param name="value"></param>
  230. public static void ExpandData(string key, object value)
  231. {
  232. string expand = Global.pat.ExpContent;
  233. JObject obj = new JObject();
  234. if (!string.IsNullOrEmpty(expand))
  235. {
  236. obj = JObject.Parse(expand);
  237. }
  238. if (!obj.ContainsKey(key))
  239. {
  240. obj.Add(key, value?.ToString());
  241. }
  242. else
  243. {
  244. obj[key] = value?.ToString();
  245. }
  246. Global.pat.ExpContent = obj.ToString();
  247. }
  248. /// <summary>
  249. /// 获取参保地编码
  250. /// </summary>
  251. /// <returns></returns>
  252. public static string GetInsuCode()
  253. {
  254. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  255. {
  256. Global.pat.insuplc_admdvs = Global.inf.areaCode;
  257. }
  258. return Global.pat.insuplc_admdvs;
  259. }
  260. /// <summary>
  261. /// 获取医院编码(社保)
  262. /// </summary>
  263. /// <returns></returns>
  264. public static string GetInsuOrgCode()
  265. {
  266. return Global.inf.hospitalNO;
  267. }
  268. public static bool Confirm(string title)
  269. {
  270. if (MessageBox.Show(title, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  271. {
  272. return false;
  273. }
  274. return true;
  275. }
  276. }
  277. }