using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ashermed.BaseFramework;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.Api.ContractModel.App;
using Microsoft.Extensions.Logging;
using ASHermed.SinoInterface.Api.ContractModel.Payment.Model;
using ASHermed.SinoInterface.Api.ContractModel.Payment.Param;
using LopSdk;
using Newtonsoft.Json;
using ASHermed.SinoInterface.Api.ContractModel;
using LinKingReference;
using System.Net;
using System.IO;
using System.Xml;
using static ASHermed.SinoInterface.Tools.Base.BusinessConfig;

namespace ASHermed.SingleSignOnFramework
{
    /// <summary>
	/// 联空服务服务(http://opendoc.linkingcloud.cn/#a_f40fdd3fc3e5484fb0b400ca846f2642)文档
	/// </summary>
	public class LinKingService : ILinKingService
    {
        //接口地址https://www.showdoc.com.cn/sinolinkingcloud?page_id=6699370832629289
        IHttpClientHelp _httpClient;
        AppConfigurtaion _appConfigurtaion;
        ILogger<LinKingService> _logger;
        //private readonly string linkingUrl = "http://192.168.50.40:8192/LinkingIF.asmx/";
        //private readonly string gatewayurl = "https://openapicore.linkingcloud.cn/gateway";
        //private readonly string appId = "LCOP20200816150536373";
        //private readonly string privateKey = "MIICWwIBAAKBgQCwoapOaIu0KnilAyfQDMg2u+5wvjuZsnIct/jC7LgY9nmFO85h+kLa1r2JvhZIWELgE8vsdUgBwqoCDkgeZIBfx20DMCq/PeZb0+896EW9wZtkQP0LDCppgBqsWsPZ/QIaUajX10Q8uvFM5Ew9y0hQBfaWRc5UUVcCNeOW6dw2QwIDAQABAoGAMNPwJY7HF/4prcwqhFwUOPVI3Afqp/lpjEDbVt9joRr5yvgWfIVXV/HHIb3XoRYtHo66b9qYFkx+/OR/0U8NIXdtTNwXb0oihrGsRevyjoLvzJrG444LnNlNbsRpELHlAXvr30rEy9xwcNZRE/elt2QaQLgNk75Go3ziYtaKmBECQQDeBK5hVh9qL8dJWFoPeOdrqpXDKW24W2nSL3nEPogw99JMiJPux4jYnvYsERfpPFDJp0ZszUsyMmSwgA2tR3KrAkEAy6qZQ5mN0R2mTOkprRLyGEywVEMgdUuS2TIzpmQ9I5UCDCTP7l02F5bBDdzAyLcvmz4hw6J9/v20H0xzctmKyQJABsWfyApHdQa2Dm+qd5RZgrg2TzihRyFKrgWSskCyCMRWXs0/reXaL7lRivCOiSe00yHODR2J8KMG3Tdas3FddwJAfPvSVjOzXfMODQDBPJ71xoe5ZdxT3Odl1LgEDkZRZraYrQ8iMysseMlg67re683O/zvfyIi9PXA/yz1FU+PAUQJAWPynDQTIQNViwptuxVZsQUeC1//hHiUxdh3+rmY662hQzc4KVbXuhDRhIt73DJSB1bLsJwmQuoH5MEwyyEgSBA==";
        //private readonly string publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGncH9TmDGpqBy6MhH5j4eyR+jL8R5FF2Fzp7AR3dmlO/G3KRvqOexLm8xfyydaV0oF5e+t4j+b4sIksg72VkX3+aFLF0rIPoUx80sOCYsbs4ng3ohWKc4x4xixp7TIbSe5x+eVlw9yqGULIrmmISlv0emFyJSuXYoXDzjPeKn+wIDAQAB";
        private readonly string linkingUrl = string.Empty;
        private readonly string gatewayurl = string.Empty;
        private readonly string appId = string.Empty;
        private readonly string privateKey = string.Empty;
        private readonly string publicKey = string.Empty;
        public LinKingService(IHttpClientHelp httpClient, AppConfigurtaion appConfigurtaion, ILogger<LinKingService> logger)
        {
            var linKingConfig = LinKingConfig.GetInstance();
            linkingUrl = linKingConfig.LinkingUrl;
            gatewayurl = linKingConfig.Gatewayurl;
            appId = linKingConfig.AppId;
            privateKey = linKingConfig.PrivateKey;
            publicKey = linKingConfig.PublicKey;
            _httpClient = httpClient;
            _appConfigurtaion = appConfigurtaion;
            _logger = logger;
        }

