синхронизация приказов

This commit is contained in:
kotcheshir73 2021-04-27 17:09:03 +04:00
parent b8ba47f2b9
commit c52d235a6c
5 changed files with 178 additions and 9 deletions

View File

@ -99,9 +99,10 @@ namespace DatabaseCore
using var context = DatabaseManager.GetContext;
// для одной записи
if (model.Id.HasValue)
if (model.Id.HasValue || AdditionalCheckForSingleGet(model))
{
var entity = IncludingWhenReading(context.Set<T>().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value);
var entity = model.Id.HasValue ? IncludingWhenReading(context.Set<T>().AsQueryable()).FirstOrDefault(x => x.Id == model.Id.Value)
: GetSingleRecord(context.Set<T>().AsQueryable(), model);
if (entity == null)
{
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
@ -225,5 +226,9 @@ namespace DatabaseCore
/// <param name="query"></param>
/// <returns></returns>
protected abstract IQueryable<T> IncludingWhenReading(IQueryable<T> query);
protected virtual bool AdditionalCheckForSingleGet(G model) => false;
protected virtual T GetSingleRecord(IQueryable<T> list, G model) => null;
}
}

View File

@ -136,12 +136,12 @@ namespace DepartmentBusinessLogic.BusinessLogics
// студент не найден, значит он ушел с кафедры, выясняем почему
if (studentSync == null)
{
await SyncStudentOrders(history, student, client, address.Value, studentSync.iduniv);
await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv);
}
// перевод в другую группу
else if (group.GroupName != studentSync.groupName)
{
await SyncStudentOrders(history, student, client, address.Value, studentSync.iduniv);
await SyncStudentOrders(history, student, groups.List, client, address.Value, studentSync.iduniv);
}
studentFromServer.CurrentStudentsList.Remove(studentSync);
@ -200,7 +200,7 @@ namespace DepartmentBusinessLogic.BusinessLogics
Information = $"Добавлен студент {newStudent}"
});
await SyncStudentOrders(history, newStudent, client, address.Value, student.iduniv);
await SyncStudentOrders(history, newStudent, groups.List, client, address.Value, student.iduniv);
studentFromServer.CurrentStudentsList.Remove(student);
}
@ -221,14 +221,16 @@ namespace DepartmentBusinessLogic.BusinessLogics
/// </summary>
/// <param name="history"></param>
/// <param name="student"></param>
/// <param name="groups"></param>
/// <param name="client"></param>
/// <param name="address"></param>
/// <param name="iduniv"></param>
/// <returns></returns>
private async Task SyncStudentOrders(OrderSyncHistoryViewModel history, StudentViewModel student, HttpClient client, string address, string iduniv)
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)
{
@ -244,10 +246,133 @@ namespace DepartmentBusinessLogic.BusinessLogics
foreach (var syncOrder in syncOrders.StudentOrders)
{
var orderType = GetOrderType(syncOrder.orderSubTypeName);
// отбираем только приказы, которые нас интересуют
if (orderType != OrderType.Неопределено)
{
var order = await orderLogic.GetListAsync(new OrderGetBindingModel { OrderNumber = syncOrder.clericNumber, OrderDate = Convert.ToDateTime(syncOrder.clericDate) });
// пытаемся найти приказ
var order = await orderLogic.GetElementAsync(new OrderGetBindingModel
{
OrderNumber = syncOrder.clericNumber,
OrderDate = Convert.ToDateTime(syncOrder.clericDate)
});
if (order == null)
{
// если не нашли - пытаемся создать
order = await orderLogic.CreateAsync(new OrderSetBindingModel
{
OrderNumber = syncOrder.clericNumber,
OrderDate = Convert.ToDateTime(syncOrder.clericDate),
OrderType = orderType
});
if (order == null)
{
var errors = orderLogic.Errors;
errors.Add(("Ошибка добавления приказа", $"Не удалось добавить приказ {syncOrder.clericNumber} {syncOrder.clericDate}"));
await recordLogic.CreateAsync(new OrderSyncHistoryRecordSetBindingModel
{
OrderSyncHistoryId = history.Id,
Information = string.Join(Environment.NewLine, errors.Select(x => x.Message))
});
continue;
}
}
// ищем в приказе запись по студенту
var studentOrder = await orderStudentRecordLogic.GetElementAsync(new OrderStudentRecordGetBindingModel
{
OrderId = order.Id,
StudentId = student.Id
});
if (studentOrder == null)
{
Guid? studentGroupFromId = null;
Guid? studentGroupToId = null;
string info = string.Empty;
if (syncOrder.groupNameBefore.IsNotEmpty())
{
studentGroupFromId = groups.FirstOrDefault(x => x.ToString() == syncOrder.groupNameBefore)?.Id;
}
if (syncOrder.groupNameAfter.IsNotEmpty())
{
studentGroupToId = groups.FirstOrDefault(x => x.ToString() == syncOrder.groupNameAfter)?.Id;
}
OrderStudentMoveType orderStudentMoveType = OrderStudentMoveType.Неопределено;
switch (syncOrder.orderSubTypeName)
{
case "Перевод":
// внутренний перевод
if (studentGroupFromId.HasValue && studentGroupToId.HasValue)
{
orderStudentMoveType = OrderStudentMoveType.ПереводВГруппу;
info = $"Перевод студента {student} из группы {groups.First(x => x.Id == studentGroupFromId)} в группу {groups.First(x => x.Id == studentGroupToId)}";
}
else if (!studentGroupFromId.HasValue && studentGroupToId.HasValue)
{
orderStudentMoveType = OrderStudentMoveType.ЗачислитьПоПереводу;
info = $"Зачисление переовдом студента {student} в группу {groups.First(x => x.Id == studentGroupToId)}";
}
else if (studentGroupFromId.HasValue && !studentGroupToId.HasValue)
{
orderStudentMoveType = OrderStudentMoveType.ОтчислитьВСвязиСПереводом;
info = $"Отчисление студента {student} из группы {groups.First(x => x.Id == studentGroupFromId)} в связи с переводом";
}
break;
case "Зачисление в вуз":
orderStudentMoveType = OrderStudentMoveType.Зачисление;
info = $"Зачисление студента {student}";
break;
case "Распределение по группам":
orderStudentMoveType = OrderStudentMoveType.Распределение;
info = $"Распределение студента {student} в группу {groups.First(x => x.Id == studentGroupToId)}";
break;
case "Перевод на следующий курс":
orderStudentMoveType = OrderStudentMoveType.ПереводНаКурс;
info = $"Перевод студента {student} на следующий курс";
break;
}
if (orderStudentMoveType == OrderStudentMoveType.Неопределено)
{
continue;
}
// создаем, если не нашли
studentOrder = await orderStudentRecordLogic.CreateAsync(new OrderStudentRecordSetBindingModel
{
OrderId = order.Id,
StudentId = student.Id,
Info = info,
OrderStudentMoveType = orderStudentMoveType,
StudentGroupFromId = studentGroupFromId,
StudentGroupToId = studentGroupToId
});
if (studentOrder == null)
{
var errors = orderStudentRecordLogic.Errors;
errors.Add(("Ошибка добавления записи приказа по студенту", $"Не удалось добавить запись приказаs {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
{
OrderSyncHistoryId = history.Id,
Information = $"Добавили запись к приказу {order.OrderNumber} по студенту {student} с формулировкой {info}"
});
if (orderStudentMoveType == OrderStudentMoveType.ОтчислитьВСвязиСПереводом ||
orderStudentMoveType == OrderStudentMoveType.ОтчислитьЗаНевыходСАкадема ||
orderStudentMoveType == OrderStudentMoveType.ОтчислитьЗаНеуспевамость ||
orderStudentMoveType == OrderStudentMoveType.ОтчислитьПоЗавершению ||
orderStudentMoveType == OrderStudentMoveType.ОтчислитьПоСобственному)
{
return;
}
}
}
}
}

View File

@ -5,6 +5,8 @@
/// </summary>
public enum OrderStudentMoveType
{
Неопределено = -1,
Зачисление = 0,
Распределение = 1,
@ -33,6 +35,8 @@
ОтчислитьПоЗавершению = 12,
ОтчислитьЗаНевыходСАкадема = 14
ОтчислитьЗаНевыходСАкадема = 14,
Перевод = 15
}
}

View File

@ -49,5 +49,22 @@ namespace DepartmentDatabaseImplementation.Implementations
protected override IQueryable<Order> OrderingWhenReading(IQueryable<Order> query) => query.OrderBy(x => x.OrderDate).ThenBy(x => x.OrderNumber);
protected override bool AdditionalCheckForSingleGet(OrderGetBindingModel model)
{
if (model.OrderDate.HasValue && model.OrderNumber.IsNotEmpty())
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override Order GetSingleRecord(IQueryable<Order> list, OrderGetBindingModel model)
{
if (model.OrderDate.HasValue && model.OrderNumber.IsNotEmpty())
{
return list.FirstOrDefault(x => x.OrderDate == model.OrderDate && x.OrderNumber == model.OrderNumber);
}
return base.GetSingleRecord(list, model);
}
}
}

