public async Task <ActionResult> Create([Bind(Include = "CustomerID,Name,Budget,Cargo,OrderDate,DriverID")] Customers customers) { if (ModelState.IsValid) { db.Customers.Add(customers); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.DriverID = new SelectList(db.Drivers, "ID", "FullName", customers.DriverID); return(View(customers)); }
public async Task <ActionResult> Create([Bind(Include = "ID,CustomerID,DriverID,DeparturePoint,ArrivalPoint,Distance,DepartureDate,ArrivalDate")] Logistics logistics) { if (ModelState.IsValid) { db.Logistics.Add(logistics); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "Name", logistics.CustomerID); ViewBag.DriverID = new SelectList(db.Drivers, "ID", "FullName", logistics.DriverID); return(View(logistics)); }
public async Task CreateAsync(T model, bool saved = true) { _db.Set <T>().Add(model); try { if (saved) { await _db.SaveChangesAsync(); } } catch (Exception e) { throw e; } }
public async Task HandleAsync(GenerateShipmentItems command) { context.Database.ExecuteSqlRaw("TRUNCATE TABLE [BoxItems]"); var parts = context.Parts.AsQueryable(); var boxes = await context.Boxes.OrderBy(x => x.Weight).Select(x => x).ToListAsync(); var localItems = context.BoxItems.Local.AsQueryable(); var minPartCount = 2; var boxesPartCounts = new Dictionary <long, int>(); //since the deterministic factor is not thepart cost or weight, it is box weight and min part count foreach (var box in boxes) { var partCountsToAvoid = boxesPartCounts.Values.ToList(); var pendingItems = localItems.Select(x => x.PartNumber).ToList(); if (box.Weight % minPartCount == 0) { var partsToInsert = await parts .Where(x => x.PartWeight == box.Weight / minPartCount && !pendingItems.Contains(x.PartNumber)) .Take(minPartCount) .ToListAsync(); partsToInsert.ForEach(x => context.BoxItems.Add(new BoxItemEntity { BoxId = box.BoxId, PartNumber = x.PartNumber, PartCost = x.PartWeight * 7 + 50, PartWeight = x.PartWeight })); box.PartCount = partsToInsert.Count(); minPartCount++; continue; } var firstItem = await parts.OrderBy(x => x.PartWeight) .FirstOrDefaultAsync(x => x.PartWeight == Math.Ceiling(box.Weight / minPartCount) && !pendingItems.Contains(x.PartNumber)); var remainningIems = await parts .Where(x => x.PartWeight == firstItem.PartWeight - 1 && !pendingItems.Contains(x.PartNumber)) .Take(minPartCount - 1) .ToListAsync(); remainningIems.Add(firstItem); remainningIems.ForEach(x => context.BoxItems.Add(new BoxItemEntity { BoxId = box.BoxId, PartNumber = x.PartNumber, PartCost = x.PartWeight * 7 + 50, PartWeight = x.PartWeight })); box.PartCount = remainningIems.Count(); minPartCount++; } await context.SaveChangesAsync(); }
/// <summary> /// 生成运货单 /// </summary> /// <param name="code">货物id</param> /// <param name="truckId">卡车id</param> /// <returns></returns> public async Task <bool> ToWayBill(string code, Guid truckId) { //查询状态是否正确--->修改订单中的状态--->生成waybill--->生成waybillLink bool flag = true; Order order; using (var orderservice = new OrderService()) { order = orderservice.GetAll().Where(p => p.BarCode == code).FirstOrDefault(); if (order == null || order.Status != "2") { flag = false; } } if (flag) { var context = new LogisticsContext(); using (var transaction = context.Database.BeginTransaction()) { try { //修改订单的状态 order.Status = "3"; context.Entry(order).State = EntityState.Modified; await context.SaveChangesAsync(); //创建waybill WayBill wayBill = new WayBill() { FinishTime = DateTime.Now, PlanPath = "NULL" }; context.Set <WayBill>().Add(wayBill); await context.SaveChangesAsync(); //创建truck waybill联系 WaybillTransportLink transportLink = new WaybillTransportLink() { TransportInfoId = truckId, WayBillId = wayBill.Id }; context.Set <WaybillTransportLink>().Add(transportLink); await context.SaveChangesAsync(); //创建order waybill联系 OrderWaybillLink orderWaybillLink = new OrderWaybillLink() { OrderId = order.Id, WaybillId = wayBill.Id }; context.Set <OrderWaybillLink>().Add(orderWaybillLink); await context.SaveChangesAsync(); transaction.Commit(); } catch (Exception errer) { transaction.Rollback(); flag = false; throw errer; } finally { context.Dispose(); } } } return(flag); }