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
{
    class MedDirDownloadProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// 自动下载医保目录，直到最新版本（返回报错）
        /// </summary>
        /// <param name="directoryType"></param>
        /// <param name="uiProcessBar"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public int AutoDownload(int directoryType, Sunny.UI.UIProcessBar uiProcessBar, out string errorMessage)
        {
            int iResult = 0;
            errorMessage = "";
            try
            {

                while (iResult == 0)
                {
                    JObject joMaxVerNO = mIS.getDirectoryMaxVersionNO(directoryType);
                    string ver = joMaxVerNO["result"]["MaxVersionNO"].ToString();
                    iResult = JsonHelper.parseIrisRtnValue(joMaxVerNO, out errorMessage);
                    iResult = SingleDownload(ver, directoryType, uiProcessBar, out errorMessage);
                }

                return iResult;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return -1;
            }
        }


        /// <summary>
        /// 医保目录单个版本号的下载
        /// </summary>
        /// <param name="ver"></param>
        /// <param name="directoryType"></param>
        /// <param name="uiProcessBar"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public int SingleDownload(string ver, int directoryType, Sunny.UI.UIProcessBar uiProcessBar, out string errorMessage)
        {

            string txtPath = string.Empty;
            errorMessage = "";
            int iResult = -1;
            TradeEnum trade = TradeEnum.DictionaryDownload;
            switch (directoryType)
            {
                case 0://药品
                    {
                        trade = TradeEnum.CMDirectory;   //"1301";
                        break;
                    }
                case 1://诊疗
                    {
                        trade = TradeEnum.MedicalServiceDirectory;
                        break;
                    }
                case 2://材料
                    {
                        trade = TradeEnum.MaterialDirectory;
                        break;
                    }
                case 3://疾病诊断
                    {
                        trade = TradeEnum.DiseaseDiagDirectory;
                        break;
                    }
                case 4://手术
                    {
                        trade = TradeEnum.OperationDirectory;
                        break;
                    }
                case 5://慢性病
                    {
                        trade = TradeEnum.OPSDiseaseDirectory;
                        break;
                    }
                case 6://DRGs
                    {
                        trade = TradeEnum.DRGDiseaseDirectory;
                        break; ;
                    }
                case 7://日间手术
                    {
                        trade = TradeEnum.DayOperationDiseaseDirectory;
                        break;
                    }
                case 8://字典表
                    {
                        break;
                    }

                case 9://中药饮片
                    {
                        trade = TradeEnum.CMPiecesDirectory;
                        break;
                    }
                case 10://医药机构制剂
                    {
                        trade = TradeEnum.MedOrgSelfDrug;
                        break;
                    }
            }
            try
            {
                JObject joRtn = hBus.DownloadDirectory(trade, ver);

                //MessageBox.Show("往IRIS插入数据准备：" + joRtn.ToString());

                //if (JsonHelper.parseCenterRtnValue(joRtn, out errorMessage) == 0)
                if (JsonHelper.parseIrisRtnValue(joRtn, out errorMessage) == 0)
                {
                    txtPath = joRtn["filePath"].ToString();

                    //MessageBox.Show("获取txt数据往IRIS插入文件路径："+ txtPath);

                    DataImoport bus = new DataImoport();
                    JObject joImportRtn = bus.importDataToIrisByTxt(txtPath, 50, directoryType + 1, uiProcessBar);

                    //MessageBox.Show("往IRIS插入数据：" + joImportRtn.ToString());

                    //if (JsonHelper.parseCenterRtnValue(joImportRtn, out errorMessage) == 0)
                    if (JsonHelper.parseIrisRtnValue(joImportRtn, out errorMessage) == 0)
                    {
                        iResult = 0;
                    }
                    else
                    {
                        Global.writeLog(joImportRtn.ToString());
                        errorMessage = "导入数据出现错误，请查看日志！";//joImportRtn["errorMessage"].ToString();
                    }
                }
                return iResult;
            }
            catch (Exception ex)
            {
                errorMessage = "SingleDownload提示:" + ex.Message;
                return -1;
            }
        }

    }
}
