Ejemplo n.º 1
0
        public List<string> ImportReferral(string serverPath, Stream stream, string destinationPath, string filename,
            int campaignId,
            int userId, string ip_address, out ImportReferralHistory history)
        {
            try
            {
                history = new ImportReferralHistory();
                List<Tuple<string, string>> listUsers = new List<Tuple<string, string>>();
                string savedFile = destinationPath + filename;
                List<string> builderErrors = new List<string>(); ;
                List<Tuple<int, string>> listValidUsername = new List<Tuple<int, string>>();
                List<string> listInValidUsername = new List<string>();

                using (CsvFileReader reader = new CsvFileReader(stream))
                {
                    CsvRow row = new CsvRow();
                    int rowNumber = 1;
                    List<string> username = new List<string>();

                    using (CsvFileWriter writer = new CsvFileWriter(savedFile))
                    {
                        while (!reader.EndOfStream)
                        {
                            var errorMsg = reader.CheckValidRow(row, rowNumber == 1, ref username);
                            if (!string.IsNullOrEmpty(errorMsg))
                                builderErrors.Add(string.Format("Line {0}: {1}", rowNumber, errorMsg));
                            else if (username != null && username.Any())
                            {
                                foreach (var item in username)
                                {
                                    if (!listValidUsername.Select(t => t.Item2).Contains(item) && !listInValidUsername.Contains(item))
                                    {
                                        var id = GetIdByUsername(item);
                                        if (id > 0)
                                            listValidUsername.Add(new Tuple<int, string>(id, item));
                                        else
                                        {
                                            listInValidUsername.Add(item);
                                            builderErrors.Add(string.Format("Line {0}: {1} is invalid username", rowNumber, item));
                                        }
                                    }
                                }
                                listUsers.Add(new Tuple<string, string>(username[0], username[1]));
                            }
                            else if (rowNumber != 1 && !username.Any())
                            {
                                builderErrors.Add(string.Format("Line {0}: This row is empty", rowNumber));
                            }
                            rowNumber++;

                            reader.ReadRow(row);
                            writer.WriteRow(row);
                        }
                    }
                }

                if (builderErrors != null && builderErrors.Any())
                    return builderErrors;

                string filePath = savedFile.Replace(serverPath, "\\");
                if (string.IsNullOrEmpty(filePath))
                {
                    builderErrors.Add(ErrorCodes.SAVE_FILE_ERROR.ToErrorMessage());
                    return builderErrors;
                }

                #region Save data to DB
                var repo = Repo.Instance;
                using (var db = repo.OpenConnectionFromPool())
                {
                    ReferralCampaign referralCampaign = repo.GetReferralCampaign(db
                        , new List<int>() { (int)ReferralCampaignStatus.Running, (int)ReferralCampaignStatus.Finished }
                        , campaignId);
                    if (referralCampaign == null)
                    {
                        builderErrors.Add(ErrorCodes.INVALID_REFERRAL_CAMPAIGN.ToErrorMessage());
                        return builderErrors;
                    }

                    //if (repo.CountRecordDownloadHistory(db, referralCampaign.id) + listUsers.Count() > referralCampaign.quantity)
                    //{
                    //    builderErrors.Add(ErrorCodes.REFERRAL_CAMPAIGN_QUANTITY_IS_OVER.ToErrorMessage());
                    //    return builderErrors;
                    //}

                    List<Tuple<string, string, string>> listFail = new List<Tuple<string, string, string>>();

                    foreach (var row in listUsers)
                    {
                        var uid = listValidUsername.FirstOrDefault(u => string.Compare(u.Item2, row.Item1, true) == 0).Item1;
                        string error = ImportUserRerralCampaigns(repo, db, uid, referralCampaign.game_id, row.Item1, row.Item2, ip_address, referralCampaign);
                        if (!string.IsNullOrEmpty(error))
                        {
                            listFail.Add(new Tuple<string, string, string>(row.Item1, row.Item2, error));
                        }
                    }

                    string failData = string.Empty;
                    if (listFail.Any())
                    {
                        failData = SaveFailedImportResult(destinationPath, filename, listFail);
                    }
                    ImportReferralResult importResult = new ImportReferralResult
                    {
                        failed = listFail.Count(),
                        file_path = !string.IsNullOrEmpty(failData)
                            ? failData.Replace(serverPath, "\\")
                            : string.Empty,
                        pass = listUsers.Count() - listFail.Count(),
                        total = listUsers.Count()
                    };

                    //TODO: need to consider about the case when 100% is fail from VV api
                    CustomerAccount creator = repo.GetCustomerById(db, userId).Data;
                    history = new ImportReferralHistory
                    {
                        file_name = filename,
                        file_path = filePath,
                        game_id = referralCampaign.game_id, //TODO: can remove
                        importer_username = creator.username,
                        result = JsonConvert.SerializeObject(importResult),
                        referral_campaign_id = campaignId
                    };
                    history.id = repo.CreateImportReferralHistory(db, history);
                    return null;
                }
                #endregion
            }
            catch (Exception ex)
            {
                logger.Fatal("ImportReferral:  " + ex.ToString());
                history = null;
                return new List<string>() { ErrorCodes.SYSTEM_ERROR.ToErrorMessage() };
            }
        }
 public int CreateImportReferralHistory(ImportReferralHistory importReferral)
 {
     var repo = Repo.Instance;
     using (var db = repo.OpenConnectionFromPool())
     {
         return repo.CreateImportReferralHistory(db, importReferral);
     }
 }