using Newtonsoft.Json.Linq; using PTMedicalInsurance.CardReaders; using PTMedicalInsurance.Common; using PTMedicalInsurance.Helper; using PTMedicalInsurance.Variables; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.ReadCard { class ReadCardProcess:AbstractProcess { private static readonly Semaphore semaphore = new Semaphore(1, 1); public override CallResult Process(JObject input) { outParam = ""; string sOutPar; JObject joRtn = new JObject(); JObject joInput = new JObject(); InvokeHelper invoker = new InvokeHelper(); try { Global.pat.certType = "01"; //电子凭证 if (Global.ssmp.cardType == "02") { Global.pat.mdtrtcertType = "01"; //CardReader reader = new CardReader(); CardReader reader = CardReader.Instance; if (reader.ECQuery(out outParam) != 0) { outParam = "读电子凭证失败!" + outParam; return Error(outParam); }; JObject joTmp = JObject.Parse(outParam); if (joTmp["code"].ToString() != "0") { outParam = "读电子凭证失败!" + joTmp["message"].ToString(); return Error(outParam); } Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joTmp, "data.ecToken"); Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joTmp, "data.insuOrg"); Global.pat.certType = JsonHelper.getDestValue(joTmp, "data.idType"); Global.pat.certNO = JsonHelper.getDestValue(joTmp, "data.idNo"); Global.pat.name = JsonHelper.getDestValue(joTmp, "data.userName"); } //身份证 if (Global.ssmp.cardType == "04") { if (Global.pat.card.SearchAdmKey != "") Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; Global.pat.mdtrtcertType = "02"; //Global.pat.mdtrtcertNO = cc.sP_CertNO; Global.pat.certType = "01"; //Global.pat.certNO = cc.sP_IDNO; //Global.pat.name = cc.PatName; Global.pat.card.SN = ""; //物理读身份证 //CardReader reader = new CardReader(); CardReader reader = CardReader.Instance; //reader.Initialize(Global.inf.cardURL, Global.inf.areaCode); if (reader.ReadSFZ(out outParam) != 0) { outParam = "读身份证失败!" + outParam ; return Error(outParam); }; Global.pat.mdtrtcertNO = JsonHelper.getDestValue(JObject.Parse(outParam), "IDCardNo"); } //社保卡 //深圳星耀自助机读卡器是德卡 if (Global.ssmp.cardType == "03") { //深圳星耀普瑞自助机是德卡 if (Global.inf.hospitalNO == "H44030305189") { CardReader reader = CardReader.Instance; Global.pat.mdtrtcertType = "03"; int rtn = 0; string basInfo = "", regInfo = ""; //重复初始化导致闪退,换一种写法 //CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode); Global.writeLog_debug($"调用单例模式类,程序应用域:{AppDomain.CurrentDomain.FriendlyName}--{AppDomain.CurrentDomain.Id}"); AppDomain currentDomain = AppDomain.CurrentDomain; //CardReader reader = CardReader.Instance; reader.Initialize(Global.inf.cardURL, Global.inf.areaCode); string mutexName = "Global\\MyApp_InitializedMutex"; // 确保名称唯一 // 创建新的互斥体 bool createdNew; using (Mutex mutex = new Mutex(true, mutexName, out createdNew)) // Mutex mutex = new Mutex(true, mutexName, out createdNew); { if (createdNew) { Global.writeLog_debug("Mutex", semaphore.ToString(), "检测到未初始化,准备初始化!"); try { // 执行初始化逻辑 if (reader.Init() != 0) { outParam = "读社保卡,初始化失败!"; return Error("读社保卡"); } Global.writeLog_debug("Mutex 初始化完成!"); } catch (Exception ex) { Global.writeLog_debug($"初始化过程中发生异常: {ex.Message}"); outParam = "初始化过程中发生错误!"; return Error("初始化失败"); } } else { Global.writeLog_debug("Mutex 检测到已初始化!"); } // 确保释放互斥体 mutex.ReleaseMutex(); } //二代社保卡可用 if (reader.ReadCardBas(out basInfo, out regInfo) != 0) { outParam = "读社保卡,获取卡信息失败!" + basInfo; return Error("读社保卡"); } else { //【社保卡读卡成功返回】 Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地 Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码 Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号 Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码 Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名 Global.pat.certType = "01"; Global.pat.mdtrtcertType = "03"; Global.pat.mdtrtcertNO = Global.pat.certNO; //异地患者需要传卡号,否则超长错误 if (Global.pat.insuplc_admdvs.Substring(0, 2) != "44") { Global.pat.mdtrtcertNO = Global.pat.card.NO; } //清除原数据 trade1101(out outParam); } } else //深圳普瑞自助机内置的是华大的读卡器 { Global.pat.mdtrtcertType = "03"; int rtn = 0; string basInfo = "", regInfo = ""; CardReaderByHdEmb reader = new CardReaderByHdEmb(); if (reader.ICC_Reader_Open("USB1", out errMsg) != 0) { //return Error(errMsg); } if (reader.ReadCardBas(3, out basInfo) != 0) { outParam = "读社保卡,获取卡信息失败!" + basInfo; return Error("读社保卡USB1"); } else { //【社保卡读卡成功返回】 Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliterCardInfo(basInfo, 1); //参保地 Global.pat.certNO = tools.getDestPosStrBySpliterCardInfo(basInfo, 2); //社会保障号码 Global.pat.card.NO = tools.getDestPosStrBySpliterCardInfo(basInfo, 3); //卡号 Global.pat.card.SN = tools.getDestPosStrBySpliterCardInfo(basInfo, 4); //卡识别码 Global.pat.name = tools.getDestPosStrBySpliterCardInfo(basInfo, 5); //姓名 Global.pat.certType = "01"; Global.pat.mdtrtcertType = "03"; Global.pat.mdtrtcertNO = Global.pat.certNO; //异地患者需要传卡号,否则超长错误 if (Global.pat.insuplc_admdvs.Substring(0, 2) != "44") { Global.pat.mdtrtcertNO = Global.pat.card.NO; } } } } if (trade1101(out outParam) == 0) { return Success(); } return Exception(-1, "读卡", outParam); } catch (Exception ex) { return Error("异常:" + ex.Message); } } /// /// 通过证件号获取基本信息(无卡) /// /// /// /// public int trade1101(out string outParam) { #region【1101获取身份信息】 JObject joData = new JObject(); joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType); joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO); joData.Add("card_sn", Global.pat.card.SN); //joData.Add("begntime", ""); joData.Add("psn_cert_type", Global.pat.certType); joData.Add("certno", Global.pat.certNO); //证件号码 joData.Add("psn_name", Global.pat.name); JObject joInput = new JObject(); joInput.Add("data", joData); //InvokeHelper invoker = new InvokeHelper(); JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput); if (JsonHelper.parseCenterRtnValue(joRtn, out outParam) != 0) { outParam = "读卡失败:" + outParam; return -1; } else { //joRtn.Add("cardinfo",joCardInfo); outParam = joRtn.ToString(); Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(outParam), "output.insuinfo.insuplc_admdvs"); //参保地 Global.pat.certNO = JsonHelper.getDestValue(JObject.Parse(outParam), "output.baseinfo.certno"); //证件号码 Global.pat.psn_no = JsonHelper.getDestValue(JObject.Parse(outParam), "output.baseinfo.psn_no"); //人员编号 outParam = outParam.Replace(@"\", "").Replace("\"{", "{").Replace("}\"", "}"); return 0; } #endregion } } }