diff --git a/DepartmentPortal/Department/DepartmentBusinessLogic/BusinessLogics/OrderSyncHistoryBusinessLogic.cs b/DepartmentPortal/Department/DepartmentBusinessLogic/BusinessLogics/OrderSyncHistoryBusinessLogic.cs index b8b89b2..193c3d6 100644 --- a/DepartmentPortal/Department/DepartmentBusinessLogic/BusinessLogics/OrderSyncHistoryBusinessLogic.cs +++ b/DepartmentPortal/Department/DepartmentBusinessLogic/BusinessLogics/OrderSyncHistoryBusinessLogic.cs @@ -24,6 +24,20 @@ namespace DepartmentBusinessLogic.BusinessLogics /// public class OrderSyncHistoryBusinessLogic : GenericBusinessLogic { + private OrderSyncHistoryRecordBusinessLogic _recordLogic; + + private EnviromentSettingBusinessLogic _enviromentSettingLogic; + + private StudentGroupBusinessLogic _groupsLogic; + + private StudentBusinessLogic _studentLogic; + + private UserBusinessLogic _userLogic; + + private OrderBusinessLogic _orderLogic; + + private OrderStudentRecordBusinessLogic _orderStudentRecordLogic; + public OrderSyncHistoryBusinessLogic(IOrderSyncHistoryService service) : base(service, "Синхронизация Приказов", AccessOperation.СинхронизацияПриказов) { } public async Task SyncOrders() @@ -35,16 +49,20 @@ namespace DepartmentBusinessLogic.BusinessLogics return false; } - var recordLogic = DependencyManager.Instance.Resolve(); + _recordLogic = DependencyManager.Instance.Resolve(); + _enviromentSettingLogic = DependencyManager.Instance.Resolve(); + _groupsLogic = DependencyManager.Instance.Resolve(); + _studentLogic = DependencyManager.Instance.Resolve(); + _orderLogic = DependencyManager.Instance.Resolve(); + _orderStudentRecordLogic = DependencyManager.Instance.Resolve(); + _userLogic = DependencyManager.Instance.Resolve(); - var enviromentSettingLogic = DependencyManager.Instance.Resolve(); - - var address = (await enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderIpAddress" }))?.List?.FirstOrDefault(); + var address = (await _enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderIpAddress" }))?.List?.FirstOrDefault(); if (address == null || address.Value.IsEmpty()) { - Errors = enviromentSettingLogic.Errors; + Errors = _enviromentSettingLogic.Errors; Errors.Add(("Ошибка получения данных", "Не удалось получить адрес серверая для получения приказов по студентам")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -52,12 +70,12 @@ namespace DepartmentBusinessLogic.BusinessLogics return false; } - var username = (await enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderUserName" }))?.List?.FirstOrDefault(); + var username = (await _enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderUserName" }))?.List?.FirstOrDefault(); if (username == null || username.Value.IsEmpty()) { - Errors = enviromentSettingLogic.Errors; + Errors = _enviromentSettingLogic.Errors; Errors.Add(("Ошибка получения данных", "Не удалось получить имя пользователя для получения приказов по студентам")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -65,12 +83,12 @@ namespace DepartmentBusinessLogic.BusinessLogics return false; } - var password = (await enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderPassword" }))?.List?.FirstOrDefault(); + var password = (await _enviromentSettingLogic.GetListAsync(new EnviromentSettingGetBindingModel { Key = "SyncStudentOrderPassword" }))?.List?.FirstOrDefault(); if (password == null || password.Value.IsEmpty()) { - Errors = enviromentSettingLogic.Errors; + Errors = _enviromentSettingLogic.Errors; Errors.Add(("Ошибка получения данных", "Не удалось получить пароль для получения приказов по студентам")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -93,7 +111,7 @@ namespace DepartmentBusinessLogic.BusinessLogics if (!response.IsSuccessStatusCode) { Errors.Add(("Ошибка получения данных", "Не удалось получить список студентов с сервера")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -103,7 +121,7 @@ namespace DepartmentBusinessLogic.BusinessLogics var studentFromServer = JsonSerializer.Deserialize(response.Content.ReadAsStringAsync().Result); if (studentFromServer.CurrentStudentsList.Count == 0) { - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = "Полученный список студентов пустой" @@ -111,13 +129,12 @@ namespace DepartmentBusinessLogic.BusinessLogics return true; } - var groupsLogic = DependencyManager.Instance.Resolve(); - var groups = await groupsLogic.GetListAsync(new StudentGroupGetBindingModel()); + var groups = await _groupsLogic.GetListAsync(new StudentGroupGetBindingModel()); if (groups == null || groups.List == null) { - Errors = groupsLogic.Errors; + Errors = _groupsLogic.Errors; Errors.Add(("Ошибка получения данных", "Не удалось получить список групп")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -125,85 +142,90 @@ namespace DepartmentBusinessLogic.BusinessLogics return false; } - var studentLogic = DependencyManager.Instance.Resolve(); - var userLogic = DependencyManager.Instance.Resolve(); - foreach (var group in groups.List) + var students = await _studentLogic.GetListAsync(new StudentGetBindingModel()); + if (students == null || students.List == null) { - var students = await studentLogic.GetListAsync(new StudentGetBindingModel { StudentGroupId = group.Id }); - foreach (var student in students.List) + Errors = _studentLogic.Errors; + Errors.Add(("Ошибка получения данных", "Не удалось получить список студентов")); + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { - var studentSync = studentFromServer.CurrentStudentsList.FirstOrDefault(x => x.recordBookName == student.NumberOfBook); - // студент не найден, значит он ушел с кафедры, выясняем почему - if (studentSync == null) - { - await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv); - } - // перевод в другую группу - else if (group.GroupName != studentSync.groupName) - { - await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv); - } + OrderSyncHistoryId = history.Id, + Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) + }); + return false; + } - studentFromServer.CurrentStudentsList.Remove(studentSync); + foreach (var student in students.List) + { + var studentSync = studentFromServer.CurrentStudentsList.FirstOrDefault(x => x.recordBookName == student.NumberOfBook); + // студент не найден, значит он ушел с кафедры, выясняем почему + if (studentSync == null) + { + await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv); } - // новые студенты в группе - var studentSyncs = studentFromServer.CurrentStudentsList.Where(x => x.groupName == group.GroupName); - foreach (var student in studentSyncs) + // не совпадение групп + else if (student.StudentGroupName != studentSync.groupName) { - var userName = $"{student.lastName}{(student.firstName.IsNotEmpty() ? $" {student.firstName[0]}." : string.Empty)}{(student.patronymicName.IsNotEmpty() ? $"{student.patronymicName[0]}." : string.Empty)}"; - var result = await userLogic.GetListAsync(new UserGetBindingModel { UserNameForSearch = userName }); + await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv); + } - var newuser = await userLogic.CreateAsync(new UserSetBindingModel - { - Login = userName, - Password = student.recordBookName - }); - if (newuser == null) - { - var errors = userLogic.Errors; - errors.Add(("Ошибка создания пользователя под студента", $"Не удалось создать пользователя под студента {student.lastName} {student.firstName} {student.patronymicName}")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel - { - OrderSyncHistoryId = history.Id, - Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) - }); - continue; - } + studentFromServer.CurrentStudentsList.Remove(studentSync); + } + // новые студенты + foreach (var student in studentFromServer.CurrentStudentsList) + { + var userName = $"{student.lastName}{(student.firstName.IsNotEmpty() ? $" {student.firstName[0]}." : string.Empty)}{(student.patronymicName.IsNotEmpty() ? $"{student.patronymicName[0]}." : string.Empty)}"; + var result = await _userLogic.GetListAsync(new UserGetBindingModel { UserNameForSearch = userName }); - var newStudent = await studentLogic.CreateAsync(new StudentSetBindingModel - { - UserId = newuser.Id, - FirstName = student.firstName, - LastName = student.lastName, - Patronymic = student.patronymicName, - NumberOfBook = student.recordBookName, - StudentGroupId = group.Id, - StudentState = GetStudentState(student.stateName), - Description = student.presenatationOfRecordBook - }); - - if (newStudent == null) - { - var errors = studentLogic.Errors; - errors.Add(("Ошибка добавления студента", $"Не удалось добавить студента {student.lastName} {student.firstName} {student.patronymicName}")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel - { - OrderSyncHistoryId = history.Id, - Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) - }); - continue; - } - - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + var newuser = await _userLogic.CreateAsync(new UserSetBindingModel + { + Login = userName, + Password = student.recordBookName + }); + if (newuser == null) + { + var errors = _userLogic.Errors; + errors.Add(("Ошибка создания пользователя под студента", $"Не удалось создать пользователя под студента {student.lastName} {student.firstName} {student.patronymicName}")); + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, - Information = $"Добавлен студент {newStudent}" + Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) }); - - await SyncStudentOrders(history, newStudent, groups.List, client, address.Value, student.iduniv); - - studentFromServer.CurrentStudentsList.Remove(student); + continue; } + + var newStudent = await _studentLogic.CreateAsync(new StudentSetBindingModel + { + UserId = newuser.Id, + FirstName = student.firstName, + LastName = student.lastName, + Patronymic = student.patronymicName, + NumberOfBook = student.recordBookName, + StudentState = GetStudentState(student.stateName), + Description = student.presenatationOfRecordBook + }); + + if (newStudent == null) + { + var errors = _studentLogic.Errors; + errors.Add(("Ошибка добавления студента", $"Не удалось добавить студента {student.lastName} {student.firstName} {student.patronymicName}")); + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + { + OrderSyncHistoryId = history.Id, + Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) + }); + continue; + } + + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + { + OrderSyncHistoryId = history.Id, + Information = $"Добавлен студент {newStudent}" + }); + + await SyncStudentOrders(history, newStudent, groups.List, client, address.Value, student.iduniv); + + studentFromServer.CurrentStudentsList.Remove(student); } return true; @@ -228,14 +250,11 @@ namespace DepartmentBusinessLogic.BusinessLogics /// private async Task SyncStudentOrders(OrderSyncHistoryViewModel history, StudentViewModel student, List groups, HttpClient client, string address, string iduniv) { - var recordLogic = DependencyManager.Instance.Resolve(); - var orderLogic = DependencyManager.Instance.Resolve(); - var orderStudentRecordLogic = DependencyManager.Instance.Resolve(); HttpResponseMessage response = await client.GetAsync($"{address}/univer_Testing/hs/Ulstu_StudentsInfo/v1/GetStudentOrdersByIdAndRecordBook?iduniv={iduniv}&recordBookName={student.NumberOfBook}&allOrders=sppd"); if (!response.IsSuccessStatusCode) { Errors.Add(("Ошибка получения данных", "Не удалось получить список приказов по студенту")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message)) @@ -250,7 +269,7 @@ namespace DepartmentBusinessLogic.BusinessLogics if (orderType != OrderType.Неопределено) { // пытаемся найти приказ - var order = await orderLogic.GetElementAsync(new OrderGetBindingModel + var order = await _orderLogic.GetElementAsync(new OrderGetBindingModel { OrderNumber = syncOrder.clericNumber, OrderDate = Convert.ToDateTime(syncOrder.clericDate) @@ -258,7 +277,7 @@ namespace DepartmentBusinessLogic.BusinessLogics if (order == null) { // если не нашли - пытаемся создать - order = await orderLogic.CreateAsync(new OrderSetBindingModel + order = await _orderLogic.CreateAsync(new OrderSetBindingModel { OrderNumber = syncOrder.clericNumber, OrderDate = Convert.ToDateTime(syncOrder.clericDate), @@ -266,9 +285,9 @@ namespace DepartmentBusinessLogic.BusinessLogics }); if (order == null) { - var errors = orderLogic.Errors; + var errors = _orderLogic.Errors; errors.Add(("Ошибка добавления приказа", $"Не удалось добавить приказ {syncOrder.clericNumber} {syncOrder.clericDate}")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) @@ -278,7 +297,7 @@ namespace DepartmentBusinessLogic.BusinessLogics } // ищем в приказе запись по студенту - var studentOrder = await orderStudentRecordLogic.GetElementAsync(new OrderStudentRecordGetBindingModel + var studentOrder = await _orderStudentRecordLogic.GetElementAsync(new OrderStudentRecordGetBindingModel { OrderId = order.Id, StudentId = student.Id @@ -338,7 +357,7 @@ namespace DepartmentBusinessLogic.BusinessLogics } // создаем, если не нашли - studentOrder = await orderStudentRecordLogic.CreateAsync(new OrderStudentRecordSetBindingModel + studentOrder = await _orderStudentRecordLogic.CreateAsync(new OrderStudentRecordSetBindingModel { OrderId = order.Id, StudentId = student.Id, @@ -349,16 +368,41 @@ namespace DepartmentBusinessLogic.BusinessLogics }); if (studentOrder == null) { - var errors = orderStudentRecordLogic.Errors; - errors.Add(("Ошибка добавления записи приказа по студенту", $"Не удалось добавить запись приказаs {syncOrder.orderSubTypeName} по студенту {student}")); - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + var errors = _orderStudentRecordLogic.Errors; + errors.Add(("Ошибка добавления записи приказа по студенту", $"Не удалось добавить запись приказа {syncOrder.orderSubTypeName} по студенту {student}")); + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) }); continue; } - await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + + if (student.StudentGroupId != studentGroupToId) + { + var st = Mapper.MapToClass(student, true); + st.StudentGroupId = studentGroupToId; + var studentName = student.ToString(); + student = await _studentLogic.UpdateAsync(st); + if (student == null) + { + var errors = _studentLogic.Errors; + errors.Add(("Ошибка изменения по студенту", $"Не удалось изменить группу у студента {studentName}")); + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + { + OrderSyncHistoryId = history.Id, + Information = string.Join(Environment.NewLine, errors.Select(x => x.Message)) + }); + continue; + } + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel + { + OrderSyncHistoryId = history.Id, + Information = $"У студента {student} сменилась группа на {student.StudentGroupName}" + }); + } + + await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel { OrderSyncHistoryId = history.Id, Information = $"Добавили запись к приказу {order.OrderNumber} по студенту {student} с формулировкой {info}" diff --git a/DepartmentPortal/Department/DepartmentBusinessLogic/ViewModels/StudentViewModels.cs b/DepartmentPortal/Department/DepartmentBusinessLogic/ViewModels/StudentViewModels.cs index 39d9807..fc29b36 100644 --- a/DepartmentPortal/Department/DepartmentBusinessLogic/ViewModels/StudentViewModels.cs +++ b/DepartmentPortal/Department/DepartmentBusinessLogic/ViewModels/StudentViewModels.cs @@ -27,6 +27,9 @@ namespace DepartmentBusinessLogic.ViewModels [MapConfiguration("StudentGroupId")] public Guid? StudentGroupId { get; set; } + [MapConfiguration("StudentGroup.ToString", IsDifficle = true)] + public string StudentGroupName { get; set; } + [ViewModelControlListProperty("Номер зачетки")] [ViewModelControlElementProperty("Номер зачетки", ControlType.ControlString, MustHaveValue = true, ReadOnly = true)] [MapConfiguration("NumberOfBook")]