djc-new/database/seeders/BuildingSeeder.php

62 lines
1.7 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Throwable;
use App\Models\Keyword;
class BuildingSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$oldDicts = DB::connection('djc_mysql')->table('t_data_dict')->where('dictKey', 'building')->get();
$newKeywords = [];
$buildings = [];
foreach($oldDicts as $dict){
if(empty($dict->dictKey) || empty($dict->valid)){
continue;
}
$_keyword = null;
$_parent = Keyword::where('oid', $dict->parentId)->first();
if($_parent){
$_keyword = [
'key' => $_parent->key.$dict->id,
'name' => $dict->txt,
'sort' => $dict->sorted,
'parent_id' => $_parent->id,
'parent_key'=> $_parent->key,
'lv' => $_parent->lv+1,
'path' => $_parent->path.$_parent->id.'-',
'oid' => $dict->id,
'created_at'=>now(),
'updated_at'=>now()
];
}
if($_keyword){
$newKeywords[$dict->id] = $_keyword;
}
}
if(count($newKeywords) > 0){
try {
DB::begintransaction();
DB::table('keywords')->insert($newKeywords);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
}
}
}