You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.6 KiB

  1. <?php
  2. use App\Business;
  3. use App\User;
  4. use App\Project;
  5. use App\System;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Database\Seeder;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Facades\DB;
  10. class BusinessSeeder extends Seeder
  11. {
  12. public function run()
  13. {
  14. Business::insert(factory(Business::class, 2000)->raw());
  15. $projects = [];
  16. $systems = [];
  17. $business_user = [];
  18. $project_user = [];
  19. $project_id = 0;
  20. for ($business_id=1; $business_id <= 2000; $business_id++) {
  21. // distinct user ids
  22. $user_ids = [];
  23. $added_user = [];
  24. for ($i=0; $i < 10; $i++) $user_ids [] = rand(1, 5000);
  25. $user_ids = array_values(array_unique($user_ids));
  26. // add 1 to 7 member
  27. for ($i=0; $i < rand(1, 8); $i++) {
  28. $added_user[] = $user_ids[$i];
  29. $business_user[] = [
  30. 'business_id' => $business_id,
  31. 'user_id' => $user_ids[$i],
  32. 'level' => $i == 0? 4: rand(0, 4),
  33. ];
  34. }
  35. $business_projects = factory(Project::class, rand(1, 3))->raw();
  36. foreach ($business_projects as $project) {
  37. $project['business_id'] = $business_id;
  38. $projects[] = $project;
  39. $project_id++;
  40. for ($i=0; $i < rand(0, 2); $i++) {
  41. if ($added_user[$i] ?? false) {
  42. $project_user[] = [
  43. 'project_id' => $project_id,
  44. 'user_id' => $added_user[$i],
  45. 'level' => rand(0, 4),
  46. ];
  47. }
  48. }
  49. for ($i=0; $i < rand(1, 5); $i++) {
  50. $systems[] = factory(System::class)->raw([
  51. 'project_id' => $project_id,
  52. 'business_id' => $business_id,
  53. ]);
  54. }
  55. }
  56. // for ($i=0; $i < rand(1, 3); $i++) {
  57. // // $project =
  58. // }
  59. // $business_user[] = [
  60. // 'business_id' => $business_id,
  61. // 'user_id' => 1,
  62. // 'owner' => true,
  63. // ];
  64. }
  65. DB::table('business_user')->insert($business_user);
  66. Project::insert($projects);
  67. DB::table('project_user')->insert($project_user);
  68. DB::table('systems')->insert($systems);
  69. /**
  70. * Every business has one or more owners, which might be the owner of other business as well
  71. */
  72. // $this->insertAdmins($business_ids, $owners);
  73. /**
  74. * Every business has one or more members, which might be the member of other business as well
  75. */
  76. // $this->insertMembers($business_ids);
  77. }
  78. /**
  79. * @param $business_ids
  80. * @param Collection $owners
  81. * @return array
  82. */
  83. public function insertAdmins($business_ids, Collection $owners)
  84. {
  85. $relations = [];
  86. foreach ($business_ids as $id) {
  87. $relations[] = [
  88. 'business_id' => $id,
  89. 'user_id' => $owners->random()->id,
  90. 'owner' => true,
  91. ];
  92. }
  93. // Specify the percentage of managers who have two or more businesses
  94. $multiple = ceil(count($relations) / 100 * 30);
  95. foreach (Arr::random($relations, $multiple) as $relation) {
  96. $relations[] = [
  97. 'business_id' => $relation['business_id'],
  98. 'user_id' => $owners->except($relation['user_id'])->random()->id,
  99. 'owner' => true,
  100. ];
  101. }
  102. DB::table('business_user')->insert($relations);
  103. }
  104. /**
  105. * @param $business_ids
  106. */
  107. public function insertMembers($business_ids): void
  108. {
  109. /** @var Collection $member_ids */
  110. $member_ids = User::whereDoesntHave('businesses')->select('id')->pluck('id');
  111. $relations = [];
  112. $current = [];
  113. foreach ($member_ids as $index => $id) {
  114. $relations[] = [
  115. 'business_id' => $business = $business_ids->random(),
  116. 'user_id' => $id,
  117. ];
  118. $current[$business][] = $index;
  119. }
  120. $multiple = ceil(count($relations) / 100 * 20);
  121. foreach (Arr::random($relations, $multiple) as $relation) {
  122. $relations[] = [
  123. 'business_id' => $relation['business_id'],
  124. 'user_id' => $member_ids->except($current[$relation['business_id']])->random(),
  125. ];
  126. }
  127. DB::table('business_user')->insert($relations);
  128. }
  129. }