69 lines
2.8 KiB
C#
69 lines
2.8 KiB
C#
using Microsoft.Extensions.Options;
|
|
using MySqlConnector;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AutoService.Models
|
|
{
|
|
public class ProceedRepository : BaseRepository, IProceedRepository
|
|
{
|
|
public ProceedRepository(IOptions<DatabaseSettings> databaseSettings) : base(databaseSettings)
|
|
{
|
|
}
|
|
|
|
public void SaveOrder(IEnumerable<Work> works, Order order)
|
|
{
|
|
try
|
|
{
|
|
OpenConnection();
|
|
string sql1 = "INSERT INTO orders (id, client_name, car_model, service_id, total_amount, discount_percent, order_date) VALUES(0, @ClientName, @AutoModel, @ServiceId, @TotalAmount, @DiscountPercent, '@Date);";
|
|
string sql2 = "SELECT LAST_INSERT_ID() as id;";
|
|
string sql3 = "INSERT INTO auto_service_db.order_items (id, order_id, work_id, work_price) VALUES(0, @OrderId, @WorkId, @WorkPrice);";
|
|
using var transaction = connection.BeginTransaction();
|
|
int newId;
|
|
try
|
|
{
|
|
using (var mc = new MySqlCommand(sql1, connection, transaction))
|
|
{
|
|
mc.Parameters.AddWithValue("@ClientName", order.ClientName);
|
|
mc.Parameters.AddWithValue("@AutoModel", order.AutoModel);
|
|
mc.Parameters.AddWithValue("@ServiceId", order.ServiceId);
|
|
mc.Parameters.AddWithValue("@TotalAmount", order.TotalPrice);
|
|
mc.Parameters.AddWithValue("@DiscountPercent", order.DiscountPercent);
|
|
mc.Parameters.AddWithValue("@Date", DateTime.Now);
|
|
mc.ExecuteNonQuery();
|
|
}
|
|
using (var mc = new MySqlCommand(sql2, connection, transaction))
|
|
using (var reader = mc.ExecuteReader())
|
|
{
|
|
newId = reader.GetInt32("id");
|
|
}
|
|
foreach (var work in works)
|
|
{
|
|
using (var mc = new MySqlCommand(sql3, connection, transaction))
|
|
{
|
|
mc.Parameters.AddWithValue("@OrderId", newId);
|
|
mc.Parameters.AddWithValue("@WorkId", work.Id);
|
|
mc.Parameters.AddWithValue("@WorkPrice", work.Price);
|
|
mc.ExecuteNonQuery();
|
|
}
|
|
}
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex);
|
|
}
|
|
CloseConnection();
|
|
}catch(Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
}
|
|
}
|
|
}
|