ReadCardProcess.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.CardReaders;
  3. using PTMedicalInsurance.Common;
  4. using PTMedicalInsurance.Entity;
  5. using PTMedicalInsurance.Forms;
  6. using PTMedicalInsurance.Helper;
  7. using PTMedicalInsurance.Variables;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace PTMedicalInsurance.Business
  16. {
  17. class ReadCardProcess : AbstractProcess
  18. {
  19. private static readonly Semaphore semaphore = new Semaphore(1, 1);
  20. public override CallResult Process(JObject input)
  21. {
  22. JObject joCardInfo = new JObject();
  23. ChooseCard cc = new ChooseCard();
  24. JObject joRtn = new JObject();
  25. JObject joInput = new JObject();
  26. string msg;
  27. try
  28. {
  29. DialogResult dialog = tools.ShowAppDiaglog(cc);
  30. if (dialog == DialogResult.OK)
  31. {
  32. Global.businessType = "";
  33. Global.pat.certType = "01";
  34. //电子凭证
  35. if (cc.cardType == "01")
  36. {
  37. Global.pat.mdtrtcertType = "01";
  38. Global.businessType = cc.businessType;
  39. //CardReader reader = new CardReader();
  40. CardReader reader = CardReader.Instance;
  41. //reader.Initialize(Global.inf.cardURL, Global.inf.areaCode);
  42. if (reader.ECQuery(out msg) != 0)
  43. {
  44. outParam = "读电子凭证失败!" + msg;
  45. return Error(outParam);
  46. };
  47. JObject joTmp = JObject.Parse(msg);
  48. if (joTmp["code"].ToString() != "0")
  49. {
  50. outParam = "读电子凭证失败!" + joTmp["message"].ToString();
  51. return Error(outParam);
  52. }
  53. Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joTmp, "data.ecToken") ;
  54. Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joTmp, "data.insuOrg");
  55. Global.pat.certType = JsonHelper.getDestValue(joTmp, "data.idType");
  56. Global.pat.certNO = JsonHelper.getDestValue(joTmp, "data.idNo");
  57. Global.pat.name = JsonHelper.getDestValue(joTmp, "data.userName");
  58. trade1101(out outParam);
  59. return Parse(outParam);
  60. }
  61. //身份证
  62. if (cc.cardType == "02")
  63. {
  64. Global.pat.mdtrtcertType = "02";
  65. int rtn = -1;
  66. //手工输入身份证
  67. if (cc.tbID.Text != "")
  68. {
  69. Global.pat.certType = "01";
  70. Global.pat.mdtrtcertNO= Global.pat.certNO = cc.tbID.Text;
  71. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  72. rtn = 0;
  73. }
  74. else
  75. {
  76. //物理读身份证
  77. //CardReader reader = new CardReader();
  78. //if (reader.ReadSFZ(out msg) != 0)
  79. //{
  80. // outParam = "读身份证失败!" + msg;
  81. // return Error(outParam);
  82. //};
  83. //Global.pat.certType = "01";
  84. //Global.pat.mdtrtcertNO = JsonHelper.getDestValue(JObject.Parse(msg), "IDCardNo") ;
  85. //Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  86. CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver();
  87. if (reader.ReadSFZ(out msg) != 0)
  88. {
  89. outParam = "读身份证失败!" + msg;
  90. return Error(outParam);
  91. };
  92. Global.pat.certType = "01";
  93. Global.pat.mdtrtcertNO = tools.getDestPosStrBySpliterCardInfo(msg, 1, "^");
  94. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
  95. rtn = 0;
  96. }
  97. if (rtn == 0)
  98. {
  99. trade1101(out outParam);
  100. }
  101. return Parse(outParam);
  102. }
  103. //社保卡
  104. if (cc.cardType == "03")
  105. {
  106. #region 调用读卡接口信息
  107. Global.pat.mdtrtcertType = "03";
  108. int rtn = 0;
  109. string basInfo = "", regInfo = "", sOutPar = "";
  110. //重复初始化导致闪退,换一种写法
  111. //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode);
  112. Global.writeLog_debug($"调用单例模式类,程序应用域:{AppDomain.CurrentDomain.FriendlyName}--{AppDomain.CurrentDomain.Id}");
  113. AppDomain currentDomain = AppDomain.CurrentDomain;
  114. CardReader reader = CardReader.Instance;
  115. reader.Initialize(Global.inf.cardURL, Global.inf.areaCode);
  116. string mutexName = "Global\\MyApp_InitializedMutex"; // 确保名称唯一
  117. bool initialized;
  118. //if (Mutex.OpenExisting(mutexName) != null)
  119. //{
  120. // Global.writeLog_debug($"Mutex:检测到已初始化");
  121. //}
  122. //else
  123. //{
  124. // Mutex mutex = new Mutex(true, mutexName, out initialized);
  125. // Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!");
  126. // if (reader.Init() != 0)
  127. // {
  128. // outParam = "读社保卡,初始化失败!";
  129. // return Error("读社保卡");
  130. // };
  131. //}
  132. // 创建新的互斥体
  133. bool createdNew;
  134. //using (Mutex mutex = new Mutex(true, mutexName, out createdNew))
  135. Mutex mutex = new Mutex(true, mutexName, out createdNew);
  136. {
  137. if (createdNew)
  138. {
  139. Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!");
  140. try
  141. {
  142. // 执行初始化逻辑
  143. if (reader.Init() != 0)
  144. {
  145. outParam = "读社保卡,初始化失败!";
  146. return Error("读社保卡");
  147. }
  148. Global.writeLog_debug("Mutex 初始化完成!");
  149. }
  150. catch (Exception ex)
  151. {
  152. Global.writeLog_debug($"初始化过程中发生异常: {ex.Message}");
  153. outParam = "初始化过程中发生错误!";
  154. return Error("初始化失败");
  155. }
  156. }
  157. else
  158. {
  159. Global.writeLog_debug("Mutex 检测到已初始化!");
  160. }
  161. // 确保释放互斥体
  162. mutex.ReleaseMutex();
  163. }
  164. //二代社保卡可用
  165. if (reader.ReadCardBas(out basInfo, out regInfo) != 0)
  166. {
  167. outParam = "读社保卡,获取卡信息失败!" + basInfo;
  168. return Error("读社保卡");
  169. }
  170. else
  171. {
  172. //【社保卡读卡成功返回】
  173. Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  174. Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  175. Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  176. Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  177. Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  178. Global.pat.certType = cc.psnCertType;
  179. Global.pat.mdtrtcertType = "03";
  180. Global.pat.mdtrtcertNO = Global.pat.certNO;
  181. if (cc.bYDSBK)
  182. {
  183. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  184. }
  185. if (Global.pat.insuplc_admdvs .Substring(0,2)!= "44")
  186. {
  187. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  188. }
  189. //清除原数据
  190. trade1101(out outParam);
  191. }
  192. //// 明泰读卡器
  193. //CardReaderBySSCardDriver reader = new CardReaderBySSCardDriver();
  194. //if (reader.ReadCardUnclassifiedBas(3,out basInfo) != 0)
  195. //{
  196. // outParam = "读社保卡,获取卡信息失败!" + basInfo;
  197. // return Error("读社保卡");
  198. //}
  199. //else
  200. //{
  201. // //【社保卡读卡成功返回】
  202. // Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地
  203. // Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码
  204. // Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号
  205. // Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码
  206. // Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名
  207. // Global.pat.certType = cc.psnCertType;
  208. // Global.pat.mdtrtcertType = "03";
  209. // Global.pat.mdtrtcertNO = Global.pat.certNO;
  210. // if (cc.bYDSBK)
  211. // {
  212. // Global.pat.mdtrtcertNO = Global.pat.card.NO;
  213. // }
  214. // if (Global.pat.mdtrtcertNO =="")
  215. // {
  216. // Global.pat.mdtrtcertNO = Global.pat.card.NO;
  217. // }
  218. // //清除原数据
  219. // trade1101(out outParam);
  220. //}
  221. return Parse(outParam);
  222. #endregion
  223. }
  224. //非中国大陆公民身份证人员
  225. if (cc.cardType == "04")
  226. {
  227. Global.pat.certType = cc.psnCertType;
  228. Global.pat.mdtrtcertNO = Global.pat.certNO = cc.tbID.Text;
  229. Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; //非大陆公民读卡参保地传440300
  230. Global.pat.mdtrtcertType = "99";
  231. trade1101(out outParam);
  232. return Parse(outParam);
  233. }
  234. }
  235. else
  236. {
  237. //outParam = JsonHelper.setExceptionJson().ToString();
  238. return Exception(-1, "读卡", "取消读卡");
  239. }
  240. return Success();
  241. }
  242. catch (Exception ex)
  243. {
  244. return Error("异常:" + ex.Message);
  245. }
  246. }
  247. /// <summary>
  248. /// 通过证件号获取基本信息(无卡)
  249. /// </summary>
  250. /// <param name="cardNo"></param>
  251. /// <param name="outParam"></param>
  252. /// <returns></returns>
  253. public int trade1101(out string outParam)
  254. {
  255. outParam = "";
  256. string errorMsg = "";
  257. JObject joInput = new JObject();
  258. JObject joData = new JObject();
  259. joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
  260. joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO); //Utils.ConvertMdtrtcertNo()
  261. joData.Add("card_sn", Global.pat.card.SN);
  262. joData.Add("begntime", Utils.GetShortDateTimeNow());
  263. joData.Add("psn_cert_type", Global.pat.certType);
  264. joData.Add("certno", Global.pat.certNO); //证件号码
  265. joData.Add("psn_name", Global.pat.name);
  266. joInput.Add("data", joData);
  267. InvokeHelper invoker = new InvokeHelper();
  268. JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput);
  269. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  270. {
  271. outParam = "获取病人信息失败:" + errorMsg;
  272. return -1;
  273. }
  274. else
  275. {
  276. //joRtn["output"]["baseinfo"]["bizCode"] = Global.pat.card.BusinessCode;
  277. outParam = joRtn.ToString();
  278. parsePatient(joRtn);
  279. return 0;
  280. }
  281. }
  282. /// <summary>
  283. /// 读卡并获取基本信息
  284. /// </summary>
  285. /// <param name="outParam"></param>
  286. /// <returns></returns>
  287. public int trade1161(out string outParam)
  288. {
  289. outParam = "";
  290. string errorMsg = "";
  291. JObject joInput = new JObject();
  292. joInput.Add("begntime", Utils.GetShortDateTimeNow());
  293. JObject joRtn = new JObject();
  294. joRtn = invoker.invokeCenterService(TradeEnum.ReadCardInfo, joInput);
  295. if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
  296. {
  297. outParam = "读卡失败:" + errorMsg;
  298. return -1;
  299. }
  300. else
  301. {
  302. parsePatient(joRtn);
  303. outParam = joRtn.ToString();
  304. }
  305. return 0;
  306. }
  307. /// <summary>
  308. /// 解析人员基本信息
  309. /// </summary>
  310. /// <param name="joRtn"></param>
  311. public void parsePatient(JObject joRtn)
  312. {
  313. // 基线版
  314. PersonCardInfo info = JsonHelper.getOutput<PersonCardInfo>(joRtn);
  315. if (info.cardInfo != null)
  316. {
  317. Global.pat.card.NO = info.cardInfo.cardno;
  318. Global.pat.card.SN = info.cardInfo.card_sn;
  319. Global.pat.card.Cardtoken = info.cardInfo.ecToken;
  320. Global.pat.ecToken = info.cardInfo.ecToken;
  321. }
  322. //Global.writeLog("病人信息:"+joRtn.ToString());
  323. if (info.insuInfo != null && info.insuInfo.Length > 0)
  324. {
  325. //参保地
  326. Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs;
  327. Global.pat.insuplc_name = info.insuInfo[0].insuplc_name;
  328. Global.pat.medType = info.insuInfo[0].med_type ?? "C";
  329. }
  330. //证件号
  331. Global.pat.certNO = info.baseInfo?.certno;
  332. // 人员证件类型
  333. Global.pat.certType = info.baseInfo?.psn_cert_type;
  334. // 就诊类型
  335. if (string.IsNullOrEmpty(Global.pat.mdtrtcertType))
  336. {
  337. Global.pat.mdtrtcertType = "03";
  338. }
  339. // 就诊凭证号
  340. if (!string.IsNullOrEmpty(Global.pat.card.NO))
  341. {
  342. Global.pat.mdtrtcertNO = Global.pat.card.NO;
  343. }
  344. // 没有卡号用证件号
  345. if (string.IsNullOrEmpty(Global.pat.mdtrtcertNO))
  346. {
  347. Global.pat.mdtrtcertNO = Global.pat.certNO;
  348. }
  349. Global.pat.payOrdId = info.platformOrderNo;
  350. }
  351. }
  352. }