public CCFieldValue RevertChangeToFieldValues(CCFieldValue FieldValueObj, CCHistoryLog HistLogObj)
        {
            CCFieldValue dbEntry = context.CCFieldValues.Find(FieldValueObj.ValueID);
            if (dbEntry != null)
            {
                CCHistoryLog HistoryLog = new CCHistoryLog();
                HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                HistoryLog.Date = DateTime.Now;
                HistoryLog.FieldID = dbEntry.FieldID;
                HistoryLog.ItemID = dbEntry.ItemID;
                HistoryLog.NewValue = FieldValueObj.Value;
                HistoryLog.OldValue = HistLogObj.NewValue;
                HistoryLog.Source = "Web";
                HistoryLog.Action = "Update";
                HistoryLog.ConnectionID = 0;
                context.CCHistoryLog.Add(HistoryLog);

                dbEntry.Value = FieldValueObj.Value;

                context.SaveChanges();
            }
            return FieldValueObj;
        }
        public long ImportInputContacts(long fid, string aguid, string path, string fileExtension, long maxItemImportCount)
        {
            if (inputdatas == null)
                inputdatas = new DataInputHelper(path, fileExtension);

            List<CCFolderField> FieldsByFolderID = null;
            FieldsByFolderID = CCFieldRepository.CCFolderFields.Where(id => id.FolderID == fid).ToList();
            DataTable valus = inputdatas.GetImportExcel();
            bool _readheader = true;
            IsFieldValueMatch = false;
            var _availablevalue = new List<Tuple<long, bool>>();

            var totalRowCount = (valus.Rows.Count)-1;

            if (totalRowCount <= maxItemImportCount)
            {
                foreach (DataRow row in valus.Rows)
                {
                    AddDedupeViewModel dedupe = new AddDedupeViewModel();
                    int _fieldcount = 0;
                    long contectID = 0;
                    string notes = "";
                    if (!_readheader && IsFieldValueMatch)
                    {
                        contectID = CCItemRepository.CreateContact(fid, aguid);
                    }
                    foreach (DataColumn col in valus.Columns)
                    {
                        string _colname = row[col].ToString();
                        if (_readheader) // run when it header
                        {
                            // set correct Header to Field name
                            var reponse = FieldsByFolderID.Find(name => name.FieldName == _colname);
                            if (reponse != null)
                            {
                                _availablevalue.Add(new Tuple<long, bool>(reponse.FieldID, true));
                                IsFieldValueMatch = true;
                            }
                            else
                            {
                                _availablevalue.Add(new Tuple<long, bool>(0, false));
                            }

                        }
                        else
                        { // reu when it columns value
                            if (_availablevalue[_fieldcount].Item2)
                            {
                                CCFieldValue objFieldValue = new CCFieldValue();
                                objFieldValue.FieldID = _availablevalue[_fieldcount].Item1;
                                if (_colname == String.Empty) objFieldValue.Value = String.Empty;
                                else objFieldValue.Value = _colname;
                                objFieldValue.ItemID = contectID;
                                objFieldValue.LastUpdated = DateTime.Now.ToUniversalTime();
                                objFieldValue.AccountGUID = aguid;
                                ObjFieldValues.Add(objFieldValue);

                                // update dedupe
                                var fieldName = FieldsByFolderID.Find(id => id.FieldID == _availablevalue[_fieldcount].Item1).FieldCaption;
                                if (fieldName == "First Name") { dedupe.FirstName = _colname; }
                                if (fieldName == "Middle Name") { dedupe.MiddleName = _colname; }
                                if (fieldName == "Last Name") { dedupe.LastName = _colname; }
                                if (fieldName == "Company") { dedupe.CompanyName = _colname; }
                                if (fieldName == "Email Address") { dedupe.Email = _colname; }
                                // End update dedupe

                                if(fieldName == "Notes")
                                    notes = _colname;

                            }

                            _fieldcount++;

                        }
                    }
                    bool res = UpdateContact(dedupe, contectID, 1, notes);
                    _readheader = false;

                }

                var savedfields = CCFieldValueRepository.SaveFieldsObjValues(ObjFieldValues);

                if (IsFieldValueMatch == false) NumberOfContacts = 0;
                else NumberOfContacts = valus.Rows.Count;

                return NumberOfContacts;
            }
            else
            {
                return -2;
            }
        }
        public CCFieldValue SaveFieldValues(CCFieldValue FieldValueObj)
        {
            if (FieldValueObj.ValueID == 0)
            {
                context.CCFieldValues.Add(FieldValueObj);
                //context.SaveChanges();

                CCHistoryLog HistoryLog = new CCHistoryLog();
                HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                HistoryLog.Date = DateTime.Now;
                HistoryLog.FieldID = FieldValueObj.FieldID;
                HistoryLog.ItemID = FieldValueObj.ItemID;
                HistoryLog.NewValue = FieldValueObj.Value;
                HistoryLog.OldValue = "";
                HistoryLog.Source = "Web";
                HistoryLog.Action = "Insert";
                HistoryLog.ConnectionID = 0;
                context.CCHistoryLog.Add(HistoryLog);
                context.SaveChanges();
            }
            else
            {
                CCFieldValue dbEntry = context.CCFieldValues.Find(FieldValueObj.ValueID);
                if (dbEntry != null)
                {
                    if (dbEntry.Value != FieldValueObj.Value)
                    {
                        CCHistoryLog HistoryLog = new CCHistoryLog();
                        HistoryLog.AccountGUID = FieldValueObj.AccountGUID;
                        HistoryLog.Date = DateTime.Now;
                        HistoryLog.FieldID = dbEntry.FieldID;
                        HistoryLog.ItemID = dbEntry.ItemID;
                        HistoryLog.NewValue = FieldValueObj.Value;
                        HistoryLog.OldValue = dbEntry.Value;
                        HistoryLog.Source = "Web";
                        HistoryLog.Action = "Update";
                        HistoryLog.ConnectionID = 0;
                        context.CCHistoryLog.Add(HistoryLog);
                        //context.SaveChanges();

                        dbEntry.LastUpdated = DateTime.UtcNow;
                    }
                    dbEntry.Value = FieldValueObj.Value;
                    context.SaveChanges();
                }

            }
            return FieldValueObj;
        }
        public bool ImportSingleContact(AddContactViewModel objContact, int type,string accountGUID, string timeZone)
        {
            List<string> fieldValus = objContact.FolderValues.Split('|').ToList();
            List<CCFieldValue> ObjFieldValues = new List<CCFieldValue>();
            List<CCFieldValue> savedfields = new List<CCFieldValue>();
            AddDedupeViewModel dedupe = new AddDedupeViewModel();
            DateTime startTime;
            DateTime endTime;
            string notes = "";

            long contectID = CCItemRepository.CreateContact(objContact.FolderID, accountGUID);
            if (contectID > 0)
            {
                List<CCFolderField> folderFields = new List<CCFolderField>();
                folderFields = CCFieldRepository.CCFolderFields.Where(fid => fid.FolderID == objContact.FolderID & fid.AccountGUID==accountGUID).ToList();

                int i = 0;
                foreach (var field in fieldValus)
                {
                    if (field != "")
                    {
                        var fieldName = folderFields[i].FieldCaption;
                        if (type == 1)
                        {
                            if (fieldName == "First Name") { dedupe.FirstName = field; }
                            if (fieldName == "Middle Name") { dedupe.MiddleName = field; }
                            if (fieldName == "Last Name") { dedupe.LastName = field; }
                            if (fieldName == "Company") { dedupe.CompanyName = field; }
                            if (fieldName == "Email Address") { dedupe.Email = field; }
                        }
                        else
                        {
                            if (fieldName == "Subject") { dedupe.Subject = field; }
                            if (fieldName == "Start Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                startTime = DateTime.Parse(field);
                                dedupe.StartDateTime = ConvertLocaltoUTC(startTime, timeZone).ToString(format);
                            }
                            if (fieldName == "End Time")
                            {
                                string format = "yyyy-MM-dd HH:mm";
                                //endTime = DateTimeOffset.Parse(field).UtcDateTime;
                                //dedupe.EndDateTime = endTime.ToString(format);
                                endTime = DateTime.Parse(field);
                                dedupe.EndDateTime = ConvertLocaltoUTC(endTime, timeZone).ToString(format);
                            }
                        }

                        if (fieldName == "Notes")
                            notes = field;

                        var fieldID = folderFields[i].FieldID;
                        i++;

                        CCFieldValue objFieldValue = new CCFieldValue();
                        objFieldValue.FieldID = fieldID;
                        if (fieldName == "Start Time") { objFieldValue.Value = dedupe.StartDateTime; }
                        else if (fieldName == "End Time") { objFieldValue.Value = dedupe.EndDateTime; }
                        else
                        {
                            if (field == " ") objFieldValue.Value = String.Empty;
                            else objFieldValue.Value = field;
                        }
                        objFieldValue.ItemID = contectID;
                        objFieldValue.LastUpdated = DateTime.UtcNow;
                        objFieldValue.AccountGUID = accountGUID;
                        ObjFieldValues.Add(objFieldValue);
                    }
                }
                savedfields = CCFieldValueRepository.SaveFieldsObjValues(ObjFieldValues);
            }
            if (savedfields.Count() > 0)
            {
                bool res = UpdateContact(dedupe, contectID, type, notes);
                return true;
            }
            else return false;
        }