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 OrganizePatientRegistrationInfo : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            //调用服务获取门诊诊断信息
            string diagnoses;
            if (hIS.getPatDiagnoses(Global.pat, out diagnoses) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "getPatDiagnoses", diagnoses).ToString();
                return Exception(-1,"",outParam);
            }
            JObject joHisRtnInfo = JObject.Parse(diagnoses);


            //组织入参
            JObject joReg = new JObject();
            try
            {
                JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));

                //医疗类别
                string med_Type = string.IsNullOrEmpty(Global.pat.medType) ? "11" : Global.pat.medType;


                //结算方式
                string psn_setlway = "01";//按项目结算

                //转换下主要诊断
                for (int i = 0; i < jaDiagnoses.Count; i++)
                {
                    string diag_type = JsonHelper.getDestValue((JObject)jaDiagnoses[i], "diag_type");
                    switch (diag_type)
                    {
                        case "1":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医主要诊断";
                                break;
                            }
                        case "2":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "西医其他诊断";
                                break;
                            }
                        case "3":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主病诊断";
                                break;
                            }
                        case "4":
                            {
                                jaDiagnoses[i]["diag_type_name"] = new JObject();
                                jaDiagnoses[i]["diag_type_name"] = "中医主证诊断";
                                break;
                            }
                    }

                }
                string diseCodg = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code");
                string diseName = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name");
                #region 组织挂号入参

                dynamic joExpContent = new JObject();
                joExpContent.card_token = Global.pat.card.Cardtoken;
                joExpContent.enddate = "";
                joExpContent.adsetl_codg = "0";
                //joExpContent.mdtrt_grp_type = frmReg.mdtrtGrpType;
                //joExpContent.opt_er_refl_flag = frmReg.relTtpFlag; ;
                //joExpContent.wlt_acct_flag = frmReg.wltAcctFlag; ;

                JObject joRegInpar = new JObject();
                string occurTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                joRegInpar.Add("psn_no", Global.pat.psn_no);
                joRegInpar.Add("insutype", Global.pat.insuType);
                string beginTime = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.begntime");
                if (string.IsNullOrEmpty(beginTime))
                {
                    beginTime = occurTime;
                }
                joRegInpar.Add("begntime", beginTime);//挂号时间 occurTime
                joRegInpar.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                joRegInpar.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);


                joRegInpar.Add("ipt_otp_no", Global.pat.adm_Dr.ToString());

                joRegInpar.Add("atddr_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.atddr_no"));
                joRegInpar.Add("dr_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.chfpdr_name"));
                joRegInpar.Add("dept_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_codg"));
                joRegInpar.Add("dept_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_name"));
                joRegInpar.Add("exp_content", joExpContent);
                joRegInpar.Add("caty", "A10");

                //南昌新增
                joRegInpar.Add("card_sn", Global.pat.card.SN);
                joRegInpar.Add("psn_cert_type", Global.pat.certType);
                joRegInpar.Add("certno", Global.pat.certNO);
                joRegInpar.Add("psn_type", Global.pat.psn_type);
                joRegInpar.Add("psn_name", Global.pat.name);
                joRegInpar.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);
                joRegInpar.Add("mdtrtarea_admvs", Global.inf.areaCode);

                #endregion

                #region 组织就诊信息入参
                JObject joMdtrtinfo = new JObject();
                for (int i = 0; i < jaDiagnoses.Count; i++)
                {
                    jaDiagnoses[i]["vali_flag"] = new JObject();
                    jaDiagnoses[i]["vali_flag"] = "1";
                }
                joMdtrtinfo.Add("mdtrt_id", Global.pat.mdtrtID);
                joMdtrtinfo.Add("psn_no", Global.pat.psn_no);          //人员编号
                joMdtrtinfo.Add("med_type", med_Type);          //医疗类别
                joMdtrtinfo.Add("begntime", beginTime);          //开始时间
                joMdtrtinfo.Add("main_cond_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.main_cond_dscr"));          //主要病情描述
                joMdtrtinfo.Add("dise_codg", diseCodg);          //病种编码
                joMdtrtinfo.Add("dise_name", diseName);          //病种名称
                joMdtrtinfo.Add("birctrl_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_type"));          //计划生育手术类别
                joMdtrtinfo.Add("matn_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.matn_type"));          //生育类别
                joMdtrtinfo.Add("expContent", "");          //字段扩展

                #endregion

                #region 组织结算入参
                JObject joSetlInpar = new JObject();
                joSetlInpar.Add("psn_no", Global.pat.psn_no);
                joSetlInpar.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                joSetlInpar.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                joSetlInpar.Add("med_type", Global.pat.medType);
                joSetlInpar.Add("medfee_sumamt", "");
                joSetlInpar.Add("psn_setlway", psn_setlway); //结算方式
                joSetlInpar.Add("mdtrt_id", Global.pat.mdtrtID);
                joSetlInpar.Add("chrg_bchno", Global.pat.adm_Dr.ToString());
                joSetlInpar.Add("acct_used_flag", "1");//个人账户使用标志
                joSetlInpar.Add("insutype", Global.pat.insuType);//险种类型
                joSetlInpar.Add("pub_hosp_rfom_flag", "");

                //成都新增
                joSetlInpar.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);
                joSetlInpar.Add("exp_content", joExpContent);


                #endregion

                joReg.Add("data", joRegInpar);
                joReg.Add("mdtrtinfo", joMdtrtinfo);
                joReg.Add("diseinfo", jaDiagnoses);
                joReg.Add("settlement", joSetlInpar);
                joReg.Add("patInfo", input);
                outParam = joReg.ToString();

                return Success();

            }
            catch (Exception ex)
            {
                outParam = "登记界面显示异常：" + ex.Message;
                return Exception(-1,"",outParam);
            }
        }
    }
}
