Exemplo n.º 1
0
        public async Task <List <CultureDto> > BrowseAsync()
        {
            var list = await _repository.BrowseAsync(x => true);

            var culturesDto = _mapper.Map <List <CultureDto> >(list);

            foreach (var cultureDto in culturesDto)
            {
                var ecn = (await _repository.BrowseAsync(x => cultureDto.EnemyCultures.Contains(x.Id)))
                          .Select(x => x.Name).ToList();
                cultureDto.EnemyCulturesNames = ecn;
                var fcn = (await _repository.BrowseAsync(x => cultureDto.FriendlyCultures.Contains(x.Id)))
                          .Select(x => x.Name).ToList();
                cultureDto.FriendlyCulturesNames = fcn;
            }

            return(culturesDto);
        }
        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);
        }