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