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.Core.SelfServiceMachine.Process.OutPat
{
    class PreSettlement : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            //调用IRIS获取医保各项金额
            if (mIS.getSumFee(out outParam) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "获取医保费用各项汇总金额", outParam).ToString();
                return Exception(-1, "", outParam);
            }
            JObject joSumFee = JObject.Parse(outParam);
            JObject joSettlement = JObject.Parse(JsonHelper.getDestValue(input, "settlement"));
            //修改joSettlement的就诊ID ,总金额等
            joSettlement["mdtrt_id"] = Global.pat.mdtrtID;
            joSettlement["medfee_sumamt"] = JsonHelper.getDestValue(joSumFee, "data.Sumamt");

            ////预结算2206
            //if (hBus.preSettlement_OutPat(joSettlement, out outParam) != 0)
            //{
            //    outParam = JsonHelper.setExceptionJson(-1, "结算信息展示", outParam).ToString();
            //    return Exception(-1, "", outParam);
            //}

            //return Success();
            //-----------------------------------------------

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

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


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

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



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

            // 返回结算参数+返回
            outParam = joSettlement.ToString();

            return Success();
        }
    }
}
