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.

174 lines
5.0 KiB

  1. <?php
  2. use App\TagTask;
  3. use App\Task;
  4. use Illuminate\Database\Seeder;
  5. class TaskSeeder extends Seeder
  6. {
  7. public function run()
  8. {
  9. for ($i = 1; $i <= 1; $i++) {
  10. \App\Task::insert(
  11. factory(\App\Task::class, 2000)->raw()
  12. );
  13. }
  14. $tags = [
  15. 1 => 'باربری',
  16. 2 => 'زیر چاپ',
  17. 3 => 'برون سپاری شود',
  18. 4 => 'مشتری نپسندید',
  19. ];
  20. $ids = Task::select('id')->pluck('id')->toArray();
  21. $tag_task_relation = [];
  22. foreach ($ids as $id) {
  23. foreach (array_rand($tags, mt_rand(2, 4)) as $index => $tag) {
  24. $tag_task_relation[] = [
  25. 'task_id' => $id,
  26. 'tag_id' => $tag,
  27. ];
  28. }
  29. }
  30. TagTask::insert($tag_task_relation);
  31. $this->createRelatedData();
  32. }
  33. public function createRelatedData()
  34. {
  35. Task::whereIn('business_id', [1, 2, 3, 4, 5])
  36. ->update(['business_id' => 1779, 'project_id' => 0, 'workflow_id' => 0, 'status_id' => 0,
  37. 'system_id' => 0, 'sprint_id' => 0]);
  38. $count = Task::where('business_id', '=', 1779)->count();
  39. $projects = [
  40. 3566 => [
  41. 'systems' => [8967, 8968, 8969, 8970],
  42. 'sprints' => [],
  43. ],
  44. 3567 => [
  45. 'systems' => [8971],
  46. 'sprints' => [526]
  47. ],
  48. 3568 => [
  49. 'systems' => [8972, 8973],
  50. 'sprints' => [5480]
  51. ]
  52. ];
  53. $workflows = [
  54. 2277 => [5100, 5101],
  55. 2278 => [5102, 5103],
  56. 2279 => [5104, 5105, 5106, 5107]
  57. ];
  58. $take = $count > 50 ? 20 : 10;
  59. $this->updateField(array_keys($projects), $count, $take, 'project_id');
  60. $this->updateField(array_keys($workflows), $count, $take, 'workflow_id');
  61. $this->fillSprintAndSystem($projects);
  62. $this->fillStatues($workflows);
  63. $this->fillTaskTag($count);
  64. }
  65. public function fillSprintAndSystem($projects)
  66. {
  67. foreach ($projects as $key => $value) {
  68. $count = Task::where('project_id', $key)->count();
  69. $take = $count > 10 ? 2 : 1;
  70. $sprintIndex = 0;
  71. $systemIndex = 0;
  72. $i = 0;
  73. while ($count > $i) {
  74. Task::where('project_id', '=', $key)
  75. ->where('system_id', 0)->where('sprint_id', 0)
  76. ->take($take)
  77. ->update(
  78. [
  79. 'sprint_id' => isset($value['sprints'][$sprintIndex]) ? $value['sprints'][$sprintIndex] : 0,
  80. 'system_id' => isset($value['systems'][$systemIndex]) ? $value['systems'][$systemIndex] : 0,
  81. ]);
  82. $systemIndex += 1;
  83. $sprintIndex += 1;
  84. if ($systemIndex >= count($value['systems'])) {
  85. $systemIndex = 0;
  86. }
  87. if ($sprintIndex >= count($value['sprints'])) {
  88. $sprintIndex = 0;
  89. }
  90. $i += $take;
  91. }
  92. }
  93. Task::whereIn('project_id', array_keys($projects))
  94. ->where('system_id', 0)
  95. ->update(['system_id' => null]);
  96. Task::whereIn('project_id', array_keys($projects))
  97. ->where('sprint_id', 0)
  98. ->update(['sprint_id' => null]);
  99. }
  100. public function updateField($values, $count, $take, $field)
  101. {
  102. $i = 0;
  103. $index = 0;
  104. while ($count > $i) {
  105. Task::where($field, '=', 0)->take($take)->update([$field => $values[$index]]);
  106. $index += 1;
  107. if (count($values) <= $index) {
  108. $index = 0;
  109. }
  110. $i += $take;
  111. }
  112. }
  113. public function fillStatues($workflows)
  114. {
  115. foreach ($workflows as $key => $value) {
  116. $count = Task::where('workflow_id', $key)->count();
  117. $take = $count > 10 ? 2 : 1;
  118. $index = 0;
  119. $i = 0;
  120. while ($count > $i) {
  121. Task::where('workflow_id', '=', $key)
  122. ->where('status_id', 0)
  123. ->take($take)
  124. ->update(
  125. [
  126. 'status_id' => $value[$index]
  127. ]);
  128. $index += 1;
  129. if ($index >= count($value)) {
  130. $index = 0;
  131. }
  132. $i += $take;
  133. }
  134. }
  135. }
  136. public function fillTaskTag($count)
  137. {
  138. $tags = [5037, 5038, 5626];
  139. $tasks = Task::where('business_id', 1779)->get();
  140. foreach ($tasks as $task) {
  141. $rand = rand(0,2);
  142. $t = [];
  143. do{
  144. array_push($t, new TagTask(['task_id' => $task->id, 'tag_id' => $tags[$rand]]));
  145. $rand--;
  146. }while($rand >= 0);
  147. $task->tags()->saveMany($t);
  148. }
  149. }
  150. }