public virtual async Task <GTINValidationCodes> CheckNewGTINAsync(Company company, GTIN gTIN, UserSession session) { var rs = this.ValidationFormat(gTIN); if (rs != GTINValidationCodes.GTINValid) { throw rs.GetException(); } var gTINs = await _gTINQueries.GetByCompanyCodeAsync(company.GS1Code); if (gTINs.Where(x => x.Numeric == gTIN.Numeric && x.PartnerId == company.Id).Count() > 0) { return(GTINValidationCodes.AlreadyExist); } var sessionBuffers = await _sessionBufferService.GetByCompanyIdAsync(company.Id, SessionBufferTypes.GTIN); var now = DateTime.Now; if (sessionBuffers.Where(x => x.ExpiredDate > now && x.SessionId != session.SessionId && (JsonConvert.DeserializeObject <GTIN>(x.DataJson)?.Numeric ?? 0) == gTIN.Numeric) .Count() > 0) { return(GTINValidationCodes.UsedByAnotherSession); } return(GTINValidationCodes.GTINValid); }
public async Task <GTIN> GenerateGTINAsync(int partnerId, GTINTypes gTINTypes, UserSession session) { var company = await companyQueries.GetByIdAsync(partnerId); var productions = (await productionQueries.GetAllAsync(company.Id)).ToList(); var gTINId = productions.Select(x => x.GTINId); var gTINs = await gTINQueries.GetByCompanyCodeAsync(company.GS1Code); long maxNumericExist = (gTINs.Max(x => (long?)x.Numeric) ?? 0) + 1; var sessionBuffers = await _sessionBufferService.GetByCompanyIdAsync(company.Id, SessionBufferTypes.GTIN); var now = DateTime.Now; long maxNumericBuffer = sessionBuffers .Where(x => x.ExpiredDate > now && x.PartnerId == partnerId) .Select(x => JsonConvert.DeserializeObject <GTIN>(x.DataJson)?.Numeric) .Max() ?? 0; long numericGenerate = (maxNumericExist > maxNumericBuffer ? maxNumericExist : maxNumericBuffer) + 1; GTIN gTIN = new GTIN() { CompanyCode = company.GS1Code, Numeric = numericGenerate, PartnerId = partnerId, Type = gTINTypes, UsedDate = DateTime.Now, IsUsed = true, ModifiedBy = session.Id, ModifiedDate = DateTime.Now, CreatedBy = session.Id, CreatedDate = DateTime.Now }; var id = await _sessionBufferService.InsertOrUpdateAsync(new SessionBuffer() { PartnerId = company.Id, SessionId = session.SessionId, DataJson = JsonConvert.SerializeObject(gTIN), ExpiredDate = DateTime.Now.AddHours(2), Type = SessionBufferTypes.GTIN, }, session); return(gTIN); }