NeximApiUtil.cs 23.7 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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
 
using Model;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using Http = Model.Http;

namespace BLL
{
    public class NeximApiUtil
    {

        public bool IsEnalbe()
        {
            //判断 Nexim_AuthUrl 是否为空
            if (string.IsNullOrEmpty(Config.Nexim_AuthUrl) || string.IsNullOrEmpty(Config.Nexim_PartMastersUrl))
            {
                return false;
            }
            return true;

        } 
        /**
         * 获取登录的token
         * @return
         */
        private string getAccessToken(out string errMsg)
        { 
            errMsg = "";
            Dictionary<string, object> paramMap = new Dictionary<string, object>();
            paramMap.Add("userName", Config.Nexim_UserName.Val);
            paramMap.Add("password", Config.Nexim_Password.Val);
            //string  paramStr = JsonConvert.ToString(paramMap);
            //LogNet.log.Info("获取Fuji的token参数为:" + paramStr);
            string apiUrl = Config.Nexim_AuthUrl;
            string accessToken = "";
            string result = "";
            try
            {
                string body = JsonConvert.SerializeObject(paramMap);
                result = Model.Http.PostWithHeader(apiUrl, body, new Dictionary<string, string>());
                LogNet.log.Info("获取Fuji的token结果为:" + result);
                Dictionary<string, object> resultObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
                if (resultObj != null && resultObj.ContainsKey("accessToken"))
                {
                    accessToken = resultObj["accessToken"].ToString();
                    if (String.IsNullOrEmpty(accessToken))
                    {
                        errMsg = "accessToken is  null";
                    }
                }
                else
                {
                    errMsg = "result="+ result;
                    LogNet.log.Info("获取Fuji的token失败,原因:" + errMsg);
                }
                return accessToken;
            }
            catch (Exception e)
            {
                errMsg =  e.Message;
                LogNet.log.Info("获取Fuji的token异常:", e); 
                return accessToken;
            }
             

        }

        public  string getReelInfo(Dictionary<string, string> reelData, string key)
        {
            string dataKey = key;
            Dictionary<string, string> paramKeyM = GetParamMap();
            if (paramKeyM.ContainsKey(key))
            {
                dataKey = paramKeyM[key];
            }
            if (reelData.ContainsKey(dataKey))
            {
                return reelData[dataKey];
            }
            return "";
        }
        private int getIntReelInfo(Dictionary<string, string> reelData, string key)
        {
            try
            {
                string data = getReelInfo(reelData, key).Trim();

                if (data != "")
                {
                    return Convert.ToInt32(data);
                }
            }
            catch (Exception e)
            {
                LogNet.log.Error(e.ToString());
            }
            return 0;
        }

        private T getDataValue<T>(Dictionary<string, object> dataMap, string key)
        { 
            if (dataMap.ContainsKey(key)&& dataMap[key] is T)
            {
                return (T)dataMap[key];
            }
            return default(T);
        }

        private string RI = "RI";
        private string PN = "PN";
        private string QTY = "QTY";
        private string SP = "SP";
        private string BATCH = "BATCH";
        private string PRODATE = "PRODATE";

        private Dictionary<string, string> _paramKeyMap = null;
        private Dictionary<string, string> GetParamMap()
        {
            if (_paramKeyMap == null)
            {
                _paramKeyMap = new Dictionary<string, string>();

                string[] configMap = Config.Nexim_ParamKeyMap; ;
                foreach (string key in configMap)
                {
                    string[] arrys = key.Trim().Split('=');
                    if (arrys.Length == 2 && arrys[0] != "" && arrys[1] != "")
                    {
                        _paramKeyMap.Add(arrys[0], arrys[1]);
                    }
                }
            }
            return _paramKeyMap;
        }

