Utils.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Forms;
  3. using PTMedicalInsurance.Helper;
  4. using PTMedicalInsurance.Variables;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace PTMedicalInsurance.Common
  14. {
  15. public class Utils
  16. {
  17. #region 工具
  18. /// <summary>
  19. /// 返回当前日期
  20. /// </summary>
  21. /// <returns></returns>
  22. public static string GetShortDateTimeNow()
  23. {
  24. return DateTime.Now.ToString("yyyy-MM-dd");
  25. }
  26. public static string GetDateTimeNow()
  27. {
  28. return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  29. }
  30. /// <summary>
  31. /// 获取交易号
  32. /// </summary>
  33. /// <returns></returns>
  34. public static string GetTradeNo()
  35. {
  36. return Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
  37. }
  38. /// <summary>
  39. /// 日期转换为时间戳Timestamp
  40. /// </summary>
  41. /// <param name="dateTime">要转换的日期</param>
  42. /// <returns></returns>
  43. public static long GetTimeStamp(DateTime dateTime)
  44. {
  45. DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
  46. //10位的时间戳
  47. long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
  48. //13位的时间戳
  49. //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
  50. return timeStamp;
  51. }
  52. /// <summary>
  53. /// UTC时间戳Timestamp转换为北京时间
  54. /// </summary>
  55. /// <param name="timestamp">要转换的时间戳</param>
  56. /// <returns></returns>
  57. public static DateTime GetDateTime(long timestamp)
  58. {
  59. //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  60. //使用上面的方式会显示TimeZone已过时
  61. DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
  62. DateTime targetDt = dtStart.AddMilliseconds(timestamp).AddHours(8);
  63. return targetDt;
  64. }
  65. /// <summary>
  66. /// 将JObject对象中的timestamp数据转为yyyy-MM-dd格式
  67. /// </summary>
  68. /// <param name="joObject"></param>
  69. /// <param name="key"></param>
  70. public static void convertTimestamp(JObject joObject,string key)
  71. {
  72. if (joObject.ContainsKey(key))
  73. {
  74. Global.writeLog(string.Format("当前对象[{0}]的类型为:{1}", key, joObject.GetValue(key).Type.GetType()));
  75. }
  76. if (joObject.ContainsKey(key) && joObject.GetValue(key).Type.GetType() != typeof(string))
  77. {
  78. long timestamp = long.Parse(joObject.GetValue(key).ToString());
  79. string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd");
  80. joObject[key] = datetime;
  81. Global.writeLog(string.Format("转换后的日期为:{0}",datetime));
  82. }
  83. }
  84. public static string ConvertLongToDate(string strValue)
  85. {
  86. DateTime dateTime = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(strValue)).DateTime;
  87. return dateTime.ToString("yyyy-MM-dd");
  88. }
  89. public static string ConvertShortDate(string strValue)
  90. {
  91. if (!strValue.Contains("-") && strValue.Length >= 8)
  92. {
  93. return strValue.Substring(0, 4) + "-" + strValue.Substring(4, 2) + "-" + strValue.Substring(6, 2);
  94. }
  95. return strValue.Length > 10 ? strValue.Substring(0, 10) : strValue;
  96. }
  97. public static string ConvertNoSplitDate(string strValue)
  98. {
  99. strValue = strValue.Replace("-", "");
  100. if(strValue.Length>8)
  101. {
  102. strValue = strValue.Substring(0, 8);
  103. }
  104. return strValue;
  105. }
  106. #endregion
  107. #region BeanUtils
  108. /// <summary>
  109. /// 用反射对实体进行属性值合并,返回合并后的属性并集
  110. /// </summary>
  111. /// <typeparam name="T"></typeparam>
  112. /// <param name="entity">合并对象1</param>
  113. /// <param name="addtional">合并对象2</param>
  114. /// <param name="forceCover">是否强制覆盖</param>
  115. /// <returns></returns>
  116. public static T merge<T>(T entity, T addtional,bool forceCover)
  117. {
  118. if (entity == null) return addtional;
  119. if (addtional == null) return entity;
  120. // 进行合并
  121. Type mType = typeof(T);
  122. object result = Activator.CreateInstance(mType);
  123. PropertyInfo[] pi = mType.GetProperties();
  124. pi.ToList().ForEach((p) =>
  125. {
  126. object v1 = p.GetValue(entity);
  127. object v2 = p.GetValue(addtional);
  128. if (forceCover)
  129. {
  130. v1 = v2;
  131. }
  132. else {
  133. v1 = (v1 == null ? v2 : v1);
  134. }
  135. p.SetValue(result, v1);
  136. });
  137. return (T)result;
  138. }
  139. /// <summary>
  140. /// 对2个对象进行合并,以第一个对象为重点
  141. /// </summary>
  142. /// <typeparam name="T"></typeparam>
  143. /// <param name="entity"></param>
  144. /// <param name="addtional"></param>
  145. /// <returns></returns>
  146. public static T merge<T>(T entity, T addtional)
  147. {
  148. return merge(entity, addtional, false);
  149. }
  150. private static JObject merge(JObject to, JObject from)
  151. {
  152. to.Merge(from);
  153. return to;
  154. }
  155. /// <summary>
  156. /// 采用Json的方式进行属性合并(默认会覆盖)
  157. /// </summary>
  158. /// <typeparam name="T"></typeparam>
  159. /// <param name="to"></param>
  160. /// <param name="from"></param>
  161. /// <returns></returns>
  162. public static T mergeObject<T>(T to, T from)
  163. {
  164. JObject toObj = JObject.FromObject(to);
  165. JObject fromObj = JObject.FromObject(from);
  166. JObject result = merge(toObj, fromObj);
  167. return result.ToObject<T>();
  168. }
  169. #endregion
  170. #region 业务个性化
  171. /// <summary>
  172. /// 在JObject对象基础上进行包装
  173. /// </summary>
  174. /// <param name="jsonInParam"></param>
  175. /// <returns></returns>
  176. public static JObject Wrapper(JObject jsonInParam)
  177. {
  178. //dynamic request = new JObject();
  179. //request.arg0 = jsonInParam;
  180. //return request;
  181. return jsonInParam;
  182. }
  183. /// <summary>
  184. /// 删除JObject上的进行包装
  185. /// </summary>
  186. /// <param name="jsonInParam"></param>
  187. /// <returns></returns>
  188. public static JObject removeWrapper(JObject jsonInParam)
  189. {
  190. //if (jsonInParam.ContainsKey("arg0"))
  191. //{
  192. // return (JObject)jsonInParam.GetValue("arg0");
  193. //}
  194. return jsonInParam;
  195. }
  196. /// <summary>
  197. /// 获取参保地编码
  198. /// </summary>
  199. /// <returns></returns>
  200. public static string GetInsuCode()
  201. {
  202. if (string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  203. {
  204. Global.pat.insuplc_admdvs = Global.inf.areaCode;
  205. }
  206. //Global.writeLog("参保地判断:"+ Global.pat.insuplc_admdvs+",就异地:"+Global.inf.areaCode);
  207. //if (!string.IsNullOrEmpty(Global.pat.insuplc_admdvs))
  208. //{
  209. // if (Global.inf.fixedPointType == "1") //省本级
  210. // {
  211. // //适用于南昌
  212. // if (Global.pat.insuplc_admdvs.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
  213. // {
  214. // Global.inf.areaCode = "369900";
  215. // }
  216. // }
  217. // else
  218. // {
  219. // //适用于红谷滩
  220. // if (Global.pat.insuplc_admdvs.Substring(0, 4) == "3699") //有且只有在患者为省直的情况下传369900
  221. // {
  222. // Global.inf.areaCode = "369900";
  223. // }
  224. // }
  225. //}
  226. return Global.pat.insuplc_admdvs;
  227. }
  228. /// <summary>
  229. /// 获取医院编码(社保)
  230. /// </summary>
  231. /// <returns></returns>
  232. public static string GetInsuOrgCode()
  233. {
  234. return Global.inf.hospitalNO;
  235. }
  236. /// <summary>
  237. /// 跨省异地
  238. /// </summary>
  239. /// <returns></returns>
  240. public static bool isOtherProvice(string areaCode)
  241. {
  242. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "36")
  243. {
  244. return true;
  245. }
  246. return false;
  247. }
  248. public static bool isOtherProvice()
  249. {
  250. return isOtherProvice(getAreaCode());
  251. }
  252. private static string getAreaCode()
  253. {
  254. string areaCode = Global.pat.insuplc_admdvs ?? Global.inf.areaCode;
  255. if (Global.pat.OtherProv == 1)
  256. {
  257. areaCode = Global.pat.card.SearchAdmCode;
  258. }
  259. return areaCode;
  260. }
  261. public static bool isOtherCity()
  262. {
  263. return isOtherCity(getAreaCode());
  264. }
  265. /// <summary>
  266. /// 省内异地
  267. /// </summary>
  268. /// <returns></returns>
  269. public static bool isOtherCity(string areaCode)
  270. {
  271. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length > 4 && areaCode.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
  272. {
  273. return true;
  274. }
  275. return false;
  276. }
  277. /// <summary>
  278. /// 将入参进行标准化转换
  279. /// </summary>
  280. /// <returns></returns>
  281. public static JObject ConvertRequest<T>(TradeEnum trade,JObject request)
  282. {
  283. JObject joOutput = request;
  284. JsonMapper mapper = new JsonMapper(trade.GetCode());
  285. // 非基线版需要转换
  286. if (!Global.curEvt.ext.BaseLineMode)
  287. {
  288. // 调试模式,可手动配置转换规则
  289. if (Global.curEvt.showJson)
  290. {
  291. JsonMappingForm form = new JsonMappingForm(request.ToString(),mapper.GetInputJson(),trade.GetCode());
  292. form.ShowDialog();
  293. mapper.reload(); //修改后重新加载
  294. }
  295. joOutput = mapper.MapRequest<JObject, T>(request);
  296. }
  297. // 日志
  298. //Global.writeLog("ConvertRequest", request.ToString(),joOutput.ToString());
  299. return joOutput;
  300. }
  301. public static JObject ConvertResponse<T>(TradeEnum trade, JObject response)
  302. {
  303. return ConvertResponse<T>(trade, response, false);
  304. }
  305. /// <summary>
  306. /// 将交易输出进行标准化转换
  307. /// </summary>
  308. /// <typeparam name="T"></typeparam>
  309. /// <param name="trade"></param>
  310. /// <param name="response"></param>
  311. /// <returns></returns>
  312. public static JObject ConvertResponse<T>(TradeEnum trade, JObject response,bool forceConvertFlag)
  313. {
  314. JsonMapper mapper = new JsonMapper(trade.GetCode());
  315. JObject joOutput = response;
  316. // 非基线版或要求转换
  317. if (!Global.curEvt.ext.BaseLineMode || forceConvertFlag)
  318. {
  319. //如果没有配置转换规则,调试模式,可手动配置
  320. if (Global.curEvt.showJson)
  321. {
  322. JsonMappingForm form = new JsonMappingForm(response.ToString(), mapper.GetOutputJson(), trade.GetCode(),false);
  323. form.ShowDialog();
  324. mapper.reload(); //修改后重新加载
  325. }
  326. joOutput = mapper.MapResponse<JObject, T>(response);
  327. }
  328. // 日志
  329. //Global.writeLog("ConvertResponse", "", joOutput.ToString());
  330. return joOutput;
  331. }
  332. /// <summary>
  333. /// 部分业务需要使用特殊的就诊凭证号
  334. /// </summary>
  335. /// <returns></returns>
  336. public static string ConvertMdtrtcertNo(bool flag = true)
  337. {
  338. if (Global.pat.mdtrtcertType == "03" && !string.IsNullOrEmpty(Global.pat.card.NO)) {
  339. return Global.pat.card.NO;
  340. }
  341. return Global.pat.mdtrtcertNO;
  342. }
  343. /// <summary>
  344. /// 保存信息到pat.Expand字段
  345. /// </summary>
  346. /// <param name="key"></param>
  347. /// <param name="value"></param>
  348. public static void ExpandData(string key,object value)
  349. {
  350. string expand = Global.pat.ExpContent;
  351. JObject obj = new JObject();
  352. if (!string.IsNullOrEmpty(expand))
  353. {
  354. obj = JObject.Parse(expand);
  355. }
  356. if (!obj.ContainsKey(key))
  357. {
  358. obj.Add(key, value?.ToString());
  359. }
  360. else
  361. {
  362. obj[key] = value?.ToString();
  363. }
  364. Global.pat.ExpContent = obj.ToString();
  365. }
  366. public static string convertAdmDr(string admNo)
  367. {
  368. // 重庆异地(每次唯一)
  369. //if (isOtherCity())
  370. //{
  371. // // 住院号15位,控制总长度不超过20
  372. // string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  373. // // 保存到扩展信息字段中
  374. // ExpandData("admDr", ret);
  375. // return ret;
  376. //}
  377. return admNo;
  378. }
  379. public static bool Confirm(string title)
  380. {
  381. if (MessageBox.Show(title, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  382. {
  383. return false;
  384. }
  385. return true;
  386. }
  387. public static void WriteMockData(bool requestFlag, string tradeNo,string content)
  388. {
  389. string dataFile = Global.curEvt.path + "/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  390. File.WriteAllText(dataFile, content);
  391. }
  392. public static string MockData(bool requestFlag,string tradeNo)
  393. {
  394. string dataFile = Global.curEvt.path +"/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  395. // 加载字段映射配置
  396. if (File.Exists(dataFile))
  397. {
  398. return File.ReadAllText(dataFile);
  399. }
  400. else
  401. {
  402. Global.writeLog("配置文件不存在:"+dataFile);
  403. }
  404. return "";
  405. }
  406. #endregion
  407. }
  408. }