Item_NanRui.cs 18.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
/*
 * 南京南瑞
 * 
 */

using System;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using Model;
using Asa.FaceControl;

namespace BLL
{
    public class NanRui : IExtension
    {
        public event IExtension.PrintDelegate Printing;
        public event IExtension.PrintDelegate SaveRetrospect;

        private string[][] jsonData;
        private string mesIP;
        private bool mesConn;
        private Config config;

        public NanRui(Config config)
        {
            this.config = config;
        }

        public void Dispose()
        {
        }

        public void Clear()
        {
            //ClearText();
        }

        public void Load(List<ExtensionControl> extensions)
        {
            //this.extensions = extensions;
            //mesIP = AppConfig.Read("MESIP");
            //SetControlText("TxtCurrentDate", string.Format("{0:yyyyMMdd}", DateTime.Now));
            //mesConn = Http.Ping(mesIP);
        }

        public bool SetKey(string[] originalCode, Dictionary<string, string> key, bool hasMatch,out string errmsg)
        {
            errmsg = "";
            return true;
            //SetComboItem("CboSupplier", originalCode);
            //SetComboItem("CboQuantity", originalCode);
            //SetComboItem("CboBatch", originalCode);
            //SetControlText(key);

            //if (!mesConn) return;
            //int index = BatchVender(originalCode);
            //SetComboSelected("CboSupplier", index);
            //if (index == -1)
            //{
            //    new FaceMessageBox("", "没有匹配到供应商编号", System.Windows.Forms.MessageBoxButtons.OK).ShowDialog();
            //    return;
            //}

            //string[] str = new string[jsonData.Length];
            //for (int i = 0; i < str.Length; i++)
            //    str[i] = jsonData[i][0];
            //SetComboItem("CboOrderNumber", str);
            //SetComboSelected("CboOrderNumber", 0);

        }

        public void Update()
        {
        }









        /// <summary>
        /// 查询mes状态事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryMES(object sender, EventArgs e)
        {
            mesConn = Http.Ping(mesIP);
            string text = mesIP + (mesConn ? " OK" : " Timeout");
            new FaceMessageBox("", text, System.Windows.Forms.MessageBoxButtons.OK, true).ShowDialog();
        }

        /// <summary>
        /// 供应商编号Click事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryPN(object sender, EventArgs e)
        {
            //string vender = GetControlText("CboSupplier");
            //int index = BatchVender(vender);
            //if (index == -1)
            //{
            //    new FaceMessageBox("", "没有匹配到供应商编号", System.Windows.Forms.MessageBoxButtons.OK).ShowDialog();
            //    return;
            //}

            //string[] str = new string[jsonData.Length];
            //for (int i = 0; i < str.Length; i++)
            //    str[i] = jsonData[i][0];
            //SetComboItem("CboOrderNumber", str);
            //SetComboSelected("CboOrderNumber", 0);
        }

        /// <summary>
        /// 解绑供应商编号事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnbindSupplier(object sender, EventArgs e)
        {
            //string vender = GetControlText("TxtSupplier");
            //UnbindVender(vender);
        }

        /// <summary>
        /// 解绑物料编号事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnbindMateriel(object sender, EventArgs e)
        {
            //string material = GetControlText("TxtMateriel");
            //UnbindItemCode(material);
        }

        /// <summary>
        /// 绑定物料编号事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BindMateriel(object sender, EventArgs e)
        {
            //string vender = GetControlText("CboSupplier");
            //string material = GetControlText("CboMateriel");

            //if (string.IsNullOrWhiteSpace(vender) || string.IsNullOrWhiteSpace(material))
            //{
            //    new FaceMessageBox("", "供应商编号或物料编号不能为空。", System.Windows.Forms.MessageBoxButtons.OK).ShowDialog();
            //    return;
            //}

            //int index = extensions.FindIndex(match => match.Control.Name == "CboMateriel");
            //if (index > -1)
            //{
            //    FaceComboBox cbo = (FaceComboBox)extensions[index].Control;
            //    cbo.Items.AddWithoutExist(material);
            //}

            //UploadMaterial(vender, material);
        }

        private void OrderNumberSelected(object sender, EventArgs e)
        {
            //int idx = (sender as FaceComboBox).SelectedIndex;
            //if (idx == -1) return;
            //string text = jsonData[idx][1];
            //SetControlText("TxtOrderLine", text);
        }

        /// <summary>
        /// 上传并打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintLabel(object sender, EventArgs e)
        {
            //if (CheckEmptyContent()) return;
            //GetReelID(out string key, out string value);
            //Dictionary<string, string> pairs = GetPrintKey();
            //pairs.Add("Reelid", value);
            //SetReelIDAdd(key);

            //string order = GetControlText("CboOrderNumber");
            //string num = GetControlText("TxtOrderLine");
            //string qty = GetControlText("TxtQuantity");
            //UploadOrder(order, num, qty, out Dictionary<string, string> value2);
            //Printing?.Invoke(pairs);
            //UploadMessage("", out string[] value3);
        }

