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.

123 lines
3.7 KiB

  1. <?php
  2. use App\Business;
  3. use App\Project;
  4. use Illuminate\Database\Seeder;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\DB;
  7. class ProjectSeeder extends Seeder
  8. {
  9. public function run()
  10. {
  11. /**
  12. * Every business has one or more projects
  13. * Every projects might have sub projects
  14. */
  15. $businesses = Business::select('id')->pluck('id');
  16. /**
  17. * One level projects
  18. */
  19. $this->insertMainProjects($businesses);
  20. /**
  21. * Multi level projects
  22. * Be careful with private projects
  23. */
  24. $this->insertChildProjects();
  25. $this->insertGrandChildrenProjects();
  26. /**
  27. * Assign people to the projects
  28. * * Be careful with private projects
  29. */
  30. $this->assignPeopleToProjects();
  31. }
  32. /**
  33. * @param $businesses
  34. * @return void
  35. */
  36. public function insertMainProjects($businesses): void
  37. {
  38. $projects = [];
  39. foreach ($businesses as $business) {
  40. $projects[] = factory(Project::class, 5)->raw([
  41. 'business_id' => $business,
  42. 'private' => rand(1, 100) <= 10,
  43. ]);
  44. }
  45. DB::table('projects')->insertOrIgnore(Arr::collapse($projects));
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function insertChildProjects(): void
  51. {
  52. $projects = Project::inRandomOrder()->select('id', 'business_id')->take(100)->get()->toArray();
  53. $child_projects = [];
  54. foreach ($projects as $project) {
  55. $child_projects[] = factory(Project::class)->raw([
  56. 'parent_id' => $project['id'],
  57. 'business_id' => $project['business_id'],
  58. 'private' => rand(1, 100) <= 10,
  59. ]);
  60. }
  61. DB::table('projects')->insert($child_projects);
  62. }
  63. public function insertGrandChildrenProjects(): void
  64. {
  65. $projects = Project::inRandomOrder()
  66. ->select('id', 'business_id', 'parent_id')
  67. ->whereNotNull('parent_id')
  68. ->take(25)->get()->toArray();
  69. $grand_child_projects = [];
  70. foreach ($projects as $project) {
  71. $grand_child_projects[] = factory(Project::class)->raw([
  72. 'parent_id' => $project['id'],
  73. 'business_id' => $project['business_id'],
  74. 'private' => rand(1, 100) <= 10,
  75. ]);
  76. }
  77. DB::table('projects')->insertOrIgnore($grand_child_projects);
  78. }
  79. public function assignPeopleToProjects(): void
  80. {
  81. // Schema::create('')
  82. // $table->unsignedBigInteger('project_id');
  83. // $table->unsignedBigInteger('');
  84. $project_members = [];
  85. $projects = Project::with('parent','children','business')->get();
  86. foreach ($projects as $project) {
  87. // detect the main projects
  88. if ($project->children->isNotEmpty()) {
  89. continue;
  90. }
  91. $business_owner = $project->business->owners->random();
  92. $business_members = $project->business->members->random($project->business->members->count() - 1);
  93. $project_members[] = [
  94. 'project_id' => $project->id,
  95. 'user_id' => $business_owner->id,
  96. ];
  97. foreach ($business_members as $business_member) {
  98. $project_members[] = [
  99. 'project_id' => $project->id,
  100. 'user_id' => $business_member->id,
  101. ];
  102. }
  103. // todo : how to detect a project has sub project
  104. // child
  105. // grand child
  106. }
  107. DB::table('project_user')->insert($project_members);
  108. }
  109. }