217 lines
6.3 KiB
PHP
217 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\model\LoveStory;
|
|
|
|
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 LoveStory extends BaseModel
|
|
{
|
|
use SoftDelete;
|
|
// 删除字段
|
|
protected $deleteTime = 'love_story_delete_time';
|
|
// 设置主键名
|
|
protected $pk = 'love_story_guid';
|
|
// 设置废弃字段
|
|
protected $disuse = [];
|
|
// 设置字段信息
|
|
protected $schema = [
|
|
'love_story_id' => 'int',
|
|
'love_story_guid' => 'string',
|
|
'love_story_title' => 'string',
|
|
'love_story_author' => 'string',
|
|
'love_story_place' => 'string',
|
|
'love_story_date' => '',
|
|
'love_story_cover' => 'string',
|
|
'love_story_sort' => 'int',
|
|
'love_story_music' => 'string',
|
|
'love_story_content' => '',
|
|
'love_story_create_time' => 'datetime',
|
|
'love_story_create_user_guid' => 'string',
|
|
'love_story_update_time' => 'datetime',
|
|
'love_story_update_user_guid' => 'string',
|
|
'love_story_delete_time' => 'datetime',
|
|
'love_story_delete_user_guid' => 'string',
|
|
|
|
];
|
|
// 设置json类型字段
|
|
protected $json = [''];
|
|
// 开启自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'datetime';
|
|
// 创建时间
|
|
protected $createTime = 'love_story_create_time';
|
|
// 修改时间
|
|
protected $updateTime = 'love_story_update_time';
|
|
|
|
//排序字段
|
|
public $order_field = 'love_story_sort';
|
|
|
|
// excel导入/下载模板表头
|
|
public const EXCELFIELD = [
|
|
'love_story_title' => '标题',
|
|
'love_story_author' => '作者',
|
|
'love_story_place' => '地点',
|
|
'love_story_date' => '日期',
|
|
'love_story_cover' => '封面',
|
|
'love_story_sort' => '排序',
|
|
'love_story_music' => '音乐',
|
|
'love_story_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();
|
|
}
|
|
|
|
|
|
/**
|
|
* 音乐名称获取器
|
|
*/
|
|
public function getLoveStoryMusicNameAttr($value, $data)
|
|
{
|
|
$love_story_music_path = $data['love_story_music'];
|
|
$file_name = basename($love_story_music_path); // 使用basename获取文件名部分
|
|
return $file_name;
|
|
}
|
|
|
|
|
|
/**
|
|
* 导出Excel
|
|
*
|
|
* @param array $select 导出的数据
|
|
*/
|
|
public static function exportExcel(array $select): void
|
|
{
|
|
$data = [[
|
|
'标题',
|
|
'作者',
|
|
'地点',
|
|
'日期',
|
|
'封面',
|
|
'排序',
|
|
'音乐',
|
|
'内容'
|
|
]];
|
|
foreach ($select as $key => $val) {
|
|
$data[] = [
|
|
$val['love_story_title'],
|
|
$val['love_story_author'],
|
|
$val['love_story_place'],
|
|
$val['love_story_date'],
|
|
Excel::ExportImgFiled($val['love_story_cover']),
|
|
$val['love_story_sort'],
|
|
$val['love_story_music'],
|
|
$val['love_story_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)
|
|
{
|
|
$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['love_story_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
|
|
{
|
|
$love_story_title = $value['love_story_title'];
|
|
$love_story_author = $value['love_story_author'];
|
|
$love_story_place = $value['love_story_place'];
|
|
$love_story_date = $value['love_story_date'];
|
|
$love_story_cover = $value['love_story_cover'];
|
|
$love_story_sort = $value['love_story_sort'];
|
|
$love_story_music = $value['love_story_music'];
|
|
$love_story_content = $value['love_story_content'];
|
|
|
|
self::create(
|
|
[
|
|
'love_story_title' => $love_story_title,
|
|
'love_story_author' => $love_story_author,
|
|
'love_story_place' => $love_story_place,
|
|
'love_story_date' => $love_story_date,
|
|
'love_story_cover' => $love_story_cover,
|
|
'love_story_sort' => $love_story_sort,
|
|
'love_story_music' => $love_story_music,
|
|
'love_story_content' => $love_story_content,
|
|
],
|
|
[
|
|
'love_story_title',
|
|
'love_story_author',
|
|
'love_story_place',
|
|
'love_story_date',
|
|
'love_story_cover',
|
|
'love_story_sort',
|
|
'love_story_music',
|
|
'love_story_content',
|
|
'love_story_guid',
|
|
'love_story_create_user_guid',
|
|
'love_story_update_user_guid'
|
|
]
|
|
);
|
|
}
|
|
}
|