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 OPSettlementProcess : AbstractProcess
    {
        public override CallResult Process(JObject joSettlement)
        {
            JObject joData = new JObject();
            joData.Add("data", joSettlement);
            JObject joRtn = invoker.invokeCenterService(TradeEnum.OutpatientSettlement, joData);
            if (JsonHelper.parseCenterRtnValue(joRtn, out outParam) != 0)
            {
                return Error(-1, "结算失败");
            }
            else
            {
                Global.pat.admType = 1;
                Global.pat.ExpContent = "";  //门诊不需要保存 外伤标志等相关扩展参数
                                             //返回给云医保平台结算信息
                if (mIS.saveSettlement(joRtn, out outParam) != 0)
                {
                    return Error(-1, "结算成功，但云医保平台保存失败");
                }
                //返回给云医保平台结算明细信息
                if (mIS.saveSettlementDetail(joRtn, out outParam) != 0)
                {
                    return Error(-1, "云医保平台保存结算明细失败");

                }
                //返回给HIS
                JObject joSetlinfo = JObject.Parse(JsonHelper.getDestValue(joRtn, "output.setlinfo"));
                if (hIS.returnOutpatSettlementInfo(joSettlement, joSetlinfo, out outParam) != 0)
                {
                    return Error(-1, "返回结算信息给HIS");
                }
                else
                {
                    JObject joHisServieRtn = JObject.Parse(outParam);
                    hBus.returnToFrontEndAfterSettlement(joRtn, joHisServieRtn, out outParam);
                    //return JsonHelper.setIrisReturnValue(0, "结算成功", JObject.Parse(outParam));
                    return IrisReturn("结算成功", JObject.Parse(outParam));
                }
            }
        }
    }
}
