using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace HH.YiDaSyncNC.Helper
{
    class DBHelper
    {
        #region 写日志
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="sLog">内容</param>
        /// <param name="sOption">标题</param>
        /// <param name="sSrc">路径</param>
        public static void WriteLog(string sLog, string sOption = "OP", string sSrc = "/Logs/")
        {
            var now = DateTime.Now;
            StreamWriter sr = null;

            var filePath = AppDomain.CurrentDomain.BaseDirectory + sSrc; // 文件路径
            var file = filePath + "Log_" + DateTime.Now.ToString("yyyy_MM_dd") + ".log"; // 文件

            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath); // 判断是否存在目录,不存在则创建

            if (!File.Exists(file))
            {
                sr = File.CreateText(file);//创建日志文件
            }
            else
            {
                sr = File.AppendText(file);//追加日志文件
            }
            sr.WriteLine($"{DateTime.Now}:【{sOption}】:{sLog}");//日志格式
            if (sr != null) sr.Close();     //关闭文件流
        }
        #endregion

        #region 生成报文数据方法
        /// <summary>
        /// 生成报文数据方法
        /// </summary>
        public static XmlElement ToXmlElementMethod(XElement xElement)
        {
            if (xElement == null) return null;

            XmlElement xmlElement = null;
            XmlReader xmlReader = null;
            try
            {
                xmlReader = xElement.CreateReader();
                var doc = new XmlDocument();
                xmlElement = doc.ReadNode(xElement.CreateReader()) as XmlElement;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (xmlReader != null) xmlReader.Close();
            }

            return xmlElement;
        }
        #endregion

        #region 调用传输凭证接口
        /// <summary>
        /// 调用传输凭证接口
        /// </summary>
        /// <param name="xmlstring">xml字符串参数</param>
        /// <param name="url">接口地址</param>
        /// <returns></returns>
        public static string SendNsg(string xmlstring, string url)
        {
            string responseMsg = "";
            try
            {
                var options = new RestClientOptions(url)
                {
                    MaxTimeout = -1,
                };
                var client = new RestClient(options);
                var request = new RestRequest(url, Method.Post);
                request.AddHeader("Content-Type", "application/xml");
                var body = xmlstring;
                request.AddParameter("application/xml", body, ParameterType.RequestBody);
                RestResponse response = client.Execute(request);
                responseMsg = response.Content;//获取返回Response消息
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return responseMsg;
        }
        #endregion
    }
}
