Utils.cs 14 KB

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