Доделки по синхронизации

This commit is contained in:
kotcheshir73 2021-04-27 19:10:38 +04:00
parent 8169b49a94
commit 68c01f1801
2 changed files with 147 additions and 100 deletions

View File

@ -24,6 +24,20 @@ namespace DepartmentBusinessLogic.BusinessLogics
/// </summary>
public class OrderSyncHistoryBusinessLogic : GenericBusinessLogic<OrderSyncHistoryGetBindingModel, OrderSyncHistorySetBindingModel, OrderSyncHistoryListViewModel, OrderSyncHistoryViewModel>
{
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<bool> SyncOrders()
@ -35,16 +49,20 @@ namespace DepartmentBusinessLogic.BusinessLogics
return false;
}
var recordLogic = DependencyManager.Instance.Resolve<OrderSyncHistoryRecordBusinessLogic>();
_recordLogic = DependencyManager.Instance.Resolve<OrderSyncHistoryRecordBusinessLogic>();
_enviromentSettingLogic = DependencyManager.Instance.Resolve<EnviromentSettingBusinessLogic>();
_groupsLogic = DependencyManager.Instance.Resolve<StudentGroupBusinessLogic>();
_studentLogic = DependencyManager.Instance.Resolve<StudentBusinessLogic>();
_orderLogic = DependencyManager.Instance.Resolve<OrderBusinessLogic>();
_orderStudentRecordLogic = DependencyManager.Instance.Resolve<OrderStudentRecordBusinessLogic>();
_userLogic = DependencyManager.Instance.Resolve<UserBusinessLogic>();
var enviromentSettingLogic = DependencyManager.Instance.Resolve<EnviromentSettingBusinessLogic>();
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<StudentListSyncModel>(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<StudentGroupBusinessLogic>();
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,11 +142,19 @@ namespace DepartmentBusinessLogic.BusinessLogics
return false;
}
var studentLogic = DependencyManager.Instance.Resolve<StudentBusinessLogic>();
var userLogic = DependencyManager.Instance.Resolve<UserBusinessLogic>();
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 });
Errors = _studentLogic.Errors;
Errors.Add(("Ошибка получения данных", "Не удалось получить список студентов"));
await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
{
OrderSyncHistoryId = history.Id,
Information = string.Join(Environment.NewLine, Errors.Select(x => x.Message))
});
return false;
}
foreach (var student in students.List)
{
var studentSync = studentFromServer.CurrentStudentsList.FirstOrDefault(x => x.recordBookName == student.NumberOfBook);
@ -138,31 +163,30 @@ namespace DepartmentBusinessLogic.BusinessLogics
{
await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv);
}
// перевод в другую группу
else if (group.GroupName != studentSync.groupName)
// не совпадение групп
else if (student.StudentGroupName != studentSync.groupName)
{
await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv);
}
studentFromServer.CurrentStudentsList.Remove(studentSync);
}
// новые студенты в группе
var studentSyncs = studentFromServer.CurrentStudentsList.Where(x => x.groupName == group.GroupName);
foreach (var student in studentSyncs)
// новые студенты
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 result = await _userLogic.GetListAsync(new UserGetBindingModel { UserNameForSearch = userName });
var newuser = await userLogic.CreateAsync(new UserSetBindingModel
var newuser = await _userLogic.CreateAsync(new UserSetBindingModel
{
Login = userName,
Password = student.recordBookName
});
if (newuser == null)
{
var errors = userLogic.Errors;
var errors = _userLogic.Errors;
errors.Add(("Ошибка создания пользователя под студента", $"Не удалось создать пользователя под студента {student.lastName} {student.firstName} {student.patronymicName}"));
await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
{
OrderSyncHistoryId = history.Id,
Information = string.Join(Environment.NewLine, errors.Select(x => x.Message))
@ -170,23 +194,22 @@ namespace DepartmentBusinessLogic.BusinessLogics
continue;
}
var newStudent = await studentLogic.CreateAsync(new StudentSetBindingModel
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;
var errors = _studentLogic.Errors;
errors.Add(("Ошибка добавления студента", $"Не удалось добавить студента {student.lastName} {student.firstName} {student.patronymicName}"));
await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
{
OrderSyncHistoryId = history.Id,
Information = string.Join(Environment.NewLine, errors.Select(x => x.Message))
@ -194,7 +217,7 @@ namespace DepartmentBusinessLogic.BusinessLogics
continue;
}
await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
await _recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
{
OrderSyncHistoryId = history.Id,
Information = $"Добавлен студент {newStudent}"
@ -204,7 +227,6 @@ namespace DepartmentBusinessLogic.BusinessLogics
studentFromServer.CurrentStudentsList.Remove(student);
}
}
return true;
}
@ -228,14 +250,11 @@ namespace DepartmentBusinessLogic.BusinessLogics
/// <returns></returns>
private async Task SyncStudentOrders(OrderSyncHistoryViewModel history, StudentViewModel student, List<StudentGroupViewModel> groups, HttpClient client, string address, string iduniv)
{
var recordLogic = DependencyManager.Instance.Resolve<OrderSyncHistoryRecordBusinessLogic>();
var orderLogic = DependencyManager.Instance.Resolve<OrderBusinessLogic>();
var orderStudentRecordLogic = DependencyManager.Instance.Resolve<OrderStudentRecordBusinessLogic>();
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<StudentViewModel, StudentSetBindingModel>(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}"

View File

@ -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")]