/******************************************************************************
 * 文件名称: HisMainBusiness.cs
 * 文件说明: HIS主体交易的封装
 * 当前版本: V1.0
 * 创建日期: 2022-06-20
 *
 * * 2020-06-20: 增加 HisMainBusiness 类
 * *****  2020-06-20: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-20: 增加 CardReader 方法重载，国家版电子凭证使用
 * *****  2020-06-20: 增加 Init 方法，SSCard环境初始化
 * *****  2020-06-20: 增加 ReadCardBas 方法，读社保卡
 * *****  2020-06-20: 增加 VerifyPIN 方法，验证密码
 * *****  2020-06-20: 增加 ChangePIN 方法，修改密码
 * *****  2020-06-20: 增加 ReadSFZ 方法，读身份证
 * *****  2020-06-20: 增加 GetQRBase 方法，读二维码
 * *****  2020-06-20: 增加 NationEcTrans 方法，读电子凭证（国家版）


******************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using PTMedicalInsurance.Forms;
using System.Data;
using System.Reflection;
using System.IO;
using System.IO.Compression;
using AnHuiMI.Common;

namespace PTMedicalInsurance.Business
{
    class HisMainBusiness
    {
        private InvokeHelper invoker = new InvokeHelper();
        private HisIrisServices hIS = new HisIrisServices();
        private MIIrisServices mIS = new MIIrisServices();
        private CenterBusiness cBus = new CenterBusiness();
        private InsuServices rIS = new InsuServices();
        /// <summary>
        /// 获取HIS的IRIS配置信息
        /// </summary>
        /// <param name="con"></param>
        public void getHisConfingue(out IrisConfigue con)
        {
            IniFile ini = new IniFile(Global.curEvt.path + @"\INSUConfigure.ini");
            con.ip = ini.ReadValue("HIS", "ip");
            con.url = ini.ReadValue("HIS", "url");
            con.authorization = ini.ReadValue("HIS", "authorization");
        }

        /// <summary>
        /// 获取医保平台的iris配置信息
        /// </summary>
        /// <param name="con"></param>
        public void getInsuConfingue(out IrisConfigue con)
        {
            IniFile ini = new IniFile(Global.curEvt.path + @"\INSUConfigure.ini");
            con.ip = ini.ReadValue("INSU", "ip");
            con.url = ini.ReadValue("INSU", "url");
            con.authorization = ini.ReadValue("INSU", "authorization");
        }

        /// <summary>
        /// 获取Session信息（目前只获取操作员信息）
        /// </summary>
        /// <param name="joSession"></param>
        public void getSessionInfo(JObject joSession)
        {
            if (string.IsNullOrEmpty(Global.user.ID))
            {
                Global.user.ID = JsonHelper.getDestValue(joSession, "userCode");
            }
            
            Global.user.name = JsonHelper.getDestValue(joSession, "userName");
            Global.user.type = "1";
        }

        /// <summary>
        /// 根据壳程序传入的入参设置全局接口变量
        /// </summary>
        /// <param name="joInpar"></param>
        /// <param name="inf"></param>
        public void setGlobalInterface(JObject joInpar,out  InterfaceInfo inf)
        {
            inf = new InterfaceInfo();
            inf.hospitalDr = int.Parse(JsonHelper.getDestValue(joInpar, "HospitalDr"));
            inf.hisHospitalDr = int.Parse(JsonHelper.getDestValue(joInpar, "hisHospitalDr"));
            inf.hospitalName = JsonHelper.getDestValue(joInpar, "HospitalName");
            inf.interfaceDr = int.Parse(JsonHelper.getDestValue(joInpar, "ID"));
            inf.interfaceID = JsonHelper.getDestValue(joInpar, "InterfaceID");
            //inf.oper = "3";
            inf.centerURL = JsonHelper.getDestValue(joInpar, "CenterURL");
            inf.areaCode = JsonHelper.getDestValue(joInpar, "AreaCode");
            inf.dllName = JsonHelper.getDestValue(joInpar, "DLLName");
            inf.hospitalNO = JsonHelper.getDestValue(joInpar, "HospitalNO");
            inf.version = "v1.0";
            inf.signatureType = "SM2";
            inf.recivedSystem = "YBXT";
            inf.AK = "583ebd87f99f4be19a6c425d5ecb89fc";
            inf.SK = "m9pKPKPnlfMcpJNtSjtvRCN2Rgg=";
            inf.dev_safe_info = "";
            inf.dev_no = Global.curEvt.mac;

            inf.cardURL = inf.centerURL;
            inf.ecURL = "http://ec.yn.hsip.gov.cn/localcfc/api/hsecfc/localQrCodeQuery";

            //瑶海正式环境应该配置到服务器
            inf.BusinessID = "41a8514ba8ed429eb4dde381eda02fe0";   //商务ID码 
            inf.CreditID = "91530112MA6Q3GGC1E";                   //服务商统一社会信用代码

            if (string.IsNullOrEmpty(inf.areaCode) || inf.areaCode.Substring(0, 4) == "3401")
            {
                inf.areaCode = "340100";
                Global.user.ID = "7571C4995A2F4290";
            }
            else
            {
                inf.areaCode = "349900";
                Global.user.ID = "48664E4455CC0F8F";
            }

            inf.cainfo = "";
        }

        /// <summary>
        /// 根据壳程序传入的入参设置全局接口变量
        /// </summary>
        /// <param name="joInpar"></param>
        /// <param name="inf"></param>
        public void setGlobalInterface_Plat(JObject joInpar, out InterfaceInfo inf)
        {
            inf = new InterfaceInfo();            

            inf.hospitalDr = int.Parse(JsonHelper.getDestValue(joInpar, "hospitalID"));            
            inf.hisHospitalDr = int.Parse(JsonHelper.getDestValue(joInpar, "hisHospitalDr"));            
            inf.hospitalName = JsonHelper.getDestValue(joInpar, "hospitalName");
            inf.interfaceDr = int.Parse(JsonHelper.getDestValue(joInpar, "medInsuInterfaceID"));            
            inf.interfaceID = JsonHelper.getDestValue(joInpar, "interfaceNO");
            //inf.oper = "3";
            inf.centerURL = JsonHelper.getDestValue(joInpar, "centerURL");
            inf.areaCode = JsonHelper.getDestValue(joInpar, "areaCode");
            inf.dllName = JsonHelper.getDestValue(joInpar, "dLLName");
            inf.hospitalNO = JsonHelper.getDestValue(joInpar, "hospitalNO");
            inf.version = "v1.0";
            inf.signatureType = "SM2";
            inf.recivedSystem = "YBXT";
            inf.AK = "583ebd87f99f4be19a6c425d5ecb89fc";
            inf.SK = "m9pKPKPnlfMcpJNtSjtvRCN2Rgg=";
            inf.dev_safe_info = "";
            inf.dev_no = Global.curEvt.mac;

            inf.cardURL = inf.centerURL;
            inf.ecURL = "http://ec.yn.hsip.gov.cn/localcfc/api/hsecfc/localQrCodeQuery";

            //瑶海正式环境应该配置到服务器
            inf.BusinessID = "41a8514ba8ed429eb4dde381eda02fe0";   //商务ID码 
            inf.CreditID = "91530112MA6Q3GGC1E";                   //服务商统一社会信用代码

            if (string.IsNullOrEmpty(inf.areaCode) || inf.areaCode.Substring(0, 4) == "3401")
            {
                inf.areaCode = "340100";
                Global.user.ID = "7571C4995A2F4290";
            }
            else
            {
                inf.areaCode = "349900";
                Global.user.ID = "48664E4455CC0F8F";
            }

            inf.cainfo = "";

        }

        /// <summary>
        /// 初始化环境
        /// </summary>
        /// <param name="inParam"></param>
        /// <returns></returns>
        public  int initEnvironment(string inParam,out string error)
        {
            error = string.Empty;
            try
            {              
                //获取HIS和医保平台的IRIS配置信息
                getHisConfingue(out Global.hisConfig);
                
                getInsuConfingue(out Global.insuConfig);

                getLocalConfingue();

                JObject joInparam = JObject.Parse(inParam);

                JObject JoSession = JObject.Parse(JsonHelper.getDestValue(joInparam, "session[0]"));
                string  hisHospitalDr = JsonHelper.getDestValue(JoSession, "hospID");

                JObject joInterface = JObject.Parse(JsonHelper.getDestValue(joInparam, "interfaceinfo"));

                joInterface.Add("hisHospitalDr", hisHospitalDr); 

                if (joInterface != null)
                {
                    //获取当前接口详细信息
                    if (Global.businessType == "P1")
                    {                        
                        setGlobalInterface_Plat(joInterface, out Global.inf);  
                    }
                    else
                    {                        
                        setGlobalInterface(joInterface, out Global.inf);
                    }
                    
                    JObject joSession = JObject.Parse(JsonHelper.getDestValue(joInparam, "session[0]"));
                    if (joSession != null)
                    {
                        //获取当前用户信息                        
                        getSessionInfo(joSession);
                        return 0;
                    }
                    else
                    {
                        error = "初始化环境失败：session获取失败！";
                        return -1;
                    }

                }
                else
                {
                    error = "初始化环境失败：interfaceinfo获取失败！";
                    return -1;
                }
                
            }
            catch (Exception ex)
            {
                error ="初始化环境失败：" + ex.Message;
                return -1;
            }
            
        }

        /// <summary>
        /// 读卡
        /// </summary>
        /// <returns></returns>
        public int readCard(out string outParam)
        {
            outParam = "";
            string errorMsg = "";
            JObject joCardInfo = new JObject();
            ChooseCard cc = new ChooseCard();            
            try
            {
                if (cc.ShowDialog() == DialogResult.OK)
                {                    
                    //电子凭证
                    if (cc.cardType == "01")
                    {
                        
                        Global.pat.mdtrtcertType = "01";
                        Global.businessType = cc.businessType;
                        outParam = ECTokenReader.ECQuery("1");

                    }
                    //身份证
                    if (cc.cardType == "02")
                    {
                         if (cc.ID == "")
                        {
                            Global.pat.certType = "01";
                            Global.pat.mdtrtcertType = "02";
                            Global.pat.name = "";
                        }
                        else
                        {
                            Global.pat.mdtrtcertType = "02";
                            Global.pat.mdtrtcertNO = cc.ID;
                            Global.pat.certType = "01";
                            Global.pat.certNO = cc.ID;
                            Global.pat.name = cc.PatName;
                        }
                        Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
                        if (Global.pat.OtherProv == 0)
                        {
                            Global.pat.insuplc_admdvs = "340100";//合肥本地固定340100
                            //MessageBox.Show(Global.pat.card.SearchAdmCode + Global.pat.card.SearchAdmName);
                        }
                        Global.pat.card.SN = "";
                    }
                    //社保卡
                    if (cc.cardType == "03")
                    {
                        CardReader_AH CardRead = new CardReader_AH();
                        string sOutPar = "";
                        if (CardRead.ReadCardBas(cc.sL_CardType, Global.inf.hospitalNO, out sOutPar) != 0)
                        {
                            outParam = "社保卡读卡异常ReadCardBas：" + sOutPar;
                            MessageBox.Show("读卡异常：" + sOutPar);
                            return -1;
                        }

                        //【社保卡读卡成功返回】
                        //发卡地区行政区划代码（卡识别码前 6 位）、社会保障号码、卡号、卡识别码、姓名、卡复位信息（仅取历史字节）、规范版本、发卡日期、卡有效期、终端机编号、终端设备号
                        Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliter(sOutPar, 1);    //参保地
                        if ((Global.inf.interfaceDr == 12) && (Global.pat.insuplc_admdvs == "340001"))
                        {
                            Global.pat.insuplc_admdvs = "349900";
                        }
                        if ((Global.inf.interfaceDr == 15) && (Global.pat.insuplc_admdvs == "340001"))
                        {
                            Global.pat.insuplc_admdvs = "349900";
                        }
                        Global.pat.card.NO = tools.getDestPosStrBySpliter(sOutPar, 2);           //社会保障号码
                        Global.pat.certNO = tools.getDestPosStrBySpliter(sOutPar, 3);            //卡号
                        Global.pat.card.SN = tools.getDestPosStrBySpliter(sOutPar, 4);           //卡识别码                                                
                        Global.pat.name = tools.getDestPosStrBySpliter(sOutPar, 5);              //姓名
                        Global.pat.certType = "90";
                        Global.pat.mdtrtcertType = "03";
                        Global.pat.mdtrtcertNO = Global.pat.certNO;
                        Global.pat.certNO = Global.pat.card.NO;

                        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.pat.insuplc_admdvs == "340001")
                    //{
                    //    Global.pat.insuplc_admdvs = "349900";
                    //}
                }
                else
                {
                    outParam = "收款员取消读卡！";
                    return -1;
                }


                InvokeHelper invoker = new InvokeHelper();

                JObject joInput = new JObject();
                #region 调用读卡接口信息
                //JObject joRtn = invoker.invokeCenterService("1161", JsonHelper.setCenterInpar("1161", joInput));
                //if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
                //{
                //    outParam = "读卡失败：" + errorMsg;
                //    return -1;
                //}
                //else {
                //    // 获取信息
                //    Global.pat.card.NO = JsonHelper.getDestValue(JObject.Parse(outParam), "output.cardecinfo.cardno"); //卡号
                //    Global.pat.card.SN = JsonHelper.getDestValue(JObject.Parse(outParam), "output.cardecinfo.card_sn"); //卡识别码
                //    Global.pat.card.Cardtoken = JsonHelper.getDestValue(JObject.Parse(outParam), "output.cardecinfo.ecToken"); //令牌
                //}

                #endregion
                if (cc.CBD!= "")
                {
                    Global.pat.insuplc_admdvs = Global.pat.card.SearchAdmCode;
                }

                #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", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                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);                
                
                 JObject joRtn = invoker.invokeCenterService("1101", JsonHelper.setCenterInpar("1101", joInput));
                if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
                {
                    outParam = "获取病人信息失败：" + errorMsg;                    
                    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.psn_no = JsonHelper.getDestValue(JObject.Parse(outParam), "output.baseinfo.psn_no"); //人员编号

                    return 0;
                }
                #endregion
            }
            catch (Exception ex)
            {
                outParam = "异常：" + ex.Message;                
                return -1;
            }
        }

        /// <summary>
        /// 正式结算前调用身份验证
        /// </summary>
        /// <param name="funNo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int IdentityVerify(string funNo, out string outParam)
        {           
            outParam = "";
            string errorMsg = "";                        
            ChooseCard cc = new ChooseCard();
            try
            {
                if ((funNo == "2207") && (Global.pat.mdtrtcertType == "02")) 
                {
                    return 0;
                }

                //住院正式结算前调用读卡窗体
                if (funNo == "2304")
                {
                    if (cc.ShowDialog() == DialogResult.OK)
                    {
                        Global.businessType = cc.businessType;
                        //电子凭证
                        if (cc.cardType == "01")
                        {
                            
                        }
                        //身份证-昆明需要从动态库获取身份信息
                        if (cc.cardType == "02")
                        {
                            if (cc.ID == "")
                            {
                                Global.pat.certType = "01";
                                Global.pat.mdtrtcertType = "02";
                            }
                            else
                            {
                                Global.pat.mdtrtcertType = "02";
                                Global.pat.mdtrtcertNO = cc.ID;
                                Global.pat.certType = "01";
                                Global.pat.certNO = cc.ID;
                                Global.pat.name = cc.Name;
                            }
                            Global.pat.name = "";
                            Global.pat.card.SN = "";
                        }
                        //社保卡
                        if (cc.cardType == "03")
                        {
                            Global.pat.mdtrtcertType = "03";
                            Global.pat.card.level = "0" + (cc.cbCardLevel.SelectedIndex + 1).ToString();
                            Global.pat.certType = "01";
                        }
                    }
                    else
                    {
                        outParam = "收款员取消读卡！";
                        return -1;
                    }

                    #region  【医保电子凭证/社保卡（1191身份认证）；身份证（1101获取身份信息）】              
                    if (cc.cardType != "02")
                    {
                        if (cBus.Authenticate(out errorMsg) != 0)
                        {
                            outParam = "身份认证失败：" + errorMsg;
                            return -1;
                        }
                        else
                        {
                            //电子凭证扫码获取身份信息
                            if (cc.cardType == "01")
                            {
                                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//社保卡获取身份信息
                            {
                                if (Global.pat.insuplc_admdvs == "") 
                                    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 (cc.cardType == "03")
                                    Global.pat.mdtrtcertNO = Global.pat.card.NO;
                            }
                        }
                    }
                    else
                    {
                        JObject joData1101 = new JObject();
                        joData1101.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                        joData1101.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                        joData1101.Add("card_sn", Global.pat.card.SN);
                        //joData1101.Add("begntime", ""); 
                        joData1101.Add("psn_cert_type", Global.pat.certType);
                        joData1101.Add("certno", Global.pat.certNO); //证件号码
                        joData1101.Add("psn_name", Global.pat.name);
                        JObject joInput1101 = new JObject();
                        joInput1101.Add("data", joData1101);

                        InvokeHelper invoker1101 = new InvokeHelper();
                        JObject joRtn1101 = invoker1101.invokeCenterService("1101", JsonHelper.setCenterInpar("1101", joInput1101));
                        if (JsonHelper.parseCenterRtnValue(joRtn1101, out errorMsg) != 0)
                        {
                            outParam = "读卡失败：" + errorMsg;
                            return -1;
                        }
                        else
                        {
                            Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joRtn1101, "output.insuinfo[0].insuplc_admdvs"); //参保地
                            outParam = joRtn1101.ToString();
                            return 0;
                        }
                    }
                    #endregion
                }
                else
                {
                    //if (cBus.Authenticate(out errorMsg) != 0)
                    //{
                    //    outParam = "身份认证失败：" + errorMsg;
                    //    return -1;
                    //}
                }

                if (Global.pat.mdtrtcertType == "01")
                    Global.pat.card.type = "1";
                else
                    Global.pat.card.type = "0";

                #region【1192身份核验（电子凭证/身份证/社保卡）】  
                JObject joData1192 = new JObject();
                joData1192.Add("infno", funNo);
                joData1192.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                joData1192.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                joData1192.Add("cardtype", Global.pat.card.type);
                joData1192.Add("psn_cert_type", Global.pat.certType);
                joData1192.Add("psn_name", Global.pat.name);
                joData1192.Add("certno", Global.pat.certNO); //证件号码
                joData1192.Add("cardtoken", Global.pat.card.Cardtoken);

                JObject joInput1192 = new JObject();
                joInput1192.Add("data", joData1192);

                InvokeHelper invoker1192 = new InvokeHelper();
                JObject joRtn1192 = invoker1192.invokeCenterService("1192", JsonHelper.setCenterInpar("1192", joInput1192));
                if (JsonHelper.parseCenterRtnValue(joRtn1192, out errorMsg) != 0)
                {
                    outParam = "1192身份核验失败：" + errorMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn1192.ToString();
                    return 0;
                }
                #endregion
            }
            catch (Exception ex)
            {
                outParam = "异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 展示患者信息界面，转换相关编码为中文，并获取操作员选中的信息
        /// </summary>
        /// <param name="patInfo"></param>
        /// <param name="outparam"></param>
        /// <returns></returns>
        public int showPatInfo(String patInfo,out string outparam )
        {
            outparam = "";
            //展示信息界面
            JObject joOutparam = JObject.Parse(patInfo);
            JObject joOutput = JObject.Parse(JsonHelper.getDestValue(joOutparam, "output"));

            // 日期处理
            JObject joBaseInfo = JObject.Parse(JsonHelper.getDestValue(joOutput, "baseinfo"));
            JArray joInsuInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "insuinfo"));
            JArray joIDInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "idetinfo"));
            Utils.convertTimestamp(joBaseInfo, "brdy");
            if (joInsuInfo.Count > 0)
            {
                for (int m = 0; m < joInsuInfo.Count; m++)
                {
                    Utils.convertTimestamp((JObject)joInsuInfo[m], "psn_insu_date");
                    Utils.convertTimestamp((JObject)joInsuInfo[m], "paus_insu_date");
                }
            }
            JObject OutIDInfo= new JObject();
            JArray joDic;
            if (joIDInfo.Count > 0)
            {  for(int i = 0;i< joIDInfo.Count; i++)
                {
                    Utils.convertTimestamp((JObject)joIDInfo[i], "begntime");
                    Utils.convertTimestamp((JObject)joIDInfo[i], "endtime");

                    OutIDInfo= rIS.getSpecDictionaryValue(Global.inf.interfaceDr.ToString(), "psn_idet_type", joIDInfo[i]["psn_idet_type"].ToString());
                    joDic= JArray.Parse(JsonHelper.getDestValue(OutIDInfo, "result.data"));
                    if (joDic.Count > 1)
                    {
                        joIDInfo[i]["psn_idet_type"] = joDic[1]["name"].ToString();
                    }

                }
            }

            joOutput["baseinfo"] = joBaseInfo;
            joOutput["insuinfo"] = joInsuInfo;
            joOutput["idetinfo"] = joIDInfo;
            joOutparam["output"] = joOutput;

            //编码转换
            JArray jaConvertCode = new JArray();
            JObject joConvertCodeParam = new JObject();
            joConvertCodeParam.Add("hospitalDr", Global.inf.hospitalDr);
            joConvertCodeParam.Add("interfaceDr", Global.inf.interfaceDr);
            joConvertCodeParam.Add("output", joOutput);
            jaConvertCode.Add(joConvertCodeParam);
            JObject joConvertCode = new JObject();
            joConvertCode.Add("code", "09010054");
            joConvertCode.Add("params", jaConvertCode);
            InvokeHelper invoker = new InvokeHelper();
            JObject joConvertCodeRtn = invoker.invokeInsuService(joConvertCode.ToString(), "患者信息编码转换");
            PatientInsuInfo frmPatientInfo = new PatientInsuInfo(joConvertCodeRtn);
            //相关信息转换到登记面板
            try
            {
                if (frmPatientInfo.ShowDialog() == DialogResult.OK)
                {
                    //原来用的是转换后的
                    //frmPatientInfo.joSelectedInsuInfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    //frmPatientInfo.joSelectedInsuInfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    //joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedInsuInfo", frmPatientInfo.joSelectedInsuInfo));

                    //现在用转换之前的
                    JArray jaInsuInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "insuinfo"));
                    JArray jaIdInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "idetinfo"));
                    JObject joSelectedInsuInfo = (JObject)jaInsuInfo[frmPatientInfo.insuInfoIndex];
                    JObject joSelectedIdInfo;
                    if (jaIdInfo.Count != 0)
                    {
                        joSelectedIdInfo = (JObject)jaIdInfo[frmPatientInfo.idInfoIndex];
                    }
                    else
                    {
                        joSelectedIdInfo = new JObject();
                    }
                    joSelectedInsuInfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    joSelectedInsuInfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedInsuInfo", joSelectedInsuInfo));
                    joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedIdInfo", joSelectedInsuInfo));

                    outparam = joOutparam.ToString();                    

                    return 0;
                }
                else
                {
                    outparam = JsonHelper.setExceptionJson(-1, "门诊读卡", "收款员取消读卡").ToString();
                    return -1;
                }
            }
            catch (Exception e)
            {
                outparam = JsonHelper.setExceptionJson(-1, "患者参保信息展示", e.Message).ToString();
                return -1;
            }
        }

        /// <summary>
        /// 患者读卡并获取中心信息（1101）后设置PAT结构体
        /// </summary>
        /// <param name="patInfo"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int setGlobalPatAfaterShowPatInfo(string patInfo,out string errMsg)
        {
            JObject joPatInfo;
            errMsg = "";
            try
            {                
                joPatInfo = JObject.Parse(patInfo);
                //基本信息
                Global.pat.certNO = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.certno");
                Global.pat.psn_no = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.psn_no");
                Global.pat.gend = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.gend");
                Global.pat.brdy = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.brdy");
                Global.pat.naty = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.naty");
                Global.pat.age = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.age");
                Global.pat.certType = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.psn_cert_type");

                //if (Global.pat.name != JsonHelper.getDestValue(joPatInfo, "output.baseinfo.psn_name"))
                //{
                //    errMsg = "患者姓名(" + Global.pat.name + ")与参保人姓名(" + JsonHelper.getDestValue(joPatInfo, "output.baseinfo.psn_name") + ")不一致，请检查！";
                //    return -1;
                //}

                Global.pat.name = JsonHelper.getDestValue(joPatInfo, "output.baseinfo.psn_name");
                //选中的参保信息
                Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.insuplc_admdvs");
                Global.pat.psn_insu_date = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.psn_insu_date");
                Global.pat.cvlserv_flag = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.cvlserv_flag");
                Global.pat.balc = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.balc");
                Global.pat.emp_name = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.emp_name");
                Global.pat.psn_type = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.psn_type");
                Global.pat.psn_insu_stas = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.psn_insu_stas");
                Global.pat.insuType = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.insutype");
                Global.pat.paus_insu_date = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.paus_insu_date");                

                //卡片信息
                //Global.pat.cardSN = JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo.paus_insu_date");
                //选中的身份信息
                return 0;
            }
            catch (Exception ex)
            {
                errMsg = "获取患者信息发生异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 校验HIS信息与医保参保信息姓名是否一致
        /// </summary>
        /// <param name="insuName"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int checkName(string insuName, out string errMsg)
        {
            try
            {
                errMsg = "";
                string hisPatBaseInfo = "";

                if (hIS.GetHisPatBaseInfo(out hisPatBaseInfo) != 0)
                {
                    errMsg = hisPatBaseInfo;
                    return -1;
                }
                string hisPatName = JsonHelper.getDestValue(JObject.Parse(hisPatBaseInfo), "result.patName");
                if (hisPatName != insuName)
                {
                    errMsg = "患者姓名(" + hisPatName + ")与参保人姓名(" + insuName + ")不一致，请检查！";
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errMsg = "核对HIS与医保平台姓名一致性发生异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 显示住院患者登记面板，返回登记入参
        /// </summary>
        /// <param name="regInfo"></param>
        /// <returns></returns>
        public int showInPatRegisterForm(out string regInfo)
        {

            //调用服务获取门诊诊断信息
            string diagnoses;
            if (hIS.getPatDiagnoses(Global.pat, out diagnoses) != 0)
            {
                regInfo = JsonHelper.setExceptionJson(-1, "getPatDiagnoses", diagnoses).ToString();
                return -1;
            }
            JObject joHisRtnInfo = JObject.Parse(diagnoses);

            //组织登记入参
            JObject joReg = new JObject();
            try
            {
                JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));
                string dscg_maindiag_code  = "",dscg_maindiag_name = "";
                //  对诊断数组进行转换 转换下主要诊断
                JArray jaConvertedDiagnoses = new JArray();
                JArray jaInPatDiagnoses = new JArray();
                JArray jaOutPatDiagnoses = new JArray();
                for (int i = 0; i < jaDiagnoses.Count; i++)
                {
                  
                    string diag_type = JsonHelper.getDestValue((JObject)jaDiagnoses[i], "diag_type");
                    switch (diag_type)
                    {
                        case "1":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医主要诊断";
                                break;
                            }
                        case "2":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医其他诊断";
                                break;
                            }
                        case "3":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主病诊断";
                                break;
                            }
                        case "4":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主证诊断";
                                break;
                            }
                    }
                    

                    if (jaDiagnoses[i]["diaTypeCode"].ToString() == "ADD") //ADD 入院 OPD 门诊
                    {
                        jaInPatDiagnoses.Add(jaDiagnoses[i]);
                    }
                    if (jaDiagnoses[i]["diaTypeCode"].ToString() == "OPD") //ADD 入院 OPD 门诊
                    {
                        jaOutPatDiagnoses.Add(jaDiagnoses[i]);
                    }
                }

                if (jaInPatDiagnoses.Count > 0)
                {
                    jaConvertedDiagnoses = (JArray)jaInPatDiagnoses.DeepClone();
                }
                else 
                {
                    jaConvertedDiagnoses = (JArray)jaOutPatDiagnoses.DeepClone();
                }

                for (int i = 0; i < jaConvertedDiagnoses.Count; i++)
                {
                    jaConvertedDiagnoses[i]["diag_srt_no"] = i + 1;
                    if (jaConvertedDiagnoses[i]["maindiag_flag"].ToString() == "1")
                    {
                        dscg_maindiag_code = jaConvertedDiagnoses[i]["diag_code"].ToString();
                        dscg_maindiag_name = jaConvertedDiagnoses[i]["diag_name"].ToString();
                    }
                }                

   
                //登记面板实例
                OutpatientRegistration frmReg = new OutpatientRegistration();
                frmReg.dblkcbxSettelmentWay.Enabled = false;
                DataTable dt = (DataTable)frmReg.dblkcbxInsuranceType.DataSource;
                int selectedIndex = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Code"].ToString() == Global.pat.insuType)
                    {
                        selectedIndex = i;
                        frmReg.dblkcbxInsuranceType.Text = dt.Rows[i]["Name"].ToString();
                        frmReg.insuType = dt.Rows[i]["Code"].ToString();
                    }
                }

                DataTable dtMedType = (DataTable)frmReg.dblkcbxMedicalType.DataSource;
                for (int i = 0; i < dtMedType.Rows.Count; i++)
                {
                    if (dtMedType.Rows[i]["Code"].ToString() == "21")
                    {
                        frmReg.dblkcbxMedicalType.Text = dtMedType.Rows[i]["Name"].ToString();
                        frmReg.med_type = dtMedType.Rows[i]["Code"].ToString();
                    }
                }
                frmReg.DtDiagnose = (DataTable)jaConvertedDiagnoses.ToObject(typeof(DataTable));
                //展示登记面板
                if (frmReg.ShowDialog() == DialogResult.OK)
                {

                    string diseCodg ="",diseName = "";

                    diseCodg = frmReg.diseCodg;
                    diseName = frmReg.diseName;

                    JObject ExportObj = new JObject();
                    //ExportObj.Add("sin_dise_codg", "");              //单病种编码
                    //ExportObj.Add("cla_trt_flag", frmReg.claTrtFlag);       //分级诊疗标志
                    ////ExportObj.Add("unif_pay_std_typ", frmReg.unifPayStdType);           //包干标准类型
                    //ExportObj.Add("bydise_setl_disediag_codg", "");  //按病种付费疾病诊断代码
                    //ExportObj.Add("bydise_setl_oprn_oprt_code", ""); //按病种付费手册操作代码
                    //ExportObj.Add("trum_flag", frmReg.traumaFlag);          //外伤标志
                    //ExportObj.Add("medcasno", "");                   //病案号
                      ExportObj.Add("ipt_type", frmReg.hospType);          //住院类型
                    //ExportObj.Add("rel_ttp_flag", frmReg.relTtpFlag);       //涉及第三方标志
                    //ExportObj.Add("mdtrt_grp_type", frmReg.mdtrtGrpType);   //就诊人群类型

                    Global.pat.ExpContent = ExportObj.ToString();

                    //哈尔滨要求必须传病种。普通疾病传主诊断
                    //if (string.IsNullOrEmpty(frmReg.diseCodg))
                    //{
                    //    diseCodg = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code");

                    //};
                    //if (string.IsNullOrEmpty(frmReg.diseName))
                    //{
                    //    diseName = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name");
                    //}

                    JObject joMdtrtinfo = new JObject();                    
                    joMdtrtinfo.Add("psn_no", Global.pat.psn_no);          //人员编号
                    joMdtrtinfo.Add("insutype", Global.pat.insuType);          //险种类型
                    joMdtrtinfo.Add("coner_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.coner_name"));          //联系人姓名
                    joMdtrtinfo.Add("tel", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.tel"));          //联系电话
                    joMdtrtinfo.Add("begntime", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.begntime"));          //开始时间
                    joMdtrtinfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);          //就诊凭证类型
                    joMdtrtinfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);          //就诊凭证编号
                    joMdtrtinfo.Add("med_type", frmReg.med_type);          //医疗类别
                    joMdtrtinfo.Add("ipt_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.ipt_no"));          //住院号
                    joMdtrtinfo.Add("medrcdno", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.medrcdno"));          //病历号
                    joMdtrtinfo.Add("atddr_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.atddr_no"));          //主治医生编码
                    joMdtrtinfo.Add("chfpdr_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.chfpdr_name"));          //主诊医师姓名
                    joMdtrtinfo.Add("adm_diag_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_diag_dscr"));          //入院诊断描述
                    joMdtrtinfo.Add("adm_dept_codg", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_codg"));          //入院科室编码
                    joMdtrtinfo.Add("adm_dept_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_name"));          //入院科室名称
                    joMdtrtinfo.Add("adm_bed", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_bed"));          //入院床位
                    joMdtrtinfo.Add("dscg_maindiag_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code"));          //住院主诊断代码
                    joMdtrtinfo.Add("dscg_maindiag_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name"));          //住院主诊断名称
                    //joMdtrtinfo.Add("dscg_maindiag_code", dscg_maindiag_code);          //住院主诊断代码
                    //joMdtrtinfo.Add("dscg_maindiag_name", dscg_maindiag_name);          //住院主诊断名称
                    joMdtrtinfo.Add("main_cond_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.main_cond_dscr"));          //主要病情描述
                    joMdtrtinfo.Add("dise_codg", diseCodg);          //病种编码
                    joMdtrtinfo.Add("dise_name", diseName);          //病种名称
                    joMdtrtinfo.Add("oprn_oprt_code", frmReg.oprn_oprt_code);          //手术操作代码
                    joMdtrtinfo.Add("oprn_oprt_name", frmReg.oprn_oprt_name);          //手术操作名称
                    joMdtrtinfo.Add("fpsc_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fpsc_no"));          //计划生育服务证号
                    joMdtrtinfo.Add("matn_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.matn_type"));          //生育类别
                    joMdtrtinfo.Add("birctrl_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_type"));          //计划生育手术类别
                    joMdtrtinfo.Add("latechb_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.latechb_flag"));          //晚育标志
                    //joMdtrtinfo.Add("geso_val", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.geso_val"));          //孕周数
                    //joMdtrtinfo.Add("fetts", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetts"));          //胎次
                    //joMdtrtinfo.Add("fetus_cnt", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetus_cnt"));          //胎儿数
                    //joMdtrtinfo.Add("pret_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.pret_flag"));          //早产标志
                    //joMdtrtinfo.Add("birctrl_matn_date", "");          //计划生育手术或生育日期 JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_matn_date") 20230228医保中心校验传正确时间或空
                    joMdtrtinfo.Add("dise_type_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dise_type_code"));          //病种类型
                    joMdtrtinfo.Add("exp_content", ExportObj);          //字段扩展


                    joMdtrtinfo.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);          //参保地医保区划
                    joMdtrtinfo.Add("mdtrtarea_admvs", Global.inf.areaCode);          //就医地医保区划

                    joReg.Add("mdtrtinfo", joMdtrtinfo);
                    joReg.Add("diseinfo", jaConvertedDiagnoses);


                    Global.pat.disCode = diseCodg;
                    Global.pat.disName = diseName;
                    Global.pat.medType = frmReg.med_type;

                    regInfo = joReg.ToString();
                    return 0;
                }
                else
                {
                    regInfo = "收费员取消操作！";
                    return -1;
                }
            }
            catch (Exception ex)
            {
                regInfo = "登记界面显示异常：" + ex.Message;
                return -1;
            }

        }


        /// <summary>
        /// 显示出院登记界面，返回出院登记和结算的入参
        /// </summary>
        /// <param name="joSumFee"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int showDischargeRegisterForm(string funNO, JObject joSumFee, JObject joHisRtnInfo, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));
                //选择结算方式等
                OutpatientRegistration frmReg = new OutpatientRegistration();
                frmReg.dblkcbxInsuranceType.Enabled = true;

                DataTable dt = (DataTable)frmReg.dblkcbxInsuranceType.DataSource;
                int selectedIndex = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Code"].ToString() == Global.pat.insuType)
                    {
                        selectedIndex = i;

                        frmReg.dblkcbxInsuranceType.Text = dt.Rows[i]["Name"].ToString();
                        frmReg.insuType = dt.Rows[i]["Code"].ToString();
                    }
                }

                dt = (DataTable)frmReg.dblkcbxMedicalType.DataSource;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["Code"].ToString() == Global.pat.medType)
                    {
                        frmReg.dblkcbxMedicalType.Text = dt.Rows[i]["Name"].ToString();
                        frmReg.med_type = dt.Rows[i]["Code"].ToString();
                    }
                }

                if (Global.pat.RYorCY == "1")
                {
                    DataTable dtMedType = (DataTable)frmReg.dblkcbxMedicalType.DataSource;
                    for (int i = 0; i < dtMedType.Rows.Count; i++)
                    {
                        if (dtMedType.Rows[i]["Code"].ToString() == "21")
                        {
                            frmReg.dblkcbxMedicalType.Text = dtMedType.Rows[i]["Name"].ToString();
                            frmReg.med_type = dtMedType.Rows[i]["Code"].ToString();
                        }
                    }
                }
                DataTable dtSettlWay = (DataTable)frmReg.dblkcbxSettelmentWay.DataSource;
                for (int i = 0; i < dtSettlWay.Rows.Count; i++)
                {
                    if (dtSettlWay.Rows[i]["Code"].ToString() == "01")
                    {
                        frmReg.dblkcbxSettelmentWay.Text = dtSettlWay.Rows[i]["Name"].ToString();
                        frmReg.psn_setlway = dtSettlWay.Rows[i]["Code"].ToString();
                    }
                }

                DataTable DTdiseCodg = (DataTable)frmReg.dblkcbxDisease.DataSource;
                for (int i = 0; i < DTdiseCodg.Rows.Count; i++)
                {
                    if (DTdiseCodg.Rows[i]["Code"].ToString() == Global.pat.DiseasecCode)
                    {
                        frmReg.dblkcbxDisease.Text = DTdiseCodg.Rows[i]["Name"].ToString();
                        frmReg.diseCodg = DTdiseCodg.Rows[i]["Code"].ToString();
                    }
                }
                if (frmReg.dblkcbxInsuranceType.Text != "")
                    frmReg.dblkcbxInsuranceType.Enabled = false;
                if (frmReg.dblkcbxMedicalType.Text != "")
                    frmReg.dblkcbxMedicalType.Enabled = false;
                if (frmReg.dblkcbxDisease.Text != "")
                    frmReg.dblkcbxDisease.Enabled = false;
                //  对诊断数组进行转换 转换下主要诊断
                JArray jaConvertedDiagnoses = new JArray();
                string dscg_maindiag_code = "", dscg_maindiag_name = "";
                for (int i = 0; i < jaDiagnoses.Count; i++)
                {
                    string diag_type = JsonHelper.getDestValue((JObject)jaDiagnoses[i], "diag_type");
                    switch (diag_type)
                    {
                        case "1":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医主要诊断";
                                break;
                            }
                        case "2":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医其他诊断";
                                break;
                            }
                        case "3":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主病诊断";
                                break;
                            }
                        case "4":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主证诊断";
                                break;
                            }
                    }

                    if (jaDiagnoses[i]["diaTypeCode"].ToString() == "DIS") //出院
                    {
                        jaConvertedDiagnoses.Add(jaDiagnoses[i]);
                    }
                }

                for (int i = 0; i < jaConvertedDiagnoses.Count; i++)
                {
                    jaConvertedDiagnoses[i]["diag_srt_no"] = i + 1;
                    if (jaConvertedDiagnoses[i]["maindiag_flag"].ToString() == "1")
                    {
                        dscg_maindiag_code = jaConvertedDiagnoses[i]["diag_code"].ToString();
                        dscg_maindiag_name = jaConvertedDiagnoses[i]["diag_name"].ToString();
                    }
                    //增加就诊ID
                    jaConvertedDiagnoses[i]["mdtrt_id"] = new JObject();
                    jaConvertedDiagnoses[i]["mdtrt_id"] = Global.pat.mdtrtID;
                }


                //出院登记扩展字段值解析，弹框展示        
                if ((Global.pat.ExpContent != "") && (Global.pat.RYorCY == "2"))
                {             
                    //JObject ExpContentObj = JObject.Parse(Global.pat.ExpContent);
                    //frmReg.claTrtFlag = JsonHelper.getDestValue(ExpContentObj, "cla_trt_flag");
                    //frmReg.unifPayStdType = JsonHelper.getDestValue(ExpContentObj, "unif_pay_std_type");
                    //frmReg.traumaFlag = JsonHelper.getDestValue(ExpContentObj, "trum_flag");
                    //frmReg.iptTypeCode = JsonHelper.getDestValue(ExpContentObj, "ipt_type");
                    //frmReg.relTtpFlag = JsonHelper.getDestValue(ExpContentObj, "rel_ttp_flag");
                    //frmReg.mdtrtGrpType = JsonHelper.getDestValue(ExpContentObj, "mdtrt_grp_type");

                    //dt = (DataTable)frmReg.dblkcbxHospType.DataSource;
                    //for (int i = 0; i < dt.Rows.Count; i++)
                    //{
                    //    if (dt.Rows[i]["Code"].ToString() == frmReg.iptTypeCode)
                    //    {
                    //        frmReg.dblkcbxHospType.Text = dt.Rows[i]["Name"].ToString();
                    //        frmReg.iptTypeCode = dt.Rows[i]["Code"].ToString();
                    //    }
                    //}
                    if (frmReg.claTrtFlag == "1") frmReg.chk_claTrtFlag.Checked = true;
                    if (frmReg.traumaFlag == "1") frmReg.Chk_TraumaFlag.Checked = true;
                    if (frmReg.relTtpFlag == "1") frmReg.Chk_RelTtpFlag.Checked = true;
                    if (frmReg.unifPayStdType != "")
                    {
                        frmReg.cbxUnifPayStdType.SelectedIndex = int.Parse(frmReg.unifPayStdType) - 1;

                        Global.pat.unifPayStdType = int.Parse(frmReg.unifPayStdType) - 1;
                    }
                    if (frmReg.mdtrtGrpType != "") frmReg.cbxMdtrtGrpType.SelectedIndex = int.Parse(frmReg.mdtrtGrpType)-1 ;
                }
                frmReg.DtDiagnose = (DataTable)jaConvertedDiagnoses.ToObject(typeof(DataTable));
                if (frmReg.ShowDialog() == DialogResult.OK)
                {
                    string diseCodg = "", diseName = "";
                    //哈尔滨要求必须传病种。普通疾病传主诊断
                    if (string.IsNullOrEmpty(frmReg.diseCodg))
                    {
                        diseCodg = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code");
                    };
                    if (string.IsNullOrEmpty(frmReg.diseName))
                    {
                        diseName = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name");
                    };
                    //出院登记入参
                    //单病种出院登记 病种信息取入院登记入参
                    if (Global.pat.RYorCY == "2")
                    {
                        if (Global.pat.medType == "26")
                        {
                            diseCodg = Global.pat.DiseasecCode;
                            diseName = Global.pat.DiseasecName;
                        }
                        else
                        {
                            frmReg.insuType = Global.pat.insuType;                            
                        }                        
                    }
                    JObject ExportObj = new JObject();
                    //出院登记扩展参数用入院登记时保存的
                    if ((Global.pat.ExpContent != "") && (Global.pat.RYorCY == "2"))
                    {                        
                        ExportObj = JObject.Parse(Global.pat.ExpContent);
                    }
                    else
                    {                        
                        ExportObj.Add("sin_dise_codg", "");              //单病种编码
                        ExportObj.Add("cla_trt_flag", frmReg.claTrtFlag);       //分级诊疗标志
                        ExportObj.Add("unif_pay_std_typ", frmReg.unifPayStdType);           //包干标准类型
                        ExportObj.Add("bydise_setl_disediag_codg", "");  //按病种付费疾病诊断代码
                        ExportObj.Add("bydise_setl_oprn_oprt_code", ""); //按病种付费手册操作代码
                        ExportObj.Add("trum_flag", frmReg.traumaFlag);          //外伤标志
                        ExportObj.Add("medcasno", "");                   //病案号
                        ExportObj.Add("ipt_type", frmReg.iptTypeCode);          //住院类型
                        ExportObj.Add("rel_ttp_flag", frmReg.relTtpFlag);       //涉及第三方标志
                        ExportObj.Add("mdtrt_grp_type", frmReg.mdtrtGrpType);   //就诊人群类型
                    }
                    JObject joDscgInfo = new JObject();
                    joDscgInfo.Add("mdtrt_id", Global.pat.mdtrtID);
                    joDscgInfo.Add("psn_no", Global.pat.psn_no);          //人员编号
                    joDscgInfo.Add("insutype", frmReg.insuType);          //险种类型
                    //joDscgInfo.Add("coner_name", JsonHelper.getJsonValue(joHisPatInfo, "coner_name"));          //联系人姓名
                    //joDscgInfo.Add("tel", JsonHelper.getJsonValue(joHisPatInfo, "tel"));          //联系电话
                    joDscgInfo.Add("endtime", Global.pat.hisDischargeTime);          //结束时间DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    joDscgInfo.Add("dise_codg", diseCodg);          //病种编码
                    joDscgInfo.Add("dise_name", diseName);          //病种名称
                    joDscgInfo.Add("oprn_oprt_code", frmReg.oprn_oprt_code);          //手术操作代码
                    joDscgInfo.Add("oprn_oprt_name", frmReg.oprn_oprt_name);          //手术操作名称
                    joDscgInfo.Add("fpsc_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fpsc_no"));          //计划生育服务证号
                    joDscgInfo.Add("matn_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.matn_type"));          //生育类别
                    joDscgInfo.Add("birctrl_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_type"));          //计划生育手术类别
                    joDscgInfo.Add("latechb_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.latechb_flag"));          //晚育标志
                    //joDscgInfo.Add("geso_val", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.geso_val"));          //孕周数
                    //joDscgInfo.Add("fetts", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetts"));          //胎次
                    //joDscgInfo.Add("fetus_cnt", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetus_cnt"));          //胎儿数
                    joDscgInfo.Add("pret_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.pret_flag"));          //早产标志
                    //joDscgInfo.Add("birctrl_matn_date", "");          //计划生育手术或生育日期 JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_matn_date") 20230228医保中心校验传正确时间或空
                    joDscgInfo.Add("cop_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dise_type_code"));          //伴有并发症标志
                    joDscgInfo.Add("dscg_dept_codg", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_codg"));          //出院科室编码
                    joDscgInfo.Add("dscg_dept_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_name"));          //出院科室名称
                    joDscgInfo.Add("dscg_bed", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_bed"));          //出院床位
                    joDscgInfo.Add("dscg_way", "1");          //离院方式
                    //joDscgInfo.Add("die_date", "");          //死亡日期
                    joDscgInfo.Add("exp_content", ExportObj);          //字段扩展
                    joDscgInfo.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);          //参保地医保区划
                    joDscgInfo.Add("mdtrtarea_admvs", Global.inf.areaCode);          //就医地医保区划

                    JObject joRegInpar = new JObject();
                    joRegInpar.Add("dscginfo", joDscgInfo);
                    joRegInpar.Add("diseinfo", jaConvertedDiagnoses);

                    //结算入参
                    JObject joSetlInpar = new JObject();
                    joSetlInpar.Add("psn_no", Global.pat.psn_no);
                    joSetlInpar.Add("chrg_bchno", Global.pat.adm_Dr.ToString());
                    joSetlInpar.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    joSetlInpar.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    joSetlInpar.Add("med_type", Global.pat.medType);
                    joSetlInpar.Add("medfee_sumamt", JsonHelper.getDestValue(joSumFee, "data.Sumamt"));
                    joSetlInpar.Add("psn_setlway", frmReg.psn_setlway); //结算方式
                    joSetlInpar.Add("mdtrt_id", Global.pat.mdtrtID);
                    joSetlInpar.Add("acct_used_flag", frmReg.personAccountUsedFlag);//个人账户使用标志
                    joSetlInpar.Add("insutype", frmReg.insuType);//险种类型
                    //joSetlInpar.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);
                    //joSetlInpar.Add("mdtrtarea_admvs", Global.inf.areaCode);
                    joSetlInpar.Add("invono", "");
                    joSetlInpar.Add("mid_setl_flag", "0");
                    joSetlInpar.Add("fulamt_ownpay_amt", JsonHelper.getDestValue(joSumFee, "data.FullOwnPayAmount"));
                    joSetlInpar.Add("overlmt_selfpay", JsonHelper.getDestValue(joSumFee, "data.OverLimitAmount"));
                    joSetlInpar.Add("preselfpay_amt", JsonHelper.getDestValue(joSumFee, "data.PreSelfPayAmount"));
                    joSetlInpar.Add("dscgTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    JObject joDataExp = new JObject();
                    //joDataExp.Add("acct_payamt", "0.00");
                    //joDataExp.Add("cla_trt_flag", "");
                    joDataExp.Add("mdtrt_grp_type", "0");//教助人群类型 1新冠肺炎确诊患者;2 其他国家突发公共事件；3 新冠肺炎疑似患者 4 无症状感染者 ;0 无
                    joDataExp.Add("rel_ttp_flag", frmReg.relTtpFlag);//涉及第三方标志 0 否   1是
                    joDataExp.Add("trum_flag", frmReg.traumaFlag);//外伤标志  0 否   1是
                    joDataExp.Add("otp_er_refl_flag",0);//门诊急诊转住院标志 1急诊 2 转诊  3 转诊合并急诊
                    joDataExp.Add("need_ipt_flag", 0);//门诊转住院标志 0 未转住院  1 转住院
                    joDataExp.Add("drt_setl_type_flag", 0);
                    // IPT_TYPE 住院类型 1  普通住院 2 急诊或抢救住院 3 转院住院 0无   
                    //DRT_SETL_TYPE_FLAG直接结算分类标志 1 免备案

                    joSetlInpar.Add("exp_content", joDataExp); //11111
                    JObject joOut = new JObject();
                    joOut.Add("DischargeRegister", joRegInpar);
                    joOut.Add("Settlement", joSetlInpar);
                    joOut.Add("errorCode", 0);
                    outParam = joOut.ToString();

                    return 0;
                }
                else
                {
                    outParam = "收款员取消结算";
                    return -1;
                }
            }
            catch (Exception ex)
            {
                outParam = "showDischargeRegisterForm 异常：" + ex.Message;
                return -1;
            }
        }
        public int showOutPatRegisterForm(out string regInfo)
        {
            
            //调用服务获取门诊诊断信息
            string diagnoses;
            if (hIS.getPatDiagnoses(Global.pat, out diagnoses) != 0)
            {
                regInfo = JsonHelper.setExceptionJson(-1, "getPatDiagnoses", diagnoses).ToString();
                return -1;
            }
            JObject joHisRtnInfo = JObject.Parse(diagnoses);            

            //组织入参
            JObject joReg = new JObject();
            try
            {
                JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));
                //登记面板实例
                OutpatientRegistration frmReg = new OutpatientRegistration();
                //frmReg.dblkcbxSettelmentWay.Enabled = false;

                //险种类型
                DataTable dtInsuType = (DataTable)frmReg.dblkcbxInsuranceType.DataSource;
                for (int i = 0; i < dtInsuType.Rows.Count; i++)
                {
                    if (dtInsuType.Rows[i]["Code"].ToString() == Global.pat.insuType)
                    {
                        frmReg.dblkcbxInsuranceType.Text = dtInsuType.Rows[i]["Name"].ToString();
                        frmReg.insuType = dtInsuType.Rows[i]["Code"].ToString();
                    }
                }

                //医疗类别
                DataTable dtMedType = (DataTable)frmReg.dblkcbxMedicalType.DataSource;
                for (int i = 0; i < dtMedType.Rows.Count; i++)
                {
                    if (dtMedType.Rows[i]["Code"].ToString() == "11" && Global.pat.insuType=="310")
                    {
                        frmReg.dblkcbxMedicalType.Text = dtMedType.Rows[i]["Name"].ToString();
                        frmReg.med_type = dtMedType.Rows[i]["Code"].ToString();
                    }
                    if (dtMedType.Rows[i]["Code"].ToString() == "990201" && Global.pat.insuType == "390")//合肥居民医保默认大额门诊
                    {
                        frmReg.dblkcbxMedicalType.Text = dtMedType.Rows[i]["Name"].ToString();
                        frmReg.med_type = dtMedType.Rows[i]["Code"].ToString();
                    }
                }

                //结算方式
                DataTable dtSettlWay = (DataTable)frmReg.dblkcbxSettelmentWay.DataSource;
                for (int i = 0; i < dtSettlWay.Rows.Count; i++)
                {
                    if (dtSettlWay.Rows[i]["Code"].ToString() == "01")
                    {
                        frmReg.dblkcbxSettelmentWay.Text = dtSettlWay.Rows[i]["Name"].ToString();
                        frmReg.psn_setlway = dtSettlWay.Rows[i]["Code"].ToString();
                    }
                }

                //转换下主要诊断
                for (int i = 0; i < jaDiagnoses.Count; i++)
                {
                    string diag_type = JsonHelper.getDestValue((JObject)jaDiagnoses[i], "diag_type");
                    switch (diag_type)
                    {
                        case "1":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医主要诊断";
                                break;
                            }
                        case "2":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医其他诊断";
                                break;
                            }
                        case "3":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主病诊断";
                                break;
                            }
                        case "4":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主证诊断";
                                break;
                            }
                    }

                }
                frmReg.DtDiagnose = (DataTable)jaDiagnoses.ToObject(typeof(DataTable));
                //展示登记面板
                if (frmReg.ShowDialog() == DialogResult.OK)
                {
                    
                    //string diseCodg = "", diseName = "";
                    ////哈尔滨要求必须传病种。普通疾病传主诊断
                    //if (string.IsNullOrEmpty(frmReg.diseCodg))
                    //{
                    //    diseCodg = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code");
                    //};
                    //if (string.IsNullOrEmpty(frmReg.diseName))
                    //{
                    //    diseName = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name");
                    //};
                    //组织挂号入参
                    JObject joRegInpar = new JObject();
                    string occurTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    joRegInpar.Add("psn_no", Global.pat.psn_no);
                    joRegInpar.Add("insutype", frmReg.insuType);
                    joRegInpar.Add("begntime", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.begntime"));//挂号时间 occurTime
                    joRegInpar.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    joRegInpar.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    joRegInpar.Add("ipt_otp_no", Global.pat.adm_Dr);
                    joRegInpar.Add("atddr_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.atddr_no"));
                    joRegInpar.Add("dr_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.chfpdr_name"));
                    joRegInpar.Add("dept_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_codg"));
                    joRegInpar.Add("dept_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_name"));
                    joRegInpar.Add("exp_content", "");
                    joRegInpar.Add("caty", "72");
                    joReg.Add("data", joRegInpar);

                    //组织就诊信息入参
                    JObject joMdtrtinfo = new JObject();
                    for (int i = 0; i < jaDiagnoses.Count; i++)
                    {
                        jaDiagnoses[i]["vali_flag"] = new JObject();
                        jaDiagnoses[i]["vali_flag"] = "1";
                    }
                    joMdtrtinfo.Add("mdtrt_id", Global.pat.mdtrtID);
                    joMdtrtinfo.Add("psn_no", Global.pat.psn_no);          //人员编号
                    Global.pat.medType = frmReg.med_type;
                    joMdtrtinfo.Add("med_type", frmReg.med_type);          //医疗类别
                    joMdtrtinfo.Add("begntime", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.begntime"));          //开始时间
                    joMdtrtinfo.Add("main_cond_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.main_cond_dscr"));          //主要病情描述
                    joMdtrtinfo.Add("dise_codg", frmReg.diseCodg);          //病种编码
                    joMdtrtinfo.Add("dise_name", frmReg.diseName);          //病种名称
                    joMdtrtinfo.Add("birctrl_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_type"));          //计划生育手术类别
                    //joMdtrtinfo.Add("birctrl_matn_date", "");          //计划生育手术或生育日期 JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_matn_date") 20230228医保中心校验传正确时间或空
                    joMdtrtinfo.Add("matn_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.matn_type"));          //生育类别
                    //joMdtrtinfo.Add("geso_val", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.geso_val"));          //孕周数
                    joMdtrtinfo.Add("exp_content", "");          //字段扩展

                    //组织结算入参
                    JObject joSetlInpar = new JObject();
                    joSetlInpar.Add("psn_no", Global.pat.psn_no);
                    joSetlInpar.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    joSetlInpar.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    joSetlInpar.Add("med_type", Global.pat.medType);
                    joSetlInpar.Add("medfee_sumamt", "");
                    joSetlInpar.Add("psn_setlway", frmReg.psn_setlway); //结算方式
                    joSetlInpar.Add("mdtrt_id", Global.pat.mdtrtID);
                    joSetlInpar.Add("chrg_bchno", Global.pat.adm_Dr.ToString());
                    joSetlInpar.Add("acct_used_flag", frmReg.personAccountUsedFlag);//个人账户使用标志
                    joSetlInpar.Add("insutype", frmReg.insuType);//险种类型
                    joSetlInpar.Add("pub_hosp_rfom_flag", "");
                    //joSetlInpar.Add("exp_content11", "");
                    JObject joDataExp = new JObject();
                    //joDataExp.Add("acct_payamt", "0.00");
                    //joDataExp.Add("cla_trt_flag", "");
                    joDataExp.Add("mdtrt_grp_type", "0");//教助人群类型 1新冠肺炎确诊患者;2 其他国家突发公共事件；3 新冠肺炎疑似患者 4 无症状感染者 ;0 无
                    joDataExp.Add("rel_ttp_flag", frmReg.relTtpFlag);//涉及第三方标志 0 否   1是
                    joDataExp.Add("trum_flag", frmReg.traumaFlag);//外伤标志  0 否   1是
                    joDataExp.Add("otp_er_refl_flag", frmReg.ouOrEmrefFlag);//门诊急诊转住院标志 0 无 1急诊 2 转诊  3 转诊合并急诊
                    joDataExp.Add("need_ipt_flag", 0);//门诊转住院标志 0 未转住院  1 转住院
                    joDataExp.Add("drt_setl_type_flag", 0);
                    // IPT_TYPE 住院类型 1  普通住院 2 急诊或抢救住院 3 转院住院 0无   
                    //DRT_SETL_TYPE_FLAG直接结算分类标志 1 免备案

                    joSetlInpar.Add("exp_content", joDataExp); //11111


                    joReg.Add("mdtrtinfo", joMdtrtinfo);
                    joReg.Add("diseinfo", jaDiagnoses);
                    joReg.Add("settlement", joSetlInpar);

                    regInfo = joReg.ToString();
                    return 0;
                }
                else
                {
                    regInfo = "收费员取消操作！";
                    return -1;
                }
            }
            catch (Exception ex)
            {
                regInfo = "登记界面显示异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 上传费用到中心
        /// </summary>
        /// <returns></returns>
        /// 
        public int  uploadFee(string funNO,int singleCount,out string outParam)
        {
            outParam = "";
            //获取IS费用
            if (hIS.getHisFee(Global.pat, out outParam) != 0)
            {
                return -1;
            }
            //调用医保平台转换HIS费用（转换医保编码等）
            JObject joHisFee = JObject.Parse(outParam);

            if (mIS.convertHisFeeWithInsuCode(joHisFee, out outParam) != 0)
            {
                return -2;
            }

            JArray jaFeeDetail = JArray.Parse(JsonHelper.getDestValue(JObject.Parse(outParam), "data"));
            //按指定条数分割后上传，保存，更新
            if (uploadFeeToCenter(funNO, singleCount, jaFeeDetail, out outParam) != 0)
            {
                return -3;
            }
            else
            {
                outParam = "成功";
                return 0;
            }
            
        }

        public int uploadFeeToCenter(string funNO,int singleCount,JArray jaFeeDetail, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JArray jaTmp = new JArray();
                for (int i = 0; i < jaFeeDetail.Count; i++)
                {
                    jaFeeDetail[i]["chrg_bchno"] = Global.pat.adm_Dr.ToString();
                    jaFeeDetail[i]["med_type"] = Global.pat.medType;
                    jaFeeDetail[i]["used_frqu_dscr"] = rIS.getFrquDictionaryValue(Global.inf.interfaceDr.ToString(), jaFeeDetail[i]["used_frqu_dscr"].ToString());
                    if (jaFeeDetail[i]["prd_days"] == null || jaFeeDetail[i]["prd_days"].ToString() == "")
                    {
                        jaFeeDetail[i]["prd_days"] = null;
                    }
                    if (jaFeeDetail[i]["validFlag"].ToString() == "N") continue;
                    if (jaFeeDetail[i]["det_item_fee_sumamt"].ToString() == "0.00") continue;
                    jaTmp.Add(jaFeeDetail[i]);
                    if (((i + 1) % singleCount == 0) || ((i + 1) == jaFeeDetail.Count))
                    {
                        JObject joFeeDetail = new JObject();
                        joFeeDetail.Add("feedetail", jaTmp);

                        JObject joRtn = invoker.invokeCenterService(funNO, JsonHelper.setCenterInpar(funNO, joFeeDetail.ToString()));

                        if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
                        {
                            outParam = errMsg;
                            return -1;
                        }
                        else
                        {
                            //成功后插入医保平台
                            if (mIS.insertFee(jaTmp, out errMsg) != 0)
                            {
                                outParam = outParam + errMsg;
                                return -1;
                            }
                            //更新2301返回的数据
                            if (mIS.updateFee(JArray.Parse(JsonHelper.getDestValue(joRtn, "output.result")), out errMsg) != 0)
                            {
                                outParam = outParam + errMsg;
                                return -1;
                            }
                        }
                        jaTmp = new JArray();
                    }

                }
                return 0;
            }
            catch (Exception ex)
            {
                outParam = "uploadFee 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 获取登记时候传入的信息，并赋值给patients结构体
        /// </summary>
        /// <param name="businessType"></param>
        /// <param name="inparam"></param>
        /// <param name="pat"></param>
        public void GetRegPatInfo(string businessType, JObject joInparam,out Patients pat)
        {
            pat = new Patients();
            try
            {
                
                switch (businessType)
                {
                    case "M2"://门诊登记
                        {
                            break;
                        }
                    case "M2C"://门诊登记撤销
                        {
                            break;
                        }
                    case "Z0"://住院修改
                        {
                            break;
                        }
                    case "Z2"://入院登记
                        {
                            pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "params[0].admID"));
                            pat.psn_no = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.output.baseinfo.psn_no");
                            pat.insuType = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.selectedInsuInfo.insutype");
                            pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.selectedInsuInfo.insuplc_admdvs");
                            pat.mdtrtcertType = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.selectedInsuInfo.mdtrt_cert_type");
                            pat.mdtrtcertNO = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.selectedInsuInfo.mdtrt_cert_no");
                            pat.name = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.output.baseinfo.psn_name");
                            pat.psn_type = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.selectedInsuInfo.psn_type");
                            pat.certType = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.output.baseinfo.psn_cert_type");
                            pat.certNO = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.output.baseinfo.certno");
                            pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                            pat.card.SN = JsonHelper.getDestValue(joInparam, "params[0].patientInfo.cardinfo.card_sn");
                            break;
                        }
                    case "Z2C"://入院登记取消
                        {
                            pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "params[0].admID"));
                            pat.psn_no = JsonHelper.getDestValue(joInparam, "params[0].psn_no");
                            pat.mdtrtID = JsonHelper.getDestValue(joInparam, "params[0].mdtrt_id");
                            //pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "params[0].insuplc_admdvs");
                            pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                            break;
                        }
                    case "Z6"://出院登记
                        {
                            pat.hisDischargeTime = Convert.ToDateTime(JsonHelper.getDestValue(joInparam, "insuAdmObj.disDateTime")).ToString("yyyy-MM-dd HH:mm:ss");

                            break;
                        }
                    case "Z6C"://出院登记取消
                        {
                            pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                            pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                            pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                //注释
                ////如果患者异地的则就医地需要该为369900
                //if (Global.pat.insuplc_admdvs != "")
                //{
                //    if (Global.pat.insuplc_admdvs.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
                //    {
                //        Global.inf.areaCode = "539900";
                //    }
                //}
            }
            catch (Exception ex)
            {
                Global.writeLog("GetRegPatInfo(" + businessType + ")", JsonHelper.Compress(joInparam),"PAT结构体设置异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 获取费用上传时前端传入的信息，并赋值给patients结构体
        /// </summary>
        /// <param name="businessType"></param>
        /// <param name="joInparam"></param>
        /// <param name="pat"></param>
        public void GetFeeHisInfo(string businessType, JObject joInparam, out Patients pat)
        {
            pat = new Patients();
            try
            {
                switch (businessType)
                {
                    case "M3"://门诊费用上传
                        {
                            break;
                        }
                    case "M3C"://门诊费用撤销
                        {

                            break;
                        }
                    case "Z3"://入院费用上传
                        {
                            pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "insuAdmObj.admID"));
                            pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                            pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                            pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                            pat.name = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_name");
                            pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                            pat.recordID = "";
                            break;
                        }
                    case "Z3C"://住院费用上传取消
                        {
                            pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "insuAdmObj.admID"));
                            pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                            pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                            pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                            break;
                        }

                    default:
                        {
                            break;
                        }

                }
                ////如果患者异地的则就医地需要该为369900
                //if (Global.pat.insuplc_admdvs != "")
                //{
                //    if (Global.pat.insuplc_admdvs.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
                //    {
                //        Global.inf.areaCode = "369900";
                //    }
                //}
            }
            catch (Exception ex)
            {
                Global.writeLog("GetFeeHisInfo(" + businessType + ")", JsonHelper.Compress(joInparam), "PAT结构体设置异常：" + ex.Message);
            }


        }

        /// <summary>
        ///  获取结算时前端传入的信息，并赋值给patients结构体
        /// </summary>
        /// <param name="businessType"></param>
        /// <param name="joInparam"></param>
        /// <param name="pat"></param>
        public void GetSettlementHisInfo(string businessType, JObject joInparam, out Patients pat)
        {
            pat = new Patients();
            switch (businessType)
            {
                case "M4"://门诊预结算
                    {

                        break;
                    }
                case "M4C"://门诊预结算撤销
                    {


                        break;
                    }
                case "M5"://门诊结算
                    {
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "params[0].admID"));
                        pat.recordID = JsonHelper.getDestValue(joInparam, "params[0].recordID");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        Global.pat.insuplc_admdvs = "";
                        break;
                    }
                case "M5C"://门诊结算撤销
                    {
                        pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                        pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                        pat.settlID = JsonHelper.getDestValue(joInparam, "insuAdmObj.setl_id");
                        pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "params[0].admID"));
                        pat.recordID = JsonHelper.getDestValue(joInparam, "params[0].recordID");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        break;
                    }
                case "Z4"://住院预结算
                    {
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "insuAdmObj.admID"));
                        pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                        pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                        pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                        pat.name = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_name");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        break;
                    }
                case "Z4C"://住院预结算撤销
                    {
                        pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                        pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                        pat.settlID = JsonHelper.getDestValue(joInparam, "insuAdmObj.setl_id");                        
                        pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "params[0].admID"));
                        pat.recordID = JsonHelper.getDestValue(joInparam, "params[0].recordID");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        break;
                    }
                case "Z5"://住院结算
                    {
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "insuAdmObj.admID"));
                        pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                        pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                        pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                        pat.name = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_name");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        pat.hisDischargeTime = Convert.ToDateTime(JsonHelper.getDestValue(joInparam, "insuAdmObj.disDateTime")).ToString("yyyy-MM-dd HH:mm:ss");
                        break;
                    }
                case "Z5C"://住院结算撤销
                    {
                        pat.adm_Dr = int.Parse(JsonHelper.getDestValue(joInparam, "insuAdmObj.admID"));
                        pat.psn_no = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_no");
                        pat.mdtrtID = JsonHelper.getDestValue(joInparam, "insuAdmObj.mdtrt_id");
                        pat.insuplc_admdvs = JsonHelper.getDestValue(joInparam, "insuAdmObj.insuplc_admdvs");
                        pat.name = JsonHelper.getDestValue(joInparam, "insuAdmObj.psn_name");
                        pat.billID = JsonHelper.getDestValue(joInparam, "params[0].billID");
                        pat.settlID = JsonHelper.getDestValue(joInparam, "insuAdmObj.setl_id");
                        pat.SettID_HISJSB = pat.settlID;  //这里是通过09010059交易获取到的结算ID
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            //如果患者异地的则就医地需要该为369900
            //if (Global.pat.insuplc_admdvs != "")
            //{
            //    if (Global.pat.insuplc_admdvs.Substring(0, 4) != Global.inf.areaCode.Substring(0, 4))
            //    {
            //        Global.inf.areaCode = "369900";
            //    }
            //}
            
        }

        ///对目录下载业务的封装
        ///
        public JObject DownloadDirectory(string funNO,string inParam)
        {
            string errorMessage = "", sRtn = "";
            JObject joRtn = new JObject();
            try
            {

                JObject joCenterRtn = cBus.DownDirecotry(funNO,inParam);
                if (JsonHelper.parseCenterRtnValue(joCenterRtn,out errorMessage) != 0)
                {
                    joRtn = JsonHelper.setExceptionJson(-100, "下载异常", errorMessage);
                    return joRtn;
                }
                string filename = JsonHelper.getDestValue(joCenterRtn, "output.filename");

                //MessageBox.Show("1306查询目录文件号返回："+ joCenterRtn.ToString());

                JObject jo9102Rtn = cBus.SaveFile(joCenterRtn,inParam);
                if (JsonHelper.parseIrisRtnValue(jo9102Rtn,out errorMessage) != 0)
                {
                    joRtn = JsonHelper.setExceptionJson(-100, "保存文件异常", errorMessage);
                    return joRtn;
                }
                //解压缩
                string zipFilePath = jo9102Rtn["filePath"].ToString();
                string txtFileDir = System.Environment.CurrentDirectory + "\\Download";
                string txtFilePath = txtFileDir + "\\" + filename.Substring(0, filename.Length - 4);

                //string txtFilePath = Global.curEvt.path + "\\Download\\" + filename;

                //MessageBox.Show("解压缩的txtFilePath："+ zipFilePath+"|解压到目标路径："+ txtFileDir+"|文件解析路径："+ txtFilePath);

                //昆明下载下来的文件是解压后的TXT
                if (tools.ExtractFile(zipFilePath, txtFileDir, out errorMessage) != 0)
                {
                    joRtn = JsonHelper.setExceptionJson(-100, "提取文件", errorMessage);
                    return joRtn;
                }

                joRtn = (JObject)jo9102Rtn.DeepClone();
                joRtn["filePath"] = txtFilePath;
                return joRtn;
            }
            catch (Exception ex)
            {
                joRtn = JsonHelper.setExceptionJson(-100, "下载中心目录", errorMessage);
                return joRtn;
            }
            finally
            {
                Global.writeLog("HisMainBusiness.DownloadDirectory", inParam, joRtn.ToString());
            }
        }

        /// <summary>
        /// 字典下载
        /// </summary>
        /// <param name="inParam"></param>
        /// <returns></returns>
        public string downDictionary(string inParam)
        {
            JObject joCenterRtn = invoker.invokeCenterService("1901", inParam);
            return joCenterRtn.ToString();
        }       

        /// <summary>
        /// 展示结算信息界面
        /// </summary>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int showSettlementForm(JObject joSettlement, out string outParam)
        {
            outParam = "";
            //string errMsg;
            try
            {
                SettlementInfo frmSettlment = new SettlementInfo(joSettlement);
                //相关信息转换到结算信息面板
                if (frmSettlment.ShowDialog() == DialogResult.OK)
                {
                    return 0;
                }
                else
                {
                    outParam = "收款员取消";
                    return -1;
                }
            }
            catch (Exception ex)
            {
                outParam = "showSettlementForm 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 门诊预结算封装，返回正式结算的入参
        /// </summary>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int preSettlement_OutPat( JObject joSettlement, out string outParam)
        {
            outParam = "";

            //预结算  
            JObject jo2206Data = new JObject();
            jo2206Data.Add("data", joSettlement);
            JObject jo2206Rtn = invoker.invokeCenterService("2206", JsonHelper.setCenterInpar("2206", jo2206Data));
            if (JsonHelper.parseCenterRtnValue(jo2206Rtn, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "出院预结算", outParam).ToString();
                return -1;
            }

            //返回给HIS进行预结算判断
            JObject joSetlinfo = JObject.Parse(JsonHelper.getDestValue(jo2206Rtn, "output.setlinfo"));
            if (hIS.preSettlement(joSettlement, joSetlinfo, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "返回结算信息给HIS", outParam).ToString();
                return -1;
            }


            //编码转换
            if (mIS.convertSettlementWithInsuCode(jo2206Rtn, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "结算信息转换", outParam).ToString();
                return -1;
            }

            JObject joSumFee = new JObject();
            string errMsg = "";
            //判断勾稽关系是否平
            if (hIS.sumInsuRtnSettlInfo(JObject.Parse(JsonHelper.getDestValue(jo2206Rtn, "output.setlinfo")), out joSumFee, out errMsg) != 0)
            {
                outParam = "勾稽关系不符合标准，请联系管理员！" + errMsg;
                return -1;
            }

            //展示结算信息
            JObject joConverted = JObject.Parse(outParam);
            if (showSettlementForm(joConverted, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "结算信息展示", outParam).ToString();
                return -1;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 预结算封装，返回正式结算的入参
        /// </summary>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int preSettlement(string funNO,out JObject joSettlement, out string outParam)
        {
            joSettlement = new JObject();
            outParam = "";
            string outPar = "";

            #region 调用IRIS获取医保各项金额
            if (mIS.getSumFee(out outPar) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "获取医保费用各项汇总金额", outPar).ToString();
                return -1;
            }
            JObject joSumFee = JObject.Parse(outPar);            

            if ((JsonHelper.getDestValue(joSumFee, "data.Sumamt") == "") || (JsonHelper.getDestValue(joSumFee, "data.Sumamt") == "0"))
            {
                outParam = "请先右击选择【医保费用上传】！";
                return -1;
            }

            //从云医保平台获取患者详细的医保登记信息
            int type;
            if (funNO == "2206")
            {
                type = 3;  //门诊预结算
            }
            else
            {
                type = 1;  //住院预结算
            }

            if (mIS.queryRegisterInfo(type, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "查询患者云平台登记信息", outParam).ToString();
                return -1;
            }

            JObject joReg = JObject.Parse(outParam);
            Global.pat.medType = JsonHelper.getDestValue(joReg, "data.MedicalType");
            Global.pat.insuType = JsonHelper.getDestValue(joReg, "data.InsuType");
            //Global.pat.mdtrtcertType = JsonHelper.getDestValue(joReg, "data.CertificateType");
            //Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joReg, "data.CertificateNO");//结算取读卡的值，不取登记信息
            Global.pat.certType = JsonHelper.getDestValue(joReg, "data.CertificateType");
            Global.pat.certNO = JsonHelper.getDestValue(joReg, "data.CertificateNO");
            Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joReg, "data.InsuranceAreaCode");
            Global.pat.psn_type = Global.pat.insuType;   //后面再改
            Global.pat.ExpContent = JsonHelper.getDestValue(joReg, "data.ExpContent");

            if ((Global.pat.RYorCY == "2") && (Global.pat.medType == "26"))
            {
                Global.pat.DiseasecCode = JsonHelper.getDestValue(joReg, "data.DiseasecCode");
                Global.pat.DiseasecName = JsonHelper.getDestValue(joReg, "data.DiseasecName");
            }
            #endregion

            #region【住院预结算前调用1192身份验证】
            //if (funNO == "2303")
            //{
            //    if (IdentityVerify("2304", out outParam) != 0)
            //    {
            //        outParam = JsonHelper.setExceptionJson(-1, "调用住院正式结算2304前，身份核验1192失败", outParam).ToString();
            //        return -1;
            //    }
            //}
            #endregion

            #region 昆明结算前读卡核验身份
            //string patInfo = "";
            //if (readCard(out patInfo) != 0)
            //{
            //    outParam = JsonHelper.setExceptionJson(-1, "readCard", patInfo).ToString();
            //    return -1;
            //}

            #endregion

            #region 出院登记
            //调用服务获取门诊诊断信息
            string diagnoses;
            if (hIS.getPatDiagnoses(Global.pat, out diagnoses) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "getPatDiagnoses", diagnoses).ToString();
                return -1;
            }
            JObject joDiagnoses = JObject.Parse(diagnoses);
            //展示出院登记界面，选择结算方式等
            if (showDischargeRegisterForm(funNO, joSumFee, joDiagnoses, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "展示出院预结算界面", outParam).ToString();
                return -1;
            }
            JObject joRtn = JObject.Parse(outParam);
            JObject jo2402Inpar = JObject.Parse(JsonHelper.getDestValue(joRtn, "DischargeRegister"));

            //昆明出院登记放再预结算之前
            JObject jo2402Rtn = invoker.invokeCenterService("2402", JsonHelper.setCenterInpar("2402", jo2402Inpar));
            string errMsg;
            if (JsonHelper.parseCenterRtnValue(jo2402Rtn, out errMsg) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "预结算失败：出院登记失败", errMsg).ToString();
                return -1;
            }
            #endregion

            #region 预结算
            JObject jo2303Inpar = JObject.Parse(JsonHelper.getDestValue(joRtn, "Settlement"));
            JObject jo2303Data = new JObject();
            jo2303Data.Add("data", jo2303Inpar);
            JObject jo2303Rtn = invoker.invokeCenterService(funNO, JsonHelper.setCenterInpar(funNO, jo2303Data));
            if (JsonHelper.parseCenterRtnValue(jo2303Rtn, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "出院预结算", outParam).ToString();
                return -1;
            }

            //预结算结果传给HIS进行HIS判断 
            if (funNO == "2303")
            {
                //返回给HIS
                JObject joSetlinfo = JObject.Parse(JsonHelper.getDestValue(jo2303Rtn, "output.setlinfo"));
                JObject joParam = new JObject();
                joParam.Add("admID", Global.pat.adm_Dr);
                joParam.Add("recordID", Global.pat.recordID);
                joParam.Add("billID", Global.pat.billID);

                if (hIS.preSettlementInfo(joParam, joSetlinfo, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "出院预结算", outParam).ToString();
                    return -1;
                }
            }
            
            //编码转换
            if (mIS.convertSettlementWithInsuCode(jo2303Rtn, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "结算信息转换", outParam).ToString();
                return -1;
            }
            //展示结算信息
            JObject joConverted = JObject.Parse(outParam);
            if (showSettlementForm(joConverted, out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "结算信息展示", outParam).ToString();
                return -1;
            }
            else
            {
                joSettlement = (JObject)joRtn.DeepClone();
                return 0;
            }
            #endregion

        }

        public int returnToFrontEndAfterSettlement(JObject joCenterRtn,JObject joHisServieRtn, out string outParam)
        {
            outParam = "";
            try
            {
                JObject johisInsuInfo = JObject.Parse(JsonHelper.getDestValue(joHisServieRtn, "result"));
                johisInsuInfo.Add("setl_id", JsonHelper.getDestValue(joCenterRtn, "output.setlinfo.setl_id"));
                JObject joTmp = (JObject)joCenterRtn.DeepClone();              
                joTmp.Add("hisInsuInfo", johisInsuInfo);
                outParam = joTmp.ToString();
                return 0;
            }
            catch (Exception ex)
            {
                outParam = "returnToFrontEndAfterSettlement：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 插入患者该次就诊参保信息
        /// </summary>
        /// <param name="mdtrtID"></param>
        /// <param name="patInfo"></param>
        /// <param name="outparam"></param>
        /// <returns></returns>
        public int insertPatCurInsuInfo(String patInfo, out string outparam)
        {
            Global.writeLog(" " + patInfo);
            outparam = "";
            try
            {
                JObject joPatInfo = JObject.Parse(patInfo);
                JObject joBaseInfo = JObject.Parse(JsonHelper.getDestValue(joPatInfo, "output.baseinfo"));
                JObject joInsuInfo = JObject.Parse(JsonHelper.getDestValue(joPatInfo, "selectedInsuInfo"));
                JObject joIdetInfo = JObject.Parse(JsonHelper.getDestValue(joPatInfo, "selectedIdInfo"));
                if (mIS.insertPatCurInsuInfo(joBaseInfo, joInsuInfo, joIdetInfo, out outparam) != 0)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
                
            }
            catch (Exception e)
            {
                outparam =  "插入患者该次就诊参保信息失败：" + e.Message;
                return -1;
            }
        }

        public void getLocalConfingue()
        {
            IniFile ini = new IniFile(Global.curEvt.path + @"\INSUConfigure.ini");
            Global.curEvt.ext.isOpenAnalysis = (ini.ReadValue("PreAndInProcessAnalysis", "isOpenAnalysis") == "1");
        }
        /// <summary>
        /// 事前分析
        /// </summary>
        /// <param name="outparam"></param>
        /// <returns></returns>
        public int PreAnalysis(string trig_scen, string inparam, out string outparam)
        {
            outparam = "";
            try
            {
                ////先签到
                //JObject jo9001Rtn = new JObject();
                //cBus.signIn(out jo9001Rtn);

                if (trig_scen == "5")
                {
                    outparam = inparam;
                }
                else
                {
                    //获取HIS返回的内容
                    if (hIS.PreAnalysis(out outparam) != 0)
                    {
                        return -1;
                    }

                }


                PreAndInProcessAnalysis preAnalysis = new PreAndInProcessAnalysis(outparam);
                if (preAnalysis.setPatientByHisBaseInfo(out outparam) != 0)
                {
                    return -1;
                }
                outparam = preAnalysis.Get3101Inpar(trig_scen, Global.pat).ToString();
                JObject joRtn = invoker.invokeCenterService("3102", JsonHelper.setCenterInpar("3102", outparam));
                if (JsonHelper.parseCenterRtnValue(joRtn, out outparam) != 0)
                {
                    return -1;
                }
                else
                {
                    JArray jaResult = JArray.Parse(JsonHelper.getDestValue(joRtn, "output.result"));
                    if (jaResult.HasValues)
                    {
                        PreAndInProcessAnalysisForm paipaForm = new PreAndInProcessAnalysisForm(joRtn);
                        if (paipaForm.ShowDialog() == DialogResult.OK)
                        {
                            return 0;
                        }
                        else
                        {
                            outparam = "事前事中审核不通过，操作员取消!";
                            return -1;
                        }
                    }
                    else
                        return 0;
                }


            }
            catch (Exception ex)
            {
                outparam = ex.Message;
                return -1;
            }
        }

    }
}
