
using Newtonsoft.Json.Linq;
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.Windows.Forms;

namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.ReadCard
{
    class ReadCardProcess:AbstractProcess
    {

        public override CallResult Process(JObject input)
        {
            outParam = "";
            string sOutPar;
            JObject joRtn = new JObject(); 
            JObject joCardInfo = new JObject();
            JObject joInput = new JObject();

            InvokeHelper invoker = new InvokeHelper();

            try
            {

                Global.pat.certType = "01";

                //电子凭证
                if (Global.ssmp.cardType == "02")
                {
                    Global.pat.mdtrtcertType = "01";
                }
                //身份证
                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 = Global.pat.IDNO;
                    Global.pat.certType = "01";
                    Global.pat.certNO = Global.pat.IDNO;
                    //Global.pat.name = cc.PatName;
                    Global.pat.card.SN = "";
                }
                //社保卡
                if (Global.ssmp.cardType == "03")
                {
                    CardReader_HLJ cr = new CardReader_HLJ();
                    Global.pat.mdtrtcertType = "03";
                    //Global.pat.card.type = (cc.cbCardType.SelectedIndex + 1).ToString();
                    //Global.pat.card.level = "0" + (cc.cbCardLevel.SelectedIndex + 1).ToString();
                    Global.pat.certType = "01";
                }

                //人脸识别
                if (Global.ssmp.cardType == "05")
                {
                    Global.pat.mdtrtcertType = "01";
                    Global.pat.card.transType = "cn.nhsa.ec.auth";
                    Global.transType = "cn.nhsa.ec.auth"; //人脸识别
                }

                if (Global.pat.OtherProv != 0)
                {
                    Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
                    //MessageBox.Show(Global.pat.card.SearchAdmCode + Global.pat.card.SearchAdmName);
                }

                #region  【1191身份认证（医保电子凭证/社保卡,先调用1191身份认证通过后再调用1101获取人员信息）】              
                if (Global.ssmp.cardType != "04")
                {
                    string errorMsg = "";
                    if (cBus.Authenticate(out errorMsg) != 0)
                    {
                        outParam = "身份认证失败：" + errorMsg;
                        return Error(outParam);
                    }
                    else
                    {
                        //电子凭证扫码获取身份信息
                        if ((Global.ssmp.cardType == "02") || (Global.ssmp.cardType == "05"))
                        {
                            Global.pat.IDNO = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.ecinfo.idno");                //身份证号
                            Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.ecinfo.insuorg");   //参保地
                            Global.pat.name = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.ecinfo.username");            //姓名
                            Global.pat.certNO = Global.pat.IDNO;
                            Global.pat.certType = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.ecinfo.psn_cert_type");   //人员证件类型
                            Global.pat.ecToken = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.ecinfo.ectoken");          //电子凭证令牌
                            Global.pat.mdtrtcertNO = Global.pat.ecToken;
                        }
                        else //社保卡获取身份信息
                        {
                            Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.insuplc_admdvs");  //参保地
                            Global.pat.name = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.psn_name");                  //姓名
                            Global.pat.card.SN = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.card_sn");                //社保卡识别码
                            Global.pat.card.Cardtoken = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.cardtoken");       //社保卡令牌
                            Global.pat.certNO = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.certno");                  //证件号码
                            Global.pat.certType = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.psn_cert_type");         //人员证件类型
                            Global.pat.mdtrtcertNO = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.ssn_code");           //社会保障号码 
                            Global.pat.card.NO = JsonHelper.getDestValue(JObject.Parse(errorMsg), "output.cardinfo.cardno");                  //社会保障卡号

                            if (Global.ssmp.cardType == "03")
                                Global.pat.mdtrtcertNO = Global.pat.card.NO;
                        }
                    }
                }

                if (trade1101(out outParam) != 0)
                {
                    return Error();
                }
                else
                { 
                    return Success(outParam);
                }
                #endregion

                return Exception(-1, "读卡", outParam);
            }
            catch (Exception ex)
            {
                return Error("异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 通过证件号获取基本信息（无卡）
        /// </summary>
        /// <param name="cardNo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        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
        }
    }

}
