raw() ); } $tags = [ 1 => 'باربری', 2 => 'زیر چاپ', 3 => 'برون سپاری شود', 4 => 'مشتری نپسندید', ]; $ids = Task::select('id')->pluck('id')->toArray(); $tag_task_relation = []; foreach ($ids as $id) { foreach (array_rand($tags, mt_rand(2, 4)) as $index => $tag) { $tag_task_relation[] = [ 'task_id' => $id, 'tag_id' => $tag, ]; } } TagTask::insert($tag_task_relation); $this->createRelatedData(); } public function createRelatedData() { Task::whereIn('business_id', [1, 2, 3, 4, 5]) ->update(['business_id' => 1779, 'project_id' => 0, 'workflow_id' => 0, 'status_id' => 0, 'system_id' => 0, 'sprint_id' => 0]); $count = Task::where('business_id', '=', 1779)->count(); $projects = [ 3566 => [ 'systems' => [8967, 8968, 8969, 8970], 'sprints' => [], ], 3567 => [ 'systems' => [8971], 'sprints' => [526] ], 3568 => [ 'systems' => [8972, 8973], 'sprints' => [5480] ] ]; $workflows = [ 2277 => [5100, 5101], 2278 => [5102, 5103], 2279 => [5104, 5105, 5106, 5107] ]; $take = $count > 50 ? 20 : 10; $this->updateField(array_keys($projects), $count, $take, 'project_id'); $this->updateField(array_keys($workflows), $count, $take, 'workflow_id'); $this->fillSprintAndSystem($projects); $this->fillStatues($workflows); $this->fillTaskTag($count); } public function fillSprintAndSystem($projects) { foreach ($projects as $key => $value) { $count = Task::where('project_id', $key)->count(); $take = $count > 10 ? 2 : 1; $sprintIndex = 0; $systemIndex = 0; $i = 0; while ($count > $i) { Task::where('project_id', '=', $key) ->where('system_id', 0)->where('sprint_id', 0) ->take($take) ->update( [ 'sprint_id' => isset($value['sprints'][$sprintIndex]) ? $value['sprints'][$sprintIndex] : 0, 'system_id' => isset($value['systems'][$systemIndex]) ? $value['systems'][$systemIndex] : 0, ]); $systemIndex += 1; $sprintIndex += 1; if ($systemIndex >= count($value['systems'])) { $systemIndex = 0; } if ($sprintIndex >= count($value['sprints'])) { $sprintIndex = 0; } $i += $take; } } Task::whereIn('project_id', array_keys($projects)) ->where('system_id', 0) ->update(['system_id' => null]); Task::whereIn('project_id', array_keys($projects)) ->where('sprint_id', 0) ->update(['sprint_id' => null]); } public function updateField($values, $count, $take, $field) { $i = 0; $index = 0; while ($count > $i) { Task::where($field, '=', 0)->take($take)->update([$field => $values[$index]]); $index += 1; if (count($values) <= $index) { $index = 0; } $i += $take; } } public function fillStatues($workflows) { foreach ($workflows as $key => $value) { $count = Task::where('workflow_id', $key)->count(); $take = $count > 10 ? 2 : 1; $index = 0; $i = 0; while ($count > $i) { Task::where('workflow_id', '=', $key) ->where('status_id', 0) ->take($take) ->update( [ 'status_id' => $value[$index] ]); $index += 1; if ($index >= count($value)) { $index = 0; } $i += $take; } } } public function fillTaskTag($count) { $tags = [5037, 5038, 5626]; $tasks = Task::where('business_id', 1779)->get(); foreach ($tasks as $task) { $rand = rand(0,2); $t = []; do{ array_push($t, new TagTask(['task_id' => $task->id, 'tag_id' => $tags[$rand]])); $rand--; }while($rand >= 0); $task->tags()->saveMany($t); } } }