public (IEnumerable <BusinessToCustomerModel>, int) ReadFileData(SaveDataModel saveDataModel)
        {
            FileInfo fileInfo = new FileInfo(saveDataModel.FilePath);
            IEnumerable <BusinessToCustomerModel> businessToCustomerModels = null;

            if (fileInfo != null)
            {
                using (ExcelPackage package = new ExcelPackage(fileInfo))
                {
                    var worksheet = package.Workbook.Worksheets[1]; // Tip: To access the first worksheet, try index 1, not 0
                    businessToCustomerModels = ReadExcelPackageToString(package, worksheet);
                    package.Dispose();
                }
            }
            return(businessToCustomerModels, totalRowCount);
        }
        public (IEnumerable <CustomerDataModel>, int) ReadFileData(SaveDataModel saveDataModel)
        {
            FileInfo fileInfo = new FileInfo(saveDataModel.FilePath);
            IEnumerable <CustomerDataModel> customerDataModels = null;

            if (fileInfo != null)
            {
                try
                {
                    using (ExcelPackage package = new ExcelPackage(fileInfo))
                    {
                        var worksheet = package.Workbook.Worksheets[1]; // Tip: To access the first worksheet, try index 1, not 0
                        customerDataModels = ReadExcelPackageToString(package, worksheet);
                        package.Dispose();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(customerDataModels, totalRows);
        }
Exemplo n.º 3
0
        public UploadSaveStatus Upload(SaveDataModel saveDataModel)
        {
            var requestId = _getNewRequestId.Get(saveDataModel.UploadTypeId);
            var tags      = !string.IsNullOrWhiteSpace(saveDataModel.Tags) ?
                            SplitSting.Split(saveDataModel.Tags, ',') : new string[] { };

            var uploadStatus = new UploadSaveStatus();

            if (saveDataModel.UploadTypeId == (int)CustomerDataUploadType.BusinessToBusiness)
            {
                var businessToBusinessData = _businessToBusinessFileToDataModel.ReadFileData(saveDataModel);
                // remove dublicate
                var businessToBusiness = businessToBusinessData.Item1.DistinctBy(x => x.PhoneNew);
                uploadStatus.TotalRows = businessToBusinessData.Item2;

                // Check business category validation
                // Ceck phone number validation
                var numbers = _getBusinesstoBusinessPhone.Get();
                businessToBusiness = businessToBusiness.Except(numbers, x => x.PhoneNew, y => y).ToList();
                numbers            = null;
                if (businessToBusiness.Count() > 0)
                {
                    // validate Business category
                    var validBusinessToBusiness = businessToBusiness;
                    uploadStatus.UploadedRows = validBusinessToBusiness.Count(); // number of rows going to update
                    if (validBusinessToBusiness.Count() > 0)
                    {
                        uploadStatus.IsUploaded = _saveBusinessToBusiness.Save(validBusinessToBusiness, requestId);
                        _saveBusinessToBusinessTags.Save(requestId, tags);
                    }
                }
                // status message update
                if (uploadStatus.TotalRows > uploadStatus.UploadedRows)
                {
                    uploadStatus.StatusMessage = "Some business category doesn't mapped or duplicate phone numers exist";
                }
            }
            else if (saveDataModel.UploadTypeId == (int)CustomerDataUploadType.BusinessToCustomer)
            {
                var businessToCustomer     = _businessToCustomerFileToDataModel.ReadFileData(saveDataModel);
                var businessToCustomerData = businessToCustomer.Item1.DistinctBy(x => x.MobileNew);
                uploadStatus.TotalRows = businessToCustomer.Item2;

                var dbNumbers    = _getBusinessToCustomerPhone.Get();
                var filteredData = businessToCustomerData.Except(dbNumbers, x => x.MobileNew, y => y).ToList();
                dbNumbers = null;
                uploadStatus.UploadedRows = filteredData.Count(); // number of rows going to update
                if (uploadStatus.UploadedRows > 0)
                {
                    uploadStatus.IsUploaded = _saveBusinessToCustomer.Save(filteredData, requestId);
                    _saveBusinessToCustomerTags.Save(requestId, tags);
                }
                // status message update
                if (uploadStatus.TotalRows > uploadStatus.UploadedRows)
                {
                    uploadStatus.StatusMessage = "Duplicate phone numers exist";
                }
            }
            else if (saveDataModel.UploadTypeId == (int)CustomerDataUploadType.CustomerData)
            {
                var customerData         = _CustomerDataFileToDataModel.ReadFileData(saveDataModel);
                var customerUploadedData = customerData.Item1.DistinctBy(x => x.Numbers);
                uploadStatus.TotalRows = customerData.Item2;

                // Mobile number filter
                var dbNumbers    = _getCustomerPhone.Get();
                var filteredData = customerUploadedData.Except(dbNumbers, x => x.Numbers, y => y).ToList();
                dbNumbers = null;                                 // Make it as empty
                uploadStatus.UploadedRows = filteredData.Count(); // number of rows going to update
                if (uploadStatus.UploadedRows > 0)
                {
                    uploadStatus.IsUploaded = _saveCustomerData.Save(filteredData, requestId);
                    _saveCustomerDataTags.Save(requestId, tags);
                }
                // status message update
                if (uploadStatus.TotalRows > uploadStatus.UploadedRows)
                {
                    uploadStatus.StatusMessage = "Duplicate phone numers exist";
                }
            }
            else
            {
                uploadStatus.IsUploaded    = false;
                uploadStatus.StatusMessage = "Please upload valid file";
            }

            if (uploadStatus.IsUploaded)
            {
                var uploadSummary = new SaveSummaryModel
                {
                    ClientFileName = saveDataModel.ClientFileName,
                    UploadTypeId   = saveDataModel.UploadTypeId,
                    TotalRows      = uploadStatus.TotalRows,
                    UploadedRows   = uploadStatus.UploadedRows,
                    Path           = saveDataModel.FilePath,
                    ServerFileName = ServerFileName(saveDataModel.FilePath),
                    RequestId      = requestId,
                    SaveStatus     = uploadStatus.IsUploaded
                };
                _saveUploadSummary.Save(uploadSummary);
            }


            return(uploadStatus);
        }