コード例 #1
0
        private void DoImport(string file)
        {
            try
            {
                //Create activity log
                var activity = new ActivityLogInfo
                {
                    CreatedDate  = DateTime.Now,
                    CreatedBy    = ImportInfo.UserId,
                    FunctionId   = (int)LogFunctionType.ImportExcel,
                    FunctionType = (int)LogFunctionType.ImportExcel,
                };
                activity.Id = ActivityLogRepository.Create(activity);

                //End log
                using (var connection = new SqlConnection(ImportConfig.ConnectionString))
                {
                    connection.Open();

                    SqlTransaction transaction = null;
                    try
                    {
                        transaction = connection.BeginTransaction();
                        // Use one transaction to put all the data in the database
                        var watch = new Stopwatch();
                        watch.Start();
                        var contactIdentity          = GetIdentity("Contacts", connection, transaction);
                        var contactDuplicateIdentity = GetIdentity("ContactDuplicates", connection, transaction);
                        using (var stream = File.Open(file, FileMode.Open, FileAccess.Read))
                        {
                            using (var dr = ExcelReaderFactory.CreateOpenXmlReader(stream))
                            {
                                //Log bat dau tu
                                var rowIndex = 0;
                                while (dr.Read())
                                {
                                    if (rowIndex >= 1)
                                    {
                                        #region Read excel row
                                        var name   = dr[1] == null ? string.Empty : dr[1].ToString().Trim();
                                        var email  = dr[2] == null ? string.Empty : dr[2].ToString().Trim().ToLower();
                                        var mobile = dr[3] == null
                                            ? string.Empty
                                            : Util.CleanAlphabetAndFirstZero(dr[3].ToString().Trim());
                                        var      address = dr[4] == null ? string.Empty : dr[4].ToString().Trim();
                                        DateTime?registeredDate;
                                        if (dr[5] == null)
                                        {
                                            registeredDate = null;
                                        }
                                        else
                                        {
                                            try
                                            {
                                                registeredDate = (DateTime)dr[5];
                                            }
                                            catch
                                            {
                                                registeredDate = dr[5].ToDateTime();
                                            }
                                        }
                                        var campaindTpe   = dr[6] == null ? string.Empty : dr[6].ToString().Trim();
                                        var campaindTpeId = StaticData.GetCampaindTpeId(campaindTpe);

                                        var landingPage   = dr[7] == null ? string.Empty : dr[7].ToString().Trim();
                                        var landingPageId = StaticData.GetLandingPageId(landingPage);

                                        var channel   = dr[8] == null ? string.Empty : dr[8].ToString().Trim();
                                        var channelId = StaticData.GetChannelId(channel, ImportInfo.TypeId, ImportInfo.ChannelId);

                                        var templateAds   = dr[9] == null ? string.Empty : dr[9].ToString().Trim();
                                        var templateAdsId = StaticData.GetTemplateAdsId(templateAds);

                                        var searchKeyword   = dr[10] == null ? string.Empty : dr[10].ToString().Trim();
                                        var searchKeywordId = StaticData.GetTemplateAdsId(searchKeyword);

                                        var package   = dr[11] == null ? string.Empty : dr[11].ToString().Trim();
                                        var packageId = StaticData.GetTemplateAdsId(package);

                                        var code = dr[12] == null ? string.Empty : dr[12].ToString().Trim();

                                        var contactIdDb      = 0;
                                        var contactTmpStatus = 0;
                                        var contactInfoDb    = string.Empty;
                                        #endregion

                                        #region validate row
                                        if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(mobile) && string.IsNullOrEmpty(email))
                                        {
                                            continue;
                                        }
                                        ImportInfo.RowIndex++;
                                        ImportInfo.TotalRow++;

                                        //Contact validation
                                        var error = Validate(mobile, string.Empty, string.Empty, email);

                                        // haihm
                                        if (error == ContactError.EmailFormat)
                                        {
                                            email = null;
                                            error = ContactError.None;
                                        }
                                        if (error != ContactError.None)
                                        {
                                            ImportInfo.ErrorCount++;
                                            contactTmpStatus = (int)error;
                                        }
                                        else
                                        {
                                            //Check if contact is internal duplicate or not
                                            if (IsInternalDuplicate(mobile, string.Empty, string.Empty, email))
                                            {
                                                contactTmpStatus = (int)ContactError.InternalDuplicate;
                                                ImportInfo.InternalDuplicateCount++;
                                                Console.WriteLine("Duplicated in INTERNAL FILE with Mobile: " + mobile + "; \tEmail: " + email + "; \t\t\t\tImportId: " + ImportInfo.ImportId + "; \tImportDate:" + ImportInfo.ImportedDate + "; \tUserImportId:" + ImportInfo.UserId);
                                            }
                                            else
                                            {
                                                //If not internal duplicate, add to hashtable data
                                                //int check_db_idcts;
                                                if (!string.IsNullOrEmpty(email))
                                                {
                                                    internalPhoneAndEmails.Add(email);
                                                }
                                                if (!string.IsNullOrEmpty(mobile))
                                                {
                                                    internalPhoneAndEmails.Add(mobile);
                                                }
                                                //int check_dulicated;
                                                var check = CheckDuplicateProvider.Instance().IsDuplicate(mobile, string.Empty, string.Empty, email, string.Empty, out contactIdDb, out contactInfoDb);

                                                //check_db_idcts = ContactRepository.ContactIsDuplicate(mobile, string.Empty, string.Empty, email, string.Empty);
                                                //if (check_db_idcts != 0)
                                                //{
                                                //    check = true;
                                                //}
                                                //if (!check)
                                                //{
                                                //    check_dulicated = ContactRepository.ContactIsDuplicate(mobile, string.Empty, string.Empty, email, string.Empty);
                                                //    if (check_dulicated != 0) check = true;
                                                //}
                                                if (check)
                                                {
                                                    ImportInfo.DuplicateCount++;
                                                    contactTmpStatus = (int)ContactError.Duplicate;
                                                    Console.WriteLine("Duplicated in REDIS CACHE with Mobile: " + mobile + "; \tEmail: " + email + "; \t\t\t\tImportId: " + ImportInfo.ImportId + "; \tImportDate:" + ImportInfo.ImportedDate + "; \tUserImportId:" + ImportInfo.UserId);
                                                }
                                            }
                                        }
                                        ImportInfo.CheckCount++;
                                        #endregion

                                        switch (contactTmpStatus)
                                        {
                                        case (int)ContactError.None:
                                        {
                                            #region create contact row
//
                                            string sTypeId = "01";         //Nguon contact: MO, CC ... 01 la MO

                                            if (ImportInfo.TypeId == 3)
                                            {
                                                sTypeId = "02";
                                            }
                                            else if (ImportInfo.TypeId == 4)
                                            {
                                                sTypeId = "01";
                                            }
                                            else if (ImportInfo.TypeId == 5)
                                            {
                                                sTypeId = "03";
                                            }
                                            else if (ImportInfo.TypeId == 6)
                                            {
                                                sTypeId = "04";
                                            }

                                            else if (ImportInfo.TypeId == 8)
                                            {
                                                sTypeId = "05";
                                            }
                                            else if (ImportInfo.TypeId == 9)
                                            {
                                                sTypeId = "06";
                                            }
                                            else if (ImportInfo.TypeId == 10)
                                            {
                                                sTypeId = "07";
                                            }
                                            else if (ImportInfo.TypeId == 11)
                                            {
                                                sTypeId = "08";
                                            }
                                            else
                                            {
                                                sTypeId = "09";
                                            }


                                            var contactId = contactIdentity + rowIndex;
                                            var sCode     = DateTime.Now.ToString("yyyyMMdd") + sTypeId + contactId.ToString();

                                            var contactRow = dtContacts.NewRow();
                                            contactRow["Gender"]        = 0;
                                            contactRow["Email"]         = email;
                                            contactRow["Id"]            = contactId;
                                            contactRow["Address"]       = address;
                                            contactRow["Fullname"]      = name;
                                            contactRow["ChannelId"]     = channelId;
                                            contactRow["PackageId"]     = packageId;
                                            contactRow["CreatedDate"]   = DateTime.Now;
                                            contactRow["TypeId"]        = ImportInfo.TypeId;
                                            contactRow["LevelId"]       = ImportInfo.LevelId;
                                            contactRow["CampaindTpeId"] = campaindTpeId;
                                            contactRow["LandingPageId"] = landingPageId;
                                            contactRow["TemplateAdsId"] = templateAdsId;
                                            contactRow["BranchId"]      = ImportInfo.BranchId;
                                            contactRow["ImportId"]      = ImportInfo.ImportId;
                                            contactRow["StatusId"]      = ImportInfo.Status;    //(int)StatusType.New;
                                            // HungNV: 18/05/2020 Trường hợp kho 1-6 cần điều kiện container để phân biệt lúc thu hồi
                                            contactRow["ContainerId"]     = ImportInfo.Status;
                                            contactRow["RegisteredDate"]  = registeredDate;
                                            contactRow["UserImportId"]    = ImportInfo.UserId;
                                            contactRow["SearchKeywordId"] = searchKeywordId;
                                            contactRow["ImportedDate"]    = ImportInfo.ImportedDate;
                                            contactRow["Code"]            = code.IsStringNullOrEmpty() ? sCode : code;
                                            dtContacts.Rows.Add(contactRow);

                                            #endregion

                                            #region create phone row
                                            if (!string.IsNullOrEmpty(mobile))
                                            {
                                                var phoneRow = dtPhones.NewRow();
                                                phoneRow["ContactId"]   = contactId;
                                                phoneRow["PhoneType"]   = (int)PhoneType.HomePhone;
                                                phoneRow["PhoneNumber"] = mobile;
                                                phoneRow["IsPrimary"]   = 1;
                                                dtPhones.Rows.Add(phoneRow);
                                            }
                                            #endregion

                                            #region create contact level row
                                            var contactLelveRow = dtContactLevels.NewRow();
                                            contactLelveRow["ContactId"] = contactId;
                                            dtContactLevels.Rows.Add(contactLelveRow);
                                            #endregion

                                            #region create object changes
                                            foreach (DataColumn col in dtContacts.Columns)
                                            {
                                                if (col.ColumnName != "TypeId" &&
                                                    col.ColumnName != "LevelId" &&
                                                    col.ColumnName != "StatusId" &&
                                                    col.ColumnName != "ChannelId" &&
                                                    col.ColumnName != "BranchId")
                                                {
                                                    continue;
                                                }
                                                var objChange = dtObjectChanges.NewRow();
                                                objChange["ActivityId"] = activity.Id;
                                                objChange["ObjectType"] = (int)LogObjectType.Contact;
                                                objChange["ObjectId"]   = contactId;
                                                var      propertyType          = 0;
                                                int?     propertyValueInt      = null;
                                                string   propertyValueString   = null;
                                                DateTime?propertyValueDateTime = null;
                                                switch (col.ColumnName)
                                                {
                                                case "BranchId":
                                                    propertyType     = (int)LogPropertyType.Contacts_Branch;
                                                    propertyValueInt = ImportInfo.BranchId;
                                                    break;

                                                case "TypeId":
                                                    propertyType     = (int)LogPropertyType.Contacts_Type;
                                                    propertyValueInt = ImportInfo.TypeId;
                                                    break;

                                                case "LevelId":
                                                    propertyType     = (int)LogPropertyType.Contacts_Level;
                                                    propertyValueInt = ImportInfo.LevelId;
                                                    break;

                                                case "StatusId":
                                                    propertyType     = (int)LogPropertyType.Contacts_Status;
                                                    propertyValueInt = ImportInfo.Status;
                                                    break;

                                                case "ChannelId":
                                                    propertyType     = (int)LogPropertyType.Contacts_Channel;
                                                    propertyValueInt = channelId;
                                                    break;
                                                }
                                                objChange["PropertyType"]          = propertyType;
                                                objChange["PropertyValueInt"]      = propertyValueInt;
                                                objChange["PropertyValueString"]   = propertyValueString;
                                                objChange["PropertyValueDateTime"] = propertyValueDateTime;
                                                objChange["ChangedDate"]           = DateTime.Now;
                                                dtObjectChanges.Rows.Add(objChange);
                                            }

                                            //Them Mobile vs Email cua moi contact vao cache Redis

                                            //CachingProvider.Instance().Set(mobile, contactId.ToString());
                                            //CachingProvider.Instance().Set(email, contactId.ToString());

                                            //list_phone_email_redis.Add(Constant.NameSystem + mobile, contactId.ToString());
                                            //list_phone_email_redis.Add(Constant.NameSystem + email, contactId.ToString());
                                            #endregion

                                            //Nếu import vào kho MOL thì save log vào bảng LogContainerMOL
                                            #region create log contact kho MOL
                                            if (ImportInfo.Status == (int)StatusType.ContainerMOL)
                                            {
                                                var logContainerMOL = dtLogContainerMOL.NewRow();

                                                logContainerMOL["CreatedDate"]         = DateTime.Now;
                                                logContainerMOL["StatusContainerType"] = (int)StatusContainerTypeMOL.ImportContainer;
                                                logContainerMOL["StatusAddContact"]    = (int)StatusAddContact.ImportContact;
                                                logContainerMOL["StatusContactFirst"]  = ImportInfo.Status;
                                                logContainerMOL["StatusContactAfter"]  = null;
                                                logContainerMOL["ContactId"]           = contactId;
                                                logContainerMOL["ChannelId"]           = channelId;

                                                dtLogContainerMOL.Rows.Add(logContainerMOL);
                                            }
                                            #endregion
                                        }
                                        break;

                                        case (int)ContactError.Duplicate:
                                        {
                                            #region create contact duplicate
                                            var sourceTypes = StaticData.GetSourceTypeCheckDuplicate();
                                            if (!sourceTypes.IsNullOrEmpty() && sourceTypes.Any(c => c.SourceTypeId == ImportInfo.TypeId))
                                            {
                                                var contactDuplicateRow = dtContactDuplicate.NewRow();
                                                var contact             = ContactRepository.GetInfo(contactIdDb);
                                                //update chuyen trang thai contact neu trung chuyen sang kho trung
                                                ContactRepository.UpdateChangeStatusId(contactIdDb);
                                                int idContactDuplicate = contactDuplicateIdentity + rowIndex;

                                                contactDuplicateRow["Id"]                = idContactDuplicate;
                                                contactDuplicateRow["ContactId"]         = contactIdDb;
                                                contactDuplicateRow["SourceTypeId"]      = ImportInfo.TypeId;
                                                contactDuplicateRow["Status"]            = contact != null ? contact.StatusId: 0;
                                                contactDuplicateRow["ImportId"]          = ImportInfo.ImportId;
                                                contactDuplicateRow["DuplicateInfo"]     = contactInfoDb;
                                                contactDuplicateRow["CreatedDate"]       = DateTime.Now;
                                                contactDuplicateRow["IsNotyfiDuplicate"] = true;

                                                dtContactDuplicate.Rows.Add(contactDuplicateRow);
                                            }
                                            #endregion
                                        }
                                        break;

                                        default:
                                        {
                                            #region create contact temp errors
                                            var contactRow = dtContactTmps.NewRow();
                                            var contactId  = contactIdentity + rowIndex;
                                            contactRow["Gender"]        = 0;
                                            contactRow["Email"]         = email;
                                            contactRow["Id"]            = contactId;
                                            contactRow["Fullname"]      = name;
                                            contactRow["Mobile1"]       = mobile;
                                            contactRow["Address"]       = address;
                                            contactRow["ChannelId"]     = channelId;
                                            contactRow["PackageId"]     = packageId;
                                            contactRow["CreatedDate"]   = DateTime.Now;
                                            contactRow["TypeId"]        = ImportInfo.TypeId;
                                            contactRow["LevelId"]       = ImportInfo.LevelId;
                                            contactRow["CampaindTpeId"] = campaindTpeId;
                                            contactRow["LandingPageId"] = landingPageId;
                                            contactRow["TemplateAdsId"] = templateAdsId;
                                            contactRow["BranchId"]      = ImportInfo.BranchId;
                                            contactRow["ImportId"]      = ImportInfo.ImportId;
                                            contactRow["StatusId"]      = (int)StatusType.New;
                                            // HungNV: 18/05/2020 Trường hợp kho 1-6 cần điều kiện container để phân biệt lúc thu hồi
                                            contactRow["ContainerId"]     = ImportInfo.Status;
                                            contactRow["RegisteredDate"]  = registeredDate;
                                            contactRow["UserImportId"]    = ImportInfo.UserId;
                                            contactRow["SearchKeywordId"] = searchKeywordId;
                                            contactRow["ImportedDate"]    = ImportInfo.ImportedDate;
                                            contactRow["Code"]            = code.IsStringNullOrEmpty() ? "TPE_" + contactId : code;
                                            dtContactTmps.Rows.Add(contactRow);
                                            #endregion
                                        }
                                        break;
                                        }
                                    }
                                    rowIndex++;
                                }
                            }
                        }

                        watch.Stop();
                        Console.WriteLine("");
                        Console.WriteLine("Thoi gian read file: " + watch.ElapsedMilliseconds);
                        watch.Reset();
                        watch.Restart();

                        dtPhones = RemoveDuplicateRows(dtPhones, "PhoneNumber");
                        // Import
                        ContactBulkImport.ImportContactPhone(dtPhones, connection, transaction);
                        ContactBulkImport.ImportContact(dtContacts, connection, transaction);
                        ContactBulkImport.ImportContactLevel(dtContactLevels, connection, transaction);
                        ContactBulkImport.ImportObjectChanges(dtObjectChanges, connection, transaction);
                        ContactBulkImport.ImportContactTmp(dtContactTmps);
                        ContactBulkImport.ImportContactDuplicate(dtContactDuplicate, connection, transaction);
                        //Log table LogContainerMOL phuc vu bao cao xuat nhap kho MOL
                        ContactBulkImport.ImportLogContainerMOL(dtLogContainerMOL, connection, transaction);

                        watch.Stop();
                        Console.WriteLine("Thoi gian Import file: " + watch.ElapsedMilliseconds);

                        Console.WriteLine("Bat dau commit");
                        transaction.Commit();
                        Console.WriteLine("Commit thanh cong");
                        ImportInfo.ImportStatus = 1;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        if (transaction != null)
                        {
                            Console.WriteLine("Commit ko thanh cong. Dang rollback lai");
                            ImportInfo.ImportStatus = 2;
                            transaction.Rollback(); // This will not reset IDENT_CURRENT
                        }
                    }
                    finally
                    {
                        // Update Duplicate
                        Console.WriteLine("Bat dau vao finally.");
                        UpdateDuplicate();
                        //ImportInfo.ImportStatus = 1;
                        ImportExcelRepository.Update(ImportInfo);

                        // Load redis
                        if (ImportInfo.ImportStatus == 1)
                        {
                            Console.WriteLine("Update xong trang thai ImportExcel. Bat dau load Redis.");
                            StaticData.LoadToRedis();
                            Console.WriteLine("Load xong redis");
                        }
                    }
                }

                dtPhones        = null;
                dtContacts      = null;
                dtContactTmps   = null;
                dtContactLevels = null;
                dtObjectChanges = null;
            }
            catch (Exception ex2)
            {
                Console.WriteLine(ex2.ToString());
            }
        }
