'上海交通大学', 'city' => '上海', 'province' => '上海', 'latitude' => 31.2041, 'longitude' => 121.4377], ['name' => '苏州大学', 'city' => '苏州', 'province' => '江苏', 'latitude' => 31.3041, 'longitude' => 120.6396], ['name' => '浙江大学', 'city' => '杭州', 'province' => '浙江', 'latitude' => 30.2994, 'longitude' => 120.0856], ['name' => '复旦大学', 'city' => '上海', 'province' => '上海', 'latitude' => 31.2970, 'longitude' => 121.5031], ]; $uniMap = []; foreach ($universities as $row) { $u = University::query()->firstOrCreate( ['name' => $row['name']], [ 'city' => $row['city'], 'province' => $row['province'], 'latitude' => $row['latitude'], 'longitude' => $row['longitude'], 'status' => 1, ] ); $u->fill([ 'city' => $row['city'], 'province' => $row['province'], 'latitude' => $row['latitude'], 'longitude' => $row['longitude'], ]); $u->save(); $uniMap[$row['name']] = $u; } $source = fn (string $v) => DictItem::query() ->where('dict_type_id', DictType::query()->where('code', 'teacher_source')->value('id')) ->where('value', $v)->value('id'); $level = fn (string $v) => DictItem::query() ->where('dict_type_id', DictType::query()->where('code', 'teacher_level')->value('id')) ->where('value', $v)->value('id'); $status = fn (string $v) => DictItem::query() ->where('dict_type_id', DictType::query()->where('code', 'teacher_status')->value('id')) ->where('value', $v)->value('id'); $samples = [ ['name' => '张某某', 'uni' => '上海交通大学', 'title' => '副教授', 'dir' => 'AI for Science', 'source' => 'paper', 'star' => '3', 'st' => 'active'], ['name' => '王某某', 'uni' => '苏州大学', 'title' => '教授', 'dir' => '纳米材料', 'source' => 'manual', 'star' => '5', 'st' => 'partner'], ['name' => '李某某', 'uni' => '浙江大学', 'title' => '讲师', 'dir' => '计算材料学', 'source' => 'miniapp', 'star' => '3', 'st' => 'paused'], ['name' => '周某某', 'uni' => '复旦大学', 'title' => '副研究员', 'dir' => '多模态学习', 'source' => 'paper', 'star' => '4', 'st' => 'active'], ]; foreach ($samples as $row) { $starId = $level($row['star']); $starItem = $starId ? DictItem::query()->find($starId) : null; $isPartner = $row['st'] === 'partner'; $teacher = Teacher::query()->updateOrCreate( ['name' => $row['name'], 'university_id' => $uniMap[$row['uni']]->id], [ 'city' => $uniMap[$row['uni']]->city, 'title' => $row['title'], 'source_dict_item_id' => $source($row['source']), 'star_level_dict_item_id' => $starId, 'status_dict_item_id' => $status($row['st']), 'next_follow_date' => $plan->nextFollowDateFromStar($starItem), 'is_partner' => $isPartner, 'converted_at' => $isPartner ? now() : null, ] ); $directionId = ResearchDirection::query()->firstOrCreate( ['name' => $row['dir']], ['sort' => 0, 'status' => 1] )->id; $teacher->researchDirections()->sync([$directionId]); } } }