コード例 #1
0
        public async Task <IActionResult> EditTeam(int?Id, int[] teamCnt, string[] selectLst)
        {
            //SubGameId
            if (Id == null)
            {
                return(NotFound());
            }

            var subGame = _context.SubGames.Single(g => g.ID == Id);  //gameID, round

            var teamMembers = await _context.TeamMembers
                              .Include(t => t.Bowler)
                              //    .ThenInclude(b => b.BowlerAverage)
                              .Include(t => t.Team)
                              .Where(t => t.Team.SubGameID == Id)
                              .OrderBy(t => t.Team.TeamOrder)
                              .ThenBy(t => t.Sequence)
                              .ToListAsync();

            //팀수정
            var AddTeamMembers  = new List <TeamMember>();
            var EditTeamMembers = new List <TeamMember>();
            var EditMember      = new TeamMember();


            var         imsiTMId            = teamMembers.Select(t => t.Team.ID).Distinct().ToList();
            int         teamMemberTeamIDCnt = imsiTMId.Count();
            List <Team> team = new List <Team>();


            int cnt  = 0;
            int tcnt = 0;

            int teamCntCnt = teamCnt.Count();


            //팀 추가 시 정리 부분
            if (teamMemberTeamIDCnt < teamCntCnt)
            {
                for (int i = teamMemberTeamIDCnt; i < teamCntCnt; i++)
                {
                    team.Add(new Team
                    {
                        SubGameID = Id ?? 0,
                        TeamName  = ((Char)(65 + i)).ToString(),
                        TeamOrder = i
                    });
                }

                _context.AddRange(team);


                //팀 추가에 따른 팀 순서 새로 정리
                foreach (var item in team)
                {
                    imsiTMId.Add(item.ID);
                }
            }



            //팀 삭제 시 정리 부분
            if (teamMemberTeamIDCnt > teamCntCnt)
            {
                for (int i = teamCntCnt; i < teamMemberTeamIDCnt; i++)
                {
                    team.Add(_context.Teams.Where(t => t.ID == imsiTMId[i]).Single());
                    //teamMembers.RemoveRange((teamMembers.Where(t => t.TeamID == teamMemberTeamID[i]).ToList());
                    var imsi = (teamMembers.Where(t => t.TeamID == imsiTMId[i]).ToList());
                    foreach (var item in imsi)
                    {
                        teamMembers.Remove(item);
                    }


                    _context.RemoveRange(team);
                }

                //팀 추가에 따른 팀 순서 새로 정리
                foreach (var item in team)
                {
                    imsiTMId.Remove(item.ID);
                }
            }


            //팀 추가 삭제에 따른 정리 부분
            teamMemberTeamIDCnt = imsiTMId.Count();
            int[] teamMemberTeamID = new int[teamMemberTeamIDCnt];


            int halfcnt = teamMemberTeamIDCnt % 2 == 0 ? teamMemberTeamIDCnt / 2 : teamMemberTeamIDCnt / 2 + 1;


            for (int i = 0; i < teamMemberTeamIDCnt; i++)
            {
                int idx = 0;

                if (i < halfcnt)
                {
                    idx = i * 2;
                }
                else
                {
                    idx = (i - halfcnt) * 2 + 1;
                }
                teamMemberTeamID[i] = imsiTMId[idx];
            }



            foreach (var item in teamCnt)
            {
                for (int i = 0; i < item; i++)
                {
                    EditMember = teamMembers.Find(t => t.BowlerID == selectLst[cnt]);

                    if (EditMember == null)
                    {
                        AddTeamMembers.Add(new TeamMember
                        {
                            BowlerID = selectLst[cnt],
                            Score    = 0,
                            TeamID   = teamMemberTeamID[tcnt], //(teamMemberTeamIDCnt > tcnt) ? teamMemberTeamID[tcnt] : team[tcnt - (teamMemberTeamIDCnt)].ID,
                            Average  = _context.BowlerAverages.Find(selectLst[cnt]).Average,
                            Sequence = i
                        });
                    }
                    else
                    {
                        if (EditMember.TeamID != teamMemberTeamID[tcnt] || //((teamMemberTeamIDCnt > tcnt) ? teamMemberTeamID[tcnt] : team[tcnt - (teamMemberTeamIDCnt)].ID) ||
                            EditMember.Sequence != i)
                        {
                            EditMember.TeamID   = teamMemberTeamID[tcnt]; //(teamMemberTeamIDCnt > tcnt) ? teamMemberTeamID[tcnt] : team[tcnt - (teamMemberTeamIDCnt)].ID;
                            EditMember.Sequence = i;
                            EditTeamMembers.Add(EditMember);
                        }

                        teamMembers.Remove(EditMember);
                    }
                    cnt++;
                }

                tcnt++;
            }

            if (AddTeamMembers.Count() > 0)
            {
                _context.AddRange(AddTeamMembers);
            }

            if (EditTeamMembers.Count() > 0)
            {
                _context.UpdateRange(EditTeamMembers);
            }

            if (teamMembers.Count() > 0)
            {
                _context.RemoveRange(teamMembers);
            }

            _context.SaveChanges();


            return(RedirectToAction(nameof(Index), new { Id = subGame.GameID, game = subGame.Round }));
        }