Ejemplo n.º 1
0
 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;
     }
 }
Ejemplo n.º 2
0
 public static function StandardItemsMap($datasetID)
 {
     $standardItems = StandardItem::where('dataset_id', $datasetID)->get();
     $standardMap = [];
     foreach ($standardItems as $standardItem) {
         $standardMap[$standardItem->slug] = $standardItem->id;
     }
     return $standardMap;
 }
 public function standardItem2DBImpl($path, $datasetId)
 {
     if (StandardItem::where('dataset_id', $datasetId)->count() == 0) {
         $maxID = DB::table("standard_items")->max('id');
         if ($maxID == null) {
             $maxID = 1;
         } else {
             $maxID += 1;
         }
         $dataset = Dataset::findOrFail($datasetId);
         $dataset->current_standard_id = $maxID;
         $dataset->save();
     }
     $fh = fopen($path, 'r');
     while ($line = fgets($fh)) {
         $row = explode(",", $line);
         StandardItem::create(['name' => $row[0], 'slug' => $row[1], 'path' => $row[2], 'dataset_id' => $datasetId]);
     }
     fclose($fh);
 }