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.

157 lines
5.2 KiB

  1. <?php
  2. use App\Models\User;
  3. use App\Models\System;
  4. use App\Models\Project;
  5. use App\Models\Business;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Database\Seeder;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Database\Eloquent\Collection;
  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. // make sure user with id 1 is owner in one business
  70. $user_businesses = DB::table('business_user')->where('user_id', 1)->get();
  71. if ($user_businesses->max('level') < enum('roles.manager.id')){
  72. $firstRecord = $user_businesses->first();
  73. DB::table('business_user')
  74. ->where([
  75. 'business_id' => $firstRecord->business_id,
  76. 'user_id' => $firstRecord->user_id,
  77. 'level' => $firstRecord->level,
  78. ]) ->update(['level' => enum('roles.manager.id')]);
  79. }
  80. /**
  81. * Every business has one or more owners, which might be the owner of other business as well
  82. */
  83. // $this->insertAdmins($business_ids, $owners);
  84. /**
  85. * Every business has one or more members, which might be the member of other business as well
  86. */
  87. // $this->insertMembers($business_ids);
  88. }
  89. /**
  90. * @param $business_ids
  91. * @param Collection $owners
  92. * @return array
  93. */
  94. public function insertAdmins($business_ids, Collection $owners)
  95. {
  96. $relations = [];
  97. foreach ($business_ids as $id) {
  98. $relations[] = [
  99. 'business_id' => $id,
  100. 'user_id' => $owners->random()->id,
  101. 'owner' => true,
  102. ];
  103. }
  104. // Specify the percentage of managers who have two or more businesses
  105. $multiple = ceil(count($relations) / 100 * 30);
  106. foreach (Arr::random($relations, $multiple) as $relation) {
  107. $relations[] = [
  108. 'business_id' => $relation['business_id'],
  109. 'user_id' => $owners->except($relation['user_id'])->random()->id,
  110. 'owner' => true,
  111. ];
  112. }
  113. DB::table('business_user')->insert($relations);
  114. }
  115. /**
  116. * @param $business_ids
  117. */
  118. public function insertMembers($business_ids): void
  119. {
  120. /** @var Collection $member_ids */
  121. $member_ids = User::whereDoesntHave('businesses')->select('id')->pluck('id');
  122. $relations = [];
  123. $current = [];
  124. foreach ($member_ids as $index => $id) {
  125. $relations[] = [
  126. 'business_id' => $business = $business_ids->random(),
  127. 'user_id' => $id,
  128. ];
  129. $current[$business][] = $index;
  130. }
  131. $multiple = ceil(count($relations) / 100 * 20);
  132. foreach (Arr::random($relations, $multiple) as $relation) {
  133. $relations[] = [
  134. 'business_id' => $relation['business_id'],
  135. 'user_id' => $member_ids->except($current[$relation['business_id']])->random(),
  136. ];
  137. }
  138. DB::table('business_user')->insert($relations);
  139. }
  140. }