View File

@ -46,5 +46,23 @@ namespace DepartmentDatabaseImplementation.Implementations
protected override IQueryable<OrderStudentRecord> IncludingWhenReading(IQueryable<OrderStudentRecord> query) => query.Include(x => x.Order).Include(x => x.Student).Include(x => x.StudentGroupFrom).Include(x => x.StudentGroupTo);
protected override IQueryable<OrderStudentRecord> OrderingWhenReading(IQueryable<OrderStudentRecord> query) => query.OrderBy(x => x.Order.OrderDate).ThenBy(x => x.Order.OrderNumber).ThenBy(x => x.OrderStudentMoveType).ThenBy(x => x.Student.LastName).ThenBy(x => x.Student.FirstName);
protected override bool AdditionalCheckForSingleGet(OrderStudentRecordGetBindingModel model)
{
if (model.OrderId.HasValue && model.StudentId.HasValue)
{
return true;
}
return base.AdditionalCheckForSingleGet(model);
}
protected override OrderStudentRecord GetSingleRecord(IQueryable<OrderStudentRecord> list, OrderStudentRecordGetBindingModel model)
{
if (model.OrderId.HasValue && model.StudentId.HasValue)
{
return list.FirstOrDefault(x => x.OrderId == model.OrderId && x.StudentId == model.StudentId);
}
return base.GetSingleRecord(list, model);
}
}
}