Utils.cs 15 KB

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