private void SetFieldingStat(FieldingStat fieldingStat, Fielding lastFieldingStat)
        {
            if (fieldingStat.Catches > lastFieldingStat.MostCatches) lastFieldingStat.MostCatches = fieldingStat.Catches;
            if (fieldingStat.Stumpings > lastFieldingStat.MostStumpings) lastFieldingStat.MostStumpings = fieldingStat.Stumpings;

        }
        public bool AddStats(List<DLStat> statsList)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        var bwccXmlDoc = new XmlDocument();

                        bwccXmlDoc.Load(
                            HttpContext.Current.Server.MapPath(
                                "~/Modules/ivNet.Club/App_Data/MySql/members.xml"));

                        var memberType = session.CreateCriteria(typeof (MemberType))
                            .List<MemberType>().FirstOrDefault(x => x.Name.Equals("Player"));

                        foreach (var dlStat in statsList)
                        {
                            // HowOut 
                            var howout = session.CreateCriteria(typeof (HowOut))
                                .List<HowOut>().FirstOrDefault(x => x.Name.Equals(dlStat.HowOut)) ??
                                         new HowOut();

                            if (howout.Id == 0)
                            {
                                howout.Name = dlStat.HowOut;

                                SetAudit(howout);
                                session.Save(howout);
                            }

                            // Fixture 
                            var fixture = session.CreateCriteria(typeof (Fixture))
                                .List<Fixture>().FirstOrDefault(x => x.FixtureKey.Equals(dlStat.FixtureKey)) ??
                                          new Fixture();

                            // Player 
                            var player = session.CreateCriteria(typeof (Player))
                                .List<Player>().FirstOrDefault(x => x.Member.MemberKey.Equals(dlStat.MemberKey)) ??
                                         new Player();

                            if (player.Id == 0)
                            {
                                // Member 
                                var member = session.CreateCriteria(typeof (Member))
                                    .List<Member>().FirstOrDefault(x => x.MemberKey.Equals(dlStat.MemberKey)) ??
                                             new Member();

                                if (member.Id != 0)
                                {
                                    member.AddMemberType(memberType);
                                    SetAudit(member);
                                    session.SaveOrUpdate(member);

                                    player = new Player
                                    {
                                        Member = member
                                    };
                                    player.Init();
                                    SetTypeNickname(player, bwccXmlDoc, member.LegacyId);

                                    SetAudit(player);
                                    session.SaveOrUpdate(player);
                                }
                                //else
                                //{
                                //    var cakes = "";
                                //}                               
                            }

                            //if (fixture.Id == 0)
                            //{
                            //    var zozo = "";
                            //}


                            if (howout.Name != "DNB")
                            {

                                var battingStat = new BattingStat
                                {
                                    Position = dlStat.Position,
                                    Runs = dlStat.RunsScored,
                                    Player = player,
                                    Fixture = fixture,
                                    HowOut = howout
                                };

                                SetAudit(battingStat);
                                session.SaveOrUpdate(battingStat);
                            }

                            if (dlStat.OversBowled>0)
                            {

                                var bowlingStat = new BowlingStat
                                {
                                    Overs = dlStat.OversBowled,
                                    Maidens = dlStat.Maidens,
                                    Wickets = dlStat.Wickets,
                                    Runs = dlStat.RunsConceeded,
                                    Player = player,
                                    Fixture = fixture
                                };

                                SetAudit(bowlingStat);
                                session.SaveOrUpdate(bowlingStat);
                            }

                            if (dlStat.Catches + dlStat.Stumpings > 0)
                            {
                                var fieldingStat = new FieldingStat
                                {
                                    Catches = dlStat.Catches,
                                    Stumpings = dlStat.Stumpings,
                                    Player = player,
                                    Fixture = fixture
                                };

                                SetAudit(fieldingStat);
                                session.SaveOrUpdate(fieldingStat);
                            }

                            var team = session.CreateCriteria(typeof(Team))
                           .List<Team>().FirstOrDefault(x => x.Fixture.Id.Equals(fixture.Id));

                            if (team == null)
                            {
                                team = new Team();
                                team.Init();
                                team.Fixture = fixture;
                                SetAudit(team);
                            }

                            team.AddPlayer(player);

                            session.SaveOrUpdate(team);

                            if (fixture.Team == null)
                            {
                                fixture.Team = team;
                                session.SaveOrUpdate(fixture);
                            }
                        }
                        transaction.Commit();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, ex, string.Empty, null);
                        transaction.Rollback();
                        return false;
                    }
                }
            }
        }
    private void CalculateFieldingStat(Fielding lastFieldingStat, FieldingStat fieldingStat)
    {
        SetFieldingStat(fieldingStat, lastFieldingStat);
 
        lastFieldingStat.Catches += fieldingStat.Catches;
        lastFieldingStat.Stumpings += fieldingStat.Stumpings;
    }
        public void UpdateFixtureStats(int id, dynamic fixtureStats)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        var sqlQuery = string.Empty;

                        // fixture details
                        var fixture = id > 0
                            ? session.CreateCriteria(typeof (Fixture))
                                .List<Fixture>()
                                .FirstOrDefault(x => x.Id.Equals(id))
                            : new Fixture();

                        if (fixture == null) return;

                        if (id == 0)
                        {

                            fixture.HomeTeam = session.CreateCriteria(typeof (TeamName))
                                .List<TeamName>().FirstOrDefault(x => x.Name.Equals((string) fixtureStats["Team"]));

                            fixture.Opposition = session.CreateCriteria(typeof (TeamName))
                                .List<TeamName>()
                                .FirstOrDefault(x => x.Name.Equals((string) fixtureStats["Opposition"]));

                            fixture.DatePlayed = DateTime.Parse((string) fixtureStats["Date"]);

                            fixture.FixtureKey = CustomStringHelper.BuildKey(new[]
                            {
                                fixture.HomeTeam.Name,
                                fixture.DatePlayed.ToShortDateString()
                            });
                        }

                        var strResultType = (string) fixtureStats["ResultType"];
                        var strResult = (string) fixtureStats["Result"];

                        var resultType = session.CreateCriteria(typeof (ResultType))
                            .List<ResultType>()
                            .FirstOrDefault(x => x.Name.Equals(strResultType));

                        fixture.ResultType = resultType;
                        fixture.Result = strResult;

                        SetAudit(fixture);
                        session.SaveOrUpdate(fixture);

                        // batting stats
                        var battingStats = (IEnumerable) fixtureStats["Batting"];

                        if (battingStats != null)
                        {
                            sqlQuery = string.Format("DELETE FROM ivNetBattingStat WHERE FixtureID = {0}", id);
                            session.CreateSQLQuery(sqlQuery)
                                .ExecuteUpdate();

                            foreach (dynamic battingStat in battingStats)
                            {
                                var memberKey = CustomStringHelper.BuildKey(new[] {(string) battingStat["Name"]});
                                var player = session.CreateCriteria(typeof (Player))
                                    .List<Player>()
                                    .FirstOrDefault(x => x.Member.MemberKey.Equals(memberKey));

                                var howOut = session.CreateCriteria(typeof (HowOut))
                                    .List<HowOut>()
                                    .FirstOrDefault(x => x.Name.Equals((string) battingStat["HowOut"]));

                                var newBattingStat = new BattingStat
                                {
                                    Player = player,
                                    Fixture = fixture,
                                    Position = Convert.ToInt32((string) battingStat["Position"]),
                                    Runs = Convert.ToInt32((string) battingStat["Runs"]),
                                    Overs = Convert.ToDecimal((string) battingStat["Overs"]),
                                    HowOut = howOut
                                };

                                SetAudit(newBattingStat);
                                session.SaveOrUpdate(newBattingStat);
                            }
                        }

                        // bowling stats
                        var bowlingStats = (IEnumerable) fixtureStats["Bowling"];

                        if (bowlingStats != null)
                        {
                            sqlQuery = string.Format("DELETE FROM ivNetBowlingStat WHERE FixtureID = {0}", id);
                            session.CreateSQLQuery(sqlQuery)
                                .ExecuteUpdate();

                            foreach (dynamic bowlingStat in bowlingStats)
                            {
                                var memberKey = CustomStringHelper.BuildKey(new[] {(string) bowlingStat["Name"]});
                                var player = session.CreateCriteria(typeof (Player))
                                    .List<Player>()
                                    .FirstOrDefault(x => x.Member.MemberKey.Equals(memberKey));

                                var newBowlingStat = new BowlingStat
                                {
                                    Player = player,
                                    Fixture = fixture,
                                    Wickets = Convert.ToInt32((string) bowlingStat["Wickets"]),
                                    Runs = Convert.ToInt32((string) bowlingStat["Runs"]),
                                    Overs = Convert.ToDecimal((string) bowlingStat["Overs"]),
                                    Maidens = Convert.ToInt32((string) bowlingStat["Maidens"])
                                };

                                SetAudit(newBowlingStat);
                                session.SaveOrUpdate(newBowlingStat);
                            }
                        }

                        // fielding stats
                        var fieldingStats = (IEnumerable) fixtureStats["Fielding"];

                        if (fieldingStats != null)
                        {
                            sqlQuery = string.Format("DELETE FROM ivNetFieldingStat WHERE FixtureID = {0}", id);
                            session.CreateSQLQuery(sqlQuery)
                                .ExecuteUpdate();

                            foreach (dynamic fieldingStat in fieldingStats)
                            {
                                var memberKey = CustomStringHelper.BuildKey(new[] {(string) fieldingStat["Name"]});
                                var player = session.CreateCriteria(typeof (Player))
                                    .List<Player>()
                                    .FirstOrDefault(x => x.Member.MemberKey.Equals(memberKey));

                                var newFieldingStat = new FieldingStat
                                {
                                    Player = player,
                                    Fixture = fixture,
                                    Catches = Convert.ToInt32((string) fieldingStat["Catches"]),
                                    Stumpings = Convert.ToInt32((string) fieldingStat["Stumpings"])
                                };

                                SetAudit(newFieldingStat);
                                session.SaveOrUpdate(newFieldingStat);
                            }
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, ex, string.Empty, null);
                        transaction.Rollback();
                    }
                }
            }
        }