Utils.cs 12 KB

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