Ejemplo n.º 1
0
        public async Task <bool> ProcessesHierarchyFile()
        {
            var InProgressFiles = await _hierarchyFileRepository.CustomFind(x => x.FileStatus.Id == (int)FileStatusEnum.InProgress, x => x.FileStatus, x => x.Network);

            if (InProgressFiles.Any())
            {
                _logger.Info($" processamento da hierarquia em progresso - Iniciado ");

                foreach (var file in InProgressFiles)
                {
                    var fileSaleList = await _saleFileRepository.CustomFind(x =>
                                                                            x.CurrentMonth == file.CurrentMonth &&
                                                                            x.CurrentYear == file.CurrentYear &&
                                                                            x.FileStatus.Id != (int)FileStatusEnum.EndedError &&
                                                                            x.Network.Id == file.Network.Id,
                                                                            x => x.FileStatus);

                    if (fileSaleList.Any() && fileSaleList.Where(x => x.FileStatus.Id == (int)FileStatusEnum.ProcessedSales).Count() == fileSaleList.Count())
                    {
                        var dataFileList = await _hierarchyFileDataRepository.CustomFind(x => x.HierarchyFile.Id == file.Id);

                        await _shopUserRepository.ClearRegionManagerShops(dataFileList.GroupBy(x => x.Cpf).Select(x => x.Key).ToList());

                        foreach (var dataFile in dataFileList)
                        {
                            var user   = (await _userRepository.CustomFind(x => x.Cpf.Equals(dataFile.Cpf), x => x.UserStatus)).FirstOrDefault();
                            var shop   = (await _shopRepository.CustomFind(x => x.Cnpj.Equals(dataFile.Cnpj))).FirstOrDefault();
                            var office = string.IsNullOrEmpty(dataFile.Office) ? await _officeRepository.GetById((int)OfficeEnum.RegionManager) : (await _officeRepository.CustomFind(x => x.Description.Replace(" ", "").ToUpper().Equals(dataFile.Office.Replace(" ", "").ToUpper()))).FirstOrDefault();

                            if (user == null)
                            {
                                await CreatePreRegistrationUser(dataFile, office, shop);
                            }
                            else
                            {
                                await UpdateHierarchyUsers(user, dataFile, office, shop);
                            }
                        }

                        file.FileStatus = await _fileStatusRepository.GetById((int)FileStatusEnum.SuccessfullyCompleted);

                        return(await _unitOfWork.CommitAsync());
                    }
                }
            }
            else
            {
                _logger.Info($" processamento da hierarquia em progresso - não possui nenhum arquivo em progresos para processar");
                return(false);
            }

            return(false);
        }