        private Dictionary<string,object> buildDidParams(Dictionary<string, string> reelData, Dictionary<string, object> pnDataMap)
        {
            DIDModel didModel = new DIDModel();
            Dictionary<string, object> paramsMap = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(didModel));
            if (pnDataMap != null)
            {
                foreach (var kv in pnDataMap)
                {
                    paramsMap[kv.Key] = kv.Value;
                }
            }
            paramsMap["did"] = getReelInfo(reelData, RI);
            paramsMap["originalPartBarcode"] = getReelInfo(reelData, RI);
            string v = getReelInfo(reelData, PN);
            if (!string.IsNullOrEmpty(v))
            {
                paramsMap["partBarcode"] = v;
                paramsMap["partNumber"] = v;
            }
            int q = getIntReelInfo(reelData, QTY);
            if (q > 0)
            {
                paramsMap["quantity"] = q;
                paramsMap["partsoutWarning"] = q;
                paramsMap["splicingWarning"] = q;
            }
            v = getReelInfo(reelData, SP);
            if (!string.IsNullOrEmpty(v))
            {
                paramsMap["vendorName"] = v;
            }
            v = getReelInfo(reelData, BATCH);
            if (!string.IsNullOrEmpty(v))
            {
                paramsMap["lotName"] = v;
            }
            v = getReelInfo(reelData, PRODATE);
            if (!string.IsNullOrEmpty(v))
            {
                paramsMap["dateCode"] = v;
            }
            if (!paramsMap.ContainsKey("packageType") || string.IsNullOrEmpty(Convert.ToString(paramsMap["packageType"])))
            {
                paramsMap["packageType"] = "paper";
            }
            //paramsMap.Add("location", "");
            //paramsMap.Add("memo", "");
            //paramsMap.Add("note1", "");
            //paramsMap.Add("note2", "");
            //paramsMap.Add("note3", "");
            //paramsMap.Add("note4", "");
            //paramsMap.Add("useSplicing", true);
            //paramsMap.Add("useTrayPackage", false);
            //paramsMap.Add("trayStackCount", 0);
            //paramsMap.Add("trayPickupPositionX", 0);
            //paramsMap.Add("trayPickupPositionY", 0);
            //paramsMap.Add("traySizeX", 0);
            //paramsMap.Add("traySizeY", 0);
            return paramsMap;
        }

        public bool registerNewDid(Dictionary<string, string> reelData,Dictionary<string,object> pnDataMap, out string msg)
        {
            msg = "";
            if (!IsEnalbe())
            {
                return true;
            }
            Dictionary<string, string> headerMap = new Dictionary<string, string>(); 
            string token = getAccessToken(out string errmsg);
            if (String.IsNullOrEmpty(token))
            {
                //获取token为空
                msg = "getAccessToken fail:" + errmsg;
                LogNet.log.Info("getAccessToken fail");
                return false;
            }
            headerMap.Add("fujiAccessToken", token);
           
            List<Dictionary<string, object>> paramList=new List<Dictionary<string, object>>();
            paramList.Add(buildDidParams(reelData,pnDataMap));
            //string paramStr = JSON.toJSONstring(params);
            //LogNet.log.Info("注册Fuji的did参数为:" + paramStr);
            string result = "";
            string apiUrl = Config.Nexim_RegisterDidInfoUrl;
            try
            {
                string body=JsonConvert.SerializeObject (paramList);
                result = Http.PostWithHeader(apiUrl, body, headerMap);
                LogNet.log.Info("注册Fuji的did结果为:" + result);

                Dictionary<string, object> resultObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
                if (resultObj != null && resultObj.ContainsKey("addedCount"))
                {
                    int addedCount = Convert.ToInt32(resultObj["addedCount"]);
                    if (addedCount > 0)
                    {
                        return true;
                    }
                } 
                {
                    msg = "Register new did failed : "+result.Replace("\t","").Replace("\n","");
                }

            }
            catch (Exception e)
            {
                //改为英文提示
                msg = "Register new did failed:" + e.Message;
                LogNet.log.Info("注册Fuji的did失败:", e);
            }
            return false;
        }