コード例 #2
0
ファイル: ContactController.cs プロジェクト: HungNV88/CRM
        public ActionResult CreateHotLine(ContactAddModel model)
        {
            int duplicateId;

            try
            {
                duplicateId = CheckDuplicateProvider.Instance().IsDuplicate(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, model.ContactInfo.Mobile3, model.ContactInfo.Email, model.ContactInfo.Email2);
                if (duplicateId == 0)
                {
                    duplicateId = ContactRepository.ContactIsDuplicate(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, model.ContactInfo.Mobile3, model.ContactInfo.Email, model.ContactInfo.Email2);
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Hệ thống cache bị lỗi, vui lòng thử lại. [" + ex.Message.ToString() + "]";
                return(CreateHotLine());
            }

            model.ContactInfo.ChannelId = ChannelRepository.GetChannelId(model.ContactInfo.Channel, model.ContactInfo.TypeId, 1);
            var entity = new ContactInfo
            {
                CollectorId            = 1,
                StatusMapConsultantId  = 1,
                StatusCareConsultantId = 1,
                CreatedDate            = DateTime.Now,
                LevelId                   = (int)LevelType.L1,
                RegisteredDate            = DateTime.Now,
                Notes                     = model.ContactInfo.Notes,
                Email                     = model.ContactInfo.Email,
                Email2                    = model.ContactInfo.Email2,
                TypeId                    = model.ContactInfo.TypeId,
                Gender                    = model.ContactInfo.Gender,
                Address                   = model.ContactInfo.Address,
                Fullname                  = model.ContactInfo.Fullname,
                HandoverConsultantDate    = DateTime.Now,
                Birthday                  = model.Birthday.ToDateTime(),
                ChannelId                 = model.ContactInfo.ChannelId,
                BranchId                  = UserContext.GetDefaultBranch(),
                AppointmentConsultantDate = DateTime.Now,
                StatusId                  = (int)StatusType.HandoverConsultant,
                ProductSellId             = model.ContactInfo.ProductSellId,
                CreatedBy                 = UserContext.GetCurrentUser().UserID,
                UserConsultantId          = model.ContactInfo.UserConsultantId,
                Mobile1                   = Util.CleanAlphabetAndFirstZero(model.ContactInfo.Mobile1),
                Mobile2                   = Util.CleanAlphabetAndFirstZero(model.ContactInfo.Mobile2),
            };

            var contactInfo = duplicateId == 0 ? null : ContactRepository.GetInfo(duplicateId);

            if (contactInfo == null)
            {
                try
                {
                    // Save Contacts
                    if (entity.UserConsultantId > 0)
                    {
                        entity.StatusId = (int)StatusType.HandoverConsultant;
                    }
                    entity.Id = ContactRepository.CreateHotline(entity);

                    if (entity.Id > 0)
                    {
                        // Message
                        ViewBag.Message = "Thêm mới contact thành công";
                        // Redis
                        StoreData.LoadRedis(entity.Id);
                    }
                    else
                    {
                        ViewBag.Message = "Thêm mới contact bị lỗi, vui lòng thử lại sau";
                        //Redis
                        StoreData.DeleteRedis(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, string.Empty, model.ContactInfo.Email, model.ContactInfo.Email2);
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Thêm mới contact bị lỗi. [" + ex.Message.ToString() + "]";

                    //Redis
                    StoreData.DeleteRedis(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, string.Empty, model.ContactInfo.Email, model.ContactInfo.Email2);
                }
            }
            else
            {
                // Update email, phone
                UpdatePhone(contactInfo.Id, new List <string> {
                    entity.Mobile1, entity.Mobile2, entity.Mobile3
                });

                // Email
                if (!entity.Email.IsStringNullOrEmpty())
                {
                    if (contactInfo.Email.IsStringNullOrEmpty())
                    {
                        contactInfo.Email = entity.Email;
                    }
                    else if (contactInfo.Email != entity.Email && contactInfo.Email2.IsStringNullOrEmpty())
                    {
                        contactInfo.Email2 = entity.Email;
                    }
                }
                ContactRepository.Update(contactInfo);

                // Log duplicate contact
                var contactDuplicateInfo = new ContactDuplicateInfo
                {
                    ContactId         = contactInfo.Id,
                    SourceTypeId      = contactInfo.TypeId,
                    Status            = contactInfo.StatusId,
                    ImportId          = contactInfo.ImportId,
                    DuplicateInfo     = contactInfo.Mobile1,
                    CreatedDate       = DateTime.Now,
                    IsNotyfiDuplicate = true
                };

                //ContactRepository.ContactUpdateDuplicate(entity.Id, entity.TypeId, entity.StatusId);
                ContactDuplicateRepository.Update(contactDuplicateInfo);

                // Message
                var user = StoreData.ListUser.FirstOrDefault(c => c.UserID == contactInfo.UserConsultantId) ?? UserRepository.GetInfo(contactInfo.UserConsultantId);
                ViewBag.Message = user == null
                                      ? "Sđt hoặc Email bạn nhập đã bị trùng với Ứng viên: " + contactInfo.Id + " - " + contactInfo.Fullname + " - Level " + contactInfo.LevelId + "- TVTS: chưa có ai chăm sóc"
                                      : "Sđt hoặc Email bạn nhập đã bị trùng với Ứng viên: " + contactInfo.Id + " - " + contactInfo.Fullname + " - Level " + contactInfo.LevelId + "- TVTS: " + user.FullName;
            }
            return(CreateHotLine());
        }
コード例 #3
0
        public string Edit(FormDataCollection form)
        {
            // Check valid
            var mobile1 = Util.CleanAlphabetAndFirstZero(form.Get("Mobile1"));
            var mobile2 = Util.CleanAlphabetAndFirstZero(form.Get("Mobile2"));
            var mobile3 = Util.CleanAlphabetAndFirstZero(form.Get("Mobile3"));

            if (mobile1.IsStringNullOrEmpty() && mobile2.IsStringNullOrEmpty() && mobile3.IsStringNullOrEmpty())
            {
                return("SĐT không được rỗng");
            }
            if (!mobile1.IsStringNullOrEmpty() && !ContactValidHelper.IsMobileValid(mobile1))
            {
                return("SĐT 1 không hợp lệ");
            }
            if (!mobile2.IsStringNullOrEmpty() && !ContactValidHelper.IsMobileValid(mobile2))
            {
                return("SĐT 2 không hợp lệ");
            }
            if (!mobile3.IsStringNullOrEmpty() && !ContactValidHelper.IsMobileValid(mobile3))
            {
                return("SĐT 3 không hợp lệ");
            }
            var email = form.Get("Email");

            if (!email.IsStringNullOrEmpty() && !ContactValidHelper.IsValidEmail(email))
            {
                return("Email 1 không hợp lệ");
            }
            var email2 = form.Get("Email2");

            if (!email2.IsStringNullOrEmpty() && !ContactValidHelper.IsValidEmail(email2))
            {
                return("Email 2 không hợp lệ");
            }

            // Check cache
            int duplicateId;

            try
            {
                duplicateId = CheckDuplicateProvider.Instance().IsDuplicate(mobile1, mobile2, mobile3, email, email2);
                if (duplicateId == 0)
                {
                    duplicateId = ContactRepository.ContactIsDuplicate(mobile1, mobile2, mobile3, email, email2);
                }
            }
            catch
            {
                return("Hệ thống cache bị lỗi, vui lòng thử lại sau");
            }
            if (duplicateId > 0)
            {
                return("Contact đã có trong hệ thống, vui lòng thử lại sau");
            }

            var retVal    = string.Empty;
            var operation = form.Get("oper");
            var userId    = UserContext.GetCurrentUser().UserID;
            var id        = ConvertHelper.ToInt32(form.Get("Id").Split(',')[0]);

            if (string.IsNullOrEmpty(operation))
            {
                return("Cập nhật contact bị lỗi, vui lòng thừ lại sau");
            }

            switch (operation)
            {
            case "edit":
                var info = ContactTmpRepository.GetInfo(id);
                if (info != null)
                {
                    try
                    {
                        info.Email    = email;
                        info.Email2   = email2;
                        info.Mobile1  = mobile1;
                        info.Mobile2  = mobile2;
                        info.Mobile3  = mobile3;
                        info.Fullname = form.Get("Fullname");
                        info.Birthday = form.Get("Birthday").ToDateTime();

                        // Delete ContactTmp
                        ContactTmpRepository.Delete(info.Id);

                        // Create Contacts
                        var contact = info;
                        contact.CreatedBy   = userId;
                        contact.UserErrorId = userId;
                        contact.Id          = ContactRepository.CreateTmp(contact);

                        //Redis
                        StoreData.LoadRedis(contact.Id);
                        return("Cập nhật thành công");
                    }
                    catch
                    {
                        return("Cập nhật contact bị lỗi, vui lòng thừ lại sau");
                    }
                }
                break;
            }
            return(retVal);
        }
コード例 #4
0
ファイル: ContactController.cs プロジェクト: HungNV88/CRM
        public ActionResult CreateTvts(ContactAddModel model)
        {
            int duplicateId;

            try
            {
                #region "Start Checkpoint"
                CheckPointApi checkPointApi = new CheckPointApi();
                var           watch         = new Stopwatch();
                watch.Start();
                checkPointApi.CheckPointNew(UserContext.GetCurrentUser().UserName, "CheckDuplicate", "Start", 0);
                #endregion

                duplicateId = CheckDuplicateProvider.Instance().IsDuplicate(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, model.ContactInfo.Mobile3, model.ContactInfo.Email, model.ContactInfo.Email2);
                if (duplicateId == 0)
                {
                    duplicateId = ContactRepository.ContactIsDuplicate(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, model.ContactInfo.Mobile3, model.ContactInfo.Email, model.ContactInfo.Email2);
                }

                #region "End CheckPoint"
                watch.Stop();
                checkPointApi.CheckPointNew(UserContext.GetCurrentUser().UserName, "CheckDuplicate", "End", watch.ElapsedMilliseconds);
                #endregion
            }
            catch (Exception ex)
            {
                ViewBag.Message = "Hệ thống bị lỗi, vui lòng thử lại. [" + ex.Message.ToString() + "]";
                return(CreateTvts());
            }
            var entity = new ContactInfo
            {
                CreatedDate               = DateTime.Now,
                LevelId                   = (int)LevelType.L1,
                RegisteredDate            = DateTime.Now,
                Notes                     = model.ContactInfo.Notes,
                Email                     = model.ContactInfo.Email,
                Email2                    = model.ContactInfo.Email2,
                TypeId                    = model.ContactInfo.TypeId,
                Gender                    = model.ContactInfo.Gender,
                Address                   = model.ContactInfo.Address,
                Fullname                  = model.ContactInfo.Fullname,
                Birthday                  = model.Birthday.ToDateTime(),
                BranchId                  = UserContext.GetDefaultBranch(),
                CollectorId               = model.ContactInfo.CollectorId,
                ProductSellId             = model.ContactInfo.ProductSellId,
                CreatedBy                 = UserContext.GetCurrentUser().UserID,
                Mobile1                   = Util.CleanAlphabetAndFirstZero(model.ContactInfo.Mobile1),
                Mobile2                   = Util.CleanAlphabetAndFirstZero(model.ContactInfo.Mobile2),
                CampaindTpeId             = model.ContactInfo.CampaindTpe.IsStringNullOrEmpty() ? 0 : StaticData.GetCampaindTpeId(model.ContactInfo.CampaindTpe),
                StatusMapConsultantId     = 1,
                StatusCareConsultantId    = 1,
                HandoverConsultantDate    = DateTime.Now,
                AppointmentConsultantDate = DateTime.Now,
                StatusId                  = (int)StatusType.HandoverConsultant,
                UserConsultantId          = UserContext.GetCurrentUser().UserID,
            };

            var contactInfo = duplicateId == 0 ? null : ContactRepository.GetInfo(duplicateId);
            if (contactInfo == null)
            {
                try
                {
                    // Contacts
                    entity.Id = ContactRepository.CreateTvts(entity);
                    if (entity.Id > 0)
                    {
                        // Redis
                        StoreData.LoadRedis(entity.Id);
                        // Message
                        ViewBag.Message = "Thêm mới contact thành công, bạn có thể chăm sóc luôn (" + "<a style=\"cursor: pointer;\" onclick=\"openDialog(" + entity.Id + ")\">Chăm sóc</a>)";
                    }
                    else
                    {
                        ViewBag.Message = "Thêm mới contact bị lỗi, vui lòng thử lại sau";
                        //Redis
                        StoreData.DeleteRedis(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, string.Empty, model.ContactInfo.Email, model.ContactInfo.Email2);
                    }
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "Thêm mới contact lỗi. Vui lòng thử lại. [" + ex.Message.ToString() + "]";
                    //Redis
                    StoreData.DeleteRedis(model.ContactInfo.Mobile1, model.ContactInfo.Mobile2, string.Empty, model.ContactInfo.Email, model.ContactInfo.Email2);
                }
            }
            else
            {
                #region "Start Checkpoint"
                CheckPointApi checkPointApi = new CheckPointApi();
                var           watch         = new Stopwatch();
                watch.Start();
                checkPointApi.CheckPointNew(UserContext.GetCurrentUser().UserName, "ContactUpdateUserId", "Start", 0);
                #endregion

                // Log duplicate
                entity.Id     = contactInfo.Id;
                entity.TypeId = contactInfo.TypeId;
                ContactRepository.ContactUpdateDuplicate(entity.Id, entity.TypeId, entity.StatusId);

                if (contactInfo.UserConsultantId == 0)
                {
                    // Update userId
                    entity.BranchId  = UserContext.GetDefaultBranch();
                    entity.CreatedBy = UserContext.GetCurrentUser().UserID;
                    ContactRepository.ContactUpdateUserId(entity.Id, entity.UserConsultantId, entity.BranchId, entity.CreatedBy);

                    // Message
                    ViewBag.Message = "Contact đã có trong hệ thống, và chưa được ai chăm sóc, bạn có thể chăm sóc luôn (" + "<a style=\"cursor: pointer;\" onclick=\"openDialog(" + entity.Id + ")\">Chăm sóc</a>)";
                }
                else if (contactInfo.UserConsultantId == entity.UserConsultantId)
                {
                    // Update userId
                    entity.BranchId  = UserContext.GetDefaultBranch();
                    entity.CreatedBy = UserContext.GetCurrentUser().UserID;
                    ContactRepository.ContactUpdateUserId(entity.Id, entity.UserConsultantId, entity.BranchId, entity.CreatedBy);
                    ViewBag.Message = "Contact đã có trong hệ thống, bạn có thể chăm sóc luôn (" + "<a style=\"cursor: pointer;\" onclick=\"openDialog(" + entity.Id + ")\">Chăm sóc</a>)";
                }
                else
                {
                    if (contactInfo.StatusId != (int)StatusType.HandoverConsultant)
                    {
                        // Update userId
                        entity.BranchId         = UserContext.GetDefaultBranch();
                        entity.UserConsultantId = UserContext.GetCurrentUser().UserID;
                        entity.CreatedBy        = UserContext.GetCurrentUser().UserID;
                        ContactRepository.ContactUpdateUserId(entity.Id, entity.UserConsultantId, entity.BranchId, entity.CreatedBy);
                        ViewBag.Message = "Contact đã có trong hệ thống, bạn có thể chăm sóc luôn (" + "<a style=\"cursor: pointer;\" onclick=\"openDialog(" + entity.Id + ")\">Chăm sóc</a>)";
                    }
                    else
                    {
                        var user = StoreData.ListUser.FirstOrDefault(c => c.UserID == contactInfo.UserConsultantId) ??
                                   UserRepository.GetInfo(contactInfo.UserConsultantId);
                        ViewBag.Message = user == null
                                              ? "Sđt hoặc Email bạn nhập đã bị trùng với Ứng viên: " + contactInfo.Id + " - " + contactInfo.Fullname + " - Level " + contactInfo.LevelId + "- TVTS: chưa có ai chăm sóc"
                                              : "Sđt hoặc Email bạn nhập đã bị trùng với Ứng viên: " + contactInfo.Id + " - " + contactInfo.Fullname + " - Level " + contactInfo.LevelId + "- TVTS: " + user.FullName;
                        //haihm
                        return(CreateTvts());
                    }
                }

                #region "End CheckPoint"
                watch.Stop();
                checkPointApi.CheckPointNew(UserContext.GetCurrentUser().UserName, "ContactUpdateUserId", "End", watch.ElapsedMilliseconds);
                #endregion
            }

            return(CreateTvts());
        }