public ValidationResult IsSatisfiedBy(WorkingTimeRange workingTimeRange)
        {
            if (workingTimeRange.TaskId == null)
            {
                return(new ValidationResult("タスクが割り当てられていません"));
            }

            var timeRangesAtSameday = _WorkingTimeRangeRepository.SelectByYmd(workingTimeRange.TimePeriod.TargetYmd)
                                      .Where(t => t.Id != workingTimeRange.Id)
                                      .ToArray();

            // 調整中...
            // 時間の重複チェック
            // 自身以外の作業時間と重複していないか
            //var overlapTimes = timeRangesAtSameday.Where(t => t.TimePeriod.IsOverlapped(workingTimeRange.TimePeriod)).ToArray();
            //if (overlapTimes.Any())
            //{

            //    return new ValidationResult("");
            //}

            // 新規登録の場合のみ
            if (workingTimeRange.Id.IsTemporary)
            {
                // 終了時間未設定のタスクがあればNG
                if (timeRangesAtSameday.Any(t => t.TimePeriod.IsStopped == false))
                {
                    return(new ValidationResult("未完了のタスクが残っています"));
                }
            }

            return(ValidationResult.Success);
        }
        public WorkingTimeRange StartWorking(Identity <WorkTask> id)
        {
            var targetTask = _WorkTaskRepository.SelectById(id);

            if (targetTask == null)
            {
                throw new NotFoundException("開始対象のタスクがみつかりませんでした");
            }

            // 実行中のタスクがあれば終了する
            var today   = SystemClockServiceLocator.Current.Now.ToString("yyyyMMdd");
            var working = _WorkingTimeRangeRepository.SelectByYmd(today).FirstOrDefault(w => w.IsDoing);

            if (working != null)
            {
                StopWorkingCore(working);
            }

            if (targetTask.IsCompleted)
            {
                _WorkTaskCompletionCommand.ReStartTask(id);
            }

            var newWorkingTime = WorkingTimeRange.ForStart(id);

            var validationResult = _WorkingTimeRegistSpecification.IsSatisfiedBy(newWorkingTime);

            if (validationResult != null)
            {
                throw new SpecificationCheckException(validationResult);
            }

            return(_WorkingTimeRangeRepository.Add(newWorkingTime));
        }