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.
81 lines
2.7 KiB
81 lines
2.7 KiB
|
1 week ago
|
<?php
|
||
|
|
|
||
|
|
namespace Database\Seeders;
|
||
|
|
|
||
|
|
use App\Models\ActivitySignup;
|
||
|
|
use App\Models\CourseSignup;
|
||
|
|
use App\Models\MiniappUser;
|
||
|
|
use Illuminate\Database\Seeder;
|
||
|
|
|
||
|
|
/** 学员库示例数据(对齐后续小程序用户;报名记录按手机号关联) */
|
||
|
|
class MiniappUserSampleSeeder extends Seeder
|
||
|
|
{
|
||
|
|
public function run(): void
|
||
|
|
{
|
||
|
|
$samples = [
|
||
|
|
[
|
||
|
|
'openid' => 'seed_miniapp_001',
|
||
|
|
'name' => '陈学员',
|
||
|
|
'mobile' => '13800001001',
|
||
|
|
'company' => '未来材料科技',
|
||
|
|
'nickname' => '小陈',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'openid' => 'seed_miniapp_002',
|
||
|
|
'name' => '林学员',
|
||
|
|
'mobile' => '13800001002',
|
||
|
|
'company' => '智算研究院',
|
||
|
|
'nickname' => 'Lin',
|
||
|
|
],
|
||
|
|
[
|
||
|
|
'openid' => 'seed_miniapp_003',
|
||
|
|
'name' => '赵学员',
|
||
|
|
'mobile' => '13800001003',
|
||
|
|
'company' => '创新创业中心',
|
||
|
|
'nickname' => null,
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
foreach ($samples as $row) {
|
||
|
|
$user = MiniappUser::query()->updateOrCreate(
|
||
|
|
['openid' => $row['openid']],
|
||
|
|
[
|
||
|
|
'unionid' => null,
|
||
|
|
'nickname' => $row['nickname'],
|
||
|
|
'avatar_url' => null,
|
||
|
|
'name' => $row['name'],
|
||
|
|
'mobile' => $row['mobile'],
|
||
|
|
'company' => $row['company'],
|
||
|
|
'status' => 1,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
if ($user->mobile) {
|
||
|
|
CourseSignup::query()
|
||
|
|
->whereNull('miniapp_user_id')
|
||
|
|
->where('mobile', $user->mobile)
|
||
|
|
->update(['miniapp_user_id' => $user->id]);
|
||
|
|
|
||
|
|
ActivitySignup::query()
|
||
|
|
->whereNull('miniapp_user_id')
|
||
|
|
->where('mobile', $user->mobile)
|
||
|
|
->update(['miniapp_user_id' => $user->id]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 若无报名记录匹配,为首个学员挂一条示例课程报名(取最新课程)
|
||
|
|
$first = MiniappUser::query()->where('openid', 'seed_miniapp_001')->first();
|
||
|
|
if ($first && ! CourseSignup::query()->where('miniapp_user_id', $first->id)->exists()) {
|
||
|
|
$signup = CourseSignup::query()->orderByDesc('id')->first();
|
||
|
|
if ($signup && ! $signup->miniapp_user_id) {
|
||
|
|
$signup->update([
|
||
|
|
'miniapp_user_id' => $first->id,
|
||
|
|
'name' => $first->name,
|
||
|
|
'mobile' => $first->mobile,
|
||
|
|
'company' => $first->company ?? $signup->company,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|