truncate(); DB::connection('djc_mysql')->table('t_person_move')->orderBy('id', 'ASC')->chunk(1000, function($personMoves){ $newlogs = []; if($personMoves->count() > 0){ foreach($personMoves as $log){ if($log->valid){//有效的记录 //处理原有时间字符串 $occurDate = substr($log->occurDate, 0, 10); $_log = [ 'type' => $log->type, 'person_id' => $log->personId, 'changed_at' => $occurDate, 'reason' => $log->reason, 'old_master'=> Person::where('name', $log->oldMaster)->first()?->id ?: null, 'new_master'=> Person::where('name', $log->currMaster)->first()?->id ?: null, 'phone' => $log->phone, ]; //处理变更文案 switch($_log['type']){ case PersonChange::TYPE_IN: case PersonChange::TYPE_BIRTH: $_log['extends'] = null; $_log['extends_mark'] = null; break; case PersonChange::TYPE_SPLIT: $_log['extends'] = null; $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '':'因'.$_log['reason']).'分户'; break; case PersonChange::TYPE_MERGE: $_log['extends'] = null; $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '':'因'.$_log['reason']).'合户'; break; case PersonChange::TYPE_OUT: $_log['extends'] = json_encode([ 'to_address' =>$log->newAddr, ]); $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '':'因'.$_log['reason']).$log->newAddr; break; case PersonChange::TYPE_CHANGE: $_log['extends'] = null; $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '':'因'.$_log['reason']).'户主变更'; break; case PersonChange::TYPE_MIGRATE: $_log['extends'] = null; $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '':'因'.$_log['reason']).'迁移'; break; case PersonChange::TYPE_DIE: $_log['extends'] = null; $_log['extends_mark'] = $occurDate.'日'.(empty($_log['reason']) ? '': $_log['reason']); break; default: $_log['extends'] = null; $_log['extends_mark'] = null; break; } $newlogs[] = $_log; } } } !empty($newlogs) && DB::table('person_changes')->insert($newlogs); }); } }