diff --git a/.idea/.idea.Policlinica/.idea/avalonia.xml b/.idea/.idea.Policlinica/.idea/avalonia.xml index 6551102..989b0ca 100644 --- a/.idea/.idea.Policlinica/.idea/avalonia.xml +++ b/.idea/.idea.Policlinica/.idea/avalonia.xml @@ -8,6 +8,7 @@ + diff --git a/Policlinica.sln.DotSettings.user b/Policlinica.sln.DotSettings.user index 6f204ae..d89b333 100644 --- a/Policlinica.sln.DotSettings.user +++ b/Policlinica.sln.DotSettings.user @@ -1,5 +1,6 @@  ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/Policlinica/App.axaml b/Policlinica/App.axaml index 62ecd0b..f7212b7 100644 --- a/Policlinica/App.axaml +++ b/Policlinica/App.axaml @@ -3,7 +3,6 @@ x:Class="Policlinica.App" xmlns:local="using:Policlinica" RequestedThemeVariant="Default"> - @@ -13,4 +12,4 @@ - \ No newline at end of file + diff --git a/Policlinica/DB/RecordItemsRepository.cs b/Policlinica/DB/RecordItemsRepository.cs index dc00ec3..e3073e5 100644 --- a/Policlinica/DB/RecordItemsRepository.cs +++ b/Policlinica/DB/RecordItemsRepository.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Microsoft.Extensions.Options; using MySqlConnector; @@ -49,4 +49,26 @@ public class RecordItemsRepository : BaseRep return result; } -} \ No newline at end of file + + public bool InsertRecordItem(RecordItem recordItem) + { + string sql = @"insert into `record_items` (record_id, service_id, service_price) + values (@record_id, @service_id, @service_price)"; + try + { + using (var mc = new MySqlCommand(sql, connection)) + { + mc.Parameters.AddWithValue("@record_id", recordItem.RecordId); + mc.Parameters.AddWithValue("@service_id", recordItem.ServiceId); + mc.Parameters.AddWithValue("@service_price", recordItem.ServicePrice); + mc.ExecuteNonQuery(); + } + return true; + } + catch (Exception e) + { + Console.WriteLine(e); + } + return false; + } +} diff --git a/Policlinica/DB/RecordRep.cs b/Policlinica/DB/RecordRep.cs index 18455ea..10e72ce 100644 --- a/Policlinica/DB/RecordRep.cs +++ b/Policlinica/DB/RecordRep.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Microsoft.Extensions.Options; using MySqlConnector; @@ -57,6 +57,66 @@ public class RecordRep:BaseRep } return recordsList; } + + public int InsertRecord(Record record) + { + string insertSql = @"insert into `records` (client_name, client_surname, doctor_id, user_id, service_id, total_amount, record_date) + values (@client_name, @client_surname, @doctor_id, @user_id, @service_id, @total_amount, @record_date)"; + try + { + using (var mc = new MySqlCommand(insertSql, connection)) + { + mc.Parameters.AddWithValue("@client_name", record.ClientName ?? ""); + mc.Parameters.AddWithValue("@client_surname", record.ClientSurname ?? ""); + mc.Parameters.AddWithValue("@doctor_id", record.DoctorId); + mc.Parameters.AddWithValue("@user_id", record.UserId); + mc.Parameters.AddWithValue("@service_id", record.ServiceId); + mc.Parameters.AddWithValue("@total_amount", record.TotalAmount); + mc.Parameters.AddWithValue("@record_date", record.RecordDate); + + int result = mc.ExecuteNonQuery(); + Console.WriteLine($"ExecuteNonQuery returned: {result}"); + } + + // Получаем ID последней вставленной записи + string lastIdSql = "SELECT LAST_INSERT_ID() as last_id"; + using (var mc = new MySqlCommand(lastIdSql, connection)) + { + object result = mc.ExecuteScalar(); + Console.WriteLine($"ExecuteScalar result type: {result?.GetType()}, value: {result}"); + + if (result != null) + { + if (result is long longId) + { + Console.WriteLine($"Got long ID: {longId}"); + return (int)longId; + } + else if (result is int intId) + { + Console.WriteLine($"Got int ID: {intId}"); + return intId; + } + else if (long.TryParse(result.ToString(), out long parsedId)) + { + Console.WriteLine($"Parsed long ID: {parsedId}"); + return (int)parsedId; + } + } + else + { + Console.WriteLine("ExecuteScalar returned null"); + } + } + } + catch (Exception e) + { + Console.WriteLine($"Error in InsertRecord: {e.Message}"); + Console.WriteLine($"Stack trace: {e.StackTrace}"); + } + return -1; + } + public bool Delete(int id) { string sql = @"delete from `records` where `id` = @id"; @@ -78,4 +138,4 @@ public class RecordRep:BaseRep return false; } -} \ No newline at end of file +} diff --git a/Policlinica/DB/UserRepository.cs b/Policlinica/DB/UserRepository.cs index 32a3b78..d7a2a32 100644 --- a/Policlinica/DB/UserRepository.cs +++ b/Policlinica/DB/UserRepository.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Threading.Tasks; @@ -88,6 +88,7 @@ public class UserRepository:BaseRep Id = reader.GetInt32("id"), Name = reader.GetString("name"), Password = reader.GetString("password"), + Login = reader.GetString("name") }); } } @@ -172,4 +173,4 @@ public class UserRepository:BaseRep } -} \ No newline at end of file +} diff --git a/Policlinica/Models/User.cs b/Policlinica/Models/User.cs index bfa5c11..a939c86 100644 --- a/Policlinica/Models/User.cs +++ b/Policlinica/Models/User.cs @@ -1,4 +1,4 @@ -namespace Policlinica.DB; +namespace Policlinica.DB; public class User { @@ -6,7 +6,9 @@ public class User public string Name { get; set; } + public string Surname { get; set; } + public string Password {get; set;} public string Login { get; set; } -} \ No newline at end of file +} diff --git a/Policlinica/Policlinica.csproj b/Policlinica/Policlinica.csproj index d9c49eb..fbff73c 100644 --- a/Policlinica/Policlinica.csproj +++ b/Policlinica/Policlinica.csproj @@ -1,10 +1,12 @@ - + + WinExe net9.0 enable app.manifest true + true @@ -17,7 +19,6 @@ - None All diff --git a/Policlinica/ViewModels/AutorizationViewModel.cs b/Policlinica/ViewModels/AutorizationViewModel.cs index 8fa5b32..1d3496e 100644 --- a/Policlinica/ViewModels/AutorizationViewModel.cs +++ b/Policlinica/ViewModels/AutorizationViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -35,15 +35,19 @@ public partial class AutorizationViewModel : ViewModelBase SpUser = repository.CheckLoginAndPassword(Login, Password); } - _user.Password = Password; - _user.Login = Login; - if (SpUser.Count == 0) { Eror = "Неверный логин или пароль"; return; } - + + // Копируем все данные авторизованного пользователя в синглтон + _user.Id = SpUser[0].Id; + _user.Name = SpUser[0].Name; + _user.Surname = SpUser[0].Surname; + _user.Login = SpUser[0].Login; + _user.Password = SpUser[0].Password; + CurrentUser.login = SpUser[0].Name; var vm = ActivatorUtilities.CreateInstance(_provider); @@ -58,4 +62,4 @@ public partial class AutorizationViewModel : ViewModelBase _navigation.Navigate(vm); } -} \ No newline at end of file +} diff --git a/Policlinica/ViewModels/DoctorViewModel.cs b/Policlinica/ViewModels/DoctorViewModel.cs index 3eb5301..0601cc3 100644 --- a/Policlinica/ViewModels/DoctorViewModel.cs +++ b/Policlinica/ViewModels/DoctorViewModel.cs @@ -32,14 +32,16 @@ public partial class DoctorViewModel : ViewModelBase public void StartTest() { - if (Name == null) + if (Name == null || Name.Trim() == "") + return; + if (Surname == null || Surname.Trim() == "") return; - if (Surname == null) - return; if (SelectedDoctor == null) return; - var vm = ActivatorUtilities.CreateInstance(_provider, SelectedDoctor); + + var repository = _provider.GetRequiredService(); + var vm = ActivatorUtilities.CreateInstance(_provider, SelectedDoctor, repository, Name, Surname); _navigation.Navigate(vm); } -} \ No newline at end of file +} diff --git a/Policlinica/ViewModels/RecordItemsViewModel.cs b/Policlinica/ViewModels/RecordItemsViewModel.cs index 2d6a5c2..e7c00f7 100644 --- a/Policlinica/ViewModels/RecordItemsViewModel.cs +++ b/Policlinica/ViewModels/RecordItemsViewModel.cs @@ -1,68 +1,135 @@ -using System; +using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.DependencyInjection; using Policlinica.DB; -/* + namespace Policlinica.ViewModels; public partial class RecordItemsViewModel : ViewModelBase { - private readonly IServiceProvider _provider; private readonly Navigation _navigation; - - [ObservableProperty] List _services; - [ObservableProperty] Service _selectedService; - private RecordItemsRepository _repository; + private readonly RecordRep _recordRepository; + private readonly RecordItemsRepository _recordItemsRepository; + [ObservableProperty] ObservableCollection selectedServices; + [ObservableProperty] Doctor selectedDoctor; + [ObservableProperty] DateTime recordDate; + [ObservableProperty] decimal totalAmount; + [ObservableProperty] User currentUser; + [ObservableProperty] string statusMessage; + [ObservableProperty] string clientName; + [ObservableProperty] string clientSurname; - - - public RecordItemsViewModel(IServiceProvider provider, Service selectedService, List services, RecordItemsRepository repository) + public RecordItemsViewModel(IServiceProvider provider, Navigation navigation, Doctor doctor, + List services, RecordRep recordRepository, RecordItemsRepository recordItemsRepository, + string name = "", string surname = "") { _provider = provider; - _services = services; - _selectedService = selectedService; - _repository = repository; + _navigation = navigation; + _recordRepository = recordRepository; + _recordItemsRepository = recordItemsRepository; + selectedDoctor = doctor; + selectedServices = new ObservableCollection(services); + recordDate = DateTime.Now; + currentUser = _provider.GetRequiredService(); + clientName = name; + clientSurname = surname; + + // Расчет общей суммы + totalAmount = selectedServices.Sum(s => s.Price); } - - - [RelayCommand] - public void SaveDB() + public void SaveToDatabase() { - - _repository.GetRecordItemsByTest(Records, Services); - if (SelectedDoctor == null) + if (selectedDoctor == null || selectedServices.Count == 0) + { + StatusMessage = "Ошибка: не все данные заполнены"; return; - var vm = ActivatorUtilities.CreateInstance(_provider); - _navigation.Navigate(vm); - - - } - - [RelayCommand] - public void Start() - { - if (SelectedService == null) - return; - var vm = _serviceProvider.GetRequiredService(); - var win = _serviceProvider.GetRequiredService(); - - vm.SetClose(win.Close); - win.DataContext = vm; - win.Show(); - close(); - } - private Action close; + } - public void SetClose(Action close) + if (string.IsNullOrWhiteSpace(clientName) || string.IsNullOrWhiteSpace(clientSurname)) + { + StatusMessage = "Ошибка: не заполнены имя и фамилия клиента"; + return; + } + + try + { + // Используем первую услугу + int mainServiceId = selectedServices[0].Id; + Console.WriteLine($"Main service ID: {mainServiceId}"); + Console.WriteLine($"Selected services count: {selectedServices.Count}"); + + // Создаем запись с данными КЛИЕНТА + var record = new Record + { + ClientName = clientName, + ClientSurname = clientSurname, + DoctorId = selectedDoctor.Id, + UserId = currentUser.Id, + ServiceId = mainServiceId, + TotalAmount = totalAmount, + RecordDate = recordDate + }; + + Console.WriteLine($"Saving record: Name={record.ClientName}, Surname={record.ClientSurname}, DoctorId={record.DoctorId}, UserId={record.UserId}, ServiceId={record.ServiceId}"); + + // Сохраняем запись в БД и получаем ID + int recordId = _recordRepository.InsertRecord(record); + + if (recordId <= 0) + { + StatusMessage = "Ошибка при сохранении записи"; + Console.WriteLine($"Failed to insert record. Returned ID: {recordId}"); + return; + } + + Console.WriteLine($"Record saved with ID: {recordId}"); + + // Сохраняем все выбранные услуги в record_items + foreach (var service in selectedServices) + { + Console.WriteLine($"Saving record item for service: {service.Id} (price: {service.Price})"); + + var recordItem = new RecordItem + { + ServiceId = service.Id, + RecordId = recordId, + ServicePrice = service.Price + }; + + bool itemSaved = _recordItemsRepository.InsertRecordItem(recordItem); + if (!itemSaved) + { + Console.WriteLine($"Failed to insert record item for service {service.Id}"); + } + } + + StatusMessage = "Запись успешно сохранена!"; + + // Переходим в админ-панель + var vm = ActivatorUtilities.CreateInstance(_provider); + _navigation.Navigate(vm); + } + catch (Exception ex) + { + StatusMessage = $"Ошибка: {ex.Message}"; + Console.WriteLine($"Exception: {ex}"); + } + } + + [RelayCommand] + public void Cancel() { - this.close = close; + var repository = _provider.GetRequiredService(); + var vm = ActivatorUtilities.CreateInstance(_provider, selectedDoctor, repository, clientName, clientSurname); + _navigation.Navigate(vm); } } -*/ \ No newline at end of file diff --git a/Policlinica/ViewModels/ServiceViewModel.cs b/Policlinica/ViewModels/ServiceViewModel.cs index 3580281..5af1f64 100644 --- a/Policlinica/ViewModels/ServiceViewModel.cs +++ b/Policlinica/ViewModels/ServiceViewModel.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -20,17 +20,19 @@ public partial class ServiceViewModel : ViewModelBase [ObservableProperty] string surname; [ObservableProperty] string name; - [ObservableProperty] ObservableCollection _services; + [ObservableProperty] ObservableCollection _services; [ObservableProperty] string _login; [ObservableProperty] Doctor _selectedDoctor; public ServiceViewModel(IServiceProvider provider, Navigation navigation, Doctor selectedDoctor, - ServiceRepository repository) + ServiceRepository repository, string clientName = "", string clientSurname = "") { _provider = provider; _navigation = navigation; _selectedDoctor = selectedDoctor; _serviceRepository = repository; + name = clientName; + surname = clientSurname; Services = new ObservableCollection(repository.GetServicesByDoctors(selectedDoctor.Id).Select(service => new ServiceSelected(service)).ToList()); //Console.WriteLine(CurrentUser.login); @@ -38,25 +40,32 @@ public partial class ServiceViewModel : ViewModelBase [RelayCommand] - public void Dobavlenie() { + List selectedServices = new List(); + + foreach (ServiceSelected s in Services) { - List services = new List(); - - foreach (ServiceSelected s in Services) + if (s.IsSelected == true) { - if (s.IsSelected == true) - { - services.Add(s.Service); - } + selectedServices.Add(s.Service); } - - var vm = ActivatorUtilities.CreateInstance(_provider, SelectedDoctor); - _navigation.Navigate(vm); - } + if (selectedServices.Count == 0) + { + // Можно добавить уведомление об ошибке + return; } + // Получаем репозитории для передачи в ViewModel + var recordRepository = _provider.GetRequiredService(); + var recordItemsRepository = _provider.GetRequiredService(); + + // Создаем ViewModel для показа подтверждения записи + var vm = ActivatorUtilities.CreateInstance(_provider, + _navigation, _selectedDoctor, selectedServices, recordRepository, recordItemsRepository, Name, Surname); + + _navigation.Navigate(vm); } +} diff --git a/Policlinica/Views/RecordItemsView.axaml b/Policlinica/Views/RecordItemsView.axaml index 6a2aa15..1a15527 100644 --- a/Policlinica/Views/RecordItemsView.axaml +++ b/Policlinica/Views/RecordItemsView.axaml @@ -1,8 +1,69 @@ - - + xmlns:viewModels="clr-namespace:Policlinica.ViewModels" + mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="600" + x:Class="Policlinica.Views.RecordItemsView" + x:DataType="viewModels:RecordItemsViewModel"> + + + + + + + + + Клиент: + + + + + + + + + Врач: + + + + + + + + Выбранные услуги: + + + + + + + + + + + + + + + + + + + + + + + + + + + +