refactor: 数据迁移程序

This commit is contained in:
2025-04-16 14:19:13 +08:00
parent 44c6ca7e70
commit 98390d0af2

View File

@@ -53,20 +53,49 @@ class DataMigration extends Command
// $this->migrateFaq();
// 迁移附件下载
$this->migrateAttachment([
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
43 => 6,
22 => 8,
24 => 11,
25 => 12,
26 => 13,
44 => 9,
45 => 10
// $this->migrateAttachment([
// 1 => 1,
// 2 => 2,
// 3 => 3,
// 4 => 4,
// 5 => 5,
// 43 => 6,
// 22 => 8,
// 24 => 11,
// 25 => 12,
// 26 => 13,
// 44 => 9,
// 45 => 10
// ]);
// 迁移视频分类
// $this->migrateVideoCategory();
// 迁移视频
$this->migrateVideo([
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
11 => 0,
36 => 9,
37 => 10,
38 => 11,
39 => 12,
40 => 13,
41 => 14,
42 => 15,
43 => 16,
60 => 17,
62 => 18,
63 => 19
]);
$output->writeln('success');
} catch(\Throwable $th) {
$output->writeln($th->getMessage() .':' . $th->getLine());
@@ -316,6 +345,78 @@ class DataMigration extends Command
$this->println('迁移附件ID' . $val['id']);
}
}
// 迁移视频
private function migrateVideoCategory()
{
$categorys = Db::connect('old')
->name('video_category')
->where('id', '>', 0)
->where('country_code', 'in', ['ZH', 'US'])
->order(['id' => 'asc'])
->cursor();
foreach ($categorys as $val) {
$item = [
'language_id' => $val['country_code'] == 'ZH' ? 1 : 2,
'name' => $val['name'],
'sort' => $val['sort'] == 9999 ? 0 : $val['sort'],
'is_show' => $val['isshow']
];
Db::name('video_category')->insert($item);
$this->println('迁移视频分类ID' . $val['id']);
}
}
// 迁移视频
private function migrateVideo($category_map = [])
{
$videos = Db::connect('old')
->name('video')
->where('id', '>', 844)
->where('country_code', 'in', ['ZH', 'US'])
->order(['id' => 'asc'])
->cursor();
$uploadMgr = new UploadMannager();
foreach ($videos as $val) {
$image = '';
$image_ret = $uploadMgr->upload($uploadMgr->download($val['picture']), 'image', 'video');
if ($image_ret['code'] == 0) {
$image = $image_ret['data']['path'];
} else {
$image = $image_ret['msg'];
}
$video = '';
if (!empty($val['videourl'])) {
$video = $val['videourl'];
} else {
$video_ret = $uploadMgr->upload($uploadMgr->download($val['videopath']), 'video', 'video');
if ($video_ret['code'] == 0) {
$video = $video_ret['data']['path'];
} else {
$video = $video_ret['msg'];
}
}
$item = [
'language_id' => $val['country_code'] == 'ZH' ? 1 : 2,
'category_id' => $category_map[$val['cid']],
'name' => $val['name'],
'desc' => $val['description'],
'image' => $image,
'video' => $video,
'sort' => $val['sort'] == 9999 ? 0 : $val['sort'],
'recommend' => $val['recommend'],
'seo_title' => $val['seo_title'],
'seo_keywords' => $val['seo_keyword'],
'seo_desc' => $val['seo_description'],
'deleted_at' => $val['stat'] == -1 ? date('Y-m-d H:i:s') : null
];
Db::name('video')->insert($item);
$this->println('迁移视频ID' . $val['id']);
}
}
}
class UploadMannager
@@ -390,9 +491,11 @@ class UploadMannager
$url_path = '/admapi/v1/attachment/upload';
break;
case 'video':
$url_path = "v1/video/$module/upload";
$url_path = "/admapi/v1/video/$module/upload";
break;
default:
$url_path = 'image';
$url_path = "/admapi/v1/images/$module/upload";
break;
}
$ch = curl_init(self::UPLOAD_BASE_API . $url_path);
$post_data = [
@@ -421,6 +524,14 @@ class UploadMannager
return $this->upload($file_path, $field_name);
}
if ($http_code != 200 && $http_code != 401) {
print($file_path . PHP_EOL);
print($field_name . PHP_EOL);
print(self::UPLOAD_BASE_API . $url_path . PHP_EOL);
print($response . PHP_EOL);
print($http_code . PHP_EOL);
}
$ret = json_decode($response, true);
if (empty($ret)) {
throw new \Exception($response);