示例#1
0
        public static async Task GetZudelloMappings(string team = null)
        {
            string Token = ZudelloLogin.Login();
            //Output object should be this
            List <Zconnections> teams = new List <Zconnections>();

            using (var db = new ZudelloContext())
            {
                //Maybe hash the records
                teams = db.Zconnections.ToList();
                await db.DisposeAsync();
            }


            int                                 numberOfLogicalCores = Environment.ProcessorCount;
            List <Thread>                       threads        = new List <Thread>(numberOfLogicalCores);
            int                                 sizeOfOneChunk = (teams.Count / numberOfLogicalCores) + 1;
            ConcurrentBag <Zmapping>            cb             = new ConcurrentBag <Zmapping>();
            ConcurrentDictionary <int, dynamic> cbd            = new ConcurrentDictionary <int, dynamic>();

            for (int i = 0; i < numberOfLogicalCores; i++)
            {
                int ab  = i;
                var thr = new Thread(
                    () =>
                {
                    int count = 0;
                    List <Zconnections> teamChunked = teams.Skip(ab * sizeOfOneChunk)
                                                      .Take(sizeOfOneChunk).ToList();

                    foreach (var t in teamChunked)
                    {
                        dynamic ZudelloTeam       = JsonConvert.DeserializeObject <ExpandoObject>(t.ZudelloCredentials);
                        bool newMappings          = false;
                        string GetMapping         = ZudelloLogin.CallZudelloMapping(Token, ZudelloTeam.team);
                        ZmappingJson MappingFiles = JsonConvert.DeserializeObject <ZmappingJson>(GetMapping);
                        dynamic zudelloObject     = JObject.Parse(GetMapping);

                        foreach (Zmapping map in MappingFiles.data)
                        {
                            Console.WriteLine(map.UpdatedAt);

                            cb.Add(map);
                        }
                    }
                }

                    );

                threads.Add(thr);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }


            foreach (var map in cb)
            {
                bool newMappings = false;
                using (var db = new ZudelloContext())
                {
                    try {
                        Zmapping AddtoMap = map;

                        Zmapping MapID = db.Zmapping.Where(i => i.DocType == map.DocType &&
                                                           i.Type == map.Type &&
                                                           i.connection_id == map.connection_id).FirstOrDefault();

                        if (MapID.Id > 0)
                        {
                            MapID.Body             = map.Body; //= map.ShallowCopy();
                            MapID.IsOutgoing       = map.IsOutgoing;
                            MapID.ProcessOrder     = map.ProcessOrder;
                            MapID.IsMasterData     = map.IsMasterData;
                            MapID.connection_id    = map.connection_id;
                            MapID.Type             = map.Type;
                            MapID.IntergrationUuid = map.IntergrationUuid;

                            // MapID.uuid = map.uuid;

                            db.Update(MapID);
                            db.SaveChanges();
                            var updateLastSync = (from a in db.Zlastsync
                                                  join c in db.Zmapping on a.MappingId equals c.Id
                                                  where c.DocType == "CallZudelloMapping"
                                                  select a).FirstOrDefault();


                            string pr = String.Format(DateTime.UtcNow.ToString(), "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
                            updateLastSync.LastSync = pr;
                            //  db.SaveChanges();
                        }
                        else

                        {
                            //add to mapping if it does not exist.
                            db.Zmapping.Add(AddtoMap);
                            db.SaveChanges();
                            Console.WriteLine(String.Format("Doc Type {0} has been added: Type {1} has been added", map.DocType, map.Type));
                            newMappings = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);//some error with mappings

                        try
                        {
                            Zmapping AddtoMap = map;
                            db.Zmapping.Add(AddtoMap);
                            db.SaveChanges();
                            Console.WriteLine(String.Format("Doc Type {0} has been added: Type {1} has been added", map.DocType, map.Type));
                            newMappings = true;
                        }

                        catch (Exception exm)
                        {
                            Console.WriteLine(exm.Message);
                        }


                        finally
                        {
                            if (map.Type == "SQL_LITE_CMD")
                            {
                                Console.WriteLine(Tools.RunSQLLiteCmd(Token, map));
                            }
                        }
                    }
                    finally
                    {
                        if (map.Type == "SQL_LITE_CMD")
                        {
                            Console.WriteLine(Tools.RunSQLLiteCmd(Token, map));
                        }
                    }

                    db.DisposeAsync();
                }

                if (newMappings == true)
                {
                    try //Add into sync table if there are any new records
                    {
                        SetSyncTable().Wait();
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }