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

namespace PTMedicalInsurance.Business
{
    class InsuServices
    {


        /// <summary>
        /// 根据HisType获取当前目录最大版本号
        /// </summary>
        /// <param name="directoryType"></param>
        /// <returns></returns>
        public JObject getDirectoryMaxVersionNO(int directoryType)
        {
            try
            {
                dynamic joParam = new JObject();
                joParam.HospitalDr = Global.inf.hospitalDr;
                joParam.InterfaceDr = Global.inf.interfaceDr;
                joParam.HisType = directoryType + 1;
                JObject joInparam = JsonHelper.setIrisInpar("09010037", joParam);
                InvokeHelper invoker = new InvokeHelper();
                JObject joRtn = invoker.invokeInsuService(joInparam.ToString(), "获取当前目录最大版本号");
                return joRtn;
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "获取版本号异常：", ex.Message);
            }
        }

        /// <summary>
        /// 通过分页的方式查询医保目录
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="irisCode"></param>
        /// <param name="directoryType"></param>
        public JObject getDirectoryByPagination(int currentPage, int pageSize, string serviceCode, int directoryType, string directoryCode, string directoryName, int directoryDr)
        {
            try
            {
                InvokeHelper invoker = new InvokeHelper();
                dynamic joInput = new JObject();
                joInput.code = serviceCode;
                dynamic joTmp = new JObject();
                JArray jaParmas = new JArray();
                joTmp.HospitalDr = Global.inf.hospitalDr;
                joTmp.InterfaceDr = directoryDr;
                joTmp.Code = directoryCode;
                joTmp.Name = directoryName;
                jaParmas.Add(joTmp);
                joInput.Add("params", JArray.FromObject(jaParmas));
                JArray jaPagination = new JArray();
                joTmp = new JObject();
                joTmp.pageSize = pageSize;
                joTmp.currentPage = currentPage;
                joTmp.sortColumn = "";
                joTmp.sortOrder = "";
                joTmp.hisType = directoryType;
                jaPagination.Add(joTmp);
                joInput.Add("pagination", JArray.FromObject(jaPagination));

                return invoker.invokeInsuService(joInput.ToString(), "通过分页的方式查询医保目录");
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "通过分页的方式查询医保目录异常", ex.Message);
            }
        }

        /// <summary>
        /// 获取接口信息
        /// </summary>
        /// <returns></returns>
        public JObject getInterface()
        {
            dynamic joParam = new JObject();
            joParam.HospitalDr = Global.inf.hospitalDr;
            joParam.InterfaceID = "";
            JObject joInparam = JsonHelper.setIrisInpar("09010003", joParam);
            InvokeHelper invoker = new InvokeHelper();
            return invoker.invokeInsuService(joInparam.ToString(), "获取接口信息");
        }

        /// <summary>
        /// 获取指定接口信息
        /// </summary>
        /// <param name="inparam"></param>
        /// <returns></returns>
        public JObject getspecifiedInterface(int HospDr,int InterfaceDr)
        {
            JObject joRtn = new JObject();
            JObject joInparam = new JObject();
            string errorMsg = "";
            try
            {
                InvokeHelper invoke = new InvokeHelper();
                dynamic joParam = new JObject();
                joParam.HospitalDr = HospDr;
                joParam.InterfaceDr = InterfaceDr;
                joInparam = JsonHelper.setIrisInpar("09010003", joParam);
                JObject joOutparam = invoke.invokeInsuService(joInparam.ToString(), "获取指定接口信息");
                if (JsonHelper.parseIrisRtnValue(joOutparam, out errorMsg) != 0)
                {
                    return joOutparam;
                }
                JArray jaParams = new JArray();
                jaParams = JArray.FromObject(joOutparam["result"]["data"]);
                JToken _token = jaParams[0];
                if (_token is null)
                {
                    joRtn = JsonHelper.setExceptionJson(-100, "获取接口信息失败！", "返回值为空");
                }
                if (_token.Type == JTokenType.String)
                {
                    joRtn = JsonHelper.setExceptionJson(-100, "获取接口信息失败！返回值为字符串", _token.ToString());
                }
                else //if (_token.Type == JTokenType.Object)
                {
                    JObject joResult = JObject.FromObject(_token);
                    joRtn.Add("errorCode", 0);
                    joRtn.Add("errorMessage", "");
                    joRtn.Add("result", joResult);
                }
                return joRtn;
            }
            catch (Exception ex)
            {
                joRtn = JsonHelper.setExceptionJson(-100, "获取接口信息异常！", ex.Message);
                Global.writeLog(joRtn.ToString());
                return joRtn;
            }
        }


        /// <summary>
        /// 获取指定字典信息
        /// </summary>
        /// <returns></returns>
        public JObject getSpecDictionary(string interfaceDr, string dicType)
        {
            string sqlStr = " select '' Code,'' Name,'' PinyinSearchCode union all ";
            sqlStr = sqlStr + " SELECT B.Code,B.Descripts AS Name,'' PinyinSearchCode FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr WHERE A.Interface_Dr= " + interfaceDr + " And A.InsuCode = '" + dicType + "' ";

            JObject joSqlstr = new JObject();
            joSqlstr.Add("sqlStr", sqlStr);
            JArray jaParam = new JArray();
            jaParam.Add(joSqlstr);
            JObject joSettlQuery = new JObject();
            joSettlQuery.Add("params", jaParam);
            joSettlQuery.Add("code", "09010063");
            InvokeHelper invoker = new InvokeHelper();
            return invoker.invokeInsuService(joSettlQuery.ToString(), "查询指定字典信息");
        }

        /// <summary>
        /// 保存登记信息到云医保平台
        /// </summary>
        /// <param name="joRegisterInfo"></param>
        /// <returns></returns>
        public JObject saveRegisterInfo(JObject joRegisterInfo, JObject joHisAdmInfo)
        {
            JObject joTmp = new JObject();
            joTmp.Add("HospitalDr", Global.inf.hospitalDr);
            joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
            joTmp.Add("AdmID", JsonHelper.getDestValue(joHisAdmInfo, "admdr"));
            joTmp.Add("PatientName", JsonHelper.getDestValue(joHisAdmInfo, "patname"));
            joTmp.Add("PersonalNO", JsonHelper.getDestValue(joRegisterInfo, "psn_no"));
            joTmp.Add("InsuRegID", JsonHelper.getDestValue(joHisAdmInfo, "insu_reg_id"));
            //joTmp.Add("InsuRegID", "510100CD000096568181");
            joTmp.Add("RegDate", JsonHelper.getDestValue(joRegisterInfo, "begntime"));
            joTmp.Add("InsuType", JsonHelper.getDestValue(joRegisterInfo, "insutype"));
            joTmp.Add("CertificateType", JsonHelper.getDestValue(joRegisterInfo, "mdtrt_cert_type"));
            joTmp.Add("CertificateNO", JsonHelper.getDestValue(joRegisterInfo, "mdtrt_cert_no"));
            joTmp.Add("MedicalType", JsonHelper.getDestValue(joRegisterInfo, "med_type"));
            joTmp.Add("AttendDoctorNO", JsonHelper.getDestValue(joRegisterInfo, "atddr_no"));
            joTmp.Add("ChiefPhyDocName", JsonHelper.getDestValue(joRegisterInfo, "chfpdr_name"));
            joTmp.Add("AdmitDiagDesc", JsonHelper.getDestValue(joRegisterInfo, "adm_diag_dscr"));
            joTmp.Add("AdmitDepartmentCode", JsonHelper.getDestValue(joRegisterInfo, "adm_dept_codg"));
            joTmp.Add("AdmitDepartmentName", JsonHelper.getDestValue(joRegisterInfo, "adm_dept_name"));
            joTmp.Add("AdmBed", JsonHelper.getDestValue(joRegisterInfo, "adm_bed"));
            joTmp.Add("MainDiagCode", JsonHelper.getDestValue(joRegisterInfo, "dscg_maindiag_code"));
            joTmp.Add("MainDiagName", JsonHelper.getDestValue(joRegisterInfo, "dscg_maindiag_name"));
            joTmp.Add("MainConditionDdesc", JsonHelper.getDestValue(joRegisterInfo, "main_cond_dscr"));
            joTmp.Add("DiseasecCode", JsonHelper.getDestValue(joRegisterInfo, "dise_codg"));
            joTmp.Add("DiseasecName", JsonHelper.getDestValue(joRegisterInfo, "dise_name"));
            joTmp.Add("OperationCode", JsonHelper.getDestValue(joRegisterInfo, "oprn_oprt_code"));
            joTmp.Add("OperationName", JsonHelper.getDestValue(joRegisterInfo, "oprn_oprt_name"));
            joTmp.Add("DiseasecTypeCode", JsonHelper.getDestValue(joRegisterInfo, "dise_type_code"));
            joTmp.Add("InsuranceAreaCode", Global.pat.insuplc_admdvs);
            joTmp.Add("TreatmentAreaCode", Global.pat.mdtrtarea_admvs);
            joTmp.Add("RegState", JsonHelper.getDestValue(joHisAdmInfo, "regstate"));
            joTmp.Add("ValidFlag", JsonHelper.getDestValue(joHisAdmInfo, "validflag"));
            joTmp.Add("Type", JsonHelper.getDestValue(joHisAdmInfo, "type"));
            joTmp.Add("updateUserID", JsonHelper.getDestValue(joHisAdmInfo, "updateUserID"));
            joTmp.Add("MSGID", JsonHelper.getDestValue(joHisAdmInfo, "msgid"));
            joTmp.Add("OccurTime", JsonHelper.getDestValue(joRegisterInfo, "OccurTime"));

            InvokeHelper invoker = new InvokeHelper();
            string serviceCode = "09010044", serviceName = "新增登记信息";
            string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
            return invoker.invokeInsuService(inpar, serviceName);
        }

        /// <summary>
        /// 撤销登记（云医保平台）
        /// </summary>
        /// <param name="joRegisterInfo"></param>
        /// <returns></returns>
        public JObject cancleRegisterInfo(JObject joRegisterInfo)
        {
            JObject joTmp = new JObject();
            joTmp.Add("HospitalDr", Global.inf.hospitalDr);
            joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
            joTmp.Add("AdmID", JsonHelper.getDestValue(joRegisterInfo, "admID"));
            joTmp.Add("PersonalNO", JsonHelper.getDestValue(joRegisterInfo, "PersonalNO"));
            joTmp.Add("InsuRegID", JsonHelper.getDestValue(joRegisterInfo, "InsuRegID"));
            joTmp.Add("CancleRegDate", JsonHelper.getDestValue(joRegisterInfo, "CancleRegDate"));
            joTmp.Add("Type", JsonHelper.getDestValue(joRegisterInfo, "Type"));
            joTmp.Add("ValidFlag", -1);
            joTmp.Add("updateUserID", Global.user.ID);
            InvokeHelper invoker = new InvokeHelper();
            string serviceCode = "09010044", serviceName = "取消登记信息";
            string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
            return invoker.invokeInsuService(inpar, serviceName);
        }

        /// <summary>
        /// 撤销结算（云医保平台）
        /// </summary>
        /// <param name="joRegisterInfo"></param>
        /// <returns></returns>
        public JObject cancleSettlement(JObject joRegisterInfo)
        {
            JObject joTmp = new JObject();
            joTmp.Add("HospitalDr", Global.inf.hospitalDr);
            joTmp.Add("InterfaceDr", Global.inf.interfaceDr);
            joTmp.Add("admID", JsonHelper.getDestValue(joRegisterInfo, "admID"));
            joTmp.Add("mdtrt_id", JsonHelper.getDestValue(joRegisterInfo, "mdtrt_id"));
            joTmp.Add("setl_id", JsonHelper.getDestValue(joRegisterInfo, "setl_id"));
            joTmp.Add("updateUserID", Global.user.ID);
            joTmp.Add("msgid", Global.curEvt.msgid);
            InvokeHelper invoker = new InvokeHelper();
            string serviceCode = "09010052", serviceName = "取消结算信息";
            string inpar = JsonHelper.setIrisInpar(serviceCode, joTmp).ToString();
            return invoker.invokeInsuService(inpar, serviceName);
        }

        /// <summary>
        /// 调用云HIS 服务
        /// </summary>
        /// <param name="joRegisterInfo"></param>
        /// <param name="jaSession"></param>
        /// <returns></returns>
        public JObject sendRegisterInfoToHis(JObject joRegisterInfo, JArray jaSession)
        {

            InvokeHelper invoker = new InvokeHelper();
            string serviceCode = "05110002", serviceName = "将登记信息返回给HIS";
            string inpar = JsonHelper.setIrisInpar(serviceCode, joRegisterInfo).ToString();
            return invoker.invokeHISService(inpar, serviceName);
        }
    }
}
