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 OPPreSettlementProcess : AbstractProcess
    {
        public string OperationType { set; get; }
        /// <summary>
        /// 返回预结算结果
        /// </summary>
        /// <param name="joReg">基本信息含挂号、就诊、诊断、结算等入参</param>
        /// <returns></returns>
        public override CallResult Process(JObject joReg)
        {
            //调用IRIS获取医保各项金额
            if (mIS.getSumFee(out outParam) != 0)
            {
                return Error(-1, "获取医保费用各项汇总金额");
            }
            JObject joSumFee = JObject.Parse(outParam);
            JObject joSettlement = JObject.Parse(JsonHelper.getDestValue(joReg, "settlement"));
            //修改joSettlement的就诊ID ,总金额等
            joSettlement["mdtrt_id"] = Global.pat.mdtrtID;
            joSettlement["medfee_sumamt"] = JsonHelper.getDestValue(joSumFee, "data.Sumamt");
            joSettlement["register_flag"] = "0"; //挂号标志
            if ("RegisterOP".Equals(OperationType))
            {
                // 门诊挂号（医疗类别/统筹类别）
                joSettlement["med_type"] = "6";
                joSettlement["register_flag"] = "1";
            }

            // 个性化（济南）,费用不单独上传
            joSettlement["patient"] = joReg;
            joSettlement.Add("psn_name", Global.pat.name);
            joSettlement.Add("gend", ("1".Equals(Global.pat.gend)?"男":"女"));
            joSettlement.Add("cardno", Global.pat.card.NO);
            joSettlement["admNo"] = Global.pat.adm_Dr; // 病历号

            var ret = hBus.getFeeDetail();
            if (ret.code < 0)
            {
                return Error(ret.code,ret.data.ToString());
            }
            JArray jaFeeDetail = (JArray)ret.data;
            joSettlement["feeDetail"] = jaFeeDetail;


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

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


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

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

            //展示结算信息
            JObject joConverted = JObject.Parse(outParam);
            if (hBus.showSettlementForm(joConverted, out outParam) != 0)
            {
                return Exception(-1, "结算信息展示", outParam);
            }
            // 如果没有返回值，则返回入参
            if (string.IsNullOrEmpty(outParam))
            {
                outParam = joSettlement.ToString();
            }

            return Success();
        }
    }
}