        /// <summary>
        /// 批量打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MultiprintLabel(object sender, EventArgs e)
        {
            //if (CheckEmptyContent()) return;
            //int count = Convert.ToInt32(GetControlText("NudMultiprint"));
            //Dictionary<string, string> pairs = GetPrintKey();

            //for (int i = 0; i < count; i++)
            //{
            //    GetReelID(out string key, out string value);
            //    if (pairs.ContainsKey("Reelid"))
            //        pairs["Reelid"] = value;
            //    else
            //        pairs.Add("Reelid", value);
            //    SetReelIDAdd(key);
            //    Printing?.Invoke(pairs);
            //}
        }






        private int BatchVender(params string[] code)
        {
            int index = -1;
            //jsonData = null;

            //for (int i = 0; i < code.Length; i++)
            //{
            //    System.Threading.Thread.Sleep(50);
            //    bool rtn = UploadVender(code[i], out jsonData);
            //    if (rtn)
            //    {
            //        index = i;
            //        break;
            //    }
            //}
            //if (index == -1)
            //    return -1;

            //List<string> temp = new List<string>();
            //for (int j = 0; j < jsonData.Length; j++)
            //{
            //    if (temp.FindIndex(s => s == jsonData[j][2]) == -1)
            //        temp.Add(jsonData[j][2]);
            //}
            //if (temp.Count > 0)
            //{
            //    int idx = extensions.FindIndex(match => match.Control.Name == "CboMateriel");
            //    if (idx > -1)
            //    {
            //        FaceComboBox cbo = (FaceComboBox)extensions[idx].Control;
            //        cbo.Items.AddRange(temp.ToArray());
            //        cbo.SelectedIndex = 0;
            //    }
            //}

            return index;
        }

        /// <summary>
        /// 上传供应商编号
        /// </summary>
        /// <param name="vender"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool UploadVender(string vender, out string[][] value)
        {
            //value = new string[2][];
            //value[0] = new string[3];
            //value[1] = new string[3];
            //value[0][0] = "1-1";
            //value[0][1] = "1-2";
            //value[0][2] = "1-3";
            //value[1][0] = "2-1";
            //value[1][1] = "2-2";
            //value[1][2] = "2-3";
            //return true;

            value = null;
            string json = "{\"VENDER\":\"" + vender + "\"}";
            string port = "ReturnPurchaseOrderAndMaterial";

            try
            {
                bool rtn = Post(port, json, out string jsonStr);
                if (!rtn) return false;

                System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
                object[] obj = (object[])serializer.DeserializeObject(jsonStr);
                value = new string[obj.Length][];
                for (int i = 0; i < value.Length; i++)
                {
                    Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
                    value[i] = new string[dic.Count];
                    value[i][0] = dic["PURCHASEORDERNO"].ToString();
                    value[i][1] = dic["PURCHASEORDERNUMBER"].ToString();
                    value[i][2] = dic["Mcode"].ToString();
                }
                LogNet.log.Info("UploadVender OK");
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("UploadVender", ex);
                return false;
            }
        }

        /// <summary>
        /// 绑定供应商编号和物料编号
        /// </summary>
        /// <param name="vender"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        private bool UploadMaterial(string vender, string material)
        {
            string json = "{\"VENDER\":\"" + vender + "\",\"ITEMCODE\":\"" + material + "\"}";
            string port = "ReturnPurchaseOrder";

            try
            {
                bool rtn = Post(port, json, out string jsonStr);
                if (!rtn) return false;
                LogNet.log.Info("UploadMaterial OK");
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("UploadMaterial", ex);
                return false;
            }
        }

        /// <summary>
        /// 解绑供应商编号
        /// </summary>
        /// <param name="vender"></param>
        /// <returns></returns>
        private bool UnbindVender(string vender)
        {
            string json = "{\"VENDER\":\"" + vender + "\"}";
            string port = "UnbindMaterialOrVender";

            try
            {
                bool rtn = Post(port, json, out string jsonStr);
                if (!rtn) return false;
                LogNet.log.Info("UnbindVender OK");
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("UnbindVender", ex);
                return false;
            }
        }

        /// <summary>
        /// 解绑物料编号
        /// </summary>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        private bool UnbindItemCode(string itemCode)
        {
            string json = "{\"ITEMCODE\":\"" + itemCode + "\"}";
            string port = "UnbindMaterialOrVender";

            try
            {
                bool rtn = Post(port, json, out string jsonStr);
                if (!rtn) return false;
                LogNet.log.Info("UnbindItemCode OK");
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("UnbindItemCode", ex);
                return false;
            }
        }