        public BaseResponse<string> LcTradeH5Pay(LcTradeH5PayParam param)
        {
            LopSdkClient lopClient = new LopSdkClient(gatewayurl, appId, privateKey, publicKey);
            string bizContent = JsonConvert.SerializeObject(param.biz_Content);
            string response_text = lopClient.InvokeMethod("lc.trade.h5.pay", bizContent);
            var response = JsonConvert.DeserializeObject<LcTradeH5PayModel>(response_text);
            var resu = new BaseResponse<string>();

            if (response != null && response.Code == "10000")
            {
                resu.Code = 1;
                resu.Msg = "成功";
                resu.Data = response.Biz_Content.Goto_Url;
            }
            else
            {
                resu.Code = 0;
                resu.Msg = "未找到数据";
            }
            return resu;
        }
        public BaseResponse<LcTradeAPPPayModel> LcTradeAPPPay(LcTradeAPPPayParam param)
        {
            LopSdkClient lopClient = new LopSdkClient(gatewayurl, appId, privateKey, publicKey);
            string bizContent = JsonConvert.SerializeObject(param.biz_Content);
            string response_text = lopClient.InvokeMethod("lc.trade.app.pay", bizContent);
            var response = JsonConvert.DeserializeObject<LcTradeAPPPayModel>(response_text);
            var resu = new BaseResponse<LcTradeAPPPayModel>();

            if (response != null && response.Code == "10000")
            {
                resu.Code = 1;
                resu.Msg = "成功";
                resu.Data = response;
            }
            else
            {
                resu.Code = 0;
                resu.Msg = "未找到数据";
            }
            return resu;
        }
        public LcTradeRefundModel LcTradeRefund(LcTradeRefundParam param)
        {
            LopSdkClient lopClient = new LopSdkClient(gatewayurl, appId, privateKey, publicKey);
            string bizContent = JsonConvert.SerializeObject(param.Biz_Content);
            string response_text = lopClient.InvokeMethod("lc.trade.refund", bizContent);
            var response = JsonConvert.DeserializeObject<LcTradeRefundModel>(response_text);
            return response;
        }

        /// <summary>
        /// 联空预约接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public ReVisitReb IFGetReVisitComfirmReservation(ReVisitComfirmParam param)
        {

            return IF_Get_ReVisit_Comfirm_Reservation(param);

        }
        /// <summary>
        /// 联空更新预约接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public UpdateReVisitReb IFUpdateReVisitStatus(UpdateReVisitReq param)
        {
            _logger.LogInformation("IFUpdateReVisitStatus:" + param.ToJson());
            return IF_Update_ReVisit_Status(param);
        }
        /// <summary>
        /// 联空药品获取接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public DrugReb IFDrugGet(DrugReq param)
        {
            return IF_Drug_Get(param);
        }
        /// <summary>
        /// 联空药品相关字典接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public DrugDictionaryReb IFDrugDictionary(DrugDictionaryReq param)
        {
            return IF_Drug_Dictionary(param);
        }

        /// <summary>
        /// 处方物流推送
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PrescriptionOrderReb IFConfirmPrescriptionOrder(PrescriptionOrderReq param)
        {
            _logger.LogInformation("IFConfirmPrescriptionOrder Request:" + JsonConvert.SerializeObject(param));
            var res = IF_Confirm_PrescriptionOrder(param);
            _logger.LogInformation("IFConfirmPrescriptionOrder Response:" + JsonConvert.SerializeObject(res));
            return res;
        }


        /// <summary>
        /// 获取IM链接
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public string IFIMTokenGet(string orderNo, int language)
        {
            return IF_IM_TokenGet(orderNo, language);
        }

