public virtual DataSet PostOrder(ulong[] orderIds, uint[] quanties, string[] messages, uint[] orderCodes1, uint[] orderCodes2, bool[] junks) { if (orderIds == null || quanties == null || orderCodes1 == null || orderCodes2 == null || junks == null || orderIds.Length != quanties.Length || orderIds.Length != orderCodes1.Length || orderIds.Length != orderCodes2.Length || orderIds.Length != junks.Length) { return(null); } var toOrders = ToOrder.FromRequest(orderIds, quanties, messages, orderCodes1, orderCodes2, junks); using (var session = _factory.OpenSession()) using (var trx = session.BeginTransaction()) { var user = ServiceContext.User; var rules = session.Load <OrderRules>(user.Client.Id); var offers = OfferQuery.GetByIds(session, user, orderIds); var orders = new List <Order>(); foreach (var toOrder in toOrders) { var offer = offers.FirstOrDefault(o => o.Id.CoreId == toOrder.OfferId); if (offer == null) { continue; } var order = orders.FirstOrDefault(o => o.PriceList.PriceCode == offer.PriceList.Id.Price.PriceCode); if (order == null) { order = new Order(offer.PriceList, ServiceContext.User, rules); order.ClientAddition = toOrder.Message; orders.Add(order); } toOrder.OrderItem = order.AddOrderItem(offer, toOrder.Quantity); } session.SaveEach(orders.Where(x => x.OrderItems.Count > 0)); trx.Commit(); } return(BuildOrderReport(toOrders)); }
public virtual DataSet PostOrder2(ulong[] orderIds, decimal[] cost, uint[] quanties, DateTime[] priceDates, string[] messages, uint[] orderCodes1, uint[] orderCodes2, bool[] junks) { orderIds = orderIds ?? new ulong[0]; cost = cost ?? new decimal[0]; quanties = quanties ?? new uint[0]; messages = messages ?? new string[0]; orderCodes1 = orderCodes1 ?? new uint[0]; orderCodes2 = orderCodes2 ?? new uint[0]; junks = junks ?? new bool[0]; priceDates = priceDates ?? new DateTime[0]; var length = orderIds.Length; if (cost.Length != length || quanties.Length != length || orderCodes1.Length != length || orderCodes2.Length != length || junks.Length != length || priceDates.Length != length) { log.Warn("Некорректное число параметров"); return(null); } using (var session = _factory.OpenSession()) using (var trx = session.BeginTransaction()) { var user = ServiceContext.User; var rules = session.Load <OrderRules>(user.Client.Id); List <ActivePrice> activePrices; using (StorageProcedures.GetActivePrices((MySqlConnection)session.Connection, user.Id)) { activePrices = session.Query <ActivePrice>().ToList(); } var orders = new List <Order>(); var toOrders = ToOrder.FromRequest(orderIds, quanties, cost, priceDates, messages, orderCodes1, orderCodes2, junks); var offers = OfferQuery.GetByIds(session, user, toOrders.Select(o => o.OfferId)); foreach (var toOrder in toOrders) { try { if (toOrder.Cost <= 0) { throw new OrderException($"Цена не может быть меньше или равной нулю, текущее значение цены {cost}"); } var offer = offers.FirstOrDefault(o => o.Id.CoreId == toOrder.OfferId); if (offer == null) { var archiveOffer = session.Get <ArchiveOffer>(toOrder.OfferId); if (archiveOffer == null) { log.WarnFormat("Не удалось найти предложение в архиве {0} игнорирую строку", toOrder.OfferId); continue; } offer = archiveOffer.ToOffer(activePrices, toOrder.Cost); if (offer == null) { log.WarnFormat("Прайс {0} больше не доступен клиенту игнорирую строку {1}", archiveOffer.PriceList.PriceCode, toOrder.OfferId); continue; } } else if ((decimal)offer.Cost != toOrder.Cost) { log.WarnFormat("Заявка сделана по неактуальному прайс-листу код предложения {0}", toOrder.OfferId); offer = Offer.Clone(offer, offer.PriceList, (float)toOrder.Cost, toOrder.OfferId); } if (toOrder.PriceDate > offer.PriceList.PriceDate) { throw new OrderException( $"Дата прайс-листа {toOrder.PriceDate} по позиции {toOrder.OfferId} больше текущей даты прайс-листа {offer.PriceList.PriceDate}"); } var order = orders.FirstOrDefault(o => o.PriceList == offer.PriceList.Id.Price && o.PriceDate == toOrder.PriceDate); if (order == null) { order = new Order(offer.PriceList, user, rules); order.PriceDate = toOrder.PriceDate; order.ClientAddition = toOrder.Message; orders.Add(order); } toOrder.OrderItem = order.AddOrderItem(offer, toOrder.Quantity); } catch (OrderException e) { log.Warn($"Не удалось сформировать заявку по позиции {toOrder.OfferId}", e); } } session.SaveEach(orders.Where(x => x.OrderItems.Count > 0)); trx.Commit(); return(BuildOrderReport(toOrders)); } }