        public Dictionary<string, object> searchDids(string code, out string msg)
        {
            msg = "";
            Dictionary<string, object> resultMap = new Dictionary<string, object>();
            if (!IsEnalbe())
            {
                return resultMap;
            }
            Dictionary<string, string> headerMap = new Dictionary<string, string>();
            string token = getAccessToken(out string errmsg);
            if (String.IsNullOrEmpty(token))
            {
                //获取token为空
                msg = "getAccessToken fail:" + errmsg;
                LogNet.log.Info("getAccessToken fail");
                return resultMap;
            }
            headerMap.Add("fujiAccessToken", token);

            String search = "did == \"" + code + "\"";
            String orderBy = "did asc";
            int pageToken = 0;
            int pageSize = 200;
            Dictionary<string, object> paramList = new Dictionary<string, object>();
            paramList.Add("pageToken", pageToken);
            paramList.Add("pageSize", pageSize);
            paramList.Add("search", search);
            paramList.Add("orderBy", orderBy);

            string result = "";
            string apiUrl = Config.Nexim_RegisterDidInfoUrl;
            try
            {
                string body = JsonConvert.SerializeObject(paramList);
                result = Http.GetWithHeader(apiUrl, headerMap, paramList);
                LogNet.log.Info("searchDids Result:" + result);

                List<Dictionary<string, object>> retMap = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(result);
                if (retMap != null && retMap.Count >= 0)
                {
                    resultMap = retMap[0]; 
                    if (resultMap != null && resultMap.ContainsKey("did"))
                    {
                        return resultMap;
                    }
                }
                else
                {
                    msg = "searchDids failed : " + result.Replace("\t", "").Replace("\n", "");
                }

            }
            catch (Exception e)
            {
                //改为英文提示
                msg = "searchDids failed:" + e.Message;
                LogNet.log.Info("searchDids Failed:", e);
            }
            return resultMap;
        }


        public Dictionary<string, object> getDid(string code, out string msg)
        {
            msg = "";
            Dictionary<string, object> resultMap = new Dictionary<string, object>();
            if (!IsEnalbe())
            {
                return resultMap;
            }
            Dictionary<string, string> headerMap = new Dictionary<string, string>();
            string token = getAccessToken(out string errmsg);
            if (String.IsNullOrEmpty(token))
            {
                //获取token为空
                msg = "getAccessToken fail:" + errmsg;
                LogNet.log.Info("getAccessToken fail");
                return resultMap;
            }
            headerMap.Add("fujiAccessToken", token);
             

            string result = "";
            string apiUrl = Config.Nexim_RegisterDidInfoUrl+"/"+code;
            try
            { 
                result = Http.GetWithHeader(apiUrl, headerMap, null);
                LogNet.log.Info("getDid Result:" + result);

                Dictionary<string,Dictionary<string,object>> reMap = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(result);
                if (reMap != null && reMap.Count >= 0 && reMap.ContainsKey("did"))
                {
                    resultMap = reMap["did"];
                    if (resultMap != null && resultMap.ContainsKey("did"))
                    {
                        return resultMap;
                    } 
                }
                else
                {
                    msg = "getDid failed : " + result.Replace("\t", "").Replace("\n", "");
                }

            }
            catch (Exception e)
            {
                //改为英文提示
                msg = "getDid failed:" + e.Message;
                LogNet.log.Info("getDid Failed:", e);
            }
            return resultMap;
        }



