houde_web_api/app/common/model/ExaminationInformation/infoArticle/infoArticle.php
2023-04-27 19:28:10 +08:00

168 lines
4.8 KiB
PHP

<?php
namespace app\common\model\ExaminationInformation\infoArticle;
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;
class infoArticle extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = 'info_article_delete_time';
// 设置主键名
protected $pk = 'info_article_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
"info_article_id" => "int",
"info_article_guid" => "string",
"info_article_title" => "string",
"info_article_cover" => "string",
"info_article_content" => "string",
"info_article_order" => "int",
"info_article_create_time" => "datetime",
"info_article_create_user_guid" => "string",
"info_article_update_time" => "datetime",
"info_article_update_user_guid" => "string",
"info_article_delete_time" => "datetime",
"info_article_delete_user_guid" => "string",
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = 'info_article_create_time';
// 修改时间
protected $updateTime = 'info_article_update_time';
// excel导入/下载模板表头
public const EXCELFIELD = [
'info_article_title' => '文章标题',
'info_article_cover' => '文章封面',
'info_article_content' => '文章内容',
'info_article_order' => '文章排序',
];
/**
* 新增前
*/
public static function onBeforeInsert(self $model): void
{
// self::checkRepeatData($model);
$model->completeCreateField();
}
/**
* 更新前
*/
public static function onBeforeUpdate(self $model): void
{
// self::checkRepeatData($model);
$model->completeUpdateField();
}
/**
* 删除前
*/
public static function onBeforeDelete(self $model): void
{
$model->completeDeleteField();
}
/**
* 导出Excel
*/
public static function exportExcel($select)
{
$data = [[
'文章标题',
'文章封面',
'文章内容',
'文章排序'
]];
foreach ($select as $key => $val) {
$data[] = [
$val['info_article_title'],
Excel::ExportImgFiled($val['info_article_cover']),
$val['info_article_content'],
$val['info_article_order'],
];
}
$excel = (new Excel())->exporTsheet($data);
$excel->save('资讯文章.xlsx');
}
/**
* 导入excel
*/
public static function importExcel($file)
{
$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'>新增成功!</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初始化
*/
public static function importExcelInit($value)
{
$info_article_title = $value['info_article_title'];$info_article_cover = $value['info_article_cover'];$info_article_content = $value['info_article_content'];$info_article_order = $value['info_article_order'];
return self::create([
'info_article_title' => $info_article_title,
'info_article_cover' => $info_article_cover,
'info_article_content' => $info_article_content,
'info_article_order' => $info_article_order,
]);
}
}