        public string IFIMEvaluateGet(string orderNo)
        {
            return IF_IM_EvaluateGet(orderNo);
        }


        /// <summary>
        /// 联空预约接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private ReVisitReb IF_Get_ReVisit_Comfirm_Reservation(ReVisitComfirmParam param)
        {
            ReVisitReb res = new ReVisitReb();
            try
            {
                XmlDocument resXml = new XmlDocument();
                #region xml
                string xml = @"xml=<xml>
   <head>
       <AccessToken>" + param.AccessToken + @"</AccessToken>
       <OpenUserID>" + param.OpenUserID + @"</OpenUserID>
       <CardNo>" + param.CardNo + @"</CardNo>
       <Tel>" + param.Tel + @"</Tel>
       <PatientID>" + param.PatientID + @"</PatientID>
       <CardType>" + param.CardType + @"</CardType>
       <PatientName>" + param.PatientName + @"</PatientName>
       <IDCard>" + param.IDCard + @"</IDCard>
       <CertType>" + param.CertType + @"</CertType>
       <ResourceID>" + param.ResourceID + @"</ResourceID>
       <OutTradeNo>" + param.OutTradeNo + @"</OutTradeNo>
       <AptType>" + param.AptType + @"</AptType>
       <TradeNo>" + param.TradeNo + @"</TradeNo>
       <DeptCode>" + param.DeptCode + @"</DeptCode>
       <DeptName>" + param.DeptName + @"</DeptName>
       <DoctorId>" + param.DoctorId + @"</DoctorId>
       <DoctorName>" + param.DoctorName + @"</DoctorName>
       <Description>" + param.Description + @"</Description>
       <Image>" + param.Image + @"</Image>
       <RegistDate>" + param.RegistDate + @"</RegistDate>
       <RegistTime>" + param.RegistTime + @"</RegistTime>
       <Amount>" + param.Amount + @"</Amount>
       <DiagnosisDetails>" + param.DiagnosisDetails + @"</DiagnosisDetails>
        <IsInsuranceUser>" + param.IsInsuranceUser + @"</IsInsuranceUser>
        <Language>" + (param.IsEnglish ? "EN" : "ZH") + @"</Language>
   </head>
   <body></body>
   <sign></sign>
</xml>";
                #endregion
                byte[] bs = Encoding.UTF8.GetBytes(xml);
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_Get_ReVisit_Comfirm_Reservation");
                req.Method = "POST";
                //req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
                //req.Headers["Accept-Encoding"] = "gzip,deflate";
                req.Headers["Accept-Charset"] = "utf-8";
                //req.AutomaticDecompression = DecompressionMethods.GZip;
                req.ContentType = "application/x-www-form-urlencoded";
                //req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";
                req.ContentLength = bs.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                }
                using (WebResponse response = req.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                    {
                        var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                        var begin = s.IndexOf("<xml>");
                        if (begin < 0)
                        {
                            return res;
                        }
                        var end = s.IndexOf("</xml>") + 6;
                        resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                        res.IsSuccess = resXml.SelectSingleNode("xml/body/ResponseResult/IsSuccess")?.InnerText == "1" ? true : false;
                        res.Message = resXml.SelectSingleNode("xml/body/ResponseResult/Message")?.InnerText;
                        try
                        {
                            res.CardNo = resXml.SelectSingleNode("xml/body/ResponseResult/CardNo")?.InnerText;
                            res.CardType = resXml.SelectSingleNode("xml/body/ResponseResult/CardType")?.InnerText;
                        }
                        catch (Exception)
                        {

                        }

                        // res.
                    }
                }
            }
            catch (System.Exception e) { }
            return res;
        }

