Utils.cs 12 KB

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