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 IPSettlementProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            string errMsg = "";
            #region 正式出院结算
            JObject joSettlement = joSettlement = JObject.Parse(JsonHelper.getDestValue(input, "Settlement"));
            JObject joDataExp = new JObject();
            joDataExp.Add("cardtoken", Global.pat.card.Cardtoken);
            joDataExp.Add("acct_payamt", Global.Set.acctPay);
            joDataExp.Add("elec_bill_code", "");
            joDataExp.Add("elec_billno_code", "");
            joDataExp.Add("elec_bill_chkcode", "");
            joSettlement["exp_content"] = joDataExp;

            joSettlement.Add("data", joSettlement);
            JObject jo2304Rtn = invoker.invokeCenterService(TradeEnum.InpatientSettlement, joSettlement);
            if (JsonHelper.parseCenterRtnValue(jo2304Rtn, out errMsg) != 0)
            {
                cBus.cancleRegister(TradeEnum.InpatientExitCancel, out errMsg);
                return Exception(-1, "结算失败", errMsg);
            }
            else
            {
                Global.pat.admType = 2;
                //返回给云医保平台结算信息
                if (mIS.saveSettlement(jo2304Rtn, out errMsg) != 0)
                {
                    return Exception(-1, "结算成功，但云医保平台保存失败", errMsg);
                }
                //返回给云医保平台结算明细信息
                if (mIS.saveSettlementDetail(jo2304Rtn, out errMsg) != 0)
                {
                    return Exception(-1, "云医保平台保存结算明细失败", errMsg);
                }
                //返回给HIS
                JObject joSetlinfo = JObject.Parse(JsonHelper.getDestValue(jo2304Rtn, "output.setlinfo"));
                if (hIS.returnInpatSettlementInfo(input, joSetlinfo, out outParam) != 0)
                {
                    return Exception(-1, "返回结算信息给HIS", outParam);
                }
                else
                {
                    //返回给前端
                    JObject joHisServieRtn = JObject.Parse(outParam);
                    hBus.returnToFrontEndAfterSettlement(jo2304Rtn, joHisServieRtn, out outParam);

                    return Success("结算成功");
                }
            }
            #endregion
        }
    }
}