        public Dictionary<string, object> getPartMasters(string originalPartBarcode, out string msg)
        {
            msg = "";
            Dictionary<string, object> resultMap = new Dictionary<string, object>();
            if (!IsEnalbe())
            {
                return resultMap;
            }
            Dictionary<string, string> headerMap = new Dictionary<string, string>();
            string token = getAccessToken(out string errmsg);
            if (String.IsNullOrEmpty(token))
            {
                //获取token为空
                msg = "getAccessToken fail:" + errmsg;
                LogNet.log.Info("getAccessToken fail");
                return resultMap;
            }
            headerMap.Add("fujiAccessToken", token);


            string result = "";
            string apiUrl = Config.Nexim_PartMastersUrl + "/" + originalPartBarcode;
            try
            {
                result = Http.GetWithHeader(apiUrl, headerMap, null);
                LogNet.log.Info("getPartMasters Result:" + result);

                Dictionary<string, Dictionary<string, object>> reMap = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object>>>(result);
                if (reMap != null && reMap.Count >= 0 && reMap.ContainsKey("partMaster"))
                {
                    resultMap = reMap["partMaster"];
                    string partNumber = getDataValue<String>(resultMap, "partNumber");
                    if (!String.IsNullOrEmpty(partNumber))
                    {
                        return resultMap;
                    }
                    else
                    { 
                        msg = "getPartMasters failed : " + result.Replace("\t", "").Replace("\n", "");
                    }
                }
                else
                {
                    msg = "getPartMasters failed : " + result.Replace("\t", "").Replace("\n", "");
                }

            }
            catch (Exception e)
            {
                //改为英文提示
                msg = "getPartMasters failed:" + e.Message;
                LogNet.log.Info("getPartMasters Failed:", e);
            }
            return resultMap;
        }


        public bool postPartMasters(Dictionary<string, string> reelData, out string msg)
        {
            msg = "";

            if (!IsEnalbe())
            {
                return true;
            }
            Dictionary<string, string> headerMap = new Dictionary<string, string>();
            string token = getAccessToken(out string errmsg);
            if (String.IsNullOrEmpty(token))
            {
                //获取token为空
                msg = "getAccessToken fail:" + errmsg;
                LogNet.log.Info("getAccessToken fail");
                return false;
            }
            headerMap.Add("fujiAccessToken", token);

            List<PartMasterModel> paramList = new List<PartMasterModel>();
            PartMasterModel model = new PartMasterModel();
            
            // Map fields from reelData
            model.originalPartBarcode = getReelInfo(reelData, RI);
            model.partNumber = getReelInfo(reelData, PN);
            model.partBarcode = getReelInfo(reelData, PN);
            model.quantity = getIntReelInfo(reelData, QTY);
            model.vendorName = getReelInfo(reelData, SP);
            model.lotName = getReelInfo(reelData, BATCH);
           
            string dateCode = getReelInfo(reelData, PRODATE);
            if (!string.IsNullOrEmpty(dateCode))
            {
                model.dateCode = dateCode;
            }
            model.defaultValue();

            paramList.Add(model);

            string result = "";
            string apiUrl = Config.Nexim_PartMastersUrl;
            try
            {
                string body = JsonConvert.SerializeObject(paramList);
                result = Http.PostWithHeader(apiUrl, body, headerMap);
                LogNet.log.Info("postPartMasters Result=:" + result);

                Dictionary<string, object> resultObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
                if (resultObj != null && resultObj.ContainsKey("addedCount"))
                {
                    int addedCount = Convert.ToInt32(resultObj["addedCount"]);
                    if (addedCount > 0)
                    {
                        return true;
                    }
                }
                {
                    msg = "postPartMasters failed : " + result.Replace("\t", "").Replace("\n", "");
                }

            }
            catch (Exception e)
            {
                //改为英文提示
                msg = "postPartMasters failed:" + e.Message;
                LogNet.log.Info("postPartMasters Failed:", e);
            }
            return false;
        }

