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.InPat
{
    class AdmissionRegistration : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            JObject joRegInfo = JObject.Parse(JsonHelper.getDestValue(input, "RegInfo"));


            JObject joRegIn = JObject.Parse(JsonHelper.setCenterInpar("2401", joRegInfo));

            //上传给中心
            JObject joRegOut = invoker.invokeCenterService("2401", joRegIn);
            //将中心返回的结果返回HIS,调用HIS服务进行保存
            if (JsonHelper.parseCenterRtnValue(joRegOut, out errMsg) == 0)
            {
                joRegOut.Add("validflag", 1);
                joRegOut.Add("regstate", 1);
                joRegOut.Add("type", 1);
                Global.pat.mdtrtID = JsonHelper.getDestValue(joRegOut, "output.result.mdtrt_id");
                //插入患者本次就诊用的参保信息
                if (hBus.insertPatCurInsuInfo(input.ToString(), out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "插入患者本次就诊用的参保信息，请联系管理员！", outParam).ToString();
                    return Exception(outParam);
                }

                //插入医保平台
                if (mIS.saveInpatRegisterInfo(joRegIn, joRegOut, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "云平台保存登记信息", outParam).ToString();
                    return Exception(outParam);
                }

                //插入医保平台全量信息表
                if (mIS.saveMIPatFullInsuInfo( JObject.Parse(JsonHelper.getDestProperty(input,"output")), out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "云平台保存登记全量信息失败", outParam).ToString();
                    return Exception(outParam);
                }

                //插入费用核查表 入院登记
                if (mIS.saveDischargeAudit(3, 2,0,joRegIn, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "云平台保存费用核查入院登记信息", outParam).ToString();
                    return Exception(outParam);
                }

                //插入费用核查表 出院登记 默认，具体需要最终审核
                JObject joDisReg = (JObject)joRegIn.DeepClone();
                //joDisReg["input"]["mdtrtinfo"]["psn_setlway"] = new JObject();
                joDisReg["input"]["mdtrtinfo"]["psn_setlway"] = "01";
                //joDisReg["input"]["mdtrtinfo"]["acct_used_flag"] = new JObject();
                joDisReg["input"]["mdtrtinfo"]["acct_used_flag"] = "1";
                //joDisReg["input"]["mdtrtinfo"]["mid_setl_flag"] = new JObject();
                joDisReg["input"]["mdtrtinfo"]["mid_setl_flag"] = "0";
                if (mIS.saveDischargeAudit(3, 3,0, joDisReg, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "云平台保存费用核查出院登记信息(默认)", outParam).ToString();
                    return Exception(outParam);
                }
                //返回登记信息，插入HIS
                if (hIS.returnRegisterInfo(joRegIn, joRegOut, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "HIS平台保存登记信息", outParam).ToString();
                    return Exception(outParam);
                }
                else
                {
                    outParam = JsonHelper.setIrisReturnValue(0, "登记成功", null).ToString();
                    return Success();
                }
            }
            else
            {
                outParam = JsonHelper.setExceptionJson(-1, "入院登记失败", errMsg).ToString();
                return Exception(outParam); ;
            }
        }

        
    }
}
