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); }
public override async Task <int> HandleCommand(DeleteCompanyCommand request, CancellationToken cancellationToken) { Company company = null; if (request.Model == 0) { throw new BusinessException("Company.NotSelected"); } else { company = await companyQueries.GetByIdAsync(request.Model); if (company == null) { throw new BusinessException("Company.NotSelected"); } } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { company.IsDeleted = true; company.ModifiedDate = DateTime.Now; company.ModifiedBy = request.LoginSession.Id; if (await companyRepository.UpdateAsync(company) > 0) { rs = 0; } } catch (Exception ex) { throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public async Task <APIResult> GetById(int id) { var rs = await companyQueries.GetByIdAsync(id); return(new APIResult() { Result = 0, Data = rs }); }
public override async Task <int> HandleCommand(UpdateCompanyCommand request, CancellationToken cancellationToken) { Company company = null; if (request.Model == null || request.Model.Id == 0) { throw new BusinessException("Company.NotExisted"); } else { company = await companyQueries.GetByIdAsync(request.Model.Id); if (company == null) { throw new BusinessException("Company.NotExisted"); } } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Model.CreatedBy = company.CreatedBy; request.Model.CreatedDate = company.CreatedDate; request.Model.ModifiedDate = DateTime.Now; request.Model.ModifiedBy = request.LoginSession.Id; var filePath = await storageFile.getCompanyFilePathAsync(request.Model); if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0) { string oldLogoPath = company?.LogoPath ?? ""; string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Model.LogoPath = await storageFile.SaveCompanyLogo(filePath, type, base64Data); if (string.IsNullOrWhiteSpace(oldLogoPath)) { var imageFolderPath = settingQueries.GetValueAsync(SettingKeys.Path_Company); var scheduleAction = (new RemoveFileAction() { FilePath = $"{imageFolderPath}/{oldLogoPath}" }) .GetScheduleAction(request.LoginSession?.Id ?? 0); await scheduleActionRepository.AddAsync(scheduleAction); } } if (await companyRepository.UpdateAsync(request.Model) > 0) { rs = 0; } } catch (Exception ex) { throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }
public override async Task <int> HandleCommand(InsertCompanyCommand request, CancellationToken cancellationToken) { var id = 0; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { request.Model.Address.CreatedDate = DateTime.Now; request.Model.Address.CreatedBy = request.LoginSession?.Id ?? 0; request.Model.Address.ModifiedDate = DateTime.Now; request.Model.Address.ModifiedBy = request.LoginSession?.Id ?? 0; var addressId = await addressRepository.AddAsync(request.Model.Address); request.Model.Contact.CreatedDate = DateTime.Now; request.Model.Contact.CreatedBy = request.LoginSession?.Id ?? 0; request.Model.Contact.ModifiedDate = DateTime.Now; request.Model.Contact.ModifiedBy = request.LoginSession?.Id ?? 0; var contactId = await contactRepository.AddAsync(request.Model.Contact); request.Model.AddressId = addressId; request.Model.ContactId = contactId; request.Model.CreatedDate = DateTime.Now; request.Model.CreatedBy = request.LoginSession?.Id ?? 0; request.Model.ModifiedDate = DateTime.Now; request.Model.ModifiedBy = request.LoginSession?.Id ?? 0; id = await companyRepository.AddAsync(request.Model); Company company = await companyQueries.GetByIdAsync(id); var filePath = await storageFile.getCompanyFilePathAsync(company); if ((request.Model.ImageData?.Length ?? 0) > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } if (request.Model.IsChangedImage && (request.Model.ImageData?.Length ?? 0) > 0) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Model.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string base64Data = request.Model.ImageData.Substring(request.Model.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); company.LogoPath = await storageFile.SaveCompanyLogo(filePath, type, base64Data); await companyRepository.UpdateAsync(company); } } catch (Exception ex) { throw ex; } finally { if (id > 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(id); }