        /// <summary>
        /// 联空更新预约接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private UpdateReVisitReb IF_Update_ReVisit_Status(UpdateReVisitReq param)
        {
            UpdateReVisitReb res = new UpdateReVisitReb();
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>
   <head>
       <AccessToken>" + param.AccessToken + @"</AccessToken>
       <OutTradeNo>" + param.OutTradeNo + @"</OutTradeNo>
       <StatusDiv>" + param.StatusDiv + @"</StatusDiv>
   </head>
   <body></body>
</xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_Update_ReVisit_Status");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    _logger.LogInformation("IF_Update_ReVisit_Status:返回参数 " + s);
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res.IsSuccess = resXml.SelectSingleNode("body/ResponseResult/IsSuccess")?.InnerText == "1" ? true : false;
                    res.Message = resXml.SelectSingleNode("body/ResponseResult/Message")?.InnerText;
                }
            }

            return res;
        }

        /// <summary>
        /// 联空药品获取接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private DrugReb IF_Drug_Get(DrugReq param)
        {
            DrugReb res = new DrugReb();
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>
   <head>
       <AccessToken>" + param.AccessToken + @"</AccessToken>
       <DrugCode></DrugCode>
   </head>
   <body></body>
</xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_Drug_Get");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res.ResponseResult.IsSuccess = resXml.SelectSingleNode("body/ResponseResult/IsSuccess")?.InnerText == "1" ? true : false;
                    res.ResponseResult.Message = resXml.SelectSingleNode("body/ResponseResult/Message")?.InnerText;
                    List<DrugList> ls = new List<DrugList>();
                    foreach (XmlNode item in resXml.SelectNodes("body/Drug_List"))
                    {
                        DrugList model = new DrugList();
                        model.DrugName = item.SelectSingleNode("DrugName")?.InnerText;
                        model.DrugUseMemo = item.SelectSingleNode("DrugUseMemo")?.InnerText;
                        model.DosageForm = item.SelectSingleNode("DosageForm")?.InnerText;
                        model.PackageUnit = item.SelectSingleNode("PackageUnit")?.InnerText;
                        model.Usage = item.SelectSingleNode("Usage")?.InnerText;
                        model.Frequency = item.SelectSingleNode("Frequency")?.InnerText;
                        model.Specification = item.SelectSingleNode("Specification")?.InnerText;
                        model.UniqueCode = item.SelectSingleNode("UniqueCode")?.InnerText;
                        model.ManufactorName = item.SelectSingleNode("ManufactorName")?.InnerText;
                        model.Price = item.SelectSingleNode("Price")?.InnerText;
                        model.SingleDosage = item.SelectSingleNode("SingleDosage")?.InnerText;
                        model.DosageUnit = item.SelectSingleNode("DosageUnit")?.InnerText;
                        model.DrugstoreName = item.SelectSingleNode("DrugstoreName")?.InnerText;
                        model.Status = item.SelectSingleNode("Status")?.InnerText;
                        model.StatusDiv = item.SelectSingleNode("StatusDiv")?.InnerText;
                        model.Image = item.SelectSingleNode("Image")?.InnerText;
                        model.TradeName = item.SelectSingleNode("TradeName")?.InnerText;
                        model.DosageFormCode = item.SelectSingleNode("DosageFormCode")?.InnerText;
                        model.DrugType = item.SelectSingleNode("DrugType")?.InnerText;
                        model.ApprovalNo = item.SelectSingleNode("ApprovalNo")?.InnerText;
                        model.DrugFlag = item.SelectSingleNode("DrugFlag")?.InnerText;
                        model.AntibioticFlag = item.SelectSingleNode("AntibioticFlag")?.InnerText;
                        model.AnaesthesiaFlag = item.SelectSingleNode("AnaesthesiaFlag")?.InnerText;
                        model.PreparationSign = item.SelectSingleNode("PreparationSign")?.InnerText;
                        model.DeptName = item.SelectSingleNode("DeptName")?.InnerText;
                        ls.Add(model);
                    }
                    res.Drug_List = ls;
                }
            }

            return res;
        }


        /// <summary>
        /// 联空药品相关字典接口
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private DrugDictionaryReb IF_Drug_Dictionary(DrugDictionaryReq param)
        {
            DrugDictionaryReb res = new DrugDictionaryReb();
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>
   <head>
       <AccessToken>" + param.AccessToken + @"</AccessToken>
       <DrugCode>" + param.DrugCode + @"</DrugCode>
   </head>
   <body></body>
</xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_Drug_Dictionary");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res.ResponseResult.IsSuccess = resXml.SelectSingleNode("body/ResponseResult/IsSuccess")?.InnerText == "1" ? true : false;
                    res.ResponseResult.Message = resXml.SelectSingleNode("body/ResponseResult/Message")?.InnerText;
                    List<DrugDictionaryFrequencyList> ls = new List<DrugDictionaryFrequencyList>();
                    foreach (XmlNode item in resXml.SelectNodes("body/FrequencyList"))
                    {
                        DrugDictionaryFrequencyList model = new DrugDictionaryFrequencyList();
                        model.ItemName = item.SelectSingleNode("ItemName")?.InnerText;
                        model.ItemCode = item.SelectSingleNode("ItemCode")?.InnerText;
                        ls.Add(model);
                    }
                    res.FrequencyList = ls;
                }
            }

            return res;
        }

        /// <summary>
        /// 处方物流推送
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private PrescriptionOrderReb IF_Confirm_PrescriptionOrder(PrescriptionOrderReq param)
        {
            PrescriptionOrderReb res = new PrescriptionOrderReb();
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>
   <head>
       <AccessToken>" + param.AccessToken + @"</AccessToken>
       <OpenUserID>" + param.OpenUserID + @"</OpenUserID>
       <PatientId>" + param.PatientId + @"</PatientId>
       <PrescriptionID>" + param.PrescriptionID + @"</PrescriptionID>
       <ReceivingName>" + param.ReceivingName + @"</ReceivingName>
       <HomeAddress>" + param.HomeAddress + @"</HomeAddress>
       <HomeAddressDetail>" + param.HomeAddressDetail + @"</HomeAddressDetail>
       <Tel>" + param.Tel + @"</Tel>
       <DeliveryManner>" + param.DeliveryManner + @"</DeliveryManner>
       <OutTradeNo>" + param.OutTradeNo + @"</OutTradeNo>
       <TradeNo>" + param.TradeNo + @"</TradeNo>
       <Amount>" + param.Amount + @"</Amount>
   </head>
   <body></body>
</xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_Confirm_PrescriptionOrder");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res.IsSuccess = resXml.SelectSingleNode("body/ResponseResult/IsSuccess")?.InnerText == "1" ? true : false;
                    res.Message = resXml.SelectSingleNode("body/ResponseResult/Message")?.InnerText;
                }
            }

            return res;
        }

        /// <summary>
        /// 获取IM链接
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        private string IF_IM_TokenGet(string orderNo, int language)
        {
            string res = "";
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>    <head>        <AccessToken>Sino</AccessToken>        <OutTradeNo>" + orderNo + "</OutTradeNo>   <Language>" + ((language == 1) ? "ZH" : "EN") + "</Language>    </head>    <body></body> </xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_IM_TokenGet");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res = resXml.SelectSingleNode("body/ChatInfo/Url")?.InnerText.Replace("amp;", "");
                }
            }

            return res;
        }

        /// <summary>
        /// 获取IM评价地址
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        private string IF_IM_EvaluateGet(string orderNo)
        {
            string res = "";
            XmlDocument resXml = new XmlDocument();
            #region xml
            string xml = @"xml=<xml>    <head>        <AccessToken>Sino</AccessToken>        <OutTradeNo>" + orderNo + "</OutTradeNo>    </head>    <body></body> </xml>";
            #endregion
            byte[] bs = Encoding.UTF8.GetBytes(xml);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(linkingUrl + "IF_IM_EvaluateGet");
            req.Method = "POST";
            req.Headers["Accept-Charset"] = "utf-8";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))//
                {
                    var s = rd.ReadToEnd().Replace("&lt;", "<").Replace("&gt;", ">");
                    var begin = s.IndexOf("<body>");
                    if (begin < 0)
                    {
                        return res;
                    }
                    var end = s.IndexOf("</body>") + 7;
                    resXml.LoadXml(s.Substring(begin, s.Length - begin - (s.Length - end)));
                    res = resXml.SelectSingleNode("body/ChatInfo/Url")?.InnerText.Replace("amp;", "");
                }
            }

            return res;
        }
    }
}
