public async Task <List <NextCropRecommendation> > GetNextCropRecommendationByFieldId(Guid fieldId, DateTime date)
        {
            var field = await _fieldRepository.GetAsync(fieldId);

            Expression <Func <Culture, bool> > culturesPredicate = c => Math.Abs((int)c.MonthToSeed - date.Month) < 2;

            var lastCrop = field.Crops.OrderByDescending(c => c.DateTo).FirstOrDefault();

            if (lastCrop != null)
            {
                culturesPredicate = culturesPredicate.And(c => c.EnemyCultures.All(x => x != lastCrop.CultureId));

                List <Guid> lastCropComponents = await GetCropComponentsAsync(lastCrop);

                culturesPredicate = culturesPredicate.And(c => !c.ForbiddenComponents.Intersect(lastCropComponents).Any());
            }

            var cultures = await _cultureRepository.BrowseAsync(culturesPredicate);

            var results = new List <NextCropRecommendation>();

            foreach (var culture in cultures)
            {
                var recommendedPesticides = await GetRecommendedPesticidesAsync(culture, field);

                NextCropRecommendation rec;
                if (lastCrop != null)
                {
                    if (culture.FriendlyCultures.Contains(lastCrop.CultureId))
                    {
                        rec = new NextCropRecommendation()
                        {
                            CultureName    = culture.Name,
                            Recommendation = RecommendationLevel.HighlyRecommended,
                            Pesticides     = recommendedPesticides
                        };
                        results.Add(rec);
                    }
                }
                else
                {
                    rec = new NextCropRecommendation()
                    {
                        CultureName    = culture.Name,
                        Recommendation = RecommendationLevel.Neutral,
                        Pesticides     = recommendedPesticides
                    };
                    results.Add(rec);
                }
            }

            return(results);
        }
Beispiel #2
0
 public async Task <FieldDto> GetAsync(Guid id, Guid organisationId)
 {
     return(_mapper.Map <FieldDto>(await _repository.GetAsync(id)));
 }