CallImplementation.cs 7.3 KB
using DataHandling;
using log4net;
using MemoryRead;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Plugin
{
    public class Plugin
    {
        private static ILog log = LogManager.GetLogger("SmartScan");
        public Dictionary<string, string> CompleteData = new Dictionary<string, string>();
        /// <summary>
        /// 请求webserver获取替换数据
        /// </summary>
        /// <param name="pasr">请求的关键字和内容</param>
        /// <param name="method">0:http_get;1:http_post;2:soap</param>
        /// <returns>返回键值对</returns>
        public Dictionary<string, string> RequestServer(Dictionary<string, string> pasr, int method)
        {
            try
            {
                log.Info($"WebServer_请求参数:{JsonConvert.SerializeObject(pasr)}");
                if (pasr == null || pasr.Count <= 0)
                {
                    log.Info("WebServer_传入参数为空");
                    return pasr;
                }
                CompleteData = pasr;
                if (!ConfigHelper.Config.Get("WebServer_Isformal", true))
                {
                    pasr = MatchDicData(pasr);
                    log.Info($"对应关系处理后数据:{JsonConvert.SerializeObject(pasr)}");
                }
                Dictionary<string, string> dic = new Dictionary<string, string>();
                String Url = ConfigHelper.Config.Get("WebServer_Url", "http://10.159.150.10/EIWebService/Service1.asmx");
                String MethodName = ConfigHelper.Config.Get("WebServer_MethodName", "VmsizBarkodOlusturUserName");
                WebServiceHelper webService = new WebServiceHelper(log);
                switch (method)
                {
                    case 0:
                        dic = webService.QueryGetWebService(Url, MethodName, pasr);
                        break;
                    case 1:
                        dic = webService.QueryPostWebService(Url, MethodName, pasr);
                        break;
                    case 2:
                        dic = webService.QuerySoapWebService(Url, MethodName, pasr);
                        break;
                    default:
                        break;
                }
                //foreach (var item in dic)
                //{
                //    if (CompleteData.ContainsKey(item.Key))
                //    {
                //        CompleteData[item.Key] = item.Value;
                //    }
                //}
                //return CompleteData;
                if (dic != null || dic.Count != 0)
                {
                    string json = JsonConvert.SerializeObject(dic);
                    log.Info($"WebServer_返回数据:{json}");
                }
                else
                {
                    log.Info($"WebServer_返回数据:为空!");
                }
                return YourMethod(dic);
            }
            catch (Exception ex)
            {
                log.Info($"WebServer请求出错:{ex.Message}");
                return CompleteData;
            }

            //string[] shuz = { "Material code", "Tracibilty Serial Number", "Qty", "Supplier Code", "Code for who printed this label", "Qr barcode" };

        }
        /// <summary>
        /// 对应键值对信息
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public Dictionary<string, string> MatchDicData(Dictionary<string, string> dic)
        {
            Dictionary<string, string> parameterToContent = new Dictionary<string, string>();
            string json = ConfigHelper.Config.Get("WebServer_Match", "{\"malzemeKodu\":\"\",\"firmaKodu\":\"\",\"malzemeAdet\":\"\",\"lot\":\"\",\"firmaUrunKodu\":\"PN\"}");
            Dictionary<string, string> matchdata = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
            foreach (var item in matchdata)
            {
                if (dic.ContainsKey(item.Value))
                {
                    parameterToContent.Add(item.Key, dic[item.Value]);
                }
            }
            parameterToContent.Add("barkodAdet", "1");
            parameterToContent.Add("printerMacId", "0F0F0F0F0F0F");
            parameterToContent.Add("userName", "bekoservice");
            parameterToContent.Add("pass", "beko1209tyu");
            parameterToContent.Add("basanUserName", "579");
            return parameterToContent;
        }
        /// <summary>
        /// 对应返回的键值对数据
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public Dictionary<string, string> YourMethod(Dictionary<string, string> dic)
        {
            List<string> strings = new List<string>();
            foreach (var item in dic)
            {
                strings.Add(item.Value);
            }
            log.Info($"请求完成数据:{string.Join(",", strings)}");
            if (strings.Contains("OK"))
            {
                string[] arr = strings[1].Split('$');
                if (ConfigHelper.Config.Get("WebServer_Isformal", true))
                {

                    Dictionary<string, string> keys = new Dictionary<string, string>()
                    {
                      {"Material code", arr[1]},
                {"Tracibilty Serial Number", arr[2]},
                {"Qty", arr[3]},
                {"Supplier Code", arr[4]},
                {"Code for who printed this label", "579579"},
                {"Qr barcode", strings[1]}
                    };
                    return keys;
                }
                else
                {
                    try
                    {
                        Dictionary<string, int> valuePairs = new Dictionary<string, int>()
            {
                {"Material code", 0},
                {"Tracibilty Serial Number", 1},
                {"Qty", 2},
                {"Supplier Code", 3},
                {"Code for who printed this label", 4},
                {"Qr barcode", 5}
            };
                        foreach (var item in valuePairs)
                        {
                            if (CompleteData.ContainsKey(item.Key))
                            {
                                if (valuePairs.Count > 4)
                                {
                                    CompleteData[item.Key] = arr[item.Value];
                                }
                            }
                        }
                        if (CompleteData.ContainsKey("Code for who printed this label"))
                        {
                            CompleteData["Code for who printed this label"] = "579579";
                        }
                        if (CompleteData.ContainsKey("Qr barcode"))
                        {
                            CompleteData["Qr barcode"] = strings[1];
                        }
                        log.Info($"请求对应完成数据:{string.Join(",", CompleteData.Values)}");
                    }
                    catch (Exception ex)
                    {
                        log.Error($"处理返回数据报错:{ex.Message}");
                    }
                }
            }
            return CompleteData;
        }
    }
}