/******************************************************************************
 * 文件名称: CardReader.cs
 * 文件说明: 读卡的封装，包括社保卡，身份证，电子凭证等等
 * 当前版本: V1.0
 * 创建日期: 2022-06-25
 *
 * * 2020-06-20: 增加 IrisServices 类(基类)
 * *****  2020-06-25: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-25: 增加 CardReader 方法重载，国家版电子凭证使用
 * 
 * 
 * * 2020-06-20: 增加 HisIrisServices 类(子类)
 * *****  2020-06-25: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-25: 增加 CardReader 方法重载，国家版电子凭证使用
 * 
 * 
 * * 2020-06-20: 增加 MIIrisServices 类(子类)
 * *****  2020-06-25: 增加 CardReader 方法，获取URL地址,USER信息，地方版SSCard.dll使用
 * *****  2020-06-25: 增加 CardReader 方法重载，国家版电子凭证使用



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

namespace PTMedicalInsurance.Business
{
    
    class IrisServices
    {
    }

    class HisIrisServices
    {
        private InvokeHelper invoker = new InvokeHelper();

        /// <summary>
        /// 获取患者基本信息
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int GetHisPatBaseInfo(out string errMsg)
        {
            errMsg = "";
            try
            {
                dynamic joTmp = new JObject();
                joTmp.admID = Global.pat.adm_Dr;                
                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110021", joTmp).ToString(), "获取HIS患者基本信息");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    errMsg = "获取HIS患者基本信息失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    Global.pat.name= joRtn["result"]["patName"].ToString();
                    Global.pat.certNO = joRtn["result"]["credNo"].ToString();//用于读卡前调用获取身份证号，节省收费员输入身份证号
                    errMsg = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errMsg = "GetHisPatBaseInfo异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 获取诊断信息
        /// </summary>
        /// <param name="jaSession"></param>
        /// <param name="diagnoses"></param>
        /// <returns></returns>
        public int getPatDiagnoses(Patients pat, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JObject joTmp = new JObject();
                joTmp.Add("admID", pat.adm_Dr);
                joTmp.Add("psn_no", pat.psn_no);

                //JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110001", joTmp).ToString(), "获取诊断信息");
                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110014", joTmp).ToString(), "获取诊断信息");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "获取诊断信息失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "getPatDiagnoses 异常：" + ex.Message;
                return -1;
            }

        }

        /// <summary>
        /// 返回登记信息给HIS
        /// </summary>
        /// <param name="joRegIn"></param>
        /// <param name="joRegOut"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int returnRegisterInfo(JObject joRegInOld, JObject joRegOut, out string outParam)
        {
            string errMsg;
            try
            {
                JObject joRegIn = Utils.removeWrapper(joRegInOld);

                joRegIn.Add("admID", Global.pat.adm_Dr);
                joRegIn.Add("mdtrt_id", Global.pat.mdtrtID);
                joRegIn.Add("interfaceDr", Global.inf.interfaceDr);
                joRegIn.Add("billID", Global.pat.billID);

                string serviceCode = "05110002", serviceName = "将登记信息返回给HIS";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joRegIn).ToString();
                JObject joRtn = invoker.invokeHISService(inpar,serviceName);
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "中心登记成功但云HIS平台保存失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = "返回给HIS成功";
                    return 0;
                }
            }
            catch (Exception ex )
            {
                outParam = "HIS保存登记信息出现异常：！" + ex.Message;
                return -1;
            }

        }

        /// <summary>
        /// 获取His费用
        /// </summary>
        /// <param name="pat"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int getHisFee(Patients pat, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                dynamic joIrisParam = new JObject();
                JArray jaIrisParams = new JArray();
                JObject joIrisTmp = new JObject();
                joIrisParam.code = "05110003";
                joIrisParam.session = Global.curEvt.jaSession;
                joIrisTmp.Add("admID", pat.adm_Dr);
                joIrisTmp.Add("billID", pat.billID);
                joIrisTmp.Add("recordID", pat.recordID);
                jaIrisParams.Add(joIrisTmp);
                joIrisParam.Add("params", JArray.FromObject(jaIrisParams));


                JObject joRtn = invoker.invokeHISService(joIrisParam.ToString(), "获取HIS费用");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "deleteFee 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 取消登记信息
        /// </summary>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int cancleRegister( out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp.Add("admID", Global.pat.adm_Dr);
                joTmp.Add("interfaceDr", Global.inf.interfaceDr);
                joTmp.Add("billID", Global.pat.billID);
                string serviceCode = "05110004";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
                JObject joRtn = invoker.invokeHISService(inpar, "取消HIS登记信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = joRtn.ToString();
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "取消登记异常" + ":" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// HIS预结算
        /// </summary>
        /// <param name="joParam"></param>
        /// <param name="joSetlinfo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int preSettlementInfo(JObject joParam, JObject joSetlinfo, out string outParam)
        {
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp = joParam.DeepClone();
                joTmp.insuPayInfo = joSetlinfo;
                joTmp.updateUserID = Global.user.ID;
                joTmp.middleSettleFlag = "";

                JObject joSumFee = new JObject();
                if (sumInsuRtnSettlInfo(joSetlinfo, out joSumFee, out errMsg) != 0)
                {
                    outParam = "汇总结算结果失败，请联系管理员！" + errMsg;
                    return -1;
                }

                joTmp.sumFeeObj = joSumFee;
                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110020", joTmp).ToString(), "返回2303结算结果给HIS");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "返回2303结算结果给HIS失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "返回2303结算结果给HIS出现异常：！" + ex.Message;
                return -1;
            }

        }
        //public int preSettlementInfo(JObject joParam, JObject joSetlinfo, out string outParam)
        //{
        //    string errMsg;
        //    try
        //    {
        //        dynamic joTmp = new JObject();
        //        joTmp = joParam.DeepClone();
        //        joTmp.insuPayInfo = joSetlinfo;
        //        joTmp.updateUserID = Global.user.ID;
        //        joTmp.middleSettleFlag = "";
        //        JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110011", joTmp).ToString(), "返回2304结算结果给HIS");
        //        if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
        //        {
        //            outParam = "返回2303结算结果给HIS失败，请联系管理员！" + errMsg;
        //            return -1;
        //        }
        //        else
        //        {
        //            outParam = joRtn.ToString();
        //            return 0;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        outParam = "返回2303结算结果给HIS出现异常：！" + ex.Message;
        //        return -1;
        //    }

        //}

        /// <summary>
        /// 返回住院结算信息给HIS
        /// </summary>
        /// <param name="joParam"></param>
        /// <param name="joSetlinfo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int returnInpatSettlementInfo(JObject joParam, JObject joSetlinfo, out string outParam)
        {
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp = joParam.DeepClone();
                joTmp.insuPayInfo = joSetlinfo;
                joTmp.updateUserID = Global.user.ID;
                joTmp.middleSettleFlag = "";

                JObject joSumFee = new JObject();
                if (sumInsuRtnSettlInfo(joSetlinfo, out joSumFee, out errMsg) != 0)
                {
                    outParam = "汇总结算结果失败，请联系管理员！" + errMsg;
                    return -1;
                }

                joTmp.sumFeeObj = joSumFee;
                //JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110007", joTmp).ToString(), "返回2304结算结果给HIS");
                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110017", joTmp).ToString(), "返回2304结算结果给HIS");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "返回2304结算结果给HIS失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "返回2304结算结果给HIS出现异常：！" + ex.Message;
                return -1;
            }

        }

        /// <summary>
        /// 预结算后返回门诊结算信息给HIS，HIS进行预结算
        /// </summary>
        /// <param name="joSetlInpar"></param>
        /// <param name="joSetlinfo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int preSettlement(JObject joSetlInpar, JObject joSetlinfo, out string outParam)
        {
            string errMsg;
            try
            {
                JObject joSumFee = new JObject();
                if (sumInsuRtnSettlInfo(joSetlinfo, out joSumFee, out errMsg) != 0)
                {
                    outParam = "汇总结算结果失败，请联系管理员！" + errMsg;
                    return -1;
                }

                dynamic joTmp = new JObject();
                joTmp.settleInfo = joSetlinfo;
                joTmp.updateUserID = Global.user.ID;
                joTmp.regInfo = joSetlInpar;
                joTmp.middleSettleFlag = "";
                joTmp.interfaceDr = Global.inf.interfaceDr;
                dynamic joHisInfo = new JObject();
                joHisInfo.admID = Global.pat.adm_Dr;
                joHisInfo.billID = Global.pat.billID;
                joHisInfo.recordID = Global.pat.recordID;
                joTmp.hisInfo = joHisInfo;
                joTmp.psn_type = Global.pat.psn_type;

                joTmp.sumFeeObj = joSumFee;

                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110019", joTmp).ToString(), "返回2206结算结果给HIS进行预结算");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "返回2206结算结果给HIS进行预结算失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "返回2206结算结果给HIS进行预结算异常！" + ex.Message;
                return -1;
            }

        }


        /// <summary>
        /// 返回门诊结算信息给HIS
        /// </summary>
        /// <param name="joSetlInpar"></param>
        /// <param name="joSetlinfo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int returnOutpatSettlementInfo(JObject joSetlInpar, JObject joSetlinfo, out string outParam)
        {
            string errMsg;
            try
            {
                JObject joSumFee = new JObject();
                if (sumInsuRtnSettlInfo(joSetlinfo, out joSumFee, out errMsg) != 0)
                {
                    outParam = "返回2207结算结果给HIS失败，请联系管理员！";
                    return -1;
                }

                dynamic joTmp = new JObject();
                joTmp.settleInfo = joSetlinfo;
                joTmp.updateUserID = Global.user.ID;
                joTmp.regInfo = joSetlInpar;
                joTmp.middleSettleFlag = "";
                joTmp.interfaceDr = Global.inf.interfaceDr;
                dynamic joHisInfo = new JObject();
                joHisInfo.admID = Global.pat.adm_Dr;
                joHisInfo.billID = Global.pat.billID;
                joHisInfo.recordID = Global.pat.recordID;
                joTmp.hisInfo = joHisInfo;
                joTmp.psn_type = Global.pat.psn_type;

                joTmp.sumFeeObj = joSumFee;

                //0511018 {"feeObj":{"ybAmt":"医保支付","psnAcctAmt":"个人账户","psnCashAmt":"个人现金","hospAmt":"医院垫付"}}

                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110018", joTmp).ToString(), "返回2207结算结果给HIS");
                //JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110009", joTmp).ToString(), "返回2207结算结果给HIS");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "返回2207结算结果给HIS失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "返回22207结算结果给HIS出现异常：！" + ex.Message;
                return -1;
            }

        }


        /// <summary>
        /// 获取结算费用的封装
        /// </summary>
        /// <param name="jo"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        private decimal getFee(JObject jo, string path)
        {
            try
            {
                string temp = JsonHelper.getDestValue(jo, path);
                if (temp == "")
                {
                    return 0;
                }
                else
                {
                    return decimal.Parse(temp);
                }
            }
            catch (Exception ex)
            {
                Global.writeLog("getFee异常：" + ex.Message);
                return 0;
            }
        }


        /// <summary>
        /// 汇总医保返回的结算金额（按照HIS的原则汇总，后期HIS按照这个来进行勾稽关系判断）
        /// </summary>
        /// <param name="jo"></param>
        /// <returns></returns>
        public int sumInsuRtnSettlInfo(JObject jo, out JObject joSumFee, out string errMsg)
        {
            // 医疗费总额是患者在医药机构花费的所有诊疗、药品、耗材、服务设施等项目费用的总和 = 基金支付总额 + 个人负担总金额 + 其他（如医院负担金额）；
            //3、基金支付总额 = 基本医保统筹基金支出（含职工基本医疗保险、居民基本医疗保险）+补充医疗保险基金支出 （含覆盖全体参保人的居民大病保险和大额医疗费用补助、覆盖部分参保人的企业职工大额医疗费用补助和公务员医疗补助等）+医疗救助基金支出 + 其他支出（如伤残人员医疗保障基金支出）；
            //5、个人账户支出中包含账户共济支付金额

            joSumFee = new JObject();

            errMsg = "";
            decimal ybAmt, psnAcctAmt, hospAmt, psnCashAmt, medFee;
            try
            {
                ybAmt = getFee(jo, "hifp_pay") //基本医保统筹基金支出
                      + getFee(jo, "hifes_pay") //企业补充医疗保险基金支出
                      + getFee(jo, "hifmi_pay") //居民大病保险
                                                //+ getFee(jo, "hifob_pay") //大额医疗费用补助
                      + getFee(jo, "hifob_pay") //职工大额医疗费用补助
                      + getFee(jo, "cvlserv_pay") //公务员医疗补助
                      + getFee(jo, "maf_pay") //医疗救助基金支出
                      + getFee(jo, "oth_pay"); //其他支出
                psnAcctAmt = getFee(jo, "acct_pay");     //账户支付金额
                psnCashAmt = getFee(jo, "psn_cash_pay"); //现金支付金额
                hospAmt = getFee(jo, "hosp_part_amt");   //医院承担金额
                medFee = getFee(jo, "medfee_sumamt");    //总费用
                joSumFee.Add("ybAmt", ybAmt);
                joSumFee.Add("psnAcctAmt", psnAcctAmt);
                joSumFee.Add("hospAmt", hospAmt);
                joSumFee.Add("psnCashAmt", psnCashAmt);

                if (medFee != (ybAmt + psnAcctAmt + psnCashAmt + hospAmt))
                {
                    errMsg = "ybAmt + psnAcctAmt + psnCashAmt + hospAmt != medFee";
                    return -1;
                }
                return 0;
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return 1;
            }

        }


        /// <summary>
        /// 取消结算信息
        /// </summary>
        /// <param name="joParam"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int cancleSettlementInfo(JObject joParam,  out string outParam)
        {
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp = joParam.DeepClone();
                joTmp.updateUserID = Global.user.ID;
                joTmp.middleSettleFlag = "";
                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110008", joTmp).ToString(), "取消HIS医保结算信息");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "取消HIS医保结算信息失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = "取消HIS医保结算信息成功";
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "返回2304结算结果给HIS出现异常：！" + ex.Message;
                return -1;
            }

        }

        public int getHISDir(JArray jaPagination, JArray jaParams, out JObject joRtn, out string outParam)
        {
            string errMsg;
            joRtn = new JObject();
            try
            {
                dynamic joTmp = new JObject();
                joTmp.pagination = jaPagination;
                joTmp.session = Global.curEvt.jaSession;
                joTmp.Add("params",jaParams);
                joTmp.code = "05111002";
                joRtn = invoker.invokeHISService(joTmp.ToString(), "查询HIS目录分页");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "查询HIS目录分页失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    if (joRtn["result"]["Data"] == null)
                    {
                        outParam = "Data节点不存在";
                        return -1;
                    }
                    outParam = "查询HIS目录分页成功";
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "查询HIS目录分页异常：！" + ex.Message;
                return -1;
            }

        }

        /// <summary>
        /// 从HIS获取医保事前事中服务交易入参报文
        /// </summary>
        /// <param name="pat"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int GetInsuPatInfo(string TrigScen, Patients pat, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                dynamic joIrisParam = new JObject();
                JArray jaIrisParams = new JArray();
                JObject joIrisTmp = new JObject();
                joIrisParam.code = "05110015";
                joIrisParam.session = Global.curEvt.jaSession;
                joIrisTmp.Add("admID", pat.adm_Dr);
                joIrisTmp.Add("billID", pat.billID);
                joIrisTmp.Add("recordID", pat.recordID);
                joIrisTmp.Add("userID",Global.user.ID);
                joIrisTmp.Add("hospID", Global.inf.hisHospitalDr);
                joIrisTmp.Add("psnNo", Global.pat.psn_no);
                joIrisTmp.Add("mdtrtId", Global.pat.mdtrtID);
                joIrisTmp.Add("InsuredType", Global.pat.insuType);
                joIrisTmp.Add("MedicalType", Global.pat.medType);
                joIrisTmp.Add("YBArea", Global.pat.insuplc_admdvs);
                joIrisTmp.Add("TrigScen", TrigScen);

                joIrisTmp.Add("InterfaceID", Global.inf.interfaceDr);

                jaIrisParams.Add(joIrisTmp);
                joIrisParam.Add("params", JArray.FromObject(jaIrisParams));

                JObject joRtn = invoker.invokeHISService(joIrisParam.ToString(), "获取HIS费用");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "GetInsuPatInfo医保事前事中服务报文获取 异常：" + ex.Message;
                return -1;
            }
        }


        /// <summary>
        /// 事前分析
        /// </summary>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int PreAnalysis(out string errMsg)
        {
            errMsg = "";
            try
            {
                JObject joTmp = new JObject();
                joTmp.Add("billID", Global.pat.billID);
                joTmp.Add("recordID", Global.pat.recordID);

                JObject joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110022", joTmp).ToString(), "获取事前分析内容");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    errMsg = "获取事前分析内容失败，请联系管理员！" + errMsg;
                    return -1;
                }
                else
                {
                    errMsg = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return -1;
            }
        }
    }


    class MIIrisServices
    {
        private InvokeHelper invoker = new InvokeHelper();

        /// <summary>
        /// 获取接口信息
        /// </summary>
        /// <returns></returns>
        public JObject getInterface()
        {
            dynamic joParam = new JObject();
            joParam.HospitalDr = Global.inf.hospitalDr;
            joParam.InterfaceID = "";
            JObject joInparam = JsonHelper.setIrisInpar("09010003", joParam);
            InvokeHelper invoker = new InvokeHelper();
            return invoker.invokeInsuService(joInparam.ToString(), "获取接口信息");
        }

        public JObject getInterface(string interfaceID)
        {
            dynamic joParam = new JObject();
            joParam.HospitalDr = Global.inf.hospitalDr;
            joParam.InterfaceID = interfaceID;
            JObject joInparam = JsonHelper.setIrisInpar("09010003", joParam);
            InvokeHelper invoke = new InvokeHelper();
            JObject joRtn = invoke.invokeInsuService(joInparam.ToString(), "获取接口信息");
            JObject joInterfaceInfo = JObject.Parse(JsonHelper.getDestValue(joRtn, "result.data[0]"));
            joInterfaceInfo.Add("operatorNO", "0");
            joInterfaceInfo.Add("operatorName", "管理员");


            return joInterfaceInfo;
        }

        public JObject getInterface_TY(string interfaceID,string hospitalDr)
        {
            dynamic joParam = new JObject();
            joParam.HospitalDr = hospitalDr;
            joParam.InterfaceID = interfaceID;
            JObject joInparam = JsonHelper.setIrisInpar("09010003", joParam);
            InvokeHelper invoke = new InvokeHelper();
            JObject joRtn = invoke.invokeInsuService(joInparam.ToString(), "获取接口信息");
            JObject joInterfaceInfo = JObject.Parse(JsonHelper.getDestValue(joRtn, "result.data[0]"));
            joInterfaceInfo.Add("operatorNO", "0");
            joInterfaceInfo.Add("operatorName", "管理员");


            return joInterfaceInfo;
        }

        /// <summary>
        /// 判断是否签到过，如果未签到，返回false,如果签到则返回ture，同时返回签到号
        /// </summary>
        /// <param name="signno"></param>
        /// <returns></returns>
        public bool isSigned(ref string signno)
        {
            JObject joIn = new JObject();
            joIn.Add("operatorNO", Global.user.ID.ToUpper());
            joIn.Add("operatorName", Global.user.name);
            joIn.Add("mac", Global.curEvt.mac);
            joIn.Add("ip", Global.curEvt.ip);
            joIn.Add("hospitalDr", Global.inf.hospitalDr);
            joIn.Add("interfaceDr", Global.inf.interfaceDr);
            joIn.Add("date", DateTime.Now.ToString("yyyy-MM-dd"));

            string data = JsonHelper.setIrisInpar("09010064", joIn).ToString();
            JObject joRtn = invoker.invokeInsuService(data, "查询签到信息");
            string errMsg = "";

            //MessageBox.Show("查询签到信息："+joRtn.ToString());

            if (JsonHelper.parseIrisRtnValue(joRtn,out errMsg)!= 0)
            {
                signno = "";
                return false;
            }
            else
            {                
                signno = JsonHelper.getDestValue(joRtn, "result.SignNo");                
                return true;
            }
        }

        /// <summary>
        ///保存签到信息
        /// </summary>
        /// <param name="signno"></param>
        /// <returns></returns>
        public int saveSignRecord( string signno,out string errMsg)
        {
            if (signno == "")
            {
                errMsg = "signno不能为空！";
                return -1;
            }
            errMsg = ""; ;
            JObject joIn = new JObject();
            joIn.Add("operatorNO", Global.user.ID);
            joIn.Add("operatorName", Global.user.name);
            joIn.Add("mac", Global.curEvt.mac);
            joIn.Add("ip", Global.curEvt.ip);
            joIn.Add("hospitalDr", Global.inf.hospitalDr);
            joIn.Add("interfaceDr", Global.inf.interfaceDr);
            joIn.Add("date", DateTime.Now.ToString("yyyy-MM-dd"));
            joIn.Add("signNo", signno);
            joIn.Add("createTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            joIn.Add("division", Global.inf.areaCode);

            string data = JsonHelper.setIrisInpar("09010065", joIn).ToString();

            //MessageBox.Show("插入签到信息入参："+ data);

            JObject joRtn = invoker.invokeInsuService(data, "插入签到信息");

            if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 保存住院登记信息
        /// </summary>
        /// <param name="joRegIn"></param>
        /// <param name="joRegOut"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int saveInpatRegisterInfo(JObject joRegInOld,JObject joRegOut,out string outParam)
        {
            //插入云医保平台
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                JObject joRegIn = Utils.removeWrapper(joRegInOld);

                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("AdmDr", Global.pat.adm_Dr);
                joTmp.Add("PatientName", Global.pat.name);
                joTmp.Add("PersonalNO", Global.pat.psn_no);
                joTmp.Add("InsuRegID", JsonHelper.getDestValue(joRegOut, "output.result.mdtrt_id"));
                joTmp.Add("RegDate", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.begntime"));
                joTmp.Add("InsuType", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.insutype"));
                joTmp.Add("CertificateType", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.mdtrt_cert_type"));
                joTmp.Add("CertificateNO", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.mdtrt_cert_no"));
                joTmp.Add("MedicalType", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.med_type"));
                joTmp.Add("AttendDoctorNO", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.atddr_no"));
                joTmp.Add("ChiefPhyDocName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.chfpdr_name"));
                joTmp.Add("AdmInDiagDesc", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_diag_dscr"));
                joTmp.Add("AdmInDepCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_dept_codg"));
                joTmp.Add("AdmInDepName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_dept_name"));
                joTmp.Add("AdmBed", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_bed"));
                joTmp.Add("MainDiagCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.dscg_maindiag_code"));
                joTmp.Add("MainDiagName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.dscg_maindiag_name"));
                joTmp.Add("MainConditionDesc", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.main_cond_dscr"));
                joTmp.Add("DiseasecCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.dise_codg"));
                joTmp.Add("DiseasecName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.dise_name"));
                joTmp.Add("OperationCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.oprn_oprt_code"));
                joTmp.Add("OperationName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.oprn_oprt_name"));
                joTmp.Add("DiseasecTypeCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.dise_type_code"));
                joTmp.Add("InsuranceAreaCode", Global.pat.insuplc_admdvs);
                joTmp.Add("TreatmentAreaCode", Global.pat.mdtrtarea_admvs);
                joTmp.Add("RegState", JsonHelper.getDestValue(joRegOut, "regstate"));
                joTmp.Add("ValidFlag", JsonHelper.getDestValue(joRegOut, "validflag"));
                joTmp.Add("Type", JsonHelper.getDestValue(joRegOut, "type"));
                joTmp.Add("updateUserID", Global.user.ID);
                joTmp.Add("MSGID", Global.curEvt.msgid);
                joTmp.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                joTmp.Add("ConerName", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.coner_name"));
                joTmp.Add("Tel", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.tel"));
                joTmp.Add("ExpContent", Global.pat.ExpContent);

                string serviceCode = "09010044";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
                JObject joRtn = invoker.invokeInsuService(inpar, "新增住院登记信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = joRtn.ToString();
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "保存住院登记信息异常：" + ex.Message;
                return -1;
            }
            
        }

        /// <summary>
        /// 保存门诊登记信息
        /// </summary>
        /// <param name="jo2201Inpar"></param>
        /// <param name="jo2201Rtn"></param>
        /// <param name="jo2203Inpar"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int saveOutpatRegisterInfo(JObject jo2201InparOld, JObject jo2201Rtn,JObject jo2203InparOld, out string outParam)
        {
            //插入云医保平台
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                // 转换
                JObject jo2201Inpar = Utils.removeWrapper(jo2201InparOld);
                JObject jo2203Inpar = Utils.removeWrapper(jo2203InparOld);

                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("AdmDr", Global.pat.adm_Dr);
                joTmp.Add("PatientName", Global.pat.name);
                joTmp.Add("PersonalNO", Global.pat.psn_no);
                joTmp.Add("InsuRegID", JsonHelper.getDestValue(jo2201Rtn, "output.data.mdtrt_id"));
                joTmp.Add("RegDate", JsonHelper.getDestValue(jo2201Inpar, "input.data.begntime"));
                joTmp.Add("InsuType", JsonHelper.getDestValue(jo2201Inpar, "input.data.insutype"));
                joTmp.Add("CertificateType", JsonHelper.getDestValue(jo2201Inpar, "input.data.mdtrt_cert_type"));
                joTmp.Add("CertificateNO", JsonHelper.getDestValue(jo2201Inpar, "input.data.mdtrt_cert_no"));
                joTmp.Add("MedicalType", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.med_type"));
                joTmp.Add("AttendDoctorNO", JsonHelper.getDestValue(jo2201Inpar, "input.data.atddr_no"));
                joTmp.Add("ChiefPhyDocName", JsonHelper.getDestValue(jo2201Inpar, "input.data.dr_name"));
                joTmp.Add("AdmInDiagDesc", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.main_cond_dscr"));
                joTmp.Add("AdmInDepCode", JsonHelper.getDestValue(jo2201Inpar, "input.data.dept_code"));
                joTmp.Add("AdmInDepName", JsonHelper.getDestValue(jo2201Inpar, "input.data.dept_name"));
                joTmp.Add("AdmBed", "");
                joTmp.Add("MainDiagCode", " ");
                joTmp.Add("MainDiagName", " ");
                joTmp.Add("MainConditionDesc", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.main_cond_dscr"));
                joTmp.Add("DiseasecCode", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.dise_codg"));
                joTmp.Add("DiseasecName", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.dise_name"));
                joTmp.Add("OperationCode", "");
                joTmp.Add("OperationName", "");
                joTmp.Add("DiseasecTypeCode", "");
                joTmp.Add("InsuranceAreaCode", Global.pat.insuplc_admdvs);
                joTmp.Add("TreatmentAreaCode", Global.pat.mdtrtarea_admvs);
                joTmp.Add("RegState", JsonHelper.getDestValue(jo2201Rtn, "regstate"));
                joTmp.Add("ValidFlag", JsonHelper.getDestValue(jo2201Rtn, "validflag"));
                joTmp.Add("Type", JsonHelper.getDestValue(jo2201Rtn, "type"));
                joTmp.Add("updateUserID", Global.user.ID);
                joTmp.Add("msgid", Global.curEvt.msgid);
                joTmp.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                joTmp.Add("ExpContent", Global.pat.ExpContent);

                string serviceCode = "09010044";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
                JObject joRtn = invoker.invokeInsuService(inpar, "新增门诊登记信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = joRtn.ToString();
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "保存门诊登记信息异常：" + ex.Message;
                return -1;
            }

        }
        /// <summary>
        /// 取消登记
        /// </summary>
        /// <param name="type"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int cancleRegister(int type,out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                dynamic joTmp = new JObject();
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("AdmDr", Global.pat.adm_Dr);
                joTmp.Add("PersonalNO",Global.pat.psn_no);
                joTmp.Add("InsuRegID", Global.pat.mdtrtID);
                joTmp.Add("CancleRegDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                joTmp.Add("Type", type);
                joTmp.Add("ValidFlag", -1);
                joTmp.Add("updateUserID", Global.user.ID);
                string serviceCode = "09010044";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
                JObject joRtn = invoker.invokeInsuService(inpar, "取消云医保平台登记信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = "退结算失败，请根据输入条件检查是否存在该笔数据！" + errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "取消登记异常" +  ":" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 删除费用明细
        /// </summary>
        /// <param name="pat"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        public int  deleteFee(out string errMsg)
        {
            try
            {
                dynamic joTmp = new JObject();
                joTmp = new JObject();
                joTmp.code = "09010048";
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.admID = Global.pat.adm_Dr;
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.updateUserID = Global.user.ID;
                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "删除2301明细");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                errMsg = "deleteFee 异常：" + ex.Message;
                return -1;
            }          
        }

        /// <summary>
        ///  根据HIS的费用明细获取对照关系
        /// </summary>
        /// <returns></returns>
        public int convertHisFeeWithInsuCode( JObject joHisFee, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JArray jaFee = JArray.FromObject(joHisFee["result"]);
                //var filtered = jaFeef.Where(jObject => jObject["det_item_fee_sumamt"].ToString()!="0.00");//过滤总费用为0的费用明细

                // 将过滤后的JArray转换回JSON字符串
               // JArray jaFee = JArray.Parse(filtered.ToString());


                JObject joTmp = JsonHelper.setIrisInpar("09010045", jaFee);
                joTmp.Add("mdtrt_id", Global.pat.mdtrtID);
                joTmp.Add("med_type", Global.pat.medType);
                joTmp.Add("psn_no", Global.pat.psn_no);
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);

                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "根据HIS的费用明细获取对照关系");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "convertHisFeeWithInsuCode 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 插入医保费用
        /// </summary>
        /// <param name="jaTmp"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int insertFee(JArray jaTmp,out string outParam)
        {
            try
            { 
                outParam = "";
                string errMsg;
                dynamic joTmp = new JObject();
                joTmp = new JObject();
                joTmp.code = "09010046";
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.admID = Global.pat.adm_Dr;
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.updateUserID = Global.user.ID;
                joTmp.Add("params", jaTmp);
                ;
                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "插入2301明细");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
                catch (Exception ex)
                {
                    outParam = "insertFee 异常：" + ex.Message;
                    return -1;
                }
        }

        /// <summary>
        /// 更新医保费用
        /// </summary>
        /// <param name="jaTmp"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int updateFee(JArray jaTmp, out string outParam)
        {
            try
            {
                outParam = "";
                string errMsg;
                dynamic joTmp = new JObject();
                joTmp.code = "09010047";
                JArray jaParams = jaTmp;
                joTmp.Add("params", jaParams);
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.admID = Global.pat.adm_Dr;
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.updateUserID =Global.user.ID;
                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "更新2301明细");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "updateFee 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 获取各项费用汇总
        /// </summary>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int getSumFee(out string outParam)
        {
            try
            {
                outParam = "";
                string errMsg;
                //调用IRIS获取医保各项金额
                dynamic joTmp = new JObject();
                joTmp = new JObject();
                joTmp.code = "09010049";
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.admID = Global.pat.adm_Dr;
                joTmp.mdtrt_id = Global.pat.mdtrtID;
                joTmp.updateUserID = Global.user.ID; ;
                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "获取各项费用汇总"); ;
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "getSumFee 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 转换结算信息
        /// </summary>
        /// <param name="pat"></param>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int convertSettlementWithInsuCode(JObject joSettlement, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JObject joOutput = JObject.FromObject(joSettlement["output"]);
                //编码转换
                JObject joInpar = new JObject();
                joInpar.Add("hospitalDr", Global.inf.hospitalDr);
                joInpar.Add("interfaceDr", Global.inf.interfaceDr);
                joInpar.Add("output", joOutput);


                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010055", joInpar).ToString(), "结算信息编码转换");
                if (JsonHelper.getDestValue(joRtn, "errorCode") != "")
                {
                    outParam = JsonHelper.getDestValue(joRtn, "errorMessage");
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "convertSettlementWithInsuCode 异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        ///  根据HIS的费用明细获取对照关系
        /// </summary>
        /// <returns></returns>
        public int convertHisFeeWithInsuCodeOfMobilePay(Patients pat, JObject joHisFee, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {
                JArray jaFee = JArray.FromObject(joHisFee["result"]);
                JObject joTmp = JsonHelper.setIrisInpar("09010086", jaFee);
                joTmp.Add("mdtrt_id", pat.mdtrtID);
                joTmp.Add("med_type", pat.medType);
                joTmp.Add("psn_no", pat.psn_no);
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);

                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "根据HIS的费用明细获取对照关系");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = " convertHisFeeWithInsuCodeOfMobilePay异常：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 插入结算信息
        /// </summary>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int saveSettlement(JObject joSettlement, out string outParam)
        {
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                JObject joSetlinfo = JObject.Parse(JsonHelper.getDestValue(joSettlement, "output.setlinfo"));
                // 异常值转换
                Utils.convertTimestamp(joSetlinfo, "brdy");
                Utils.convertTimestamp(joSetlinfo, "setl_time");

                joSetlinfo.Add("HospitalDr", Global.inf.hospitalDr);
                joSetlinfo.Add("admID", Global.pat.adm_Dr);
                joSetlinfo.Add("ValidFlag", 1);
                joSetlinfo.Add("BillType", 1);
                joSetlinfo.Add("updateUserID",Global.user.ID);
                joSetlinfo.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                joSetlinfo.Add("billID", Global.pat.billID);
                joSetlinfo.Add("recordID", Global.pat.recordID);
                joSetlinfo.Add("msgid", Global.curEvt.msgid);
                joSetlinfo.Add("interfaceDr", Global.inf.interfaceDr);
                joSetlinfo.Add("admType", Global.pat.admType);
                joSetlinfo.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);
                joSetlinfo.Add("ExpContent", Global.pat.ExpContent);

                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010051", joSetlinfo).ToString(), "插入结算信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "插入结算信息：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 保存结算信息
        /// </summary>
        /// <param name="joSettlement"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int saveSettlementDetail(JObject joSettlement, out string outParam)
        {
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                JObject joSetlDetail = new JObject();
                joSetlDetail.Add("HospitalDr", Global.inf.hospitalDr);
                joSetlDetail.Add("InterfaceDr", Global.inf.interfaceDr);
                joSetlDetail.Add("MdtrtID", Global.pat.mdtrtID);
                joSetlDetail.Add("SettlementID", JsonHelper.getDestValue(joSettlement, "output.setlinfo.setl_id"));
                joSetlDetail.Add("AdmID", Global.pat.adm_Dr);
                joSetlDetail.Add("updateUserID", Global.user.ID);
                if (JsonHelper.getDestValue(joSettlement, "output.setldetail") == "")
                {
                    outParam = "中心返回结算明细为空！";
                    Global.writeLog(outParam);
                    return 0;
                }
                JArray jaSetlDetail = JArray.Parse(JsonHelper.getDestValue(joSettlement, "output.setldetail"));
                joSetlDetail.Add("setldetail", jaSetlDetail);
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010056", joSetlDetail).ToString(), "插入结算明细信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "插入结算信息：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 取消结算
        /// </summary>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int cancleSettlement(string newSettlID, out string outParam)
        {
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("admID", Global.pat.adm_Dr);
                joTmp.Add("mdtrt_id", Global.pat.mdtrtID);
                joTmp.Add("setl_id", Global.pat.settlID);
                joTmp.Add("new_setl_id", newSettlID);
                joTmp.Add("updateUserID", Global.user.ID);
                joTmp.Add("msgid", Global.curEvt.msgid);
                joTmp.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010052", joTmp).ToString(), "取消结算信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "取消结算信息：" + ex.Message;
                return -1;
            }
        }

        public JObject DynamicQuerySettlInfo(JObject joSqlStr)
        {
            try
            {
                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010081", joSqlStr).ToString(), "动态查询结算信息");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "查询结算信息异常：", ex.Message);
            }

        }

        /// <summary>
        /// 查询登记信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int queryRegisterInfo(int type,out string outParam)
        {
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {                
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("admID", Global.pat.adm_Dr);
                joTmp.Add("mdtrt_id", Global.pat.mdtrtID);
                joTmp.Add("type", type);

                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010050", joTmp).ToString(), "查询云平台患者登记信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "查询云平台患者登记信息：" + ex.Message;
                return -1;
            }
        }

        /// <summary>
        /// 插入患者该次就诊参保信息
        /// </summary>
        /// <param name="joBaseInfo"></param>
        /// <param name="joInsuInfo"></param>
        /// <param name="joIdetInfo"></param>
        /// <param name="outParam"></param>
        /// <returns></returns>
        public int insertPatCurInsuInfo(JObject joBaseInfo, JObject joInsuInfo, JObject joIdetInfo, out string outParam)
        {
            JObject joTmp = new JObject();
            string errMsg = "";
            try
            {
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                joTmp.Add("AdmID", Global.pat.adm_Dr);
                joTmp.Add("PatientName", Global.pat.name);
                joTmp.Add("PersonalNO", Global.pat.psn_no);
                joTmp.Add("MdtrtID", Global.pat.mdtrtID);
                joTmp.Add("PsnCertType", JsonHelper.getDestValue(joBaseInfo, "psn_cert_type"));
                joTmp.Add("PsnCertNO", JsonHelper.getDestValue(joBaseInfo, "certno"));
                joTmp.Add("Gend", JsonHelper.getDestValue(joBaseInfo, "gend"));
                joTmp.Add("Naty", JsonHelper.getDestValue(joBaseInfo, "naty"));
                joTmp.Add("Brdy", JsonHelper.getDestValue(joBaseInfo, "brdy"));
                joTmp.Add("Age", JsonHelper.getDestValue(joBaseInfo, "age"));
                joTmp.Add("Balc", JsonHelper.getDestValue(joInsuInfo, "balc"));
                joTmp.Add("Insutype", JsonHelper.getDestValue(joInsuInfo, "insutype"));
                joTmp.Add("PsnType", JsonHelper.getDestValue(joInsuInfo, "psn_type"));
                joTmp.Add("PsnInsuStas", JsonHelper.getDestValue(joInsuInfo, "psn_insu_stas"));
                joTmp.Add("PsnInsuDate", JsonHelper.getDestValue(joInsuInfo, "psn_insu_date"));
                joTmp.Add("PausInsuDate", JsonHelper.getDestValue(joInsuInfo, "paus_insu_date"));
                joTmp.Add("Cvlservflag", JsonHelper.getDestValue(joInsuInfo, "cvlserv_flag"));
                joTmp.Add("insuplcAdmdvs", JsonHelper.getDestValue(joInsuInfo, "insuplc_admdvs"));
                joTmp.Add("EmpName", JsonHelper.getDestValue(joInsuInfo, "emp_name"));
                joTmp.Add("PsnIdettype", JsonHelper.getDestValue(joIdetInfo, "psn_idet_type"));
                joTmp.Add("PsnTypeLv", JsonHelper.getDestValue(joIdetInfo, "psn_type_lv"));
                joTmp.Add("IdetBegntime", JsonHelper.getDestValue(joIdetInfo, "begntime"));
                joTmp.Add("IdetEndtime", JsonHelper.getDestValue(joIdetInfo, "endtime"));
                joTmp.Add("updateUserID", Global.user.ID);

                string serviceCode = "09010070";
                string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
                JObject joRtn = invoker.invokeInsuService(inpar, "插入患者该次就诊参保信息");

                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "医保平台插入患者该次就诊参保信息：" + ex.Message;
                return -1;
            }

        }

        #region 目录下载及对照
        /// <summary>
        /// 通过分页的方式查询医保目录
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="irisCode"></param>
        /// <param name="directoryType"></param>
        public JObject getMIDirectoryByPagination(JObject jo)
        {
            string currentPage;
            string pageSize;
            string serviceCode;
            string directoryType;
            string directoryCode;
            string directoryName;
            string directoryDr;
            string approvalNO ;

            string dosageForm;
            string specifications;
            string manufacturers;

            try
            {
                currentPage = JsonHelper.getDestValue(jo, "currentPage");
                pageSize = JsonHelper.getDestValue(jo, "pageSize");
                serviceCode = JsonHelper.getDestValue(jo, "serviceCode");
                directoryType = JsonHelper.getDestValue(jo, "directoryType");
                directoryCode = JsonHelper.getDestValue(jo, "directoryCode");
                directoryName = JsonHelper.getDestValue(jo, "directoryName");
                directoryDr = JsonHelper.getDestValue(jo, "directoryDr");
                approvalNO = JsonHelper.getDestValue(jo, "approvalNO");

                dosageForm = JsonHelper.getDestValue(jo, "dosageForm");
                specifications = JsonHelper.getDestValue(jo, "specifications");
                manufacturers = JsonHelper.getDestValue(jo, "manufacturers");

                InvokeHelper invoker = new InvokeHelper();
                dynamic joInput = new JObject();
                joInput.code = serviceCode;
                dynamic joTmp = new JObject();
                JArray jaParmas = new JArray();
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.InterfaceDr = directoryDr;
                joTmp.Code = directoryCode;
                joTmp.Name = directoryName;
                joTmp.ApprovalNO = approvalNO;

                joTmp.Manufacturer = manufacturers;
                joTmp.DosageForm = dosageForm;
                joTmp.Specifications = specifications;

                jaParmas.Add(joTmp);
                joInput.Add("params", JArray.FromObject(jaParmas));
                JArray jaPagination = new JArray();
                joTmp = new JObject();
                joTmp.pageSize = pageSize;
                joTmp.currentPage = currentPage;
                joTmp.sortColumn = "";
                joTmp.sortOrder = "";
                joTmp.hisType = directoryType;
                jaPagination.Add(joTmp);
                joInput.Add("pagination", JArray.FromObject(jaPagination));

                return invoker.invokeInsuService(joInput.ToString(), "查询医保目录分页");
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "getDirectoryByPagination", ex.Message);
            }

        }

        /// <summary>
        /// 根据HisType获取当前目录最大版本号
        /// </summary>
        /// <param name="directoryType"></param>
        /// <returns></returns>
        public JObject getDirectoryMaxVersionNO(int directoryType)
        {
            try
            {
                dynamic joParam = new JObject();
                joParam.HospitalDr = Global.inf.hospitalDr;
                joParam.InterfaceDr = Global.inf.interfaceDr;
                joParam.HisType = directoryType + 1;
                JObject joInparam = JsonHelper.setIrisInpar("09010037", joParam);
                JObject joRtn = invoker.invokeInsuService(joInparam.ToString(), "获取版本最大号");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "获取版本号异常：", ex.Message);
            }
        }

        public JObject insertDictionary(JObject joData)
        {
            try
            {
                JObject joInparam = JsonHelper.setIrisInpar("09010073", joData);
                JObject joRtn = invoker.invokeInsuService(joInparam.ToString(), "插入字典表");
                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "插入字典表异常：", ex.Message);
            }

        }

        public JObject insertDictionaryDataDetail(JArray jaData)
        {
            try
            {
                dynamic joParam = new JObject();
                joParam.HospitalDr = Global.inf.hospitalDr;
                joParam.InterfaceDr = Global.inf.interfaceDr;
                JObject joInparam = JsonHelper.setIrisInpar("09010074", jaData);
                JObject joRtn = invoker.invokeInsuService( joInparam.ToString(),"插入字典明细表");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "插入字典明细表异常：", ex.Message);
            }

        }

        public JObject ConvertHISDir(JObject joHisRtnDir)
        {
            try
            {
                //传给医保平台，进行对照匹配
                dynamic  joParam = new JObject();
                joParam.TotalCount = joHisRtnDir["result"]["TotalCount"];
                joParam.HospitalDr = Global.inf.hospitalDr;
                joParam.InterfaceDr = Global.inf.interfaceDr;
                joParam.Data = joHisRtnDir["result"]["Data"];
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010057", joParam).ToString(), "转换HIS目录分页，显示对照状态及对照的医保编码");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "转换HIS目录分页异常：", ex.Message);
            }

        }

        public JObject GetMappedHISDir(JArray jaPagination, JArray jaParams)
        {
            try
            {
                //传给医保平台，进行对照匹配
                dynamic joParam = new JObject();
                joParam.pagination = jaPagination;
                joParam.session = Global.curEvt.jaSession;
                joParam.Add("params", jaParams);
                joParam.code = "09010058";
                JObject joRtn = invoker.invokeInsuService(joParam.ToString(), "获取已对照目录分页");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "获取已对照目录分页异常：", ex.Message);
            }

        }

        public JObject GetNoMappHISDir(JObject joHisRtnDir)
        {
            try
            {
                //传给医保平台，进行对照匹配
                dynamic joParam = new JObject();
                joParam.TotalCount = joHisRtnDir["result"]["TotalCount"];
                joParam.HospitalDr = Global.inf.hospitalDr;
                joParam.InterfaceDr = Global.inf.interfaceDr;
                joParam.Data = joHisRtnDir["result"]["Data"];
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010088", joParam).ToString(), "获取未对照目录");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "获取未对照目录异常：", ex.Message);
            }
        }

        public JObject UpdateDirectoryBySelf(JObject joIn)
        {
            try
            {
                //查询区域通用目录 
                MIIrisServices mIS = new MIIrisServices();
                JObject joRtnTY = mIS.getInterface_TY(Global.inf.interfaceDr.ToString(), "");
                string InsuCurrencyCataLogue = JsonHelper.getDestValue(joRtnTY, "InsuCurrencyCataLogue");  //通用目录编码 6
                string InterfaceName_TY = JsonHelper.getDestValue(joRtnTY, "InterfaceName_TY");            //通用目录接口名称  
                string HospName_TY = JsonHelper.getDestValue(joRtnTY, "HospitalName");                     //通用目录所属医院名称
                string HospitalDr_TY = JsonHelper.getDestValue(joRtnTY, "HospitalDr_TY");                  //通用目录所属医院ID

                JObject joParam = (JObject)joIn.DeepClone();
                if ((InsuCurrencyCataLogue != "") && (HospitalDr_TY != ""))
                {                    
                    joParam.Add("updateUserID", Global.user.ID);
                    joParam.Add("HospitalDr", HospitalDr_TY);
                    joParam.Add("InterfaceDr", InsuCurrencyCataLogue);
                }
                else
                {                    
                    joParam.Add("updateUserID", Global.user.ID);
                    joParam.Add("HospitalDr", Global.inf.hospitalDr);
                    joParam.Add("InterfaceDr", Global.inf.interfaceDr);
                }

                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010035", joParam).ToString(), "手动修改医保目录表");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "手动修改医保目录表异常：", ex.Message);
            }

        }

        public JObject QueryTransactionLog(JObject joSqlStr)
        {
            try
            {
                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010060", joSqlStr).ToString(), "查询交易日志（冲正）");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "查询交易日志（冲正）异常：", ex.Message);
            }

        }

        public JObject QuerySettlementInfo(JObject joSqlStr)
        {
            try
            {

                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010059", joSqlStr).ToString(), "查询结算信息");

                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "查询结算信息：", ex.Message);
            }

        }
        #endregion

        /// <summary>
        ///  转换事前事中 处方信息集合
        /// </summary>
        /// <returns></returns>
        public int convertFsiOrderDtos(JArray fsiOrderDtos, out string outParam)
        {
            outParam = "";
            string errMsg;
            try
            {

                JObject joTmp = JsonHelper.setIrisInpar("09010077", fsiOrderDtos);
                joTmp.Add("HospitalDr", Global.inf.hospitalDr);
                joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
                JObject joRtn = invoker.invokeInsuService(joTmp.ToString(), "转换事前事中处方信息集合");
                if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
                {
                    outParam = errMsg;
                    return -1;
                }
                else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "convertHisFeeWithInsuCode 异常：" + ex.Message;
                return -1;
            }
        }
    }
}
