172 lines
5.7 KiB
PHP
172 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace app\common;
|
||
|
||
use think\Db;
|
||
|
||
/**
|
||
* 参考文献外部文献内容(t_production_article_refer_literature)
|
||
*/
|
||
class ProductionArticleReferLiteratureService
|
||
{
|
||
/**
|
||
* @return array|null
|
||
*/
|
||
public function getByPReferId($pReferId, $pArticleId = 0)
|
||
{
|
||
$pReferId = intval($pReferId);
|
||
if ($pReferId <= 0) {
|
||
return null;
|
||
}
|
||
|
||
$q = Db::name('production_article_refer_literature')->where('p_refer_id', $pReferId);
|
||
if (intval($pArticleId) > 0) {
|
||
$q->where('p_article_id', intval($pArticleId));
|
||
}
|
||
$row = $q->find();
|
||
|
||
return is_array($row) ? $row : null;
|
||
}
|
||
|
||
/**
|
||
* 从 t_production_article_refer_literature 读取校对用文献字段(不回落 refer 主表)
|
||
*
|
||
* @return array{
|
||
* abstract_text:string,
|
||
* content_text:string,
|
||
* mesh_terms:string,
|
||
* refer_content_cleaned:string,
|
||
* literature_pdf_url:string,
|
||
* fetch_sources:string,
|
||
* fetch_log:string,
|
||
* refer_doi:string
|
||
* }
|
||
*/
|
||
public function loadForCheck($pReferId, $pArticleId = 0)
|
||
{
|
||
$empty = [
|
||
'abstract_text' => '',
|
||
'content_text' => '',
|
||
'mesh_terms' => '',
|
||
'refer_content_cleaned' => '',
|
||
'literature_pdf_url' => '',
|
||
'fetch_sources' => '',
|
||
'fetch_log' => '',
|
||
'refer_doi' => '',
|
||
];
|
||
$stored = $this->getByPReferId($pReferId, $pArticleId);
|
||
if (empty($stored)) {
|
||
return $empty;
|
||
}
|
||
|
||
return [
|
||
'abstract_text' => trim((string)($stored['abstract_text'] ?? '')),
|
||
'content_text' => trim((string)($stored['content_text'] ?? '')),
|
||
'mesh_terms' => trim((string)($stored['mesh_terms'] ?? '')),
|
||
'refer_content_cleaned' => trim((string)($stored['refer_content_cleaned'] ?? '')),
|
||
'literature_pdf_url' => trim((string)($stored['literature_pdf_url'] ?? '')),
|
||
'fetch_sources' => trim((string)($stored['fetch_sources'] ?? '')),
|
||
'fetch_log' => trim((string)($stored['fetch_log'] ?? '')),
|
||
'refer_doi' => trim((string)($stored['refer_doi'] ?? '')),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @deprecated 使用 loadForCheck;保留兼容,仅读下属表
|
||
*/
|
||
public function resolveLiteratureFields(array $refer, $pArticleId = 0)
|
||
{
|
||
$pReferId = intval($refer['p_refer_id'] ?? 0);
|
||
$lit = $this->loadForCheck($pReferId, $pArticleId);
|
||
|
||
return [
|
||
'abstract_text' => $lit['abstract_text'],
|
||
'content_text' => $lit['content_text'],
|
||
'mesh_terms' => $lit['mesh_terms'],
|
||
'refer_content_cleaned' => $lit['refer_content_cleaned'],
|
||
'literature_pdf_url' => $lit['literature_pdf_url'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 写入/更新文献内容
|
||
*
|
||
* @param array $data abstract_text, content_text, mesh_terms, refer_content_cleaned, literature_pdf_url, refer_doi, fetch_sources, fetch_log
|
||
*/
|
||
public function upsert($pArticleId, $pReferId, array $data)
|
||
{
|
||
$pArticleId = intval($pArticleId);
|
||
$pReferId = intval($pReferId);
|
||
if ($pReferId <= 0) {
|
||
return 0;
|
||
}
|
||
|
||
$now = date('Y-m-d H:i:s');
|
||
$row = [
|
||
'p_article_id' => $pArticleId,
|
||
'p_refer_id' => $pReferId,
|
||
'refer_doi' => $this->clip((string)($data['refer_doi'] ?? ''), 128),
|
||
'abstract_text' => (string)($data['abstract_text'] ?? ''),
|
||
'content_text' => (string)($data['content_text'] ?? ''),
|
||
'mesh_terms' => $this->formatMeshTerms($data['mesh_terms'] ?? ''),
|
||
'refer_content_cleaned' => (string)($data['refer_content_cleaned'] ?? ''),
|
||
'literature_pdf_url' => $this->clip((string)($data['literature_pdf_url'] ?? ''), 1024),
|
||
'fetch_sources' => $this->clip($this->formatSources($data['fetch_sources'] ?? ''), 255),
|
||
'fetch_log' => $this->clip((string)($data['fetch_log'] ?? ''), 512),
|
||
'updated_at' => $now,
|
||
];
|
||
|
||
$existing = Db::name('production_article_refer_literature')->where('p_refer_id', $pReferId)->find();
|
||
if (!empty($existing)) {
|
||
Db::name('production_article_refer_literature')
|
||
->where('p_refer_id', $pReferId)
|
||
->update($row);
|
||
|
||
return intval($existing['id']);
|
||
}
|
||
|
||
$row['created_at'] = $now;
|
||
|
||
return intval(Db::name('production_article_refer_literature')->insertGetId($row));
|
||
}
|
||
|
||
public function formatMeshTerms($mesh)
|
||
{
|
||
if (is_array($mesh)) {
|
||
$parts = [];
|
||
foreach ($mesh as $term) {
|
||
$term = trim((string)$term);
|
||
if ($term !== '') {
|
||
$parts[$term] = $term;
|
||
}
|
||
}
|
||
|
||
return implode('; ', array_values($parts));
|
||
}
|
||
|
||
return trim((string)$mesh);
|
||
}
|
||
|
||
private function formatSources($sources)
|
||
{
|
||
if (is_array($sources)) {
|
||
return implode(',', array_values(array_unique(array_filter(array_map('strval', $sources)))));
|
||
}
|
||
|
||
return trim((string)$sources);
|
||
}
|
||
|
||
private function clip($text, $max)
|
||
{
|
||
$text = trim((string)$text);
|
||
if ($text === '' || $max <= 0) {
|
||
return '';
|
||
}
|
||
if (mb_strlen($text) <= $max) {
|
||
return $text;
|
||
}
|
||
|
||
return mb_substr($text, 0, $max);
|
||
}
|
||
}
|