/******************************************************************************
 * 文件名称: HisBusiness.cs
 * 文件说明: HIS业务的封装
 * 当前版本: V1.0
 * 创建日期: 2022-04-14
 *
 * 2020-04-14: 增加 SingleDownload 方法
 * 2020-04-14: 增加 AutoDownload 方法

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


namespace DirectoryDownload.Business
{
    class HisBusiness
    {
        /// <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;
            string funNo = "";
            switch (directoryType)
            {
                case 0://药品
                    {
                        funNo = "1301";
                        break;
                    }
                case 1://诊疗
                    {
                        funNo = "1305";
                        break;
                    }
                case 2://材料
                    {
                        funNo = "1306";
                        break;
                    }
                case 3://疾病诊断
                    {
                        funNo = "1307";
                        break;
                    }
                case 4://手术
                    {
                        funNo = "1308";
                        break;
                    }
                case 5://慢性病
                    {
                        funNo = "1309";
                        break;
                    }
                case 6://DRGs
                    {
                        funNo = "1310";
                        break; ;
                    }
                case 7://日间手术
                    {
                        funNo = "1311";
                        break;
                    }
                case 8://字典表
                    {
                        break;
                    }
            }
            try
            {
                CenterBusiness centerBusiness = new CenterBusiness();
                JObject joRtn = centerBusiness.DownDirecotry(funNo, ver);

                if (JsonHelper.parseBusinessJson(joRtn, out errorMessage) == 0)
                {
                    txtPath = joRtn["filePath"].ToString();
                    DataImoport bus = new DataImoport();
                    JObject joImportRtn = bus.importDataToIrisByTxt(txtPath, 50, directoryType + 1, uiProcessBar);
                    if (JsonHelper.parseBusinessJson(joImportRtn, out errorMessage) == 0)
                    {
                        iResult = 0;
                    }
                    else
                    {
                        GlobalVariables.writeLog(joImportRtn.ToString());
                        errorMessage = "导入数据出现错误，请查看日志！";//joImportRtn["errorMessage"].ToString();
                    }
                }
                return iResult;
            }
            catch (Exception ex)
            {
                errorMessage = "SingleDownload提示:" + 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 downloadDicionary(JObject joData, Sunny.UI.UIProcessBar uiProcessBar, out string errorMessage)
        {
            errorMessage = "";
            int iResult = -1;          
            try
            {
                CenterBusiness centerBusiness = new CenterBusiness();
                JObject joRtn = centerBusiness.DownDictionay(joData);

                if (JsonHelper.parseCenterJson(joRtn, out errorMessage) == 0)
                {
                    DataImoport bus = new DataImoport();
                    JArray jaList = JArray.Parse(JsonHelper.getJsonValue(joRtn,"output.list"));
                    //MessageBox.Show(jaList.ToString());
                    JObject joImportRtn = bus.importDictinary(uiProcessBar,jaList);
                    if (JsonHelper.parseBusinessJson(joImportRtn, out errorMessage) == 0)
                    {
                        iResult = 0;
                    }
                    else
                    {
                        GlobalVariables.writeLog(joImportRtn.ToString());
                        errorMessage = "导入数据出现错误，请查看日志！";
                    }
                }
                return iResult;
            }
            catch (Exception ex)
            {
                errorMessage = "downloadDicionary提示:" + ex.Message;
                return -1;
            }
        }

        /// <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)
                {
                    IrisInterfaceService irisBus = new IrisInterfaceService();
                    JObject joMaxVerNO = irisBus.getDirectoryMaxVersionNO(directoryType);
                    string ver = joMaxVerNO["result"]["MaxVersionNO"].ToString();
                    iResult = JsonHelper.parseBusinessJson(joMaxVerNO, out errorMessage);
                    iResult = SingleDownload(ver, directoryType,uiProcessBar, out errorMessage);
                }

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