hyperf-chat/seeders/initialize.php

76 lines
2.1 KiB
PHP
Raw Normal View History

2021-08-01 19:57:41 +08:00
<?php
declare(strict_types=1);
2021-09-04 14:25:39 +08:00
use App\Helpers\HashHelper;
2021-08-01 19:57:41 +08:00
use Hyperf\Database\Seeders\Seeder;
use App\Model\User;
use App\Model\Article\ArticleClass;
use Hyperf\DbConnection\Db;
2022-01-17 21:27:27 +08:00
use App\Model\Contact\Contact;
2021-08-01 19:57:41 +08:00
class Initialize extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
if (User::count() > 0) {
echo "数据库已存在数据,不能执行初始化数据脚本...\n";
return;
}
$users = [];
for ($i = 0; $i < 9; $i++) {
$users[] = [
'mobile' => '1879827205' . $i,
2021-08-29 22:24:45 +08:00
'password' => HashHelper::make('admin123'),
2021-08-01 19:57:41 +08:00
'nickname' => 'test' . $i,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
}
User::insert($users);
$defaultArticleClass = [];
$usersFriends = [];
foreach (User::all() as $user) {
$defaultArticleClass[] = [
'user_id' => $user->id,
'class_name' => '我的笔记',
'sort' => 1,
'is_default' => 1,
'created_at' => time(),
];
}
ArticleClass::insert($defaultArticleClass);
2021-09-04 14:25:39 +08:00
$prefix = config('databases.default.prefix');
2021-12-06 21:11:24 +08:00
$list = Db::select("SELECT u1.id as user_id,u2.id as friend_id FROM {$prefix}users as u1,{$prefix}users as u2 where u1.id != u2.id");
2021-08-01 19:57:41 +08:00
$friends = [];
foreach ($list as $item) {
$friends[] = [
'user_id' => $item->user_id,
'friend_id' => $item->friend_id,
'status' => 1,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
}
2022-01-16 10:29:16 +08:00
Contact::insert($friends);
2021-08-01 19:57:41 +08:00
2022-01-22 16:04:54 +08:00
$service = new \App\Service\TalkSessionService();
2021-08-01 19:57:41 +08:00
foreach ($list as $item) {
$service->create($item->user_id, $item->friend_id, \App\Constants\TalkModeConstant::PRIVATE_CHAT);
}
}
}