Exemplo n.º 1
0
        public async Task <int> SaveChangeAsync()
        {
            if (LstAdd.Count() > 0)
            {
                await db.AddRangeAsync(LstAdd);

                LstAdd = new List <TEntity>();

                return(await db.SaveChangesAsync());
            }

            if (LstDelete.Count() > 0)
            {
                db.RemoveRange(LstDelete);
                LstDelete = new List <TEntity>();

                return(await db.SaveChangesAsync());
            }

            if (LstUpdate.Count() > 0)
            {
                db.UpdateRange(LstUpdate);
                LstUpdate = new List <TEntity>();

                return(await db.SaveChangesAsync());
            }

            return(0);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Restart(int id)
        {
            using var tr = _context.Database.BeginTransaction();
            try
            {
                var campaignEntity = await _context.Campaigns.FirstAsync(x => x.Id == id);

                var newCampaign = new Campaign
                {
                    IsDeleted  = false,
                    LineLimit  = campaignEntity.LineLimit,
                    Modified   = DateTime.Now,
                    ModifierId = _currentUserId.Value,
                    Name       = GetCampaignName(campaignEntity.Name),
                    //AbonentsCount = await _context.CampaignAbonents.Where(x=>x.CampaignId==id && !x.HasErrors).CountAsync(),
                    AsteriskExtension = campaignEntity.AsteriskExtension,
                    AbonentsFileName  = campaignEntity.AbonentsFileName,
                    VoiceFileName     = campaignEntity.VoiceFileName,
                    Extension         = campaignEntity.Extension,
                    ClonedFromId      = id
                };

                _context.Add(newCampaign);
                await _context.SaveChangesAsync();

                System.IO.File.Copy(Path.Combine(_uploadsDir, FileType.Abonents.ToFileName(id)),
                                    Path.Combine(_uploadsDir, FileType.Abonents.ToFileName(newCampaign.Id)));
                System.IO.File.Copy(Path.Combine(_uploadsDir, FileType.Voice.ToFileName(id)),
                                    Path.Combine(_uploadsDir, FileType.Voice.ToFileName(newCampaign.Id)));

                var abonents = await _context.CampaignAbonents.Where(x => x.CampaignId == id && !x.HasErrors && (x.Status == 3 || !x.Status.HasValue || x.Status < 1))
                               .Select(x => new CampaignAbonent
                {
                    CampaignId = newCampaign.Id,
                    ModifierId = _currentUserId.Value,
                    Modified   = DateTime.Now,
                    Phone      = x.Phone,
                    UniqueId   = Guid.NewGuid()
                }).ToArrayAsync();

                await _context.AddRangeAsync(abonents);

                newCampaign.AbonentsCount = abonents.Count();
                _context.Update(newCampaign);

                var schedules = await _context.CampaignSchedules.Where(x => x.CampaignId == id)
                                .Select(x => new CampaignSchedule
                {
                    CampaignId = newCampaign.Id,
                    DateEnd    = x.DateEnd,
                    DateStart  = x.DateStart,
                    DaysOfWeek = x.DaysOfWeek,
                    ModifierId = _currentUserId.Value,
                    TimeEnd    = x.TimeEnd,
                    TimeStart  = x.TimeStart
                }).ToArrayAsync();

                await _context.AddRangeAsync(schedules);

                await _context.SaveChangesAsync();

                tr.Commit();

                return(Json(new { Success = true }));
            }
            catch
            {
                tr.Rollback();
                throw;
            }
        }