private function newBatch($dbId, $userId)
 {
     $expiresAt = Carbon::now()->subDay(2);
     \DebugBar::warning('New Batch');
     $ownBatch = \App\Batch::where('user_id', $userId)->where('remain_count', '>', 0)->first();
     if ($ownBatch != null) {
         \DebugBar::warning('You own a batch');
         return $ownBatch;
         // if a batch is still owns by a user, give it to our user.
     }
     $expireBatch = \App\Batch::where('updated_at', '<=', $expiresAt)->first();
     $db = \App\Dataset::find($dbId);
     if ($expireBatch == null) {
         \DebugBar::warning('if($expireBatch == null)');
         $current_standard_id = $db->current_standard_id;
         if ($current_standard_id == 0) {
             // no standard id
             \DebugBar::warning('if($current_standard_id == 0)');
             return null;
         }
         $current_standard = \App\StandardItem::find($current_standard_id);
         $batches = \App\Batch::where('user_id', $userId)->orderBy('standard_item_id', 'desc')->first();
         if ($current_standard->batch_count == 3) {
             // all dispatched
             \DebugBar::warning('if( $current_standard->batch_count == 3 )');
             $current_standard = \App\StandardItem::where('id', '>', $current_standard_id)->first();
             if ($current_standard == null) {
                 return null;
             }
             $current_standard_id = $current_standard->id;
             $db->current_standard_id = $current_standard_id;
             $db->save();
         }
         if ($batches) {
             $max_standard_id = max($batches->standard_item_id + 1, $current_standard_id);
         } else {
             $max_standard_id = $current_standard_id;
         }
         $current_standard = \App\StandardItem::where("id", ">=", $max_standard_id)->first();
         \DebugBar::warning('$current_standard:' . $current_standard);
         if ($current_standard == null) {
             return null;
         }
         $newBatch = new \App\Batch();
         $newBatch->fill(['standard_item_id' => $current_standard->id, 'user_id' => $userId, 'remain_count' => count($current_standard->Items)]);
         $newBatch->save();
         $current_standard->batch_count++;
         $current_standard->save();
         \DebugBar::warning('$newBatch:' . $newBatch);
         return $newBatch;
     } else {
         $expiredUserId = $expireBatch->user_id;
         $expiredUser = \App\User::where("user_id", $expiredUserId);
         $expiredUser->batch_id = -1;
         $expiredUser->save();
         $expireBatch->user_id = $userId;
         $expireBatch->save();
         return $expireBatch;
     }
 }
Exemplo n.º 2
0
 public function test_batch_model_group()
 {
     $batch = App\Batch::find(1);
     $group = $batch->project_group->name;
     $this->assertNotNull($group);
 }