protected void TryResizeIfNeed(IEmployeeRelationService service)
        {
            EmployeeRelation firstRange = null;
            EmployeeRelation lastRange  = null;

            if (_Relations != null && _Relations.Count > 0)
            {
                _Relations.Sort();
                firstRange = _Relations[0];

                if (firstRange.BeginTime != BeginDate)
                {
                    firstRange.BeginTime = BeginDate;
                }
                lastRange = _Relations[_Relations.Count - 1];
                if (firstRange.EndTime != EndDate)
                {
                    firstRange.EndTime = EndDate;
                }

                if (_Relations.Count == 1)
                {
                    service.SaveOrUpdate(firstRange);
                }
                else
                {
                    service.SaveOrUpdate(firstRange);
                    service.SaveOrUpdate(lastRange);
                }
            }
            if (NextContract != null)
            {
                NextContract.TryResizeIfNeed(service);
            }
        }
        public void CheckAndModify(EmployeeRelation relation, IEmployeeRelationService service)
        {
            if (CheckIfOutside(relation))
            {
                //relation.EndTime = relation.BeginTime.AddDays(-1);
                service.Delete(relation);
                return;
            }


            if (Include(relation))
            {
                Relations.Add(relation);
                return;
            }
            else if (DateTimeHelper.IsIntersectExc(relation.BeginTime, relation.EndTime, BeginDate, EndDate))
            {
                if (DateTimeHelper.Between(relation.BeginTime, BeginDate, EndDate))
                {
                    EmployeeRelation newrelation = relation.GetCopy();
                    newrelation.ID        = 0;
                    newrelation.BeginTime = NextDay;

                    relation.EndTime = EndDate;
                    Debug.Assert(relation.IsValidRelation());
                    Relations.Add(relation);
                    service.SaveOrUpdate(relation);
                    relation = newrelation;
                }
                else if (DateTimeHelper.Between(relation.EndTime, BeginDate, EndDate))
                {
                    EmployeeRelation newrelation = relation.GetCopy();
                    newrelation.ID      = 0;
                    newrelation.EndTime = PrevDay;

                    relation.BeginTime = BeginDate;
                    Debug.Assert(relation.IsValidRelation());

                    Relations.Add(relation);
                    service.SaveOrUpdate(relation);
                    relation = newrelation;
                }
                if (NextContract != null)
                {
                    NextContract.CheckAndModify(relation, service);
                }
            }
            else if (NextContract != null)
            {
                NextContract.CheckAndModify(relation, service);
            }
            return;
        }