        private class DIDModel
        {
            public string did { get; set; }
            public string partNumber { get; set; }
            public string partBarcode { get; set; }
            public string originalPartBarcode { get; set; }
            public int quantity { get; set; }
            public string packageType { get; set; } = "paper";
            public int partsoutWarning { get; set; }
            public int splicingWarning { get; set; }
            public string vendorName { get; set; }
            public string lotName { get; set; }
            public string dateCode { get; set; }
            public string area { get; set; }
            public string location { get; set; }
            public string machine { get; set; }
            public int module { get; set; }
            public int stageNo { get; set; }
            public int slotNo { get; set; }
            public string createdUser { get; set; }
            public string createdDate { get; set; }
            public string updatedUser { get; set; }
            public string updatedDate { get; set; }
            public string dryStatus { get; set; }
            public string  boxLife { get; set; }
            public string floorLife { get; set; }
            public string memo { get; set; }
            public string lightingClass { get; set; }
            public int lightingLimit { get; set; }
            public string note1 { get; set; }
            public string note2 { get; set; }
            public string note3 { get; set; }
            public string note4 { get; set; }
            public bool useSplicing { get; set; } = true;
            public bool useTrayPackage { get; set; } = false;
            public int trayStackCount { get; set; } = 1;
            public int trayPickupPositionX { get; set; } = 1;
            public int trayPickupPositionY { get; set; } = 1;
            public int traySizeX { get; set; } = 1;
            public int traySizeY { get; set; } = 1;
        }
        private class PartMasterModel
        {
            public PartMasterModel()
            {

            }
            public void defaultValue()
            {

                // Set other fields with default values from the requirement
                this.bodyThickness = null;
                this.msLevel = null;
                this.trayPartQuantity = 0;
                this.partsOutWarning = 0;
                this.splicingWarning = 0;
                this.location = null;
                this.memo = null;
                this.packageType = "paper";
                this.indexPitch = 0;
                this.reelWidth = 0;
                this.lightingClassRequired = false;
                this.lightingLimit = 0;
                this.note1 = null;
                this.note2 = null;
                this.note3 = null;
                this.note4 = null;
                this.traySizeX = 0;
                this.traySizeY = 0;
                this.useTrayPackage = false;
                this.useSplicing = true;
                this.verifyPolicy = "every_time";
                this.applicableMachineCheck = false;
                this.didAffixChecks = false;
                this.markCheckHint = null;
                this.markCheckAnswer = null;
                this.directionCheck = 0;
                this.mpnCheck = null;
                this.useLCRChecking = false;
                this.lcrMeasurementType = "L";
                this.unit = "string";
                this.lcrMeasurementMethod = "None";
                this.normalValue = 0;
                this.upperLimit = 0;
                this.lowerLimit = 0;
                this.frequency = 0;
                this.voltage = 0;

            }
            public string originalPartBarcode { get; set; }
            public string partNumber { get; set; }
            public string partBarcode { get; set; }
            public string bodyThickness { get; set; }
            public string msLevel { get; set; }
            public int quantity { get; set; }
            public int trayPartQuantity { get; set; }
            public int partsOutWarning { get; set; }
            public int splicingWarning { get; set; }
            public string vendorName { get; set; }
            public string lotName { get; set; }
            public string dateCode { get; set; }
            public string location { get; set; }
            public string memo { get; set; }
            public string packageType { get; set; }
            public int indexPitch { get; set; }
            public int reelWidth { get; set; }
            public bool lightingClassRequired { get; set; }
            public int lightingLimit { get; set; }
            public string note1 { get; set; }
            public string note2 { get; set; }
            public string note3 { get; set; }
            public string note4 { get; set; }
            public int traySizeX { get; set; }
            public int traySizeY { get; set; }
            public bool useTrayPackage { get; set; }
            public bool useSplicing { get; set; }
            public string verifyPolicy { get; set; }
            public bool applicableMachineCheck { get; set; }
            public bool didAffixChecks { get; set; }
            public string markCheckHint { get; set; }
            public string markCheckAnswer { get; set; }
            public int directionCheck { get; set; }
            public string mpnCheck { get; set; }
            public bool useLCRChecking { get; set; }
            public string lcrMeasurementType { get; set; }
            public string unit { get; set; }
            public string lcrMeasurementMethod { get; set; }
            public int normalValue { get; set; }
            public int upperLimit { get; set; }
            public int lowerLimit { get; set; }
            public int frequency { get; set; }
            public int voltage { get; set; }
        }

    }

}