/******************************************************************************
 * 文件名称: DataImoport.cs
 * 文件说明: 医保目录数据导入
 * 当前版本: V1.0
 * 创建日期: 2022-04-12
 *
 * 2020-04-12: 增加GetDrugsJsonByTxt方法
 * 2020-04-12: 增加GetMedicalServiceJsonByTxt方法
 * 2020-04-12: 增加 GetMaterialJsonByTxt 方法
 * 2020-04-14: 增加 GetDiagnoseJsonByTxt 方法
 * 2020-04-14: 增加 GetOperationJsonByTxt 方法
 * 2020-04-14: 增加 applyDataToIris 方法
 * 2020-04-14: 增加 importDataToIrisByTxt 方法
 * 2020-04-14: 增加 GetOperationJsonByTxt 方法
 * 2020-04-14: 增加 GetOperationJsonByTxt 方法
 * 2020-04-14: 增加 GetOperationJsonByTxt 方法
 * 2020-04-14: 增加 GetOperationJsonByTxt 方法


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

namespace PTMedicalInsurance
{
    class DataImoport
    {
        MIIrisServices mis = new MIIrisServices();
        
        /// <summary>
        /// 读取TXT后，Array中的空值会返回NULL，将其转换为"".应有其他方式处理，后面再优化
        /// 明天试试Convert.ToString(a)
        /// </summary>
        /// <param name="arrayValue"></param>
        /// <returns></returns>
        private string  convertArrayNull(string arrayValue)
        {
            string returnValue = string.Empty;
            if (arrayValue == "null")
            {
                returnValue = "";
            }
            else
            {
                returnValue = arrayValue;
            }
            return returnValue;
        }
        /// <summary>
        /// 从TXT文件中获取药品数据，并组装成JSON
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetDrugsJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[7];
            jsonTemp.HisType = "1";
            jsonTemp.HisTypeName = "药品";
            jsonTemp.LocateCode = "";
            jsonTemp.LocateName = "";
            jsonTemp.DosageFormCode = arr[9];
            jsonTemp.DosageFormName = arr[10];
            jsonTemp.CategoryCode = arr[11];
            jsonTemp.CategoryName = arr[12];
            jsonTemp.Specification = arr[13];
            jsonTemp.SpecificationCode = arr[14];
            jsonTemp.UnitOfPackag = arr[42];
            jsonTemp.UnitOfValuation = arr[40];
            jsonTemp.StartDate = convertArrayNull(arr[36]);
            jsonTemp.EndDate = convertArrayNull(arr[37]);
            jsonTemp.PinyinSearchCode = arr[50];
            jsonTemp.Instructions = arr[35];
            jsonTemp.ExceptContent = "";
            jsonTemp.Connotation = "";
            jsonTemp.ValidFlag = convertArrayNull(arr[78]);
            jsonTemp.Note = arr[77];
            jsonTemp.VersionNO = arr[82];
            jsonTemp.VersionName = arr[83];
            jsonTemp.UseFlag = convertArrayNull(arr[78]);
            jsonTemp.DrugSafetyStandardCode = arr[8];
            jsonTemp.ApprovalNO = arr[61];
            jsonTemp.SpecialFlag = convertArrayNull(arr[55]);
            jsonTemp.LimitFlag = convertArrayNull(arr[57]);
            jsonTemp.LimitRange = arr[56];
            jsonTemp.UniqueRecordNO = arr[79];
            jsonTemp.Manufacturers = arr[53];
            jsonTemp.Company = arr[85];

            jsonTemp.ChargeItemLevelName = arr[91];

            if (jsonTemp.ChargeItemLevelName == "甲")
            {
                jsonTemp.ChargeItemLevel = "01";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevelName == "乙")
            {
                jsonTemp.ChargeItemLevel = "02";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevelName == "丙")
            {
                jsonTemp.ChargeItemLevel = "03";
                jsonTemp.SelfPercent = "100";
            }
            else
            {
                jsonTemp.ChargeItemLevel = "04";
                jsonTemp.SelfPercent = "100";
                //jsonTemp.ChargeItemLevelName = "未知";
            }
            

            jsonTemp.MinDosageUnit = arr[41];  //最小包装数量
            jsonTemp.MinDosageUnit = arr[45];  //最小制剂单位
            jsonTemp.MinDosageUnit = arr[44];  //最小包装单位
      
            return jsonTemp;
        }

        /// <summary>
        /// 从TXT文件中获取诊疗数据，并组装成JSON
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetMedicalServiceJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[9];
            jsonTemp.HisType = "2";
            jsonTemp.HisTypeName = "诊疗";
            jsonTemp.LocateCode = "";
            jsonTemp.LocateName = "";
            jsonTemp.DosageFormCode = arr[9];
            jsonTemp.DosageFormName = arr[10];
            jsonTemp.CategoryCode = arr[8];
            jsonTemp.CategoryName = arr[12];
            jsonTemp.Specification = "";
            jsonTemp.SpecificationCode = "";
            jsonTemp.UnitOfPackag = arr[1];
            jsonTemp.UnitOfValuation = arr[2];
            //jsonTemp.StartDate = convertArrayNull(arr[11]);
            //jsonTemp.EndDate = convertArrayNull(arr[12]);
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[9]);
            jsonTemp.Instructions = arr[3];
            jsonTemp.ExceptContent = arr[4];
            jsonTemp.Connotation = arr[5];
            jsonTemp.ValidFlag = arr[6];
            jsonTemp.Note = ""; //arr[7];
            jsonTemp.VersionNO = arr[14];
            jsonTemp.VersionName = arr[15];
            jsonTemp.UseFlag = arr[6];
            jsonTemp.DrugSafetyStandardCode = "";
            jsonTemp.ApprovalNO = "";
            jsonTemp.SpecialFlag = "";
            jsonTemp.LimitFlag = "";
            jsonTemp.LimitRange = "";
            jsonTemp.UniqueRecordNO = arr[13];

            jsonTemp.ChargeItemLevel = arr[0];

            if (jsonTemp.ChargeItemLevel == "1")
            {
                jsonTemp.ChargeItemLevelName = "甲";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevel == "2")
            {
                jsonTemp.ChargeItemLevelName = "乙";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevel == "3")
            {
                jsonTemp.ChargeItemLevelName = "丙";
                jsonTemp.SelfPercent = "100";
            }
            else
            {
                //jsonTemp.ChargeItemLevel = "4";
                jsonTemp.SelfPercent = "100";
                jsonTemp.ChargeItemLevelName = "未知";
            }

            return jsonTemp;
        }

        /// <summary>
        /// 从TXT文件中获取材料数据，并组装成JSON
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetMaterialJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[1];
            jsonTemp.HisType = "3";
            jsonTemp.HisTypeName = "材料";
            jsonTemp.LocateCode = "";
            jsonTemp.LocateName = "";
            jsonTemp.DosageFormCode = "";
            jsonTemp.DosageFormName = "";
            jsonTemp.CategoryCode = "";
            jsonTemp.CategoryName = "";
            jsonTemp.Specification = arr[7];
            jsonTemp.SpecificationCode = arr[6];
            jsonTemp.UnitOfPackag = arr[15];
            jsonTemp.UnitOfValuation = convertArrayNull(arr[35]);
            jsonTemp.StartDate = convertArrayNull(arr[46]);
            jsonTemp.EndDate = convertArrayNull(arr[47]);
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[5]);
            jsonTemp.Instructions = arr[27];
            jsonTemp.ExceptContent = arr[63]; //生产国
            jsonTemp.Connotation = "";
            jsonTemp.ValidFlag = arr[67];
            jsonTemp.Note = arr[7];
            jsonTemp.VersionNO = arr[69];
            jsonTemp.VersionName = arr[70];
            jsonTemp.UseFlag = arr[67];
            jsonTemp.DrugSafetyStandardCode = "";
            jsonTemp.ApprovalNO = arr[50]; //注册备案号
            jsonTemp.SpecialFlag = "";
            jsonTemp.LimitFlag = convertArrayNull(arr[33]);
            jsonTemp.LimitRange = arr[34];
            jsonTemp.UniqueRecordNO = arr[68];
            jsonTemp.Manufacturers = arr[59];

            jsonTemp.ChargeItemLevelName = arr[37];

            if (jsonTemp.ChargeItemLevelName == "甲")
            {
                jsonTemp.ChargeItemLevel = "01";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevelName == "乙")
            {
                jsonTemp.ChargeItemLevel = "02";
                jsonTemp.SelfPercent = "0";
            }
            else if (jsonTemp.ChargeItemLevelName == "丙")
            {
                jsonTemp.ChargeItemLevel = "03";
                jsonTemp.SelfPercent = "100";
            }
            else
            {
                jsonTemp.ChargeItemLevel = "04";
                jsonTemp.SelfPercent = "100";
                //jsonTemp.ChargeItemLevelName = "未知";
            }

            return jsonTemp;
        }

        /// <summary>
        /// 从TXT文件中获取诊断数据，并组装成JSON
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetDiagnoseJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[10];
            jsonTemp.Name = arr[11];
            jsonTemp.HisType = "4";
            jsonTemp.HisTypeName = "诊断";
            jsonTemp.LocateCode = "";
            jsonTemp.LocateName = "";
            jsonTemp.DosageFormCode = "";
            jsonTemp.DosageFormName = "";
            jsonTemp.CategoryCode = "";
            jsonTemp.CategoryName = "";
            jsonTemp.Specification = "";
            jsonTemp.SpecificationCode = "";
            jsonTemp.UnitOfPackag = "";
            jsonTemp.UnitOfValuation = "";
            jsonTemp.StartDate = "";
            jsonTemp.EndDate = "";
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.Instructions = "";
            jsonTemp.ExceptContent = "";
            jsonTemp.Connotation = "";
            jsonTemp.ValidFlag = arr[18];
            jsonTemp.Note = arr[17];
            jsonTemp.VersionNO = arr[22];
            jsonTemp.VersionName = arr[23];
            jsonTemp.UseFlag = arr[18];
            jsonTemp.DrugSafetyStandardCode = "";
            jsonTemp.ApprovalNO = "";
            jsonTemp.SpecialFlag = "";
            jsonTemp.LimitFlag = "";
            jsonTemp.LimitRange = "";
            jsonTemp.UniqueRecordNO = arr[19];
            jsonTemp.Manufacturers = "";
            return jsonTemp;
        }

        /// <summary>
        /// 慢性病
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetChronicDiseaseJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[1];
            jsonTemp.HisType = "6";
            jsonTemp.HisTypeName = "慢性病";
            jsonTemp.LocateCode = "";
            jsonTemp.LocateName = "";
            jsonTemp.DosageFormCode = "";
            jsonTemp.DosageFormName = "";
            jsonTemp.CategoryCode = arr[14];
            jsonTemp.CategoryName = arr[15];
            jsonTemp.Specification = "";
            jsonTemp.SpecificationCode = "";
            jsonTemp.UnitOfPackag = "";
            jsonTemp.UnitOfValuation = "";
            jsonTemp.StartDate = "";
            jsonTemp.EndDate = "";
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[2]);
            jsonTemp.Instructions = "";
            jsonTemp.ExceptContent = "";
            jsonTemp.Connotation = arr[10];
            jsonTemp.ValidFlag = arr[5];
            jsonTemp.Note = arr[4];
            jsonTemp.VersionNO = arr[9];
            jsonTemp.VersionName = arr[11];
            jsonTemp.UseFlag = arr[5];
            jsonTemp.DrugSafetyStandardCode = "";
            jsonTemp.ApprovalNO = "";
            jsonTemp.SpecialFlag = "";
            jsonTemp.LimitFlag = "";
            jsonTemp.LimitRange = "";
            jsonTemp.UniqueRecordNO = arr[6];
            jsonTemp.Manufacturers = "";
            return jsonTemp;
        }

        /// <summary>
        /// 从TXT文件中获取手术数据，并组装成JSON
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetOperationJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[10];
            jsonTemp.Name = arr[11];
            jsonTemp.HisType = "5";
            jsonTemp.HisTypeName = "手术";

            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.ValidFlag = arr[18];
            jsonTemp.Note = arr[17];
            jsonTemp.VersionNO = arr[22];
            jsonTemp.VersionName = arr[23];
            jsonTemp.UseFlag = arr[18];
            jsonTemp.UniqueRecordNO = arr[19];
            return jsonTemp;
        }

        /// <summary>
        /// DRGs
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetDRGsJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[1];
            jsonTemp.Name = arr[2];
            jsonTemp.HisType = "7";
            jsonTemp.HisTypeName = "DRGs";
            jsonTemp.Connotation = arr[10];

            jsonTemp.Instructions = arr[3];     //适用手术操作代码
            jsonTemp.ExceptContent = arr[4];    //适用手术操作名称

            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.ValidFlag = arr[5];
            jsonTemp.Note = arr[11];
            jsonTemp.VersionNO = arr[9];
            jsonTemp.VersionName = arr[12];
            jsonTemp.UseFlag = arr[5];
            jsonTemp.UniqueRecordNO = arr[6];
            return jsonTemp;
        }

        private JObject GetDaySurgeryJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[1];
            jsonTemp.Name = arr[2];
            jsonTemp.HisType = "8";
            jsonTemp.HisTypeName = "日间手术";
            jsonTemp.Connotation = arr[8];
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.ValidFlag = arr[3];
            jsonTemp.Note = arr[9];
            jsonTemp.VersionNO = arr[7];
            jsonTemp.VersionName = arr[10];
            jsonTemp.UseFlag = arr[3];
            jsonTemp.UniqueRecordNO = arr[4];
            return jsonTemp;
        }

        /// <summary>
        /// 中药饮片
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetChineseDrugJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[1];
            jsonTemp.HisType = "10";
            jsonTemp.HisTypeName = "中药饮片";
            jsonTemp.Connotation = arr[8];
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.ValidFlag = arr[3];
            jsonTemp.Note = arr[9];
            jsonTemp.VersionNO = arr[7];
            jsonTemp.VersionName = arr[10];
            jsonTemp.UseFlag = arr[3];
            jsonTemp.UniqueRecordNO = arr[4];
            return jsonTemp;
        }

        /// <summary>
        /// 医疗机构制剂
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        private JObject GetHosPreparationJsonByTxt(string[] arr)
        {
            dynamic jsonTemp = new JObject();
            jsonTemp.ID = "";
            jsonTemp.updateUserID = Global.user.ID;
            jsonTemp.HospitalDr = Global.inf.hospitalDr;
            jsonTemp.InterfaceDr = Global.inf.interfaceDr;
            jsonTemp.Code = arr[0];
            jsonTemp.Name = arr[1];
            jsonTemp.HisType = "11";
            jsonTemp.HisTypeName = "医疗机构制剂";
            jsonTemp.Connotation = arr[8];
            //jsonTemp.PinyinSearchCode = SpellCodeHelper.GetFirstPYLetter(arr[11]);
            jsonTemp.ValidFlag = arr[64];
            jsonTemp.Note = arr[9];
            jsonTemp.VersionNO = arr[70];
            jsonTemp.VersionName = arr[71];
            jsonTemp.UseFlag = arr[64];
            jsonTemp.UniqueRecordNO = arr[46]; //药品注册证号
            jsonTemp.Instructions = arr[77];

            jsonTemp.NationalCode = arr[57]; //国家医保药品目录编码
            jsonTemp.NationalName = arr[58]; //国家医保药品目录名称

            return jsonTemp;
        }


        /// <summary>
        /// 提交数据给IRIS
        /// </summary>
        /// <param name="code"></param>
        /// <param name="jaParam"></param>
        /// <returns></returns>
        private JObject applyDataToIris(string code, JArray jaParam)
        {
            JObject joIn = new JObject();
            JObject joRtn = new JObject();
            string outParam = "";
            try
            {
                
                joIn.Add(new JProperty("params", jaParam));
                joIn.Add("code", code);
                joIn.Add("updateUserID", Global.user.ID);
                InvokeHelper invoker = new InvokeHelper();
                string sInput = joIn.ToString();
                joRtn = invoker.invokeInsuService(sInput, "applyDataToIris");
                outParam = joRtn.ToString();
                return joRtn;
            }
            catch (Exception ex)
            {
                joRtn = JsonHelper.setExceptionJson(-1, "applyDataToIris", ex.Message);
                outParam = joRtn.ToString();
                return joRtn;
            }           
        }
        /// <summary>
        /// 从TXT里导数据到IRIS
        /// </summary>
        /// <param name="txtPath"></param>
        /// <param name="singleRows"></param>
        /// <param name="direcType"></param>
        /// <param name="processBar"></param>
        /// <returns></returns>
        public JObject importDataToIrisByTxt(string txtPath, int size, int direcType, Sunny.UI.UIProcessBar processBar)
        {
            int errorCount = 0;
            string errorMsg = "";
            try
            {
                /*据说速度更快 
                 * https://www.cnblogs.com/eniac12/p/4398310.html
                 * using (StreamReader sr = File.OpenText(fileName))
                {
                        string s = String.Empty;
                        while ((s = sr.ReadLine()) != null)
                        {
                               //do what you have to here
                        }
                }

                Convert.ToString(a)
                */

                string[] lines = System.IO.File.ReadAllLines(txtPath);
                dynamic jaParams = new JArray();
                int rows = lines.Length;
                processBar.Maximum = rows;
                for (int i = 0; i < rows; i++)
                {
                    processBar.Value = i + 1;
                    Application.DoEvents();

                    string lineTmp = lines[i];

                    string[] arr = lineTmp.Split('\t');
                    //判断是哪种目录类型
                    string code = string.Empty;
                    switch (direcType)
                    {
                        //药品
                        case 1:
                            {
                                jaParams.Add(GetDrugsJsonByTxt(arr));
                                break;
                            }
                        //诊疗
                        case 2:
                            {
                                jaParams.Add(GetMedicalServiceJsonByTxt(arr));
                                break;
                            }
                        //材料
                        case 3:
                            {
                                jaParams.Add(GetMaterialJsonByTxt(arr));
                                break;
                            }
                        //诊断
                        case 4:
                            {
                                jaParams.Add(GetDiagnoseJsonByTxt(arr));
                                break;
                            }
                        //手术
                        case 5:
                            {
                                jaParams.Add(GetOperationJsonByTxt(arr));
                                break;
                            }
                        //慢性病
                        case 6:
                            {
                                jaParams.Add(GetChronicDiseaseJsonByTxt(arr));
                                break;
                            }
                        //DRGs
                        case 7:
                            {
                                jaParams.Add(GetDRGsJsonByTxt(arr));
                                break;
                            }

                        //日间手术
                        case 8:
                            {
                                jaParams.Add(GetDaySurgeryJsonByTxt(arr));
                                break;
                            }

                        //中药饮片
                        case 10:
                            {
                                jaParams.Add(GetChineseDrugJsonByTxt(arr));
                                break;
                            }
                        //医疗机构制剂
                        case 11:
                            {
                                jaParams.Add(GetHosPreparationJsonByTxt(arr));
                                break;
                            }


                    }

                    code = "09010035";
                    //每50行提交一次给后台
                    int currentRows = i + 1;
                    if ((currentRows % size == 0) && (i != 0))
                    {
                        JObject joRtn = applyDataToIris(code, jaParams);
                        if (joRtn["errorCode"].ToString() != "0")
                        {
                            errorCount = errorCount + 1;
                            errorMsg = "\r\n" +errorMsg + joRtn["errorMessage"].ToString();
                        }
                        jaParams = new JArray();
                    }

                    if ((currentRows == rows) && (currentRows % size != 0))
                    {
                        JObject joRtn = applyDataToIris(code, jaParams);
                        if (joRtn["errorCode"].ToString() != "0")
                        {
                            errorCount = errorCount + 1;
                            errorMsg = "\r\n" + errorMsg + joRtn["errorMessage"].ToString();
                        }
                        jaParams = new JArray();
                    }
                   
                }
                return JsonHelper.setExceptionJson(0 - errorCount, errorMsg, null);
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "importDataToIrisByTxt", ex.Message);
            }
            
        }

        public JObject importDictionary(Sunny.UI.UIProcessBar uiProcessBar,JArray jaList,string type)
        {
            int errorCount = 0;
            string errorMsg = "",outParam = "", HBDictionaryDr ="";

            

            try
            {

                //插入并获取字典主表的DR
                JObject joData = new JObject();
                joData.Add("HospitalDr", Global.inf.hospitalDr);
                joData.Add("InterfaceDr", Global.inf.interfaceDr);
                joData.Add("InsuCode", type);
                JObject joRtn = mis.insertDictionary(joData);
                if (JsonHelper.parseIrisRtnValue(joRtn, out errorMsg) != 0)
                {
                    return JsonHelper.setExceptionJson(-1, "insertDictionary", errorMsg);
                }
                else
                {
                    HBDictionaryDr = JsonHelper.getDestValue(joRtn, "data.HBDictionaryDr");
                    if (HBDictionaryDr == "")
                    {
                        return JsonHelper.setExceptionJson(-1, "insertDictionary", "HBDictionaryDr返回值为空");
                    }
                }
            }
            catch (Exception ex)
            {
                return JsonHelper.setExceptionJson(-1, "insertDictionary", ex.Message);
            }

            

            JObject joReturn = new JObject();
            try
            {
                //插入字典明细表
                JArray jaParams = new JArray();
                int rows = jaList.Count ;
                uiProcessBar.Maximum = rows;
                for (int i = 0; i < jaList.Count ; i++)
                {
                    uiProcessBar.Value = i + 1;
                    dynamic joTmp = new JObject();
                    joTmp.ID = "";
                    joTmp.updateUserID = Global.user.ID;
                    joTmp.HospitalDr = Global.inf.hospitalDr;
                    joTmp.InterfaceDr = Global.inf.interfaceDr;
                    joTmp.HBDictionaryDr = HBDictionaryDr;
                    joTmp.Code = jaList[i]["value"].ToString();
                    joTmp.Descripts = jaList[i]["label"].ToString();
                    jaParams.Add(joTmp);
               
                    //每50行提交一次给后台
                    int currentRows = i + 1;
                    if ((currentRows % 50 == 0) && (i != 0))
                    {
                        JObject joRtn = mis.insertDictionaryDataDetail(jaParams);
                        if (joRtn["errorCode"].ToString() != "0")
                        {
                            errorCount = errorCount + 1;
                            errorMsg = "\r\n" + errorMsg + joRtn["errorMessage"].ToString();
                        }
                        jaParams = new JArray();
                    }

                    if ((currentRows == rows) && (currentRows % 50 != 0))
                    {
                        JObject joRtn = mis.insertDictionaryDataDetail(jaParams);
                        if (joRtn["errorCode"].ToString() != "0")
                        {
                            errorCount = errorCount + 1;
                            errorMsg = "\r\n" + errorMsg + joRtn["errorMessage"].ToString();
                        }
                        jaParams = new JArray();
                    }
                }
                joReturn = JsonHelper.setExceptionJson(0 - errorCount, errorMsg, null);
                outParam = joReturn.ToString();
                return joReturn;
            }
            catch (Exception ex)
            {
                joReturn = JsonHelper.setExceptionJson(-1, "importDictinary", ex.Message);
                outParam = joReturn.ToString();
                return joReturn;
            }
        }

    }


}
