houde_web_api/app/common/model/Teachers_strength/Teacher.php

180 lines
5.0 KiB
PHP

<?php
namespace app\common\model\Teachers_strength;
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 Teacher extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = 'teacher_delete_time';
// 设置主键名
protected $pk = 'teacher_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
"teacher_id" => "int",
"teacher_guid" => "string",
"teacher_name" => "string",
"teacher_position" => "int",
"teacher_img" => "string",
"subject_guid" => "string",
"teacher_intro" => "string",
"teacher_order" => "int",
"teacher_create_time" => "datetime",
"teacher_create_user_guid" => "string",
"teacher_update_time" => "datetime",
"teacher_update_user_guid" => "string",
"teacher_delete_time" => "datetime",
"teacher_delete_user_guid" => "string",
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = 'teacher_create_time';
// 修改时间
protected $updateTime = 'teacher_update_time';
// excel导入/下载模板表头
public const EXCELFIELD = [
'teacher_name' => '教师名称',
'teacher_position' => '教师职位',
'teacher_img' => '教师图片',
'subject_guid' => '授课科目',
'teacher_intro' => '教师简介',
'teacher_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['teacher_name'],
$val['teacher_position'],
Excel::ExportImgFiled($val['teacher_img']),
$val['subject_guid'],
$val['teacher_intro'],
$val['teacher_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)
{
$teacher_name = $value['teacher_name'];$teacher_position = $value['teacher_position'];$teacher_img = $value['teacher_img'];$subject_guid = $value['subject_guid'];$teacher_intro = $value['teacher_intro'];$teacher_order = $value['teacher_order'];
return self::create([
'teacher_name' => $teacher_name,
'teacher_position' => $teacher_position,
'teacher_img' => $teacher_img,
'subject_guid' => $subject_guid,
'teacher_intro' => $teacher_intro,
'teacher_order' => $teacher_order,
]);
}
}