Пример #1
0
        private async Task HandleBlock(int objectId, Granit block, ObjectAddEditRequest request)
        {
            if (request.BlockTypeId.HasValue)
            {
                if (block != null)
                {
                    block.BlockNumber = request.BlockNumber;
                    block.BlockTypeId = request.BlockTypeId.Value;

                    await _granitRepository.UpdateAsync(block);
                }
                else
                {
                    var granit = new Granit
                    {
                        ObjectId    = objectId,
                        BlockNumber = request.BlockNumber,
                        BlockTypeId = request.BlockTypeId.Value
                    };

                    await _granitRepository.AddAsync(granit);
                }
            }
            else
            {
                if (block != null)
                {
                    await _granitRepository.DeleteByIdAsync(block.Id);
                }
            }
        }
Пример #2
0
        public async Task CreateLog(
            ClaimsPrincipal principal,
            EventEnum eventEnum,
            Objects oldVehicle,
            Objects newVehicle,
            Granit oldGranit,
            Granit newGranit)
        {
            await WrapCall(async() =>
            {
                var trackableProperties = new string[]
                {
                    nameof(Objects.ProjectId),
                    nameof(Objects.ObjId),
                    nameof(Objects.CarTypeId),
                    nameof(Objects.Name),
                    nameof(Objects.CarBrandId),
                    nameof(Objects.ProviderId),
                    nameof(Objects.LastRouteId),
                    nameof(Objects.ObjectOutput),
                    nameof(Objects.ObjectOutputDate),
                    nameof(Objects.Phone),
                    nameof(Objects.YearRelease)
                };

                var fields = GetChangedFields(
                    oldVehicle,
                    newVehicle,
                    trackableProperties);

                if (oldGranit != null || newGranit != null)
                {
                    var granitFields = GetChangedFields(
                        oldGranit,
                        newGranit,
                        nameof(Granit.BlockNumber), nameof(Granit.BlockTypeId));

                    fields.AddRange(granitFields);
                }

                if (fields.Any())
                {
                    var entityId   = oldVehicle != null ? oldVehicle.Id : newVehicle.Id;
                    var entityName = newVehicle != null ? newVehicle.Name : oldVehicle.Name;
                    var entityType = nameof(Objects);

                    if ((fields.Count == 1 && fields[0].FieldName == nameof(Objects.LastRouteId)) ||
                        fields.Count == 2 && fields.Any(x => x.FieldName == nameof(Objects.LastRouteId)) && fields.Any(x => x.FieldName == nameof(Objects.ProjectId)))
                    {
                        eventEnum = EventEnum.ChangeObjectRoute;
                    }

                    string message = null;

                    switch (eventEnum)
                    {
                    case EventEnum.Create:
                        message = $"ТС {newVehicle.Name} было добавлено";
                        break;

                    case EventEnum.Update:
                        message = $"ТС {newVehicle.Name} было отредактировано";
                        break;

                    case EventEnum.Delete:
                        message = $"ТС {oldVehicle.Name} было удалёно";
                        break;

                    case EventEnum.ChangeObjectRoute:
                        var oldRoute = await _routeRepository.GetByIdAsync(oldVehicle.LastRouteId.Value);
                        var newRoute = await _routeRepository.GetByIdAsync(newVehicle.LastRouteId.Value);
                        message      = $"Маршрут ТС {newVehicle.Name} был изменён с {oldRoute.Name} на {newRoute.Name}";
                        break;

                    case EventEnum.EnableObject:
                        newRoute = await _routeRepository.GetByIdAsync(newVehicle.LastRouteId.Value);
                        message  = $"ТС {newVehicle.Name} было введено в эксплуатацию. Маршрут - {newRoute.Name}";
                        break;

                    case EventEnum.DisableObject:
                        message = $"ТС {newVehicle.Name} было выведено из эксплуатации";
                        break;
                    }

                    await CreateEventLog(
                        principal,
                        eventEnum,
                        message,
                        entityType,
                        entityId,
                        entityName,
                        fields);
                }
            });
        }