ReadCardProcess.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Common;
  3. using PTMedicalInsurance.Entity;
  4. using PTMedicalInsurance.Forms;
  5. using PTMedicalInsurance.Helper;
  6. using PTMedicalInsurance.Variables;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace PTMedicalInsurance.Business
  15. {
  16. class ReadCardProcess : AbstractProcess
  17. {
  18. public override CallResult Process(JObject input)
  19. {
  20. JObject joCardInfo = new JObject();
  21. ChooseCard cc = new ChooseCard();
  22. JObject joRtn = new JObject();
  23. JObject joInput = new JObject();
  24. try
  25. {
  26. //DialogResult dialog = tools.ShowAppDiaglog(cc);
  27. DialogResult dialog = cc.ShowDialog();
  28. if (dialog == DialogResult.OK)
  29. {
  30. Global.businessType = "";
  31. Global.pat.certType = "01";
  32. Thread currentThread = Thread.CurrentThread;
  33. Global.writeLog($"Current threadID: {currentThread.ManagedThreadId}");
  34. //电子凭证
  35. if (cc.cardType == "01")
  36. {
  37. Global.pat.mdtrtcertType = "01";
  38. Global.businessType = cc.businessType;
  39. if (tradeEcToken(out outParam) != 0)
  40. {
  41. return Error(outParam);
  42. }
  43. //int ret = -1;
  44. //SafeTradeEcToken(cc, out ret, out outParam);
  45. //if (ret != 0)
  46. //{
  47. // return Error(outParam);
  48. //}
  49. if (trade1101(out outParam) != 0)
  50. {
  51. return Error(-1,outParam);
  52. }
  53. return Success();
  54. }
  55. ////身份证
  56. //if (cc.cardType == "02")
  57. //{
  58. // Global.pat.mdtrtcertType = "02";
  59. // //读身份证
  60. // CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.hospitalNO);
  61. // if (reader.Init() != 0)
  62. // {
  63. // outParam = "读身份证,读卡初始化失败!";
  64. // return Error("读身份证");
  65. // };
  66. // int rtn = reader.ReadSFZ(out outParam);
  67. // if (rtn == 0)
  68. // {
  69. // if (trade1101(out outParam) != 0)
  70. // {
  71. // return Error(-1, outParam);
  72. // }
  73. // }
  74. // return Success();
  75. //}
  76. //身份证
  77. if (cc.cardType == "02")
  78. {
  79. Global.pat.mdtrtcertType = "02";
  80. ////读身份证
  81. //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.hospitalNO);
  82. //if (reader.Init() != 0)
  83. //{
  84. // outParam = "读身份证,读卡初始化失败!";
  85. // return Error("读身份证");
  86. //};
  87. //int rtn = reader.ReadSFZ(out outParam);
  88. //if (rtn == 0)
  89. //{
  90. // if (trade1101(out outParam) != 0)
  91. // {
  92. // return Error(-1, outParam);
  93. // }
  94. //}
  95. if (trade1101(out outParam) != 0)
  96. {
  97. return Error(-1, outParam);
  98. }
  99. return Success();
  100. }
  101. //社保卡
  102. if (cc.cardType == "03")
  103. {
  104. #region 调用读卡接口信息
  105. Global.pat.mdtrtcertType = "03";
  106. int rtn = 0;
  107. if (!Global.curEvt.enabledDebug)
  108. {
  109. //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode);
  110. //if (reader.Init() != 0)
  111. //{
  112. // outParam = "读社保卡,初始化失败!";
  113. // return Error("读社保卡");
  114. //};
  115. //rtn = reader.ReadCardBas(out outParam);
  116. ///
  117. CardReaderNew reader = new CardReaderNew(Global.inf.cardURL, Global.inf.areaCode);
  118. if (reader.Init() != 0)
  119. {
  120. outParam = "读社保卡,初始化失败!";
  121. return Error("读社保卡");
  122. };
  123. rtn = reader.ReadCardBas(out outParam);
  124. }
  125. if (rtn == 0)
  126. {
  127. //清除原数据
  128. if (trade1101(out outParam) != 0)
  129. {
  130. return Error(-1, outParam);
  131. }
  132. return Success();
  133. }
  134. return Exception(-1, "读卡", outParam);
  135. #endregion
  136. }
  137. //人脸识别
  138. if (cc.cardType == "04")
  139. {
  140. Global.pat.mdtrtcertType = "01";
  141. Global.businessType = cc.businessType;
  142. if (identifyFace(out outParam) != 0)
  143. {
  144. return Error(outParam);
  145. }
  146. if (trade1101(out outParam) != 0)
  147. {
  148. return Error(-1, outParam);
  149. }
  150. return Success();
  151. }
  152. //港澳通行证
  153. if (cc.cardType == "05")
  154. {
  155. Global.pat.certType ="03";
  156. Global.pat.mdtrtcertNO = Global.pat.certNO = cc.tbID.Text;
  157. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; //非大陆公民读卡参保地传440300
  158. Global.pat.mdtrtcertType =cc.psnCertType;
  159. if (trade1101(out outParam) != 0)
  160. {
  161. return Error(-1, outParam);
  162. }
  163. return Success();
  164. }
  165. return Error(-1,"未进入对应读卡类型!");
  166. }
  167. else
  168. {
  169. //outParam = JsonHelper.setExceptionJson().ToString();
  170. return Exception(-1, "读卡", "取消读卡");
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. return Error("异常:" + ex.Message);
  176. }
  177. }
  178. /// <summary>
  179. /// 通过证件号获取基本信息(无卡)
  180. /// </summary>
  181. /// <param name="cardNo"></param>
  182. /// <param name="outParam"></param>
  183. /// <returns></returns>
  184. public int trade1101(out string outParam)
  185. {
  186. outParam = "";
  187. string errorMsg = "";
  188. JObject joInput = new JObject();
  189. JObject joData = new JObject();
  190. joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
  191. joData.Add("mdtrt_cert_no", Utils.ConvertMdtrtcertNo());
  192. joData.Add("card_sn", Global.pat.card.SN);
  193. joData.Add("begntime", Utils.GetShortDateTimeNow());
  194. joData.Add("psn_cert_type", Global.pat.certType);
  195. joData.Add("certno", Global.pat.certNO); //证件号码
  196. joData.Add("psn_name", Global.pat.name);
  197. joInput.Add("data", joData);
  198. InvokeHelper invoker = new InvokeHelper();
  199. JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput);
  200. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  201. {
  202. outParam = "获取病人信息失败:" + errorMsg;
  203. return -1;
  204. }
  205. else
  206. {
  207. //joRtn["output"]["baseinfo"]["bizCode"] = Global.pat.card.BusinessCode;
  208. outParam = joRtn.ToString();
  209. parsePatient(joRtn);
  210. return 0;
  211. }
  212. }
  213. /// <summary>
  214. /// 读卡并获取基本信息
  215. /// </summary>
  216. /// <param name="outParam"></param>
  217. /// <returns></returns>
  218. public int trade1161(out string outParam)
  219. {
  220. outParam = "";
  221. string errorMsg = "";
  222. JObject joInput = new JObject();
  223. joInput.Add("begntime", Utils.GetShortDateTimeNow());
  224. JObject joRtn = new JObject();
  225. joRtn = invoker.invokeCenterService(TradeEnum.ReadCardInfo, joInput);
  226. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  227. {
  228. outParam = "读卡失败:" + errorMsg;
  229. return -1;
  230. }
  231. else
  232. {
  233. parsePatient(joRtn);
  234. outParam = joRtn.ToString();
  235. }
  236. return 0;
  237. }
  238. /// <summary>
  239. /// 解析人员基本信息
  240. /// </summary>
  241. /// <param name="joRtn"></param>
  242. public void parsePatient(JObject joRtn)
  243. {
  244. // 基线版
  245. PersonCardInfo info = JsonHelper.getOutput<PersonCardInfo>(joRtn);
  246. if (info.cardInfo != null)
  247. {
  248. Global.pat.card.NO = info.cardInfo.cardno;
  249. Global.pat.card.SN = info.cardInfo.card_sn;
  250. Global.pat.card.Cardtoken = info.cardInfo.ecToken;
  251. Global.pat.ecToken = info.cardInfo.ecToken;
  252. }
  253. //Global.writeLog("病人信息:"+joRtn.ToString());
  254. if (info.insuInfo != null && info.insuInfo.Length > 0)
  255. {
  256. //参保地
  257. Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs;
  258. Global.pat.insuplc_name = info.insuInfo[0].insuplc_name;
  259. Global.pat.medType = info.insuInfo[0].med_type ?? "C";
  260. }
  261. //证件号
  262. Global.pat.certNO = info.baseInfo?.certno;
  263. // 人员证件类型
  264. Global.pat.certType = info.baseInfo?.psn_cert_type;
  265. // 就诊类型
  266. if (string.IsNullOrEmpty(Global.pat.mdtrtcertType))
  267. {
  268. Global.pat.mdtrtcertType = "03";
  269. }
  270. // 就诊凭证号
  271. if (!string.IsNullOrEmpty(Global.pat.card.NO))
  272. {
  273. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  274. }
  275. // 没有卡号用证件号
  276. if (string.IsNullOrEmpty(Global.pat.mdtrtcertNO))
  277. {
  278. Global.pat.mdtrtcertNO = Global.pat.certNO;
  279. }
  280. Global.pat.payOrdId = info.platformOrderNo;
  281. }
  282. /// <summary>
  283. /// 通过电子凭证获取基本信息
  284. /// </summary>
  285. /// <param name="outParam"></param>
  286. /// <returns></returns>
  287. public int tradeEcToken(out string outParam)
  288. {
  289. int ret = ECTokenReader.ECQuery("1",out outParam);
  290. return ret;
  291. }
  292. public void SafeTradeEcToken(Form frm, out int ret, out string outParam)
  293. {
  294. Thread currentThread = Thread.CurrentThread;
  295. Global.writeLog($"Current threadID: {currentThread.ManagedThreadId}");
  296. // 确保在UI线程上调用ECTokenReader.ECQuery,并且可以正确获取返回值和输出参数
  297. if (frm.InvokeRequired)
  298. {
  299. Global.writeLog("不在UI线程");
  300. // 使用 Invoke 来确保在 UI 线程上执行
  301. Tuple<int, string> result = (Tuple<int, string>)frm.Invoke(new Func<Tuple<int, string>>(() =>
  302. {
  303. string localOutParam;
  304. int localRet = ECTokenReader.ECQuery("1", out localOutParam);
  305. return Tuple.Create(localRet, localOutParam);
  306. }));
  307. // 解包结果
  308. ret = result.Item1;
  309. outParam = result.Item2;
  310. }
  311. else
  312. {
  313. Global.writeLog("在UI线程");
  314. // 如果已经在UI线程上,则直接调用
  315. ret = ECTokenReader.ECQuery("1", out outParam);
  316. }
  317. }
  318. /// <summary>
  319. /// 人脸识别
  320. /// </summary>
  321. /// <param name="outParam"></param>
  322. /// <returns></returns>
  323. public int identifyFace(out string outParam)
  324. {
  325. int ret = ECTokenReader.ECQuery("2", out outParam);
  326. return ret;
  327. }
  328. }
  329. }