using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PTMedicalInsurance.Business
{
    class PreAndInProcessAnalysis
    {
        private HisIrisServices hIS = new HisIrisServices();

        private JArray patient_dtos; //患者信息集合
        private JArray fsi_encounter_dtos; //就诊信息集合
        private JArray fsi_diagnose_dtos;  //诊断信息集合
        private JArray fsi_order_dtos; //处方信息集合
        private JArray fsi_operation_dtos; // 手术信息集合

        MIIrisServices mis = new MIIrisServices();
        public PreAndInProcessAnalysis(string inpar)
        {
            try
            {
                JObject jo = JObject.Parse(inpar);
                JObject joResult = JObject.FromObject(jo["result"]);
                patient_dtos = JArray.FromObject(joResult["patient_dtos"]);
                fsi_encounter_dtos = JArray.FromObject(patient_dtos[0]["fsi_encounter_dtos"]);
                fsi_diagnose_dtos = JArray.FromObject(fsi_encounter_dtos[0]["fsi_diagnose_dtos"]);
                fsi_order_dtos = JArray.FromObject(fsi_encounter_dtos[0]["fsi_order_dtos"]);
                fsi_operation_dtos = JArray.FromObject(fsi_encounter_dtos[0]["fsi_operation_dtos"]);
            }
            catch (Exception ex)
            {
                Global.writeLog("PreAndInProcessAnalysis构造异常：" + ex.Message);
                throw;
            }
        }
        #region 设置Patient
        public int setPatientByHisBaseInfo(out string msg)
        {
            string errMsg = string.Empty;
            msg = "";
            try
            {
                if (hIS.GetHisPatBaseInfo(out errMsg) != 0)
                {
                    msg = errMsg;
                    return -1;
                }
                JObject jo = JObject.Parse(errMsg);
                Global.pat.name = JsonHelper.getDestValue(jo, "result.patName");
                Global.pat.brdy = JsonHelper.getDestValue(jo, "result.patBirthdate");
                Global.pat.gend = JsonHelper.getDestValue(jo, "result.patSex");
                Global.pat.age = JsonHelper.getDestValue(jo, "result.Age");
                Global.pat.naty = JsonHelper.getDestValue(jo, "result.naty");
                Global.pat.certNO = JsonHelper.getDestValue(jo, "result.credNo");
                Global.pat.certType = JsonHelper.getDestValue(jo, "result.credCode");
                return 0;
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return -1;
            }
        }

        #endregion

        public JObject Get3101Inpar(string trig_scen, Patients p)
        {
            try
            {
                JObject joInpar = new JObject();
                joInpar.Add("syscode", "YBJK");
                joInpar.Add("patient_dtos", GetPatient_dtos(trig_scen, p));
                JArray ruleid = new JArray();
                joInpar.Add("rule_ids", ruleid);
                joInpar.Add("task_id", GetMsgID());
                joInpar.Add("trig_scen", trig_scen);
                JObject joData = new JObject();
                joData.Add("data", joInpar);
                return joData;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Get3101Inpar:" + ex.Message);
                return null;
            }
        }

        private string GetMsgID()
        {
            return Global.inf.hospitalNO + DateTime.Now.ToString("yyyyMMddHHmmssffff");
        }

        public JArray GetPatient_dtos(string trig_scen, Patients p)
        {
            try
            {
                string curr_mdtrt_id = p.mdtrtID;
                if (string.IsNullOrEmpty(curr_mdtrt_id))
                {
                    curr_mdtrt_id = Global.pat.adm_Dr.ToString();
                }
                JObject joInpar = new JObject();
                joInpar.Add("patn_id", p.psn_no);
                joInpar.Add("patn_name", p.name);
                string gend = p.gend;
                if (gend == "男") gend = "1";
                if (gend == "女") gend = "2";
                joInpar.Add("gend", gend);
                joInpar.Add("brdy", p.brdy);
                joInpar.Add("poolarea", p.insuplc_admdvs);
                joInpar.Add("curr_mdtrt_id", curr_mdtrt_id);
                joInpar.Add("fsi_encounter_dtos", GetFsi_encounter_dtos(trig_scen, p));
                joInpar.Add("fsi_his_data_dto", new JArray());
                JArray ja = new JArray();
                ja.Add(joInpar);
                return ja;
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetPatient_dtos:" + ex.Message);
                return null;
            }
        }

        public JArray GetFsi_encounter_dtos(string trig_scen, Patients p)
        {
            try
            {
                JObject jofsi_encounter_dtos = JObject.FromObject(fsi_encounter_dtos[0]);
                JObject joInpar = new JObject();
                string mdtrtID = p.mdtrtID;
                if (string.IsNullOrEmpty(mdtrtID)) mdtrtID = Global.pat.adm_Dr.ToString();
                joInpar.Add("mdtrt_id", mdtrtID);  //就诊标识
                joInpar.Add("medins_id", Global.inf.hospitalNO);  //医疗服务机构标识
                joInpar.Add("medins_name", Global.inf.hospitalName);  //医疗机构名称
                joInpar.Add("medins_admdvs", Global.inf.areaCode);  //医疗机构行政区划编码
                joInpar.Add("medins_type", "A");  //医疗服务机构类型
                joInpar.Add("medins_lv", "03");  //医疗机构等级
                joInpar.Add("wardarea_codg", JsonHelper.getDestValue(jofsi_encounter_dtos, "wardarea_codg"));  //病区标识
                joInpar.Add("wardno", JsonHelper.getDestValue(jofsi_encounter_dtos, "wardno"));  //病房号
                joInpar.Add("bedno", JsonHelper.getDestValue(jofsi_encounter_dtos, "bedno"));  //病床号
                joInpar.Add("adm_date", JsonHelper.getDestValue(jofsi_encounter_dtos, "adm_date"));  //入院日期
                joInpar.Add("dscg_date", JsonHelper.getDestValue(jofsi_encounter_dtos, "dscg_date"));  //出院日期
                joInpar.Add("dscg_main_dise_codg", JsonHelper.getDestValue(jofsi_encounter_dtos, "dscg_main_dise_codg"));  //主诊断编码
                joInpar.Add("dscg_main_dise_name", JsonHelper.getDestValue(jofsi_encounter_dtos, "dscg_main_dise_name"));  //主诊断名称
                joInpar.Add("fsi_diagnose_dtos", GetFsi_diagnose__dtos(trig_scen, p));  //诊断信息DTO
                joInpar.Add("dr_codg", JsonHelper.getDestValue(jofsi_encounter_dtos, "dr_codg"));  //医师标识
                joInpar.Add("adm_dept_codg", JsonHelper.getDestValue(jofsi_encounter_dtos, "adm_dept_codg"));  //入院科室标识
                joInpar.Add("adm_dept_name", JsonHelper.getDestValue(jofsi_encounter_dtos, "adm_dept_name"));  //入院科室名称
                joInpar.Add("dscg_dept_codg", JsonHelper.getDestValue(jofsi_encounter_dtos, "dscg_dept_codg"));  //出院科室标识
                joInpar.Add("dscg_dept_name", JsonHelper.getDestValue(jofsi_encounter_dtos, "dscg_dept_name"));  //出院科室名称
                joInpar.Add("med_mdtrt_type", JsonHelper.getDestValue(jofsi_encounter_dtos, "med_mdtrt_type"));  //就诊类型
                //银海和基线版不一样
                string med_type = Global.pat.medType;
                if (trig_scen == "1") { med_type = "11"; }
                if (trig_scen == "2") { med_type = "11"; }
                if (trig_scen == "3") { med_type = "21"; }
                if (trig_scen == "4") { med_type = "21"; }
                if (trig_scen == "5") { med_type = "21"; }
                //if (med_type == "31") med_type = "21";
                //if (med_type == "12") med_type = "41";
                //if (med_type == "37") med_type = "23";
                //if (med_type == "11") med_type = "11";
                //if (med_type == "36") med_type = "23";
                //if (med_type == "18") med_type = "14";
                //if (med_type == "32") med_type = "92";
                //if (med_type == "1102") med_type = "91";
                if(Global.pat.RYorCY == "2")
                {
                    joInpar.Add("med_type", "21");  //医疗类别
                }
                else
                {
                    joInpar.Add("med_type", "11");
                }

                    joInpar.Add("fsi_order_dtos", GetFsi__order_dtos(trig_scen, p));  //处方(医嘱)信息
                string matn_stas = JsonHelper.getDestValue(jofsi_encounter_dtos, "matn_stas");
                if (string.IsNullOrEmpty(matn_stas)) matn_stas = "0";
                joInpar.Add("matn_stas", matn_stas);  //生育状态
                joInpar.Add("medfee_sumamt", JsonHelper.getDestValue(jofsi_encounter_dtos, "medfee_sumamt"));  //总费用
                //joInpar.Add("ownpay_amt", JsonHelper.getDestValue(jofsi_encounter_dtos, "ownpay_amt"));  //自费金额
                //joInpar.Add("selfpay_amt", JsonHelper.getDestValue(jofsi_encounter_dtos, "selfpay_amt"));  //自付金额
                //joInpar.Add("acct_payamt", JsonHelper.getDestValue(jofsi_encounter_dtos, "acct_payamt"));  //个人账户支付金额
                //joInpar.Add("ma_amt", JsonHelper.getDestValue(jofsi_encounter_dtos, "ma_amt"));  //救助金支付金额
                //joInpar.Add("hifp_payamt", JsonHelper.getDestValue(jofsi_encounter_dtos, "hifp_payamt"));  //统筹金支付金额
                joInpar.Add("ownpay_amt", null);  //自费金额
                joInpar.Add("selfpay_amt", null);  //自付金额
                joInpar.Add("acct_payamt", null);  //个人账户支付金额
                joInpar.Add("ma_amt", null);  //救助金支付金额
                joInpar.Add("hifp_payamt", null);  //统筹金支付金额
                joInpar.Add("setl_totlnum", JsonHelper.getDestValue(jofsi_encounter_dtos, "setl_totlnum"));  //结算总次数
                //string insutype = Global.pat.insuType;
                //if (insutype == "1") insutype = "310";
                //if (insutype == "2") insutype = "310";
                //if (insutype == "3") insutype = "390";
                //if (insutype == "4") insutype = "340";
                joInpar.Add("insutype", JsonHelper.getDestValue(jofsi_encounter_dtos, "insutype"));  //险种
                joInpar.Add("reim_flag", JsonHelper.getDestValue(jofsi_encounter_dtos, "reim_flag"));  //报销标志
                joInpar.Add("out_setl_flag", JsonHelper.getDestValue(jofsi_encounter_dtos, "out_setl_flag"));  //异地结算标志
                joInpar.Add("fsi_operation_dtos", GetFsi__operation__dtos(trig_scen, p));  //手术操作集合

                JArray ja = new JArray();
                ja.Add(joInpar);
                return ja;
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetPatient_dtos:" + ex.Message);
                return null;
            }
        }
        public JArray GetFsi_diagnose__dtos(string trig_scen, Patients p)
        {
            try
            {
                return fsi_diagnose_dtos;
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetPatient_dtos:" + ex.Message);
                return null;
            }
        }


        public JArray GetFsi__operation__dtos(string trig_scen, Patients p)
        {
            try
            {
                return fsi_operation_dtos;
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetPatient_dtos:" + ex.Message);
                return null;
            }
        }

        #region 组织入参
        public JArray GetFsi__order_dtos(string trig_scen, Patients p)
        {
            string errMsg = "";
            try
            {
                for (int i = 0; i < fsi_order_dtos.Count; i++)
                {
                    decimal pric = decimal.Parse(fsi_order_dtos[i]["pric"].ToString());
                    fsi_order_dtos[i]["pric"] = pric.ToString("#0.00");
                    fsi_order_dtos[i]["hilist_pric"] = pric.ToString("#0.00");
                    fsi_order_dtos[i]["lv1_hosp_item_pric"] = pric.ToString("#0.00");
                    fsi_order_dtos[i]["lv2_hosp_item_pric"] = pric.ToString("#0.00");
                    fsi_order_dtos[i]["lv3_hosp_item_pric"] = pric.ToString("#0.00");
                    fsi_order_dtos[i]["ownpay_amt"] = null;
                    fsi_order_dtos[i]["selfpay_amt"] = null;
                    fsi_order_dtos[i]["drord_stop_date"] = null;
                    //MessageBox.Show("提示" + Convert.ToString(fsi_order_dtos[i]["spec"])+"阿萨");
                    // if ((string.IsNullOrEmpty(Convert.ToString(fsi_order_dtos[i]["spec"])))| (Convert.ToString(fsi_order_dtos[i]["spec"])==""))
                    //if (fsi_order_dtos[i]["spec"] == null || fsi_order_dtos[i]["spec"].ToString() == "")
                    //{
                    //    fsi_order_dtos[i]["spec"] = fsi_order_dtos[i]["spec_unt"];
                    //}
                    /*if (fsi_order_dtos[i]["hilist_code"].ToString().Substring(0, 1) == "C")
                    {
                        fsi_order_dtos[i]["chrg_type"] = "08";
                    }else if (fsi_order_dtos[i]["hilist_code"].ToString().Substring(0, 1) == "X")
                    {
                        fsi_order_dtos[i]["chrg_type"] = "09";
                    }
                    else if (fsi_order_dtos[i]["hilist_code"].ToString().Substring(0, 1) == "Z")
                    {
                        fsi_order_dtos[i]["chrg_type"] = "11";
                    }else
                    {
                        fsi_order_dtos[i]["chrg_type"] = "14";
                    }*/
                    fsi_order_dtos[i]["chrg_type"] = "14";//HIS未传收费类别，临时全部默认位其他费
                }
                //调用医保平台进行转换
                if (mis.convertFsiOrderDtos(fsi_order_dtos, out errMsg) != 0)
                {
                    return null;
                }
                else
                {
                    JObject joRtn = JObject.Parse(errMsg);
                    JArray jarry = JArray.FromObject(joRtn["data"]);

                    for (int m = 0; m < jarry.Count; m++)
                    {
                        jarry[m]["drord_dr_profttl"] = "2";
                        if (string.IsNullOrEmpty(Convert.ToString(jarry[m]["hilist_lv"])))
                        {
                            MessageBox.Show(Convert.ToString(jarry[m]["hosplist_name"]));
                        }
                        if (string.IsNullOrEmpty(Convert.ToString(jarry[m]["hilist_code"]))) continue;

                        if (jarry[m]["hilist_code"].ToString().Substring(0, 1) == "C")
                        {
                            jarry[m]["chrg_type"] = "08";
                            jarry[m]["hilist_type"] = "301";
                        }
                        else if (jarry[m]["hilist_code"].ToString().Substring(0, 1) == "X")
                        {
                            jarry[m]["chrg_type"] = "09";
                            jarry[m]["hilist_type"] = "101";
                        }
                        else if (jarry[m]["hilist_code"].ToString().Substring(0, 1) == "Z")
                        {
                            jarry[m]["chrg_type"] = "11";
                            jarry[m]["hilist_type"] = "101";
                        }
                        else
                        {
                            jarry[m]["chrg_type"] = "14";
                            jarry[m]["hilist_type"] = "201";
                        }
                        if (jarry[m]["spec"] == null || jarry[m]["spec"].ToString() == "")
                        {
                            jarry[m]["spec"] = jarry[m]["spec_unt"];
                        }
                    }

                    //return JArray.FromObject(joRtn["data"]);
                    return jarry;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetFsi__order_dtos:" + ex.Message);
                return null;
            }
        }
        #endregion
    }


}
