using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Entity;
using PTMedicalInsurance.Forms;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PTMedicalInsurance.Business
{
class ReadCardProcess : AbstractProcess
{
public override CallResult Process(JObject input)
{
JObject joCardInfo = new JObject();
ChooseCard cc = new ChooseCard();
JObject joRtn = new JObject();
JObject joInput = new JObject();
try
{
//DialogResult dialog = tools.ShowAppDiaglog(cc);
DialogResult dialog = cc.ShowDialog();
if (dialog == DialogResult.OK)
{
Global.businessType = "";
Global.pat.certType = "01";
Thread currentThread = Thread.CurrentThread;
Global.writeLog($"Current threadID: {currentThread.ManagedThreadId}");
//电子凭证
if (cc.cardType == "01")
{
Global.pat.mdtrtcertType = "01";
Global.businessType = cc.businessType;
if (tradeEcToken(out outParam) != 0)
{
return Error(outParam);
}
//int ret = -1;
//SafeTradeEcToken(cc, out ret, out outParam);
//if (ret != 0)
//{
// return Error(outParam);
//}
if (trade1101(out outParam) != 0)
{
return Error(-1,outParam);
}
return Success();
}
////身份证
//if (cc.cardType == "02")
//{
// Global.pat.mdtrtcertType = "02";
// //读身份证
// CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.hospitalNO);
// if (reader.Init() != 0)
// {
// outParam = "读身份证,读卡初始化失败!";
// return Error("读身份证");
// };
// int rtn = reader.ReadSFZ(out outParam);
// if (rtn == 0)
// {
// if (trade1101(out outParam) != 0)
// {
// return Error(-1, outParam);
// }
// }
// return Success();
//}
//身份证
if (cc.cardType == "02")
{
Global.pat.mdtrtcertType = "02";
////读身份证
//CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.hospitalNO);
//if (reader.Init() != 0)
//{
// outParam = "读身份证,读卡初始化失败!";
// return Error("读身份证");
//};
//int rtn = reader.ReadSFZ(out outParam);
//if (rtn == 0)
//{
// if (trade1101(out outParam) != 0)
// {
// return Error(-1, outParam);
// }
//}
if (trade1101(out outParam) != 0)
{
return Error(-1, outParam);
}
return Success();
}
//社保卡
if (cc.cardType == "03")
{
#region 调用读卡接口信息
Global.pat.mdtrtcertType = "03";
int rtn = 0;
if (!Global.curEvt.enabledDebug)
{
//CardReader reader = new CardReader(Global.inf.cardURL, Global.inf.areaCode);
//if (reader.Init() != 0)
//{
// outParam = "读社保卡,初始化失败!";
// return Error("读社保卡");
//};
//rtn = reader.ReadCardBas(out outParam);
///
CardReaderNew reader = new CardReaderNew(Global.inf.cardURL, Global.inf.areaCode);
if (reader.Init() != 0)
{
outParam = "读社保卡,初始化失败!";
return Error("读社保卡");
};
rtn = reader.ReadCardBas(out outParam);
}
if (rtn == 0)
{
//清除原数据
if (trade1101(out outParam) != 0)
{
return Error(-1, outParam);
}
return Success();
}
return Exception(-1, "读卡", outParam);
#endregion
}
//人脸识别
if (cc.cardType == "04")
{
Global.pat.mdtrtcertType = "01";
Global.businessType = cc.businessType;
if (identifyFace(out outParam) != 0)
{
return Error(outParam);
}
if (trade1101(out outParam) != 0)
{
return Error(-1, outParam);
}
return Success();
}
//港澳通行证
if (cc.cardType == "05")
{
Global.pat.certType ="03";
Global.pat.mdtrtcertNO = Global.pat.certNO = cc.tbID.Text;
Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode; //非大陆公民读卡参保地传440300
Global.pat.mdtrtcertType =cc.psnCertType;
if (trade1101(out outParam) != 0)
{
return Error(-1, outParam);
}
return Success();
}
return Error(-1,"未进入对应读卡类型!");
}
else
{
//outParam = JsonHelper.setExceptionJson().ToString();
return Exception(-1, "读卡", "取消读卡");
}
}
catch (Exception ex)
{
return Error("异常:" + ex.Message);
}
}
///
/// 通过证件号获取基本信息(无卡)
///
///
///
///
public int trade1101(out string outParam)
{
outParam = "";
string errorMsg = "";
JObject joInput = new JObject();
JObject joData = new JObject();
joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
joData.Add("mdtrt_cert_no", Utils.ConvertMdtrtcertNo());
joData.Add("card_sn", Global.pat.card.SN);
joData.Add("begntime", Utils.GetShortDateTimeNow());
joData.Add("psn_cert_type", Global.pat.certType);
joData.Add("certno", Global.pat.certNO); //证件号码
joData.Add("psn_name", Global.pat.name);
joInput.Add("data", joData);
InvokeHelper invoker = new InvokeHelper();
JObject joRtn = invoker.invokeCenterService(TradeEnum.PatientInfo, joInput);
if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
{
outParam = "获取病人信息失败:" + errorMsg;
return -1;
}
else
{
//joRtn["output"]["baseinfo"]["bizCode"] = Global.pat.card.BusinessCode;
outParam = joRtn.ToString();
parsePatient(joRtn);
return 0;
}
}
///
/// 读卡并获取基本信息
///
///
///
public int trade1161(out string outParam)
{
outParam = "";
string errorMsg = "";
JObject joInput = new JObject();
joInput.Add("begntime", Utils.GetShortDateTimeNow());
JObject joRtn = new JObject();
joRtn = invoker.invokeCenterService(TradeEnum.ReadCardInfo, joInput);
if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
{
outParam = "读卡失败:" + errorMsg;
return -1;
}
else
{
parsePatient(joRtn);
outParam = joRtn.ToString();
}
return 0;
}
///
/// 解析人员基本信息
///
///
public void parsePatient(JObject joRtn)
{
// 基线版
PersonCardInfo info = JsonHelper.getOutput(joRtn);
if (info.cardInfo != null)
{
Global.pat.card.NO = info.cardInfo.cardno;
Global.pat.card.SN = info.cardInfo.card_sn;
Global.pat.card.Cardtoken = info.cardInfo.ecToken;
Global.pat.ecToken = info.cardInfo.ecToken;
}
//Global.writeLog("病人信息:"+joRtn.ToString());
if (info.insuInfo != null && info.insuInfo.Length > 0)
{
//参保地
Global.pat.insuplc_admdvs = info.insuInfo[0].insuplc_admdvs;
Global.pat.insuplc_name = info.insuInfo[0].insuplc_name;
Global.pat.medType = info.insuInfo[0].med_type ?? "C";
}
//证件号
Global.pat.certNO = info.baseInfo?.certno;
// 人员证件类型
Global.pat.certType = info.baseInfo?.psn_cert_type;
// 就诊类型
if (string.IsNullOrEmpty(Global.pat.mdtrtcertType))
{
Global.pat.mdtrtcertType = "03";
}
// 就诊凭证号
if (!string.IsNullOrEmpty(Global.pat.card.NO))
{
Global.pat.mdtrtcertNO = Global.pat.card.NO;
}
// 没有卡号用证件号
if (string.IsNullOrEmpty(Global.pat.mdtrtcertNO))
{
Global.pat.mdtrtcertNO = Global.pat.certNO;
}
Global.pat.payOrdId = info.platformOrderNo;
}
///
/// 通过电子凭证获取基本信息
///
///
///
public int tradeEcToken(out string outParam)
{
int ret = ECTokenReader.ECQuery("1",out outParam);
return ret;
}
public void SafeTradeEcToken(Form frm, out int ret, out string outParam)
{
Thread currentThread = Thread.CurrentThread;
Global.writeLog($"Current threadID: {currentThread.ManagedThreadId}");
// 确保在UI线程上调用ECTokenReader.ECQuery,并且可以正确获取返回值和输出参数
if (frm.InvokeRequired)
{
Global.writeLog("不在UI线程");
// 使用 Invoke 来确保在 UI 线程上执行
Tuple result = (Tuple)frm.Invoke(new Func>(() =>
{
string localOutParam;
int localRet = ECTokenReader.ECQuery("1", out localOutParam);
return Tuple.Create(localRet, localOutParam);
}));
// 解包结果
ret = result.Item1;
outParam = result.Item2;
}
else
{
Global.writeLog("在UI线程");
// 如果已经在UI线程上,则直接调用
ret = ECTokenReader.ECQuery("1", out outParam);
}
}
///
/// 人脸识别
///
///
///
public int identifyFace(out string outParam)
{
int ret = ECTokenReader.ECQuery("2", out outParam);
return ret;
}
}
}