Пример #1
0
        public static int GetBestTime(DateTime?date, LocationModel location, Action <TableModelN> callback)
        {
            int threadID = GetNewThreadID();


            asyncDB[threadID] = new Thread(new ThreadStart(delegate()
            {
                if (date == null)
                {
                    date = new DateTime(DateTime.Now.Year, 1, 1);
                }
                TableModelN value = null;
                var teams         = TeamModel.GetTeamsByLocation(location);
                var isLock        = new ManualResetEvent(false);

                GetTables(Order.HurtigsteTider, "", "", location, date.Value.ToShortDateString(), null, o =>
                {
                    if (o != null && o.Count > 0)
                    {
                        value = o[0];
                    }

                    isLock.Set();
                });
                isLock.WaitOne();


                callback?.Invoke(value);

                if (asyncDB.ContainsKey(threadID))
                {
                    asyncDB.Remove(threadID);
                }
            }));

            asyncDB[threadID].Name = "GetBestTime";
            asyncDB[threadID].Start();

            return(threadID);
        }
Пример #2
0
        public static TableModelN GetBestTime(DateTime?date, LocationModel location)
        {
            if (date == null)
            {
                date = new DateTime(DateTime.Now.Year, 1, 1);
            }

            var         teams  = TeamModel.GetTeamsByLocation(location);
            TableModelN value  = new TableModelN();
            var         isLock = new ManualResetEvent(false);

            GetTables(Order.HurtigsteTider, "", "", location, date.Value.ToShortDateString(), null, o =>
            {
                if (o != null && o.Count > 0)
                {
                    value = o[0];
                }
                isLock.Set();
            });
            isLock.WaitOne();

            return(value);
        }
Пример #3
0
        public static int GetTables(
            Order order,
            string schoolName,
            string personName,
            LocationModel location,
            string from,
            string to,
            Action <ObservableCollection <TableModelN> > callback
            )
        {
            int threadID = GetNewThreadID();

            asyncDB[threadID] = new Thread(new ThreadStart(delegate()
            {
                var result = new ObservableCollection <TableModelN>();
                var db     = Scripts.Database.GetDB();


                string table_team      = db.GetTableName("Teams");
                string table_schools   = db.GetTableName("Schools");
                string table_locations = db.GetTableName("Locations");

                string cmd = string.Format("SELECT \n" +
                                           "`Team`.`ID`, `Team`.`Class`, `Team`.`Time`, `Team`.`Date`, `School`.`ID`, `School`.`Name`, `Location`.`ID`, `Location`.`Name` \n" +
                                           "FROM `{0}` AS `Team`\n" +
                                           "INNER JOIN `{1}` AS `School` ON `Team`.`SchoolID` = `School`.`ID` \n" +
                                           "INNER JOIN `{2}` AS `Location` ON `Team`.`LocationID` = `Location`.`ID`\n", table_team, table_schools, table_locations);

                List <string> where = new List <string>();



                if (personName != "")
                {
                    personName     = db.EscapeString(personName);
                    db.UseDistinct = true;
                    var persons    = db.GetRows("Persons", "TeamID", "WHERE `Name` LIKE '%{0}%'", personName);
                    if (persons != null)
                    {
                        List <int> id = new List <int>();
                        foreach (var item in persons)
                        {
                            id.Add(Convert.ToInt32(item[0]));
                        }
                        if (id.Count > 0)
                        {
                            string cmdID = string.Join(", ", id.ToArray());
                            where.Add("`Team`.`ID` IN (" + cmdID + ")");
                        }
                    }
                    else
                    {
                        callback?.Invoke(result);

                        if (asyncDB.ContainsKey(threadID))
                        {
                            asyncDB.Remove(threadID);
                        }
                        return;
                    }
                }
                if (schoolName != "")
                {
                    schoolName = db.EscapeString(schoolName);
                    where.Add("`School`.`Name` LIKE '%" + schoolName + "%'");
                }
                if (location != null && location.Name != "")
                {
                    location.Name = db.EscapeString(location.Name);
                    where.Add("`Location`.`Name` LIKE '%" + location.Name + "%'");
                }

                if (where.Count > 0)
                {
                    string cmdWhere = string.Join(" AND ", where.ToArray());
                    cmd            += " WHERE " + cmdWhere;
                }



                cmd         += ";";
                var dataTeam = db.ExecuteQuery(cmd);

                if (dataTeam != null && dataTeam.Count > 0)
                {
                    List <int> teamIDs = new List <int>();

                    foreach (var item in dataTeam)
                    {
                        if (!Regex.IsMatch(item[3].ToString(), @"\d{2}-\d{2}-\d{4}"))
                        {
                            continue;
                        }
                        var dt = Convert.ToDateTime(item[3]);

                        if (from != null && from != "" && Regex.IsMatch(from, @"\d{2}-\d{2}-\d{4}"))
                        {
                            var dtFrom = Convert.ToDateTime(from);
                            if (dt < dtFrom)
                            {
                                continue;
                            }
                        }

                        if (to != null && to != "" && Regex.IsMatch(to, @"\d{2}-\d{2}-\d{4}"))
                        {
                            var dtTo = Convert.ToDateTime(to);
                            if (dt > dtTo)
                            {
                                continue;
                            }
                        }

                        var model = new TableModelN();
                        teamIDs.Add(Convert.ToInt32(item[0]));
                        model.Team.ID       = Convert.ToInt32(item[0]);
                        model.Team.Class    = Convert.ToString(item[1]);
                        model.Team.Time     = Convert.ToString(item[2]);
                        model.Team.Date     = Convert.ToString(item[3]);
                        model.School.ID     = Convert.ToInt32(item[4]);
                        model.School.Name   = Convert.ToString(item[5]);
                        model.Location.ID   = Convert.ToInt32(item[6]);
                        model.Location.Name = Convert.ToString(item[7]);

                        result.Add(model);
                    }

                    if (teamIDs.Count > 0)
                    {
                        string cmdTeamIDs = string.Join(", ", teamIDs);

                        var persons = db.GetRows("Persons", new string[] { "ID", "Name", "TeamID" }, "WHERE `TeamID` IN ({0})", cmdTeamIDs);

                        if (persons != null)
                        {
                            foreach (var item in persons)
                            {
                                var model    = new PersonModel();
                                model.ID     = Convert.ToInt32(item[0]);
                                model.Name   = Convert.ToString(item[1]);
                                model.TeamID = Convert.ToInt32(item[2]);

                                var team = result.Where(o => o.Team.ID == model.TeamID).ToArray();

                                if (team.Length > 0)
                                {
                                    team[0].Persons.Add(model);
                                }
                            }
                        }
                    }
                }

                switch (order)
                {
                case Order.NyesteTider:
                    result = new ObservableCollection <TableModelN>(result.OrderBy(t => Convert.ToDateTime(t.Team.Date)).ToList());
                    break;

                case Order.HurtigsteTider:
                    result = new ObservableCollection <TableModelN>(result.OrderBy(t => t.Team.Time).ToList());
                    break;

                case Order.Alfabetisk:
                    result = new ObservableCollection <TableModelN>(result.OrderBy(t => t.School.Name).ToList());
                    break;
                }


                callback?.Invoke(result);

                if (asyncDB.ContainsKey(threadID))
                {
                    asyncDB.Remove(threadID);
                }
            }));

            asyncDB[threadID].Name = "GetTables";
            asyncDB[threadID].Start();

            return(threadID);
        }