| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Common; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 12 | | |
| | | 13 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 14 | | { |
| | | 15 | | public class DownloadGoodsImagesTaskService : SvetaServiceBase, IDownloadGoodsImagesTaskService |
| | | 16 | | { |
| | | 17 | | private readonly SvetaDbContext _db; |
| | | 18 | | private readonly ILogger<DownloadGoodsImagesTaskService> _logger; |
| | | 19 | | |
| | | 20 | | public DownloadGoodsImagesTaskService(SvetaDbContext db, ILogger<DownloadGoodsImagesTaskService> logger, IAuthen |
| | 0 | 21 | | : base(authenticationService) |
| | 0 | 22 | | { |
| | 0 | 23 | | _db = db; |
| | 0 | 24 | | _logger = logger; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<DownloadGoodsImagesTask> Create(bool activeGoodsOnly) |
| | 0 | 28 | | { |
| | 0 | 29 | | var currentTask = await _db.DownloadGoodsImagesTasks.FirstOrDefaultAsync(x => !x.IsDeleted |
| | 0 | 30 | | && (x.InProgress || (!x.InProgress && !x.FinishedDate.HasValue)) |
| | 0 | 31 | | && x.CreatedByUserId == CurrentUserId); |
| | 0 | 32 | | if (currentTask != null) |
| | 0 | 33 | | { |
| | 0 | 34 | | return currentTask; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | var tasks = _db.DownloadGoodsImagesTasks.Where(x => !x.IsDeleted && x.CreatedByUserId == CurrentUserId); |
| | 0 | 38 | | foreach(var t in tasks) |
| | 0 | 39 | | { |
| | 0 | 40 | | t.IsDeleted = true; |
| | 0 | 41 | | } |
| | 0 | 42 | | await _db.SaveChangesAsync(CurrentUserId); |
| | | 43 | | |
| | 0 | 44 | | currentTask = new DownloadGoodsImagesTask(); |
| | 0 | 45 | | currentTask.ActiveGoodsOnly = activeGoodsOnly; |
| | 0 | 46 | | _db.Add(currentTask); |
| | 0 | 47 | | await _db.SaveChangesAsync(CurrentUserId); |
| | | 48 | | |
| | 0 | 49 | | return currentTask; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async Task<DownloadGoodsImagesTask> FindCurrent() |
| | 0 | 53 | | { |
| | 0 | 54 | | return await _db.DownloadGoodsImagesTasks.FirstOrDefaultAsync(x => !x.IsDeleted && x.CreatedByUserId == Curr |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<DownloadGoodsImagesTask> FindNextPending() |
| | 0 | 58 | | { |
| | 0 | 59 | | return await _db.DownloadGoodsImagesTasks.FirstOrDefaultAsync(x => !x.IsDeleted && !x.InProgress && !x.Finis |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | public async Task Update(DownloadGoodsImagesTask data) |
| | 0 | 63 | | { |
| | 0 | 64 | | _db.Update(data); |
| | 0 | 65 | | await _db.SaveChangesAsync(); |
| | 0 | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |