public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Order == null || request.Order.Id == 0 || request.Order.Items == null || request.Order.Items.Count == 0) { throw new BusinessException("Order.Retailer.Order.NotExisted"); } var retailerId = -1; if (request.LoginSession.Roles.FirstOrDefault(r => r == "Administrator") == null) { var retailer = await WebHelper.HttpGet <Retailer>(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetRetailerByUser}?userId={request.LoginSession.Id}", request.LoginSession.AccessToken); if (retailer == null) { throw new BusinessException("Retailer.NotExisted"); } retailerId = retailer.Id; } using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { retailerOrderRepository.JoinTransaction(conn, trans); retailerOrderQueries.JoinTransaction(conn, trans); productQueries.JoinTransaction(conn, trans); var order = await retailerOrderQueries.Get(request.Order.Id); if (order == null) { throw new BusinessException("Order.Retailer.Order.NotExisted"); } if (retailerId != -1 && retailerId != order.RetailerId) { throw new NotPermissionException(); } if (order.CreatedDate.AddSeconds(GlobalConfiguration.TimeoutOfRetailerOrder) < DateTime.Now) { throw new BusinessException("Order.Retailer.Order.TimeOut"); } decimal totalAmount = 0; await retailerOrderRepository.DeleteItems(request.Order.Id); foreach (var item in request.Order.Items) { var prod = (await WebHelper.HttpGet <IEnumerable <ProductViewModel> >(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProduct}?productId={item.ProductId}", request.LoginSession.AccessToken)).FirstOrDefault(p => p.CurrentUoM == item.UoMId); if (prod == null) { throw new BusinessException("Product.NotExisted"); } item.RetailerOrderId = request.Order.Id; item.StatusId = (int)RetailerOrderStatuses.Ordered; item.AdapQuantity = 0; item.DeliveriedQuantity = 0; item.Price = prod.Prices.FirstOrDefault(p => p.UoMId == item.UoMId).SellingPrice; await retailerOrderRepository.AddItem(item); totalAmount += item.Price * item.OrderedQuantity; } request.Order = UpdateBuild(request.Order, request.LoginSession); request.Order.RetailerId = order.RetailerId; request.Order.StatusId = (int)RetailerOrderStatuses.Ordered; request.Order.RetailerBuyingCalendarId = order.RetailerBuyingCalendarId; request.Order.TotalAmount = totalAmount; request.Order.CreatedBy = order.CreatedBy; request.Order.CreatedDate = order.CreatedDate; if (await retailerOrderRepository.Update(request.Order) != 0) { throw new Exception("Update Order failed"); } trans.Commit(); } catch (Exception ex) { try { trans.Rollback(); } catch { } throw ex; } } } return(0); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { productRepository.JoinTransaction(conn, trans); productQueries.JoinTransaction(conn, trans); if (request.Product == null) { throw new BusinessException("AddWrongInformation"); } var product = (await productQueries.Gets($"p.code = '{request.Product.Code}' and p.is_deleted = 0")).FirstOrDefault(); if (product != null) { throw new BusinessException("Product.ExistedCode"); } if (request.Product.ImageData?.Length > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData. if (request.Product.ImageData?.Length > 200) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Product.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Product.ImageData.Substring(request.Product.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Product.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.ProductImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } request.Product.Code = await productQueries.GenarateCode(); request.Product = CreateBuild(request.Product, request.LoginSession); var productId = await productRepository.Add(request.Product); // languages foreach (var item in request.Product.Languages) { item.ProductId = productId; await productRepository.AddOrUpdateLanguage(item); } // prices foreach (var item in request.Product.Prices) { item.ProductId = productId; await productRepository.AddPrice(item); } rs = 0; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } CommonHelper.DeleteImage(request.Product.ImageURL); } } } } return(rs); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Order == null || request.Order.FarmerId == 0 || request.Order.Items == null || request.Order.Items.Count == 0) { return(-1); } var rs = 0; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)) { try { farmerOrderRepository.JoinTransaction(conn, trans); farmerOrderQueries.JoinTransaction(conn, trans); productQueries.JoinTransaction(conn, trans); IEnumerable <FarmerRetailerOrderItems> farmerRetailerItems = null; if (request.Order.FarmerBuyingCalendarId == 0) { request.Order.FarmerBuyingCalendarId = null; } // update status for buying calendar if (request.Order.FarmerBuyingCalendarId != null) { farmerBuyingCalendarQueries.JoinTransaction(conn, trans); farmerRetailerOrderItemQueries.JoinTransaction(conn, trans); var buyingCalendar = await farmerBuyingCalendarQueries.Get(request.Order.FarmerBuyingCalendarId.Value); if (buyingCalendar != null) { farmerBuyingCalendarRepository.JoinTransaction(conn, trans); buyingCalendar = UpdateBuild(buyingCalendar, request.LoginSession); buyingCalendar.IsOrdered = true; var rsUpdate = await farmerBuyingCalendarRepository.Update(buyingCalendar); if (rs != 0) { throw new Exception("Update Buying calendar failed"); } farmerRetailerItems = await farmerRetailerOrderItemQueries.GetByBC(buyingCalendar.Id); } } request.Order.Code = await farmerOrderQueries.GenarateCode(); request.Order = CreateBuild(request.Order, request.LoginSession); request.Order.StatusId = (int)UI.FarmerOrderStatuses.BeginOrder; request.Order.Id = await farmerOrderRepository.Add(request.Order); decimal totalAmount = 0; foreach (var item in request.Order.Items) { var prod = (await WebHelper.HttpGet <IEnumerable <ProductViewModel> >(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProduct}?productId={item.ProductId}", request.LoginSession.AccessToken)).FirstOrDefault(p => p.CurrentUoM == item.UoMId); if (prod == null) { throw new Exception("Product doesn't existed"); } item.FarmerOrderId = request.Order.Id; item.StatusId = (int)UI.FarmerOrderStatuses.BeginOrder; item.DeliveriedQuantity = 0; item.Price = prod.BuyingCurrentPrice; var itemId = await farmerOrderRepository.AddItem(item); // Mapping From FarmerRetailer BuyingCalendar to FarmerRetailer Order if (farmerRetailerItems != null) { var FRItems = farmerRetailerItems.Where(i => i.ProductId == item.ProductId && i.UoMId == item.UoMId); int totalQuantity = item.DeliveriedQuantity; foreach (var FRItem in FRItems) { await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems() { IsPlanning = false, FarmerId = FRItem.FarmerId, FarmerOrderId = FRItem.FarmerOrderId, FarmerOrderItemId = FRItem.FarmerOrderItemId, RetailerId = FRItem.RetailerId, RetailerOrderId = FRItem.RetailerOrderId, RetailerOrderItemId = FRItem.RetailerOrderItemId, ProductId = item.ProductId, Quantity = (totalQuantity < FRItem.Quantity) ? totalQuantity : FRItem.Quantity, UoMId = item.UoMId }); totalQuantity = (totalQuantity - FRItem.Quantity < 0) ? 0 : totalQuantity - FRItem.Quantity; } } totalAmount += prod.BuyingCurrentPrice * item.OrderedQuantity; } request.Order.TotalAmount = totalAmount; if (await farmerOrderRepository.Update(request.Order) != 0) { throw new Exception("Update Order failed"); } } catch (Exception ex) { rs = -1; throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(0); }
public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken) { if (request.Order == null || request.Order.Id == 0 || request.Order.Items == null || request.Order.Items.Count == 0) { return(-1); } var rs = 0; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted)) { try { farmerOrderRepository.JoinTransaction(conn, trans); farmerOrderQueries.JoinTransaction(conn, trans); productQueries.JoinTransaction(conn, trans); var order = await farmerOrderQueries.Get(request.Order.Id); if (order.FarmerId != request.Order.FarmerId) { return(rs = -1); //fake api } #warning Hoang Uncompleted //check business update farmer order here bool isOk = false; if (isOk) { return(rs = -1); //Time out } decimal totalAmount = 0; await farmerOrderRepository.DeleteItems(request.Order.Id); foreach (var item in request.Order.Items) { var prod = (await productQueries.Get(item.ProductId)).FirstOrDefault(p => p.CurrentUoM == item.UoMId); if (prod == null) { throw new Exception("Product doesn't existed"); } item.Id = request.Order.Id; item.StatusId = (int)FarmerOrderStatuses.BeginOrder; item.DeliveriedQuantity = 0; item.Price = prod.BuyingCurrentPrice; await farmerOrderRepository.AddItem(item); totalAmount += prod.BuyingCurrentPrice * item.OrderedQuantity; } request.Order = UpdateBuild(request.Order, request.LoginSession); request.Order.StatusId = (int)FarmerOrderStatuses.BeginOrder; request.Order.FarmerBuyingCalendarId = order.FarmerBuyingCalendarId; request.Order.TotalAmount = totalAmount; if (await farmerOrderRepository.Update(request.Order) != 0) { throw new Exception("Update Order failed"); } } catch (Exception ex) { rs = -1; throw ex; } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Rollback(); } catch { } } } } } return(rs); }