Utils.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. //适用于南昌,赣州等还是得传本地区划
  210. if ((Global.inf.areaCode.Substring(0, 4) == "3601"))
  211. {
  212. if (Global.inf.fixedPointType == "1") //省本级
  213. {
  214. if (Global.pat.insuplc_admdvs.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
  215. {
  216. Global.inf.areaCode = "369900";
  217. }
  218. }
  219. else
  220. {
  221. //适用于红谷滩
  222. if (Global.pat.insuplc_admdvs.Substring(0, 4) == "3699") //有且只有在患者为省直的情况下传369900
  223. {
  224. Global.inf.areaCode = "369900";
  225. }
  226. }
  227. }
  228. else
  229. {
  230. }
  231. }
  232. return Global.pat.insuplc_admdvs;
  233. }
  234. /// <summary>
  235. /// 获取医院编码(社保)
  236. /// </summary>
  237. /// <returns></returns>
  238. public static string GetInsuOrgCode()
  239. {
  240. return Global.inf.hospitalNO;
  241. }
  242. /// <summary>
  243. /// 跨省异地
  244. /// </summary>
  245. /// <returns></returns>
  246. public static bool isOtherProvice(string areaCode)
  247. {
  248. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length>2 && areaCode.Substring(0, 2) != "36")
  249. {
  250. return true;
  251. }
  252. return false;
  253. }
  254. public static bool isOtherProvice()
  255. {
  256. return isOtherProvice(getAreaCode());
  257. }
  258. private static string getAreaCode()
  259. {
  260. string areaCode = Global.pat.insuplc_admdvs ?? Global.inf.areaCode;
  261. if (Global.pat.OtherProv == 1)
  262. {
  263. areaCode = Global.pat.card.SearchAdmCode;
  264. }
  265. return areaCode;
  266. }
  267. public static bool isOtherCity()
  268. {
  269. return isOtherCity(getAreaCode());
  270. }
  271. /// <summary>
  272. /// 省内异地
  273. /// </summary>
  274. /// <returns></returns>
  275. public static bool isOtherCity(string areaCode)
  276. {
  277. if (!string.IsNullOrEmpty(areaCode) && areaCode.Length > 4 && areaCode.Substring(0, 4) != "3601")
  278. {
  279. return true;
  280. }
  281. return false;
  282. }
  283. /// <summary>
  284. /// 将入参进行标准化转换
  285. /// </summary>
  286. /// <returns></returns>
  287. public static JObject ConvertRequest<T>(TradeEnum trade,JObject request)
  288. {
  289. JObject joOutput = request;
  290. JsonMapper mapper = new JsonMapper(trade.GetCode());
  291. // 非基线版需要转换
  292. if (!Global.curEvt.ext.BaseLineMode)
  293. {
  294. // 调试模式,可手动配置转换规则
  295. if (Global.curEvt.showJson)
  296. {
  297. JsonMappingForm form = new JsonMappingForm(request.ToString(),mapper.GetInputJson(),trade.GetCode());
  298. form.ShowDialog();
  299. mapper.reload(); //修改后重新加载
  300. }
  301. joOutput = mapper.MapRequest<JObject, T>(request);
  302. }
  303. // 日志
  304. //Global.writeLog("ConvertRequest", request.ToString(),joOutput.ToString());
  305. return joOutput;
  306. }
  307. public static JObject ConvertResponse<T>(TradeEnum trade, JObject response)
  308. {
  309. return ConvertResponse<T>(trade, response, false);
  310. }
  311. /// <summary>
  312. /// 将交易输出进行标准化转换
  313. /// </summary>
  314. /// <typeparam name="T"></typeparam>
  315. /// <param name="trade"></param>
  316. /// <param name="response"></param>
  317. /// <returns></returns>
  318. public static JObject ConvertResponse<T>(TradeEnum trade, JObject response,bool forceConvertFlag)
  319. {
  320. JsonMapper mapper = new JsonMapper(trade.GetCode());
  321. JObject joOutput = response;
  322. // 非基线版或要求转换
  323. if (!Global.curEvt.ext.BaseLineMode || forceConvertFlag)
  324. {
  325. //如果没有配置转换规则,调试模式,可手动配置
  326. if (Global.curEvt.showJson)
  327. {
  328. JsonMappingForm form = new JsonMappingForm(response.ToString(), mapper.GetOutputJson(), trade.GetCode(),false);
  329. form.ShowDialog();
  330. mapper.reload(); //修改后重新加载
  331. }
  332. joOutput = mapper.MapResponse<JObject, T>(response);
  333. }
  334. // 日志
  335. //Global.writeLog("ConvertResponse", "", joOutput.ToString());
  336. return joOutput;
  337. }
  338. /// <summary>
  339. /// 部分业务需要使用特殊的就诊凭证号
  340. /// </summary>
  341. /// <returns></returns>
  342. public static string ConvertMdtrtcertNo(bool flag = true)
  343. {
  344. if (Global.pat.mdtrtcertType == "03" && !string.IsNullOrEmpty(Global.pat.card.NO)) {
  345. return Global.pat.card.NO;
  346. }
  347. return Global.pat.mdtrtcertNO;
  348. }
  349. /// <summary>
  350. /// 保存信息到pat.Expand字段
  351. /// </summary>
  352. /// <param name="key"></param>
  353. /// <param name="value"></param>
  354. public static void ExpandData(string key,object value)
  355. {
  356. string expand = Global.pat.ExpContent;
  357. JObject obj = new JObject();
  358. if (!string.IsNullOrEmpty(expand))
  359. {
  360. obj = JObject.Parse(expand);
  361. }
  362. if (!obj.ContainsKey(key))
  363. {
  364. obj.Add(key, value?.ToString());
  365. }
  366. else
  367. {
  368. obj[key] = value?.ToString();
  369. }
  370. Global.pat.ExpContent = obj.ToString();
  371. }
  372. public static string convertAdmDr(string admNo)
  373. {
  374. // 重庆异地(每次唯一)
  375. //if (isOtherCity())
  376. //{
  377. // // 住院号15位,控制总长度不超过20
  378. // string ret = admNo + "-" + DateTime.Now.ToString("HHmm");
  379. // // 保存到扩展信息字段中
  380. // ExpandData("admDr", ret);
  381. // return ret;
  382. //}
  383. return admNo;
  384. }
  385. public static bool Confirm(string title)
  386. {
  387. if (MessageBox.Show(title, "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  388. {
  389. return false;
  390. }
  391. return true;
  392. }
  393. public static void WriteMockData(bool requestFlag, string tradeNo,string content)
  394. {
  395. string dataFile = Global.curEvt.path + "/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  396. File.WriteAllText(dataFile, content);
  397. }
  398. public static string MockData(bool requestFlag,string tradeNo)
  399. {
  400. string dataFile = Global.curEvt.path +"/config/mock/" + (requestFlag ? "request" : "response") + "/" + tradeNo + ".json";
  401. // 加载字段映射配置
  402. if (File.Exists(dataFile))
  403. {
  404. return File.ReadAllText(dataFile);
  405. }
  406. else
  407. {
  408. Global.writeLog("配置文件不存在:"+dataFile);
  409. }
  410. return "";
  411. }
  412. #endregion
  413. }
  414. }