233 lines
6.8 KiB
PHP
233 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\News;
|
|
|
|
use app\common\arw\adjfut\src\Validate;
|
|
use app\BaseModel;
|
|
use think\model\concern\SoftDelete;
|
|
use app\common\arw\adjfut\src\Excel;
|
|
use app\Request;
|
|
use app\common\exception\Tool;
|
|
use think\facade\Db;
|
|
use app\common\model\Dictionary\Dictionary as ModelDictionary;
|
|
|
|
class News extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
// 删除字段
|
|
protected $deleteTime = 'news_delete_time';
|
|
// 设置主键名
|
|
protected $pk = 'news_guid';
|
|
// 设置废弃字段
|
|
protected $disuse = [];
|
|
// 设置字段信息
|
|
protected $schema = [
|
|
'news_id' => 'int',
|
|
'news_guid' => 'string',
|
|
'news_type' => 'int',
|
|
'news_title' => 'string',
|
|
'news_cover' => 'string',
|
|
'news_intro' => 'string',
|
|
'news_source' => 'string',
|
|
'news_link' => 'string',
|
|
'news_issue_date' => '',
|
|
'news_views_num' => 'int',
|
|
'news_sort' => 'int',
|
|
'news_content' => '',
|
|
'news_create_time' => 'datetime',
|
|
'news_create_user_guid' => 'string',
|
|
'news_update_time' => 'datetime',
|
|
'news_update_user_guid' => 'string',
|
|
'news_delete_time' => 'datetime',
|
|
'news_delete_user_guid' => 'string',
|
|
|
|
];
|
|
// 设置json类型字段
|
|
protected $json = [''];
|
|
// 开启自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'datetime';
|
|
// 创建时间
|
|
protected $createTime = 'news_create_time';
|
|
// 修改时间
|
|
protected $updateTime = 'news_update_time';
|
|
|
|
//排序字段
|
|
public $order_field = 'news_sort';
|
|
|
|
// excel导入/下载模板表头
|
|
public const EXCELFIELD = [
|
|
'news_type' => '新闻类型',
|
|
'news_title' => '新闻标题',
|
|
'news_cover' => '新闻封面',
|
|
'news_intro' => '新闻简介',
|
|
'news_source' => '文章来源',
|
|
'news_link' => '跳转链接',
|
|
'news_issue_date' => '发布日期',
|
|
'news_views_num' => '浏览次数',
|
|
'news_sort' => '新闻排序',
|
|
'news_content' => '新闻内容',
|
|
];
|
|
|
|
/**
|
|
* 新增前
|
|
*/
|
|
public static function onBeforeInsert(self $model): void
|
|
{
|
|
Tool::dataAddSortProc($model);
|
|
$model->completeCreateField();
|
|
}
|
|
|
|
/**
|
|
* 更新前
|
|
*/
|
|
public static function onBeforeUpdate(self $model): void
|
|
{
|
|
Tool::dataEditSortProc($model);
|
|
$model->completeUpdateField();
|
|
}
|
|
|
|
/**
|
|
* 删除前
|
|
*/
|
|
public static function onBeforeDelete(self $model): void
|
|
{
|
|
Tool::dataDeleteSortProc($model);
|
|
$model->completeDeleteField();
|
|
}
|
|
|
|
|
|
/**
|
|
* 导出Excel
|
|
*
|
|
* @param array $select 导出的数据
|
|
*/
|
|
public static function exportExcel(array $select): void
|
|
{
|
|
$data = [[
|
|
'新闻类型',
|
|
'新闻标题',
|
|
'新闻封面',
|
|
'新闻简介',
|
|
'文章来源',
|
|
'跳转链接',
|
|
'发布日期',
|
|
'浏览次数',
|
|
'新闻排序',
|
|
// '新闻内容'
|
|
]];
|
|
foreach ($select as $key => $val) {
|
|
|
|
// 获取新闻类型名称
|
|
$news_type = ModelDictionary::getDictionaryData('news_type');
|
|
$val['news_type'] = ModelDictionary::getDataDictionaryName($news_type, $val['news_type']);
|
|
|
|
$data[] = [
|
|
$val['news_type'],
|
|
$val['news_title'],
|
|
Excel::ExportImgFiled($val['news_cover']),
|
|
$val['news_intro'],
|
|
$val['news_source'],
|
|
$val['news_link'],
|
|
$val['news_issue_date'],
|
|
$val['news_views_num'],
|
|
$val['news_sort'],
|
|
// $val['news_content'],
|
|
];
|
|
}
|
|
$excel = (new Excel())->exporTsheet($data);
|
|
$excel->save('新闻.xlsx');
|
|
}
|
|
|
|
|
|
/**
|
|
* 导入excel
|
|
*
|
|
* @param \app\common\arw\adjfut\src\UploadFile $file excel
|
|
*/
|
|
public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string
|
|
{
|
|
$msg = [];
|
|
|
|
Db::startTrans();
|
|
try {
|
|
$excel = new Excel($file);
|
|
$data = $excel->parseExcel(
|
|
Tool::getExcelRule(self::EXCELFIELD),
|
|
['titleLine' => [1]]
|
|
);
|
|
if (!$data) throwErrorMsg('excel无数据', 1);
|
|
$msg = [];
|
|
foreach ($data as $line => $value) {
|
|
try {
|
|
$model = self::importExcelInit($value);
|
|
$msg[] = "{$line} <span style='color:#27af49'>新闻:【{$value['news_title']}】 新增成功!</span><br>";
|
|
} catch (\Throwable $th) {
|
|
$msg[] = "{$line} <span style='color:red'>{$th->getMessage()}</span><br>";
|
|
}
|
|
}
|
|
Db::commit();
|
|
return implode(', ', $msg);
|
|
} catch (\Throwable $th) {
|
|
Db::rollback();
|
|
throw $th;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 导入excel初始化
|
|
*
|
|
* @param array $value excel每行数据
|
|
*/
|
|
public static function importExcelInit(array $value): void
|
|
{
|
|
$news_type = $value['news_type'];
|
|
$news_title = $value['news_title'];
|
|
$news_cover = $value['news_cover'];
|
|
$news_intro = $value['news_intro'];
|
|
$news_source = $value['news_source'];
|
|
$news_link = $value['news_link'];
|
|
$news_issue_date = $value['news_issue_date'];
|
|
$news_views_num = $value['news_views_num'];
|
|
$news_sort = $value['news_sort'];
|
|
$news_content = $value['news_content'];
|
|
|
|
|
|
// 根据新闻类型名称获取新闻类型的字段值
|
|
$news_type_dic = ModelDictionary::getDictionaryData('news_type');
|
|
$news_type_value = ModelDictionary::getDataDictionaryValue($news_type_dic, $news_type);
|
|
if (!$news_type_value) throwErrorMsg("新闻类型:【{$news_type}】 不存在,请重新确认!");
|
|
|
|
|
|
self::create(
|
|
[
|
|
'news_type' => $news_type_value,
|
|
'news_title' => $news_title,
|
|
'news_cover' => $news_cover,
|
|
'news_intro' => $news_intro,
|
|
'news_source' => $news_source,
|
|
'news_link' => $news_link,
|
|
'news_issue_date' => $news_issue_date,
|
|
'news_views_num' => $news_views_num,
|
|
'news_sort' => $news_sort,
|
|
'news_content' => $news_content,
|
|
],
|
|
[
|
|
'news_type',
|
|
'news_title',
|
|
'news_cover',
|
|
'news_intro',
|
|
'news_source',
|
|
'news_link',
|
|
'news_issue_date',
|
|
'news_views_num',
|
|
'news_sort',
|
|
'news_content',
|
|
'news_guid',
|
|
'news_create_user_guid',
|
|
'news_update_user_guid'
|
|
]
|
|
);
|
|
}
|
|
}
|