| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 10 | | using WinSolutions.Sveta.Common; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 12 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 13 | | |
| | | 14 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 15 | | { |
| | | 16 | | public class BarcodeService : SvetaServiceBase, IBarcodeService |
| | | 17 | | { |
| | | 18 | | private readonly SvetaDbContext _db; |
| | | 19 | | private readonly ILogger<BarcodeService> _logger; |
| | | 20 | | |
| | | 21 | | public BarcodeService(SvetaDbContext db, ILogger<BarcodeService> logger, IAuthenticationService authenticationSe |
| | 10 | 22 | | : base(authenticationService) |
| | 10 | 23 | | { |
| | 10 | 24 | | _db = db; |
| | 10 | 25 | | _logger = logger; |
| | 10 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<BarCode> CreateBarcode(string code) |
| | 10 | 29 | | { |
| | 10 | 30 | | if(_db.BarCodes.Where(x => !x.IsDeleted && x.Code.ToLower() == code.NormalizeName().ToLower()).Any()) |
| | 2 | 31 | | { |
| | 2 | 32 | | throw new Exception($"Баркод '{code}' уже существует"); |
| | | 33 | | } |
| | | 34 | | |
| | 8 | 35 | | var barcode = new BarCode() |
| | 8 | 36 | | { |
| | 8 | 37 | | Code = code |
| | 8 | 38 | | }; |
| | 8 | 39 | | await _db.BarCodes.AddAsync(barcode); |
| | 8 | 40 | | await _db.SaveChangesAsync(CurrentUserId); |
| | | 41 | | |
| | 8 | 42 | | return barcode; |
| | 8 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<BarCode> GetBarcode(long id) |
| | 4 | 46 | | { |
| | 4 | 47 | | return await _db.BarCodes.Where(e => !e.IsDeleted).FirstAsync(b=>b.Id == id); |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task<BarCode> GetOrCreateBarcode(string code) |
| | 2 | 51 | | { |
| | 2 | 52 | | BarCode barcode = _db.BarCodes.FirstOrDefault(d => d.Code.ToUpper().Equals(code.NormalizeName().ToUpper())); |
| | 2 | 53 | | if (barcode == null) |
| | 1 | 54 | | { |
| | 1 | 55 | | barcode = new BarCode |
| | 1 | 56 | | { |
| | 1 | 57 | | Code = code |
| | 1 | 58 | | }; |
| | 1 | 59 | | await _db.BarCodes.AddAsync(barcode); |
| | 1 | 60 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 61 | | } |
| | | 62 | | |
| | 2 | 63 | | return barcode; |
| | 2 | 64 | | } |
| | | 65 | | |
| | | 66 | | public async Task<BarCode> GetBarcode(string code) => |
| | 4 | 67 | | await _db.BarCodes |
| | 4 | 68 | | .FirstOrDefaultAsync(x => !x.IsDeleted && x.Code.ToUpper() == code.NormalizeName().ToUpper()); |
| | | 69 | | |
| | | 70 | | public async Task UpdateGoodBarcodes(List<GoodBarcode> goodBarcodes) |
| | 0 | 71 | | { |
| | 0 | 72 | | for (int i = 0; i < goodBarcodes.Count; i++) |
| | 0 | 73 | | { |
| | 0 | 74 | | _db.GoodBarcodes.Update(goodBarcodes[i]); |
| | 0 | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<List<BarCode>> FindBarcodesByCodes(IEnumerable<string> codes) |
| | | 81 | | { |
| | 7 | 82 | | var cds = codes.Select(x => x.NormalizeName().ToLower()).ToList(); |
| | | 83 | | return await _db.BarCodes.Where(x => !x.IsDeleted && cds.Contains(x.Code.ToLower())).ToListAsync(); |
| | | 84 | | } |
| | | 85 | | public async Task UpdateGoodBarcode(GoodBarcode goodBarcodes) |
| | 0 | 86 | | { |
| | 0 | 87 | | _db.GoodBarcodes.Update(goodBarcodes); |
| | 0 | 88 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |