master
cody 3 months ago
parent 1e0d8a6d9e
commit d42da8724f

@ -17,14 +17,14 @@ class UpdateBookIsbnData extends Command
*
* @var string
*/
protected $signature = 'book:update-isbn-data';
protected $signature = 'book:update-isbn-data {--book_id=} ';
/**
* The console command description.
*
* @var string
*/
protected $description = '从ISBN接口获取书籍数据并下载封面图片';
protected $description = '从ISBN接口获取书籍数据,更新出版社、作者、出版年份、简介等信息并下载封面图片';
/**
* Create a new command instance.
@ -44,16 +44,30 @@ class UpdateBookIsbnData extends Command
public function handle()
{
$apiKey = Config::getValueByKey('book_key');
$book_id = $this->option('book_id');
// 获取所有有ISBN的书籍
$books = Book::whereNotNull('isbn')
->where('isbn', '!=', '')
->whereNull('cover_id')
// ->where('id', 1)
->get();
->where(function ($query) use ($book_id) {
if ($book_id) {
$query->where('id', $book_id);
}
})->where('isbn', '!=', '')
->where(function ($query) {
// 如果没有封面或者缺少基本信息字段,都需要处理
$query->whereNull('cover_id')
->orWhereNull('publisher')
->orWhereNull('author')
->orWhereNull('publish_year')
->orWhereNull('description')
->orWhere('publisher', '')
->orWhere('author', '')
->orWhere('publish_year', '')
->orWhere('description', '');
})->get();
if ($books->isEmpty()) {
$this->info('没有找到未处理封面的书籍');
$this->info('没有找到需要更新数据的书籍');
return 0;
}
@ -169,7 +183,24 @@ class UpdateBookIsbnData extends Command
$bookData = $data['data'];
// 更新书籍的other_data字段
// 更新书籍的基本信息字段
if (!empty($bookData['publisher'])) {
$book->publisher = $bookData['publisher'];
}
if (!empty($bookData['author'])) {
$book->author = $bookData['author'];
}
if (!empty($bookData['pubdate'])) {
$book->publish_year = $bookData['pubdate'];
}
if (!empty($bookData['summary'])) {
$book->description = $bookData['summary'];
}
// 更新书籍的other_data字段保存完整的API响应数据
$book->other_data = $bookData;
// 如果有图片URL下载图片

@ -9,6 +9,7 @@ use App\Models\Book;
use App\Models\CustomForm;
use App\Models\CustomFormField;
use App\Models\SupplyDemand;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
@ -217,7 +218,33 @@ class BookController extends BaseController
*/
public function save()
{
return parent::save();
$all = \request()->all();
DB::beginTransaction();
try {
if (isset($all['id'])) {
$model = $this->model->find($all['id']);
if (empty($model)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
}
} else {
$model = $this->model;
$all['admin_id'] = $this->getUserId();
$all['department_id'] = $this->getUser()->department_id;
}
$original = $model->getOriginal();
$model->fill($all);
$model->save();
DB::commit();
// 写封面和其他信息
// 调用命令行更新
Artisan::call("book:update-isbn-data --book_id={$model->id}");
// 记录日志
$this->saveLogs($original, $model);
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**

@ -4,11 +4,21 @@ namespace App\Models;
class Book extends SoftDeletesModel
{
protected $casts = ['other_data' => 'json'];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'other_data' => 'json'
];
/**
* Get the cover image for the book.
*/
public function cover()
{
return $this->hasOne(Upload::class, 'id', 'cover_id');
}
}

Loading…
Cancel
Save