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 GetAdmissionRequirements : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            if (GetAuditInfo(out errMsg) != 0)
            {
                if (OrganizeInPatRegisterInpar(out errMsg) != 0)
                {
                    return Exception(errMsg);
                }

     
            }
            input.Add("RegInfo", JObject.Parse(errMsg));
            outParam = input.ToString();
            return Success();

        }

        private int GetAuditInfo(out string auditInfo)
        {
            auditInfo = "";
            return -1;
        }

        /// <summary>
        /// 组织登记入参
        /// </summary>
        /// <param name="regInfo"></param>
        /// <returns></returns>
        public int OrganizeInPatRegisterInpar(out string regInfo)
        {

            //调用服务获取门诊诊断信息
            string diagnoses;
            if (hIS.getPatDiagnoses(Global.pat, out diagnoses) != 0)
            {
                regInfo = JsonHelper.setExceptionJson(-1, "getPatDiagnoses", diagnoses).ToString();
                return -1;
            }
            JObject joHisRtnInfo = JObject.Parse(diagnoses);

            //组织登记入参
            JObject joReg = new JObject();
            try
            {
                JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));
                string dscg_maindiag_code = "", dscg_maindiag_name = "";
                //  对诊断数组进行转换 转换下主要诊断
                JArray jaConvertedDiagnoses = new JArray();
                JArray jaInPatDiagnoses = new JArray();
                JArray jaOutPatDiagnoses = new JArray();
                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;
                            }
                    }


                    //if (jaDiagnoses[i]["diaTypeCode"].ToString() == "ADD") //ADD 入院 OPD 门诊
                    //{
                    //    jaConvertedDiagnoses.Add(jaDiagnoses[i]);
                    //}


                    if (jaDiagnoses[i]["diaTypeCode"].ToString() == "ADD") //ADD 入院 OPD 门诊
                    {
                        jaInPatDiagnoses.Add(jaDiagnoses[i]);
                    }
                    if (jaDiagnoses[i]["diaTypeCode"].ToString() == "OPD") //ADD 入院 OPD 门诊
                    {
                        jaOutPatDiagnoses.Add(jaDiagnoses[i]);
                    }
                }

                if (jaInPatDiagnoses.Count > 0)
                {
                    jaConvertedDiagnoses = (JArray)jaInPatDiagnoses.DeepClone();
                }
                else
                {
                    jaConvertedDiagnoses = (JArray)jaOutPatDiagnoses.DeepClone();
                }

                for (int i = 0; i < jaConvertedDiagnoses.Count; i++)
                {
                    jaConvertedDiagnoses[i]["diag_srt_no"] = i + 1;
                    if (jaConvertedDiagnoses[i]["maindiag_flag"].ToString() == "1")
                    {
                        dscg_maindiag_code = jaConvertedDiagnoses[i]["diag_code"].ToString();
                        dscg_maindiag_name = jaConvertedDiagnoses[i]["diag_name"].ToString();
                    }
                }


                Global.pat.medType = "2101";
                string diseCodg = "", diseName = "";


                diseCodg = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code");

                diseName = JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name");


                JObject joMdtrtinfo = new JObject();
                joMdtrtinfo.Add("psn_no", Global.pat.psn_no);          //人员编号
                joMdtrtinfo.Add("insutype", Global.pat.insuType);          //险种类型
                joMdtrtinfo.Add("coner_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.coner_name"));          //联系人姓名
                joMdtrtinfo.Add("tel", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.tel"));          //联系电话
                joMdtrtinfo.Add("begntime", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.begntime"));          //开始时间
                joMdtrtinfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);          //就诊凭证类型
                joMdtrtinfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);          //就诊凭证编号
                joMdtrtinfo.Add("med_type", Global.pat.medType);          //医疗类别
                joMdtrtinfo.Add("ipt_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.ipt_no"));          //住院号
                joMdtrtinfo.Add("medrcdno", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.medrcdno"));          //病历号
                joMdtrtinfo.Add("atddr_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.atddr_no"));          //主治医生编码
                joMdtrtinfo.Add("chfpdr_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.chfpdr_name"));          //主诊医师姓名
                joMdtrtinfo.Add("adm_diag_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_diag_dscr"));          //入院诊断描述
                joMdtrtinfo.Add("adm_dept_codg", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_codg"));          //入院科室编码
                joMdtrtinfo.Add("adm_dept_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_dept_name"));          //入院科室名称
                joMdtrtinfo.Add("adm_bed", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.adm_bed"));          //入院床位
                joMdtrtinfo.Add("dscg_maindiag_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_code"));          //住院主诊断代码
                joMdtrtinfo.Add("dscg_maindiag_name", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dscg_maindiag_name"));          //住院主诊断名称
                //joMdtrtinfo.Add("dscg_maindiag_code", dscg_maindiag_code);          //住院主诊断代码
                //joMdtrtinfo.Add("dscg_maindiag_name", dscg_maindiag_name);          //住院主诊断名称
                joMdtrtinfo.Add("main_cond_dscr", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.main_cond_dscr"));          //主要病情描述
                joMdtrtinfo.Add("dise_codg", diseCodg);          //病种编码
                joMdtrtinfo.Add("dise_name", diseName);          //病种名称
                joMdtrtinfo.Add("oprn_oprt_code", "");          //手术操作代码
                joMdtrtinfo.Add("oprn_oprt_name", "");          //手术操作名称
                joMdtrtinfo.Add("fpsc_no", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fpsc_no"));          //计划生育服务证号
                joMdtrtinfo.Add("matn_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.matn_type"));          //生育类别
                joMdtrtinfo.Add("birctrl_type", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_type"));          //计划生育手术类别
                joMdtrtinfo.Add("latechb_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.latechb_flag"));          //晚育标志
                joMdtrtinfo.Add("geso_val", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.geso_val"));          //孕周数
                joMdtrtinfo.Add("fetts", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetts"));          //胎次
                joMdtrtinfo.Add("fetus_cnt", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.fetus_cnt"));          //胎儿数
                joMdtrtinfo.Add("pret_flag", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.pret_flag"));          //早产标志
                joMdtrtinfo.Add("birctrl_matn_date", "");          //计划生育手术或生育日期 JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.birctrl_matn_date") 20230228医保中心校验传正确时间或空
                joMdtrtinfo.Add("dise_type_code", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.dise_type_code"));          //病种类型
                joMdtrtinfo.Add("exp_content", "");          //字段扩展
                joMdtrtinfo.Add("insuplc_admdvs", Global.pat.insuplc_admdvs);          //参保地医保区划
                joMdtrtinfo.Add("mdtrtarea_admvs", Global.inf.areaCode);          //就医地医保区划

                joMdtrtinfo.Add("patID", JsonHelper.getDestValue(joHisRtnInfo, "mdtrtinfo.patID"));          //HIS患者ID

                joReg.Add("mdtrtinfo", joMdtrtinfo);
                joReg.Add("diseinfo", jaConvertedDiagnoses);

                Global.pat.disCode = diseCodg;
                Global.pat.disName = diseName;

                regInfo = joReg.ToString();
                return 0;

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

        }
    }
}