        /// <summary>
        /// 上传订单号,行号
        /// </summary>
        /// <param name="order"></param>
        /// <param name="num"></param>
        /// <param name="qty"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool UploadOrder(string order, string num, string qty, out Dictionary<string, string> value)
        {
            value = new Dictionary<string, string>();
            string json = "{\"PURCHASEORDERNO\":\"" + order + "\",\"PURCHASEORDERNUMBER\":\"" + num + "\"}";
            string port = "ReturnPurchaseOrderByOrderCodeAndNumber";

            try
            {
                bool rtn = Post(port, json, out string jsonStr);
                if (!rtn) return false;

                System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
                object[] obj = (object[])serializer.DeserializeObject(jsonStr);
                Dictionary<string, object> dic = (Dictionary<string, object>)obj[0];
                value.Add("MATERIALDEC", dic["MATERIALDEC"].ToString());
                value.Add("STORAGE", dic["STORAGE"].ToString());
                value.Add("MATERIALNO", dic["MATERIALNO"].ToString());
                value.Add("PURCHASEORDERNO", dic["PURCHASEORDERNO"].ToString());
                value.Add("PURCHASEORDERNUMBER", dic["PURCHASEORDERNUMBER"].ToString());
                value.Add("QTY", qty);
                value.Add("BATCHNO", string.Format("{0:yyMMddHHmm}", DateTime.Now));
                LogNet.log.Info("UploadOrder OK");
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("UploadOrder", ex);
                return false;
            }
        }

        /// <summary>
        /// 上传最终数据
        /// </summary>
        /// <param name="spliceString">二维码字符串</param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool UploadMessage(string spliceString, out string[] value)
        {
            value = null;

            //List<string> arr = new List<string>();
            //string[] key = new string[] { "PURCHASEORDERNO", "PURCHASEORDERNUMBER", "MATERIALNO" };
            //Dictionary<string, string> printKey = GetPrintKey();

            //for (int i = 0; i < key.Length; i++)
            //{
            //    if (printKey.ContainsKey(key[i]))
            //        arr.Add(string.Format("\"{0}\":\"{1}\"", key[i], printKey[key[i]]));
            //    else
            //        arr.Add(string.Format("\"{0}\":\"\"", key[i]));
            //}
            //arr.Add(string.Format("\"MATERIALLOTNO\":\"{0}\"", spliceString));
            //arr.Add(string.Format("\"VENDERMCODE\":\"{0}\"", GetControlText("CboSupplier")));

            //string json = "{" + string.Join(",", arr) + "}";
            //string port = "InsertPurchaseOrderstringMessage";

            try
            {
                //bool rtn = Post(port, json, out string jsonStr);
                //if (!rtn) return false;
                //Log.Info("UploadMessage OK");
                return true;
            }
            catch (Exception ex)
            {
                //Log.Error("UploadMessage", ex);
                return false;
            }
        }

        private bool Post(string fun, string json, out string text)
        {
            text = "";
            try
            {
                string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                param += "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">";
                param += "<soap12:Body>";
                param += "<" + fun + " xmlns=\"http://tempuri.org/\">";
                param += "<json>" + json + "</json>";
                param += "</" + fun + ">";
                param += "</soap12:Body>";
                param += "</soap12:Envelope>";
                //string param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                //param += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
                //param += "<soap:Body>";
                //param += "<" + fun + " xmlns=\"http://tempuri.org/\">";
                //param += "<json>" + json + "</json>";
                //param += "</" + fun + ">";
                //param += "</soap:Body>";
                //param += "</soap:Envelope>";


                string url = "http://" + mesIP + "/NariWebService/MESReturnInfo.asmx?op=" + fun;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/soap+xml;charset=utf-8";
                LogNet.log.Info("POST URL=" + url);
                LogNet.log.Info("POST Body=" + json);

                request.Method = "POST";
                request.Timeout = 5000;
                byte[] buff = Encoding.UTF8.GetBytes(param);
                request.ContentLength = buff.Length;

                System.IO.Stream writer = request.GetRequestStream();
                writer.Write(buff, 0, buff.Length);
                writer.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.Stream stream = response.GetResponseStream();

                System.IO.StreamReader read = new System.IO.StreamReader(stream, Encoding.UTF8);
                text = read.ReadToEnd();
                read.Close();
                LogNet.log.Info("POST Return=" + text);



                string s1 = "<" + fun + "Result>";
                string s2 = "</" + fun + "Result>";
                int n1 = text.IndexOf(s1);
                int n2 = text.IndexOf(s2);
                if (n1 == -1 || n2 == -1)
                {
                    LogNet.log.Info("返回的json格式不正确");
                    return false;
                }
                text = text.Substring(n1 + s1.Length, n2 - n1 - s1.Length);
                if (text == "" || text == "[]" || text.Contains("Error") || !text.StartsWith("[{"))
                    return false;
                LogNet.log.Info("Post OK text=" + text);
                return true;
            }
            catch (Exception ex)
            {
                LogNet.log.Error("POST", ex);
                return false;
            }
        }

        public void DrawTextBackground(Dictionary<string, string> key)
        {
        }

        public void Print(bool match, Dictionary<string, string> key)
        {

        }
    }
}