/******************************************************************************
 * 文件名称: 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;

namespace PTMedicalInsurance.Business
{
    
    class IrisServices
    {
    }

    class HisIrisServices
    {
        private InvokeHelper invoker = new InvokeHelper();
        /// <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 joRegIn, JObject joRegOut, out string outParam)
        {
            string errMsg;
            try
            {
                JObject joSendToIris2401 = joRegIn;
                joSendToIris2401.Add("admID", Global.pat.adm_Dr);
                joSendToIris2401.Add("mdtrt_id", JsonHelper.getDestValue(joRegOut, "output.result.mdtrt_id"));
                joSendToIris2401.Add("interfaceDr", Global.inf.interfaceDr);
                joSendToIris2401.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 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 joRtn = invoker.invokeHISService(JsonHelper.setIrisInpar("05110007", 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
        /// </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
            {
                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;

                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="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;
            }

        }

    }

    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(), "获取接口信息");
        }      

        /// <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 = "";
            if ( JsonHelper.parseIrisRtnValue(joRtn,out errMsg)!= 0)
            {
                signno = errMsg;
                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();
            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 joRegIn,JObject joRegOut,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("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("AdmitDiagDesc", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_diag_dscr"));
                joTmp.Add("AdmitDepartmentCode", JsonHelper.getDestValue(joRegIn, "input.mdtrtinfo.adm_dept_codg"));
                joTmp.Add("AdmitDepartmentName", 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("MainConditionDdesc", 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.inf.areaCode);
                joTmp.Add("TreatmentAreaCode", Global.pat.insuplc_admdvs);
                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"));

                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 jo2201Inpar, JObject jo2201Rtn,JObject jo2203Inpar, 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("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("AdmitDiagDesc", JsonHelper.getDestValue(jo2203Inpar, "input.mdtrtinfo.main_cond_dscr"));
                joTmp.Add("AdmitDepartmentCode", JsonHelper.getDestValue(jo2201Inpar, "input.data.dept_code"));
                joTmp.Add("AdmitDepartmentName", JsonHelper.getDestValue(jo2201Inpar, "input.data.dept_name"));
                joTmp.Add("AdmBed", "");
                joTmp.Add("MainDiagCode", " ");
                joTmp.Add("MainDiagName", " ");
                joTmp.Add("MainConditionDdesc", 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.inf.areaCode);
                joTmp.Add("TreatmentAreaCode", Global.pat.insuplc_admdvs);
                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"));

                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("AdmID", 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"]);
                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 = "updateFee 异常：" + 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.parseIrisRtnValue(joRtn, out errMsg) != 0)
                //{
                //    outParam = errMsg;
                //    return -1;
                //}
                //else
                {
                    outParam = joRtn.ToString();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                outParam = "convertSettlementWithInsuCode 异常：" + 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"));
                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", 2);
                joSetlinfo.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);
;
                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(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("updateUserID", Global.user.ID);
                joTmp.Add("msgid", Global.curEvt.msgid);
                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;
            }
        }
        /// <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;
            }

        }
    }
}
