159 lines
7.4 KiB
PHP
159 lines
7.4 KiB
PHP
<?php
|
||
|
||
namespace app\master\service\push;
|
||
|
||
use think\Queue;
|
||
use sftp\Sftp;
|
||
|
||
/**
|
||
* Portico 推送器
|
||
* 生成 Excel(SPREADSHEET) + 下载 PDF,随后通过队列 SFTP 上传到 Portico。
|
||
* 逻辑迁移自 Datebase::bf_db_push / push_sftp。
|
||
*/
|
||
class PorticoPusher extends AbstractPusher
|
||
{
|
||
public function code()
|
||
{
|
||
return 'portico';
|
||
}
|
||
|
||
public function label()
|
||
{
|
||
return 'Portico';
|
||
}
|
||
|
||
public function pushStage($stage, array $options = [])
|
||
{
|
||
Vendor('PHPExcel.PHPExcel');
|
||
Vendor('PHPExcel.PHPExcel.Worksheet.Drawing');
|
||
Vendor('PHPExcel.PHPExcel.Writer.Excel2007');
|
||
|
||
$pdfBase = $this->conf('article_pdf');
|
||
$subBase = $this->conf('article_sub');
|
||
|
||
$baseDir = ROOT_PATH . 'public' . DS . 'davExcel' . DS . $stage . DS;
|
||
if (!is_dir($baseDir)) {
|
||
@mkdir($baseDir, 0777, true);
|
||
}
|
||
$objExcel = new \PHPExcel();
|
||
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
|
||
|
||
$objActSheet = $objExcel->getActiveSheet();
|
||
$letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,M,N");
|
||
$arrHeader = array('Journal Title', 'Journal P-ISSN', 'Journal E-ISSN', 'Article DOI/ID', 'PDF File Name', 'Article Title', 'Authors', 'Publication Date', 'Volume', 'Issue', 'Supplement', 'First Page', 'Last Page', 'Copyright Statement');
|
||
$lenth = count($arrHeader);
|
||
for ($i = 0; $i < $lenth; $i++) {
|
||
$objActSheet->setCellValue("$letter[$i]1", "$arrHeader[$i]");
|
||
$objActSheet->getRowDimension('1')->setRowHeight(25);
|
||
}
|
||
|
||
$stage_info = $this->journal_stage_obj->where('journal_stage_id', $stage)->find();
|
||
$journal_info = $this->journal_obj->where('journal_id', $stage_info['journal_id'])->find();
|
||
$articles = $this->article_obj
|
||
->field('j_article.*,j_journal.title journalTitle,j_journal.issn')
|
||
->join("j_journal", "j_article.journal_id = j_journal.journal_id", 'left')
|
||
->where('j_article.journal_stage_id', $stage)
|
||
->where('j_article.state', 0)
|
||
->select();
|
||
|
||
foreach ($articles as $k => $v) {
|
||
$k += 2;
|
||
$objActSheet->setCellValue('A' . $k, choiseti1($v['article_id'], $v['journalTitle']));
|
||
$objActSheet->setCellValue('B' . $k, $v['issn']);
|
||
$objActSheet->setCellValue('C' . $k, '');
|
||
$objActSheet->setCellValue('D' . $k, $v['doi']);
|
||
$fpdf = explode("/", $v['file_pdf']);
|
||
$objActSheet->setCellValue('E' . $k, $fpdf[1]);
|
||
copy($pdfBase . $v['file_pdf'], $baseDir . $fpdf[1]);
|
||
$objActSheet->setCellValue('F' . $k, $v['title']);
|
||
|
||
$cache_author = $this->article_author_obj->where('article_id', $v['article_id'])->where('state', 0)->column('author_name');
|
||
$objActSheet->setCellValue('G' . $k, implode(",", $cache_author));
|
||
$objActSheet->setCellValue('H' . $k, $v['pub_date']);
|
||
$objActSheet->setCellValue('I' . $k, $stage_info['stage_vol']);
|
||
if (($journal_info['journal_id'] == 2 && $stage_info['stage_year'] >= 2022) || ($journal_info['journal_id'] == 3 && $stage_info['stage_year'] >= 2022) || ($journal_info['journal_id'] == 17 && $stage_info['stage_year'] >= 2019)) {
|
||
$objActSheet->setCellValue('J' . $k, '');
|
||
} else {
|
||
$objActSheet->setCellValue('J' . $k, $stage_info['issue_date']);
|
||
}
|
||
|
||
if ($v['file_sub'] != '') {
|
||
$spdf = explode("/", $v['file_sub']);
|
||
copy($subBase . $v['file_sub'], $baseDir . $spdf[1]);
|
||
$objActSheet->setCellValue('K' . $k, $spdf[1]);
|
||
} else {
|
||
$objActSheet->setCellValue('K' . $k, '');
|
||
}
|
||
|
||
$npp = trim($v['npp']);
|
||
$npp = str_replace('–', '-', $npp);
|
||
$npp = str_replace("—", '-', $npp);
|
||
if (strpos($npp, '-') == false) {
|
||
$objActSheet->setCellValue('L' . $k, '');
|
||
$objActSheet->setCellValue('M' . $k, '');
|
||
} else {
|
||
$cc = explode('-', trim($npp));
|
||
$objActSheet->setCellValue('L' . $k, $cc[0]);
|
||
$objActSheet->setCellValue('M' . $k, $cc[1]);
|
||
}
|
||
$objActSheet->setCellValue('N' . $k, "© " . $stage_info['stage_year'] . " By Author(s). Published by TMR Publishing Group Limited. This is an open access article under the CC-BY license. (http://creativecommons.org/licenses/BY/4.0/)");
|
||
$objActSheet->getRowDimension($k)->setRowHeight(25);
|
||
}
|
||
$width = array(10, 15, 20, 25, 30);
|
||
$objActSheet->getColumnDimension('A')->setWidth($width[2]);
|
||
$objActSheet->getColumnDimension('B')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('C')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('D')->setWidth($width[2]);
|
||
$objActSheet->getColumnDimension('E')->setWidth($width[4]);
|
||
$objActSheet->getColumnDimension('F')->setWidth($width[4]);
|
||
$objActSheet->getColumnDimension('G')->setWidth($width[4]);
|
||
$objActSheet->getColumnDimension('H')->setWidth($width[2]);
|
||
$objActSheet->getColumnDimension('I')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('J')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('K')->setWidth($width[4]);
|
||
$objActSheet->getColumnDimension('L')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('M')->setWidth($width[1]);
|
||
$objActSheet->getColumnDimension('N')->setWidth($width[4]);
|
||
|
||
$outfileTitle = $stage_info['journal_stage_id'] . "dav";
|
||
$objWriter->save($baseDir . $outfileTitle . ".xlsx");
|
||
|
||
$this->pushSftp($stage, $stage_info, $journal_info);
|
||
|
||
return PushResult::success('Portico 文件已生成并加入 SFTP 上传队列');
|
||
}
|
||
|
||
/**
|
||
* 创建远程目录并将目录内文件通过队列上传到 Portico SFTP
|
||
*/
|
||
private function pushSftp($stage, $stage_info, $journal_info)
|
||
{
|
||
$cred = $this->cred('portico');
|
||
// 连接 sftp 服务器,确保远程目录存在
|
||
$config['host'] = $cred['host'];
|
||
$config['port'] = $cred['port'];
|
||
$config['username'] = $cred['user'];
|
||
$config['password'] = $cred['pass'];
|
||
$sftp_obj = new Sftp($config);
|
||
|
||
$j_base = $sftp_obj->ssh2_dir_exits("/SPREADSHEET/" . $journal_info['issn']);
|
||
if (!$j_base) {
|
||
$sftp_obj->ssh2_sftp_mchkdir("/SPREADSHEET/" . $journal_info['issn']);
|
||
}
|
||
$m_base = $sftp_obj->ssh2_dir_exits("/SPREADSHEET/" . $journal_info['issn'] . '/' . $stage_info['stage_year'] . '-' . $stage_info['stage_vol'] . '-' . $stage_info['stage_no']);
|
||
if (!$m_base) {
|
||
$sftp_obj->ssh2_sftp_mchkdir("/SPREADSHEET/" . $journal_info['issn'] . '/' . $stage_info['stage_year'] . '-' . $stage_info['stage_vol'] . '-' . $stage_info['stage_no']);
|
||
}
|
||
|
||
$files = scandir(ROOT_PATH . 'public' . DS . 'davExcel' . DS . $stage);
|
||
foreach ($files as $v) {
|
||
if ($v == '.' || $v == '..') {
|
||
continue;
|
||
}
|
||
$cdata['local'] = ROOT_PATH . 'public' . DS . 'davExcel' . DS . $stage . DS . $v;
|
||
$cdata['remote'] = "/SPREADSHEET/" . $journal_info['issn'] . '/' . $stage_info['stage_year'] . '-' . $stage_info['stage_vol'] . '-' . $stage_info['stage_no'] . "/" . $v;
|
||
Queue::push('app\api\job\mysftp@push', $cdata, "mysftp");
|
||
}
|
||
}
|
||
}
|