示例#1
0
        /// <summary>
        /// This function will update the tag as deleted in DB and wll de-associate modules
        /// </summary>
        /// <param name="tagId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool DeleteTag(int tagId, int userId)
        {
            Tag tag = new Tag();


            tag = tagRepository.SingleOrDefault(x => x.TagId == tagId);
            if (tag != null)
            {
                tag.RecordDeleted = true;
                tag.ModifiedBy    = userId;
                tag.ModifiedDate  = DateTime.Now;

                //Delete tag associated Accounts
                accountTagRepository.Delete(x => x.TagId == tag.TagId);
                //Delete tag associated Contacts
                contactTagRepository.Delete(x => x.TagId == tag.TagId);

                //Delete tag associatd with lead
                leadTagRepository.Delete(x => x.TagId == tag.TagId);

                //update tag in db as deleted
                tagRepository.Update(tag);

                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Function used to Add/Update Account information
        /// </summary>
        /// <param name="accountModel"></param>
        public void AddUpdateAccount(AccountModel accountModel)
        {
            Account account = new Account();

            if (accountModel.ParentAccountId == 0)
            {
                accountModel.ParentAccountId = null;
            }
            if (accountModel.AccountTypeId == 0)
            {
                accountModel.AccountTypeId = null;
            }
            if (accountModel.IndustryId == 0)
            {
                accountModel.IndustryId = null;
            }


            //if account id is 0 then account will be added
            if (accountModel.AccountId == 0)
            {
                AutoMapper.Mapper.Map(accountModel, account);
                account.CreatedDate  = DateTime.UtcNow;
                account.ModifiedDate = DateTime.UtcNow;
                account.Address      = ValidateAddress(account.Address, accountModel.AddressModel);
                account.Address1     = ValidateAddress(account.Address1, accountModel.AddressModel1);


                accountRepository.Insert(account);
            }
            else   //if account id is not 0 then account will be updated
            {
                account = accountRepository.SingleOrDefault(x => x.AccountId == accountModel.AccountId);

                // Created by and CreatedDate will not change during mapping
                accountModel.CreatedBy   = account.CreatedBy;
                accountModel.CreatedDate = account.CreatedDate;

                AutoMapper.Mapper.Map(accountModel, account);
                account.ModifiedDate = DateTime.UtcNow;
                account.Address      = ValidateAddress(account.Address, accountModel.AddressModel);
                account.Address1     = ValidateAddress(account.Address1, accountModel.AddressModel1);
                accountRepository.Update(account);
            }


            List <int> listAllExistingTagIds       = new List <int>();
            List <int> listNewTagIds               = new List <int>();
            List <int> listContactAssociatedTagIds = new List <int>();
            List <int> listDeleteTagIds            = new List <int>();

            listContactAssociatedTagIds = account.AccountTags.Select(x => x.TagId).ToList();

            if (!string.IsNullOrEmpty(accountModel.AccountTagIds))
            {
                string[] arrTagIds = accountModel.AccountTagIds.Split(',');
                for (int i = 0; i < arrTagIds.Length; i++)
                {
                    int tagId = arrTagIds[i].Decrypt();

                    if (listContactAssociatedTagIds.Where(x => x == tagId).Count() == 0)
                    {
                        listNewTagIds.Add(tagId);
                    }
                    else
                    {
                        listAllExistingTagIds.Add(tagId);
                    }
                }
            }


            if (!string.IsNullOrEmpty(accountModel.NewTagNames))
            {
                List <int> lisNewAddedTagIds = new List <int>();
                lisNewAddedTagIds = AddTagsToSystem(accountModel.NewTagNames, accountModel.CompanyId, accountModel.CreatedBy.Value);

                if (lisNewAddedTagIds.Count > 0)
                {
                    listNewTagIds = listNewTagIds.Concat(lisNewAddedTagIds).ToList();
                }
            }
            for (int i = 0; i < listContactAssociatedTagIds.Count; i++)
            {
                if (listAllExistingTagIds.Where(x => x == listContactAssociatedTagIds[i]).Count() == 0)
                {
                    listDeleteTagIds.Add(listContactAssociatedTagIds[i]);
                }
            }

            //Associate Tagids to contact
            if (listNewTagIds.Count > 0)
            {
                AccountTag accountTag;
                for (int i = 0; i < listNewTagIds.Count; i++)
                {
                    accountTag           = new AccountTag();
                    accountTag.AccountId = account.AccountId;
                    accountTag.TagId     = listNewTagIds[i];
                    accountTagRepository.Insert(accountTag);
                }
            }

            if (listDeleteTagIds.Count > 0)
            {
                accountTagRepository.Delete(x => x.AccountId == account.AccountId && listDeleteTagIds.Contains(x.TagId));
            }
        }