было! готово

This commit is contained in:
a.puchkina 2024-05-15 10:59:19 +04:00
parent e07e520f57
commit a97cf5f041
17 changed files with 154 additions and 67 deletions

View File

@ -66,8 +66,12 @@ namespace TransportLogisticBusinessLogic
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
model.Status = status;
if (model.Status == OrderStatus.Доставлен) model.DateDelivered = DateTime.Now;
_orderStorage.Update(model);
if (model.Status == OrderStatus.Выдан) model.DateDelivered = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
model.Status--;
return false;
}
return true;
}

View File

@ -4,5 +4,7 @@
{
public int? Id { get; set; }
public int? CustomerId { get; set; }
public DateTime? DateCreate { get; set; }
public DateTime? DateDelivered { get; set;}
}
}

View File

@ -14,6 +14,9 @@ namespace TransportLogisticDatabaseImplement.Implement
{
using var context = new TransportLogisticDatabase();
return context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.ThenInclude(x => x.Transport)
.Include(x => x.Goods)
@ -29,6 +32,9 @@ namespace TransportLogisticDatabaseImplement.Implement
if (model.Id.HasValue)
{
return context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.ThenInclude(x => x.Transport)
.Include(x => x.Goods)
@ -41,6 +47,9 @@ namespace TransportLogisticDatabaseImplement.Implement
else if (model.CustomerId.HasValue)
{
return context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.ThenInclude(x => x.Transport)
.Include(x => x.Goods)
@ -61,6 +70,9 @@ namespace TransportLogisticDatabaseImplement.Implement
}
using var context = new TransportLogisticDatabase();
return context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.ThenInclude(x => x.Transport)
.Include(x => x.Goods)
@ -79,7 +91,15 @@ namespace TransportLogisticDatabaseImplement.Implement
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
return context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.ThenInclude(x => x.Transport)
.Include(x => x.Goods)
.ThenInclude(x => x.Good)
.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
@ -95,10 +115,12 @@ namespace TransportLogisticDatabaseImplement.Implement
}
order.Update(model);
context.SaveChanges();
order.UpdateTransports(context, model);
order.UpdateGoods(context, model);
transaction.Commit();
return order.GetViewModel;
return context.Orders.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.Include(x => x.Goods).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
catch
{
@ -111,12 +133,18 @@ namespace TransportLogisticDatabaseImplement.Implement
{
using var context = new TransportLogisticDatabase();
var element = context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.Include(x => x.Transports)
.Include(x => x.Goods)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.StartStorage)
.Include(x => x.FinishStorage)
.Include(x => x.Customer)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);

View File

@ -12,7 +12,7 @@ using TransportLogisticDatabaseImplement;
namespace TransportLogisticDatabaseImplement.Migrations
{
[DbContext(typeof(TransportLogisticDatabase))]
[Migration("20240514175153_InitialCreate")]
[Migration("20240515053955_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -100,14 +100,13 @@ namespace TransportLogisticDatabaseImplement.Migrations
b.Property<int>("Status")
.HasColumnType("int");
b.Property<int?>("StorageId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("StorageId");
b.HasIndex("FinishStorageId");
b.HasIndex("StartStorageId");
b.ToTable("Orders");
});
@ -207,15 +206,29 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Order", b =>
{
b.HasOne("TransportLogisticDatabaseImplement.Models.Customer", null)
.WithMany("Order")
b.HasOne("TransportLogisticDatabaseImplement.Models.Customer", "Customer")
.WithMany("Orders")
.HasForeignKey("CustomerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", null)
.WithMany("Orders")
.HasForeignKey("StorageId");
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", "FinishStorage")
.WithMany("FinishOrders")
.HasForeignKey("FinishStorageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", "StartStorage")
.WithMany("StartOrders")
.HasForeignKey("StartStorageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Customer");
b.Navigation("FinishStorage");
b.Navigation("StartStorage");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.OrderGood", b =>
@ -258,7 +271,7 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Customer", b =>
{
b.Navigation("Order");
b.Navigation("Orders");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Good", b =>
@ -275,7 +288,9 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Storage", b =>
{
b.Navigation("Orders");
b.Navigation("FinishOrders");
b.Navigation("StartOrders");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Transport", b =>

View File

@ -80,8 +80,7 @@ namespace TransportLogisticDatabaseImplement.Migrations
DateDelivered = table.Column<DateTime>(type: "datetime2", nullable: true),
StartStorageId = table.Column<int>(type: "int", nullable: false),
FinishStorageId = table.Column<int>(type: "int", nullable: false),
CustomerId = table.Column<int>(type: "int", nullable: false),
StorageId = table.Column<int>(type: "int", nullable: true)
CustomerId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
@ -93,10 +92,17 @@ namespace TransportLogisticDatabaseImplement.Migrations
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Orders_Storages_StorageId",
column: x => x.StorageId,
name: "FK_Orders_Storages_FinishStorageId",
column: x => x.FinishStorageId,
principalTable: "Storages",
principalColumn: "Id");
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
table.ForeignKey(
name: "FK_Orders_Storages_StartStorageId",
column: x => x.StartStorageId,
principalTable: "Storages",
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
});
migrationBuilder.CreateTable(
@ -169,9 +175,14 @@ namespace TransportLogisticDatabaseImplement.Migrations
column: "CustomerId");
migrationBuilder.CreateIndex(
name: "IX_Orders_StorageId",
name: "IX_Orders_FinishStorageId",
table: "Orders",
column: "StorageId");
column: "FinishStorageId");
migrationBuilder.CreateIndex(
name: "IX_Orders_StartStorageId",
table: "Orders",
column: "StartStorageId");
migrationBuilder.CreateIndex(
name: "IX_OrderTransports_OrderId",

View File

@ -97,14 +97,13 @@ namespace TransportLogisticDatabaseImplement.Migrations
b.Property<int>("Status")
.HasColumnType("int");
b.Property<int?>("StorageId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("StorageId");
b.HasIndex("FinishStorageId");
b.HasIndex("StartStorageId");
b.ToTable("Orders");
});
@ -204,15 +203,29 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Order", b =>
{
b.HasOne("TransportLogisticDatabaseImplement.Models.Customer", null)
.WithMany("Order")
b.HasOne("TransportLogisticDatabaseImplement.Models.Customer", "Customer")
.WithMany("Orders")
.HasForeignKey("CustomerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", null)
.WithMany("Orders")
.HasForeignKey("StorageId");
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", "FinishStorage")
.WithMany("FinishOrders")
.HasForeignKey("FinishStorageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("TransportLogisticDatabaseImplement.Models.Storage", "StartStorage")
.WithMany("StartOrders")
.HasForeignKey("StartStorageId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Customer");
b.Navigation("FinishStorage");
b.Navigation("StartStorage");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.OrderGood", b =>
@ -255,7 +268,7 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Customer", b =>
{
b.Navigation("Order");
b.Navigation("Orders");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Good", b =>
@ -272,7 +285,9 @@ namespace TransportLogisticDatabaseImplement.Migrations
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Storage", b =>
{
b.Navigation("Orders");
b.Navigation("FinishOrders");
b.Navigation("StartOrders");
});
modelBuilder.Entity("TransportLogisticDatabaseImplement.Models.Transport", b =>

View File

@ -18,7 +18,7 @@ namespace TransportLogisticDatabaseImplement.Models
public string Email { get; private set; } = string.Empty;
[ForeignKey("CustomerId")]
public virtual List<Order> Order { get; set; } = new();
public virtual List<Order> Orders { get; set; } = new();
public static Customer? Create(CustomerBindingModel model)
{

View File

@ -11,7 +11,9 @@ namespace TransportLogisticDatabaseImplement.Models
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Weight { get; private set; }
[Required]
public string Size { get; private set; } = string.Empty;
[ForeignKey("GoodId")]

View File

@ -10,19 +10,20 @@ namespace TransportLogisticDatabaseImplement.Models
public class Order : IOrderModel
{
public int Id { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public DateTime? DateDelivered { get; set; }
[Required]
public int StartStorageId { get; set; }
[Required]
[InverseProperty("StartOrders")]
public virtual Storage StartStorage { get; set; }
public int FinishStorageId { get; set; }
[Required]
[InverseProperty("FinishOrders")]
public virtual Storage FinishStorage { get; set; }
public int CustomerId { get; set; }
public virtual Customer Customer { get; set; }
[Required]
private Dictionary<int, (ITransportModel, int)>? _orderTransports = null;
@ -61,7 +62,7 @@ namespace TransportLogisticDatabaseImplement.Models
[ForeignKey("OrderId")]
public virtual List<OrderGood> Goods { get; set; } = new();
public static Order? Create(TransportLogisticDatabase context, OrderBindingModel? model)
public static Order? Create(TransportLogisticDatabase context, OrderBindingModel model)
{
if (model == null)
{
@ -107,8 +108,11 @@ namespace TransportLogisticDatabaseImplement.Models
DateCreate = DateCreate,
DateDelivered = DateDelivered,
StartStorageId = StartStorageId,
StartStorageName = StartStorage.Name,
FinishStorageId = FinishStorageId,
FinishStorageName = FinishStorage.Name,
CustomerId = CustomerId,
CustomerName = Customer.Lastname,
OrderTransports = OrderTransports,
OrderGoods = OrderGoods
};

View File

@ -16,8 +16,10 @@ namespace TransportLogisticDatabaseImplement.Models
[Required]
public string Address { get; private set; } = string.Empty;
[ForeignKey("StorageId")]
public virtual List<Order> Orders{ get; set; } = new();
[InverseProperty("StartStorage")]
public virtual List<Order> StartOrders { get; set; } = new();
[InverseProperty("FinishStorage")]
public virtual List<Order> FinishOrders { get; set; } = new();
public static Storage? Create(StorageBindingModel model)
{

View File

@ -9,7 +9,7 @@ namespace TransportLogisticDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=PC-Anna\SQLEXPRESS;Initial Catalog=TransportLogisticDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
optionsBuilder.UseSqlServer(@"Data Source=PC-Anna\SQLEXPRESS;Initial Catalog=TransportLogisticDatabase2;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"
);
}
base.OnConfiguring(optionsBuilder);

View File

@ -105,7 +105,7 @@ namespace TransportLogisticForm
dataGridView.Rows.Clear();
foreach (var pc in _orderGoods)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Name, pc.Value.Item2 });
dataGridView1.Rows.Add(new object[] { pc.Key, pc.Value.Item1.Name, pc.Value.Item2 });
}
foreach (var pc in _orderTransports)
{
@ -140,12 +140,16 @@ namespace TransportLogisticForm
}
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
var model = new OrderBindingModel
{
Id = _id ?? 0,
StartStorageId = Convert.ToInt32(comboBoxStart.SelectedValue),
FinishStorageId = Convert.ToInt32(comboBoxFinish.SelectedValue),
CustomerId = Convert.ToInt32(comboBoxCustomer.SelectedValue)
});
CustomerId = Convert.ToInt32(comboBoxCustomer.SelectedValue),
OrderTransports = _orderTransports,
OrderGoods = _orderGoods,
};
var operationResult = _id.HasValue ? _logicO.Update(model) : _logicO.Create(model);
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа.Дополнительная информация в логах.");
@ -263,12 +267,12 @@ namespace TransportLogisticForm
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
if (dataGridView1.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormOrderGoods));
if (service is FormOrderGoods form)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
form.Id = id;
form.GoodsCount = _orderGoods[id].Item2;
if (form.ShowDialog() == DialogResult.OK)
@ -286,13 +290,13 @@ namespace TransportLogisticForm
private void button2_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
if (dataGridView1.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_orderGoods?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
_orderGoods?.Remove(Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{

View File

@ -76,7 +76,7 @@
//
// textBox1
//
textBox1.Location = new Point(98, 107);
textBox1.Location = new Point(98, 65);
textBox1.Name = "textBox1";
textBox1.Size = new Size(269, 27);
textBox1.TabIndex = 6;
@ -92,7 +92,7 @@
//
// textBox2
//
textBox2.Location = new Point(98, 65);
textBox2.Location = new Point(98, 103);
textBox2.Name = "textBox2";
textBox2.Size = new Size(269, 27);
textBox2.TabIndex = 8;

View File

@ -67,28 +67,28 @@ namespace TransportLogisticForm
// ЗаказчикиToolStripMenuItem
//
ЗаказчикиToolStripMenuItem.Name = "ЗаказчикиToolStripMenuItem";
ЗаказчикиToolStripMenuItem.Size = new Size(180, 24);
ЗаказчикиToolStripMenuItem.Size = new Size(152, 24);
ЗаказчикиToolStripMenuItem.Text = "Заказчики";
ЗаказчикиToolStripMenuItem.Click += ЗаказчикиToolStripMenuItem_Click;
//
// ТоварыToolStripMenuItem
//
ТоварыToolStripMenuItem.Name = "ТоварыToolStripMenuItem";
ТоварыToolStripMenuItem.Size = new Size(180, 24);
ТоварыToolStripMenuItem.Size = new Size(152, 24);
ТоварыToolStripMenuItem.Text = "Товары";
ТоварыToolStripMenuItem.Click += ТоварыToolStripMenuItem_Click;
//
// СкладыToolStripMenuItem
//
СкладыToolStripMenuItem.Name = "СкладыToolStripMenuItem";
СкладыToolStripMenuItem.Size = new Size(180, 24);
СкладыToolStripMenuItem.Size = new Size(152, 24);
СкладыToolStripMenuItem.Text = "Склады";
СкладыToolStripMenuItem.Click += СкладыToolStripMenuItem_Click;
//
// ТранспортToolStripMenuItem
//
ТранспортToolStripMenuItem.Name = "ТранспортToolStripMenuItem";
ТранспортToolStripMenuItem.Size = new Size(180, 24);
ТранспортToolStripMenuItem.Size = new Size(152, 24);
ТранспортToolStripMenuItem.Text = "Транспорт";
ТранспортToolStripMenuItem.Click += ТранспортToolStripMenuItem_Click;
//
@ -126,7 +126,7 @@ namespace TransportLogisticForm
buttonTakeOrderInWork.Name = "buttonTakeOrderInWork";
buttonTakeOrderInWork.Size = new Size(205, 53);
buttonTakeOrderInWork.TabIndex = 2;
buttonTakeOrderInWork.Text = "Заказ выполняется";
buttonTakeOrderInWork.Text = "Заказ в пути";
buttonTakeOrderInWork.UseVisualStyleBackColor = true;
buttonTakeOrderInWork.Click += buttonTakeOrderInWork_Click;
//
@ -136,7 +136,7 @@ namespace TransportLogisticForm
buttonOrderReady.Name = "buttonOrderReady";
buttonOrderReady.Size = new Size(205, 53);
buttonOrderReady.TabIndex = 3;
buttonOrderReady.Text = "Заказ готов";
buttonOrderReady.Text = "Заказ доставлен";
buttonOrderReady.UseVisualStyleBackColor = true;
buttonOrderReady.Click += buttonOrderReady_Click;
//

View File

@ -33,8 +33,8 @@ namespace TransportLogisticForm
dataGridView.Columns["FinishStorageName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["CustomerId"].Visible = false;
dataGridView.Columns["CustomerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["OrderTransports"].Visible = true;
dataGridView.Columns["OrderGoods"].Visible = true;
dataGridView.Columns["OrderTransports"].Visible = false;
dataGridView.Columns["OrderGoods"].Visible = false;
}
}
catch (Exception ex)

View File

@ -29,6 +29,7 @@ namespace TransportLogisticForm
public int GoodsCount { get { return Convert.ToInt32(textBox.Text); } set { textBox.Text = value.ToString(); } }
public FormOrderGoods(IGoodLogic logic)
{
InitializeComponent();
_list = logic.ReadList(null);
if (_list != null)
{
@ -37,7 +38,6 @@ namespace TransportLogisticForm
comboBox.DataSource = _list;
comboBox.SelectedItem = null;
}
InitializeComponent();
}
private void buttonSave_Click(object sender, EventArgs e)

View File

@ -29,6 +29,7 @@ namespace TransportLogisticForm
public int TransportsCount { get { return Convert.ToInt32(textBox.Text); } set { textBox.Text = value.ToString(); } }
public FormOrderTransports(ITransportLogic logic)
{
InitializeComponent();
_list = logic.ReadList(null);
if (_list != null)
{
@ -37,7 +38,6 @@ namespace TransportLogisticForm
comboBox.DataSource = _list;
comboBox.SelectedItem = null;
}
InitializeComponent();
}
private void buttonSave_Click(object sender, EventArgs e)