|
|
<?php
use App\Models\User; use App\Models\System; use App\Models\Project; use App\Models\Business; use Illuminate\Support\Arr; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Database\Eloquent\Collection;
class BusinessSeeder extends Seeder { public function run() { Business::insert(factory(Business::class, 2000)->raw()); $projects = []; $systems = []; $business_user = []; $project_user = []; $project_id = 0; for ($business_id=1; $business_id <= 2000; $business_id++) { // distinct user ids
$user_ids = []; $added_user = []; for ($i=0; $i < 10; $i++) $user_ids [] = rand(1, 5000); $user_ids = array_values(array_unique($user_ids)); // add 1 to 7 member
for ($i=0; $i < rand(1, 8); $i++) { $added_user[] = $user_ids[$i]; $business_user[] = [ 'business_id' => $business_id, 'user_id' => $user_ids[$i], 'level' => $i == 0? 4: rand(0, 4), ]; }
$business_projects = factory(Project::class, rand(1, 3))->raw(); foreach ($business_projects as $project) { $project['business_id'] = $business_id; $projects[] = $project; $project_id++;
for ($i=0; $i < rand(0, 2); $i++) { if ($added_user[$i] ?? false) { $project_user[] = [ 'project_id' => $project_id, 'user_id' => $added_user[$i], 'level' => rand(0, 4), ]; } }
for ($i=0; $i < rand(1, 5); $i++) { $systems[] = factory(System::class)->raw([ 'project_id' => $project_id, 'business_id' => $business_id, ]); } }
// for ($i=0; $i < rand(1, 3); $i++) {
// // $project =
// }
// $business_user[] = [
// 'business_id' => $business_id,
// 'user_id' => 1,
// 'owner' => true,
// ];
} DB::table('business_user')->insert($business_user); Project::insert($projects); DB::table('project_user')->insert($project_user); DB::table('systems')->insert($systems); /** * Every business has one or more owners, which might be the owner of other business as well */ // $this->insertAdmins($business_ids, $owners);
/** * Every business has one or more members, which might be the member of other business as well */ // $this->insertMembers($business_ids);
}
/** * @param $business_ids * @param Collection $owners * @return array */ public function insertAdmins($business_ids, Collection $owners) { $relations = []; foreach ($business_ids as $id) { $relations[] = [ 'business_id' => $id, 'user_id' => $owners->random()->id, 'owner' => true, ]; }
// Specify the percentage of managers who have two or more businesses
$multiple = ceil(count($relations) / 100 * 30); foreach (Arr::random($relations, $multiple) as $relation) { $relations[] = [ 'business_id' => $relation['business_id'], 'user_id' => $owners->except($relation['user_id'])->random()->id, 'owner' => true, ]; }
DB::table('business_user')->insert($relations); }
/** * @param $business_ids */ public function insertMembers($business_ids): void { /** @var Collection $member_ids */ $member_ids = User::whereDoesntHave('businesses')->select('id')->pluck('id'); $relations = []; $current = []; foreach ($member_ids as $index => $id) { $relations[] = [ 'business_id' => $business = $business_ids->random(), 'user_id' => $id, ];
$current[$business][] = $index; }
$multiple = ceil(count($relations) / 100 * 20); foreach (Arr::random($relations, $multiple) as $relation) { $relations[] = [ 'business_id' => $relation['business_id'], 'user_id' => $member_ids->except($current[$relation['business_id']])->random(), ]; }
DB::table('business_user')->insert($relations); } }
|