using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PTMedicalInsurance.Business
{
    class IPPreSettlementProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            //JObject joSettlement = new JObject();
            outParam = "";
            string outPar = "";

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

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

            int type =  1; //住院预结算
            if (mIS.queryRegisterInfo(type, out outParam) != 0)
            {
                return Exception(-1, "查询患者云平台登记信息", outParam);
            }

            JObject joReg = JObject.Parse(outParam);
            Global.pat.medType = JsonHelper.getDestValue(joReg, "data.MedicalType");
            Global.pat.insuType = JsonHelper.getDestValue(joReg, "data.InsuType");
            Global.pat.mdtrtcertType = JsonHelper.getDestValue(joReg, "data.CertificateType");
            Global.pat.mdtrtcertNO = JsonHelper.getDestValue(joReg, "data.CertificateNO");
            Global.pat.certType = JsonHelper.getDestValue(joReg, "data.CertificateType");
            Global.pat.certNO = JsonHelper.getDestValue(joReg, "data.CertificateNO");
            Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joReg, "data.InsuranceAreaCode");
            Global.pat.psn_type = Global.pat.insuType;   //后面再改
            Global.pat.ExpContent = JsonHelper.getDestValue(joReg, "data.ExpContent");

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

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

            #region 事中分析
            string errMsg;
            if (Global.curEvt.ext.isOpenAnalysis)
            {
                if (hBus.InProcessAnalysis(out errMsg) != 0)
                {
                    return Exception(-1, "InProcessAnalysis", errMsg);
                }
            }

            #endregion

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

            #endregion

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

            if (JsonHelper.parseCenterRtnValue(jo2402Rtn, out errMsg) != 0)
            {
                return Exception(-1, "预结算失败：出院登记失败", errMsg);
            }
            #endregion

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

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

                if (hIS.preSettlementInfo(joParam, joSetlinfo, out outParam) != 0)
                {
                    return Exception(-1, "出院预结算", outParam);
                }

            //编码转换
            if (mIS.convertSettlementWithInsuCode(jo2303Rtn, out outParam) != 0)
            {
                return Exception(-1, "结算信息转换", outParam);
            }
            //展示结算信息
            JObject joConverted = JObject.Parse(outParam);
            if (hBus.showSettlementForm(joConverted, out outParam) != 0)
            {
                return Exception(-1, "结算信息展示", outParam);
            }
            //joSettlement = (JObject)joRtn.DeepClone();
            outParam = joRtn.ToString();
            #endregion
            return Success();
        }
    }
}
