feat:TOOL类处理(树形数据父子)伦理关系方法新增,祖级guid构建方法修改、后台作品模块接口修改,完成关于我们-教学环境-环境类型模块接口

This commit is contained in:
xjh 2023-04-22 17:23:53 +08:00
parent 3f7cd27c54
commit 9264980ae6
6 changed files with 507 additions and 30 deletions

View File

@ -0,0 +1,227 @@
<?php
namespace app\admin\controller\AboutUs\TeachingEnvir;
use app\BaseController;
use app\common\model\AboutUs\TeachingEnvir\TeachingEnvirType as ModelTeachingEnvirType;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use app\common\arw\adjfut\src\Traverse;
use think\facade\Db;
use think\facade\Env;
class TeachingEnvirType extends BaseController
{
/**
* 获取教学环境类型列表接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function getTeachingEnvirTypeList(Request $request): array
{
$con = Tool::getOptionalQuery(['teaching_envir_type_name', 'LIKE']);
$con[] = ['teaching_envir_type_parent_guid', '=', "0"];
$type_guids = self::pageWrapper(ModelTeachingEnvirType::where($con))
->order('teaching_envir_type_order')
->column('teaching_envir_type_guid');
$type_tree = [];
if ($type_guids) {
$type_data = ModelTeachingEnvirType::whereOr([
[["a.teaching_envir_type_guid", 'in', $type_guids]],
[["a.teaching_envir_type_ancestors_guid", 'REGEXP', implode('|', $type_guids)]],
])
->where([['b.teaching_envir_type_delete_time', 'NULL', null]])
->field([
'a.teaching_envir_type_id',
'a.teaching_envir_type_guid',
'a.teaching_envir_type_name',
'a.teaching_envir_type_order',
'a.teaching_envir_type_parent_guid',
'a.teaching_envir_type_ancestors_guid',
'b.teaching_envir_type_name' => "teaching_envir_type_parent_name",
])->alias('a')
->leftjoin('teaching_envir_type b', 'a.teaching_envir_type_parent_guid = b.teaching_envir_type_guid')
->order('teaching_envir_type_order')
->select()
->toArray();
$Traverse = new Traverse('teaching_envir_type_guid', 'teaching_envir_type_parent_guid');
$type_tree = $Traverse->tree($type_data, '0', function ($v) {
return [
'teaching_envir_type_name' => $v['teaching_envir_type_name'],
'teaching_envir_type_parent_name' => $v['teaching_envir_type_parent_name'],
'teaching_envir_type_guid' => $v['teaching_envir_type_guid'],
'teaching_envir_type_parent_guid' => $v['teaching_envir_type_parent_guid'],
'teaching_envir_type_ancestors_guid' => $v['teaching_envir_type_ancestors_guid'],
'teaching_envir_type_order' => $v['teaching_envir_type_order'],
];
});
}
return msg(0, "获取作品类型列表成功!", [
'data' => $type_tree,
'count' => ModelTeachingEnvirType::where('teaching_envir_type_parent_guid', '0')->count()
]);
}
/**
* 编辑教学环境类型接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function editTeachingEnvirType(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'teaching_envir_type_name|环境类型名称' => 'require',
'teaching_envir_type_order|环境类型排序' => 'require',
'teaching_envir_type_parent_guid|作品类型父级guid' => 'require',
]);
$model = ModelTeachingEnvirType::where('teaching_envir_type_guid', $params['teaching_envir_type_guid'])->find();
if (!$model) throwErrorMsg("该教学环境类型不存在", 1);
$model->allowField([
'teaching_envir_type_update_user_guid',
'teaching_envir_type_name',
'teaching_envir_type_order',
'teaching_envir_type_ancestors_guid',
'teaching_envir_type_parent_guid'
])->save($params);
return msg('编辑成功!');
}
/**
* 添加教学环境类型接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function addTeachingEnvirType(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'teaching_envir_type_name|环境类型名称' => 'require',
'teaching_envir_type_order|环境类型排序' => 'require',
'teaching_envir_type_parent_guid|作品类型父级guid' => 'require',
]);
ModelTeachingEnvirType::create($params, [
'teaching_envir_type_guid',
'teaching_envir_type_create_user_guid',
'teaching_envir_type_update_user_guid',
'teaching_envir_type_name',
'teaching_envir_type_order',
'teaching_envir_type_ancestors_guid',
'teaching_envir_type_parent_guid'
]);
return msg('添加成功!');
}
/**
* 删除教学环境类型接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function deleteTeachingEnvirType(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'teaching_envir_type_guid' => 'require',
]);
$teaching_envir_type = ModelTeachingEnvirType::where([
'teaching_envir_type_guid' => explode(',', $params['teaching_envir_type_guid'])
])->select();
$teaching_envir_type->delete();
return msg('删除成功!');
}
/**
* 导出Excel接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function exportExcel(Request $request)
{
$params = $request->param();
$con = [];
if (isset($params['teaching_envir_type_guids']) && $params['teaching_envir_type_guids']) {
$con['a.teaching_envir_type_guid'] = explode(',', $params['teaching_envir_type_guids']);
}
$select = ModelTeachingEnvirType::field([
'a.teaching_envir_type_name',
'a.teaching_envir_type_order',
// 'a.teaching_envir_type_parent_guid',
// 'b.teaching_envir_type_name' => 'teaching_envir_type_parent_name',
])
->where($con)
->alias('a')
// ->leftjoin('teaching_envir_type b', 'a.teaching_envir_type_parent_guid = b.teaching_envir_type_guid')
->order('teaching_envir_type_order', 'asc')
->select();
return ModelTeachingEnvirType::exportExcel($select);
}
/**
* 下载导入模板接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function downloadTemplate(Request $request)
{
$data = [
array_values(ModelTeachingEnvirType::EXCELFIELD),
// ['', '素描', '1']
['校园', '1']
];
$excel = (new Excel())->exporTsheet($data);
$excel->save('教学环境类型导入模板.xlsx');
}
/**
* 导入excel接口
*
* @param Request request
* @return array
* @date 2023-04-22
* @author xjh
* @since 1.0.0
*/
public function importExcel(Request $request): array
{
$file = new UploadFile('uploads', 'fileExt:xlsx');
$file->putFile('teaching_envir_type');
$msg = ModelTeachingEnvirType::importExcel($file);
return [
'code' => 0,
'msg' => $msg
];
}
}

View File

@ -198,12 +198,12 @@ class WorksType extends BaseController
$select = ModelWorksType::field([
'a.works_type_name',
'a.works_type_order',
'a.works_type_parent_guid',
'b.works_type_name' => 'works_type_parent_name',
// 'a.works_type_parent_guid',
// 'b.works_type_name' => 'works_type_parent_name',
])
->where($con)
->alias('a')
->leftjoin('works_type b', 'a.works_type_parent_guid = b.works_type_guid')
// ->leftjoin('works_type b', 'a.works_type_parent_guid = b.works_type_guid')
->order('works_type_order', 'desc')
->select()
->toArray();
@ -224,7 +224,8 @@ class WorksType extends BaseController
{
$data = [
array_values(ModelWorksType::IMPORT_EXCEL_FIELD),
['', '素描', '1']
// ['', '素描', '1']
['素描', '1']
];
$excel = (new Excel())->exporTsheet($data);
$excel->save('作品类型导入模板.xlsx');

View File

@ -440,23 +440,66 @@ class Tool
}
/**
* 祖级guid构建
* @param string $model 模型层命名空间地址
* @param string $parent_guid 父级guid
* @param string $first_parent 首父级值 默认"0"
* 处理(树形数据父子)伦理关系
*
* @param \think\Model $model 模型层对象
*/
public static function buildAncestorsGuid(string $model, string $parent_guid, string $first_parent = "0"): string
public static function handleEthicalRel(\think\Model $model): void
{
//模型层实例化
$model = new $model;
if (!isset($model->parent_guid_field) || !isset($model->ancestors_guid_field)) {
throwErrorMsg(__METHOD__
. "方法:"
. get_class($model)
. "模型层必须定义public \$parent_guid_field,public \$works_type_ancestors_guid");
}
//获取当前的主键字段名
$guld_field = $model->db()->getPk();
//当前父级主键字段名
$parent_guid_field = $model->parent_guid_field;
//当前祖级级主键字段名
$ancestors_guid_field = $model->ancestors_guid_field;
//处理一
if ($model[$guld_field] == $model[$parent_guid_field]) {
throwErrorMsg("不可以当自己的子级!");
}
//处理二
$is_children = $model->where([
[$guld_field, '=', $model[$parent_guid_field]],
[$ancestors_guid_field, 'REGEXP', $model[$guld_field]],
])->find();
if ($is_children) {
throwErrorMsg("不可以当自己孩子们的子级!");
}
}
/**
* 祖级guid构建
* @param \think\Model $model 模型层命名空间地址
* @param bool $is_unipolar 是否为单极结构
*/
public static function buildAncestorsGuid(\think\Model &$model, bool $is_unipolar = false): void
{
//获取最大父级guid
$first_parent_guid = isset($model->first_parent_guid) ? $model->first_parent_guid : "0";
//获取当前的主键字段名
$guld_field_name = $model->db()->getPk();
//获取当前父级主键集字段名
$parent_guid_field = $model->parent_guid_field;
$parent_guid = $model[$parent_guid_field];
//获取当前祖级主键集字段名
$ancestors_guid_field = $model->ancestors_guid_field;
if ($parent_guid == $first_parent) return $first_parent;
//单极结构或父级guid已经为最大父级guid时直接返回最大父级guid
if ($is_unipolar || $parent_guid == $first_parent_guid) {
$model[$ancestors_guid_field] = $first_parent_guid;
return;
}
//开始构建祖级guid
$parent = $model->where($guld_field_name, $parent_guid)->find();
if (!$parent) throwErrorMsg('该父级数据不存在!');
return $parent[$ancestors_guid_field] . ',' . $parent_guid;
$model[$ancestors_guid_field] = $parent[$ancestors_guid_field] . ',' . $parent_guid;
}
/**

View File

@ -0,0 +1,201 @@
<?php
namespace app\common\model\AboutUs\TeachingEnvir;
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 TeachingEnvirType extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = 'teaching_envir_type_delete_time';
// 设置主键名
protected $pk = 'teaching_envir_type_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
"teaching_envir_type_id" => "int",
"teaching_envir_type_guid" => "string",
"teaching_envir_type_name" => "string",
"teaching_envir_type_order" => "int",
"teaching_envir_type_create_time" => "datetime",
"teaching_envir_type_create_user_guid" => "string",
"teaching_envir_type_update_time" => "datetime",
"teaching_envir_type_update_user_guid" => "string",
"teaching_envir_type_delete_time" => "datetime",
"teaching_envir_type_delete_user_guid" => "string",
"teaching_envir_type_parent_guid" => "string",
"teaching_envir_type_ancestors_guid" => "string",
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = 'teaching_envir_type_create_time';
// 修改时间
protected $updateTime = 'teaching_envir_type_update_time';
//排序字段
public $order_field = 'teaching_envir_type_order';
//父级主键
public $parent_guid_field = 'teaching_envir_type_parent_guid';
//祖级主键集字段
public $ancestors_guid_field = 'teaching_envir_type_ancestors_guid';
// excel导入/下载模板表头
public const EXCELFIELD = [
'teaching_envir_type_name' => '*环境类型名称',
'teaching_envir_type_order' => '环境类型排序',
];
/**
* 新增前
*/
public static function onBeforeInsert(self $model): void
{
Validate::unique(self::class, $model->teaching_envir_type_guid, $model->getData(), [
'teaching_envir_type_name' => '环境类型名称',
]);
Tool::buildAncestorsGuid($model, true);
Tool::sortInsertProc(
self::class,
$model->teaching_envir_type_order,
// ['teaching_envir_type_parent_guid' => $model->teaching_envir_type_parent_guid]
);
$model->completeCreateField();
}
/**
* 更新前
*/
public static function onBeforeUpdate(self $model): void
{
Validate::unique(self::class, $model->teaching_envir_type_guid, $model->getData(), [
'teaching_envir_type_name' => '环境类型名称',
]);
Tool::handleEthicalRel($model);
Tool::buildAncestorsGuid($model);
Tool::sortEditProc(
self::class,
$model->teaching_envir_type_guid,
$model->teaching_envir_type_order,
// ["teaching_envir_type_parent_guid" => $model->teaching_envir_type_parent_guid],
);
$model->completeUpdateField();
}
/**
* 删除前
*/
public static function onBeforeDelete(self $model): void
{
Tool::sortDeleteProc(self::class, $model->teaching_envir_type_guid, ["teaching_envir_type_parent_guid"]);
$model->completeDeleteField();
}
/**
* 导出Excel
*
* @param array $select导出的数据集合
*/
public static function exportExcel($select): void
{
$data = [[
'环境类型名称',
'环境类型排序'
]];
foreach ($select as $key => $val) {
$data[] = [
// $val['teaching_envir_type_parent_name'] ?? "无",
$val['teaching_envir_type_name'],
strval($val['teaching_envir_type_order']),
];
}
$excel = (new Excel())->exporTsheet($data);
$excel->save('教学环境类型.xlsx');
}
/**
* 导入excel
*
* @param \app\common\arw\adjfut\src\UploadFile $file excel
*/
public static function importExcel($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 {
$op = self::importExcelInit($value);
if ($op == 'create') {
$msg[] = "{$line} 环境类型名:【{$value['teaching_envir_type_name']}】 <span style='color:#27af49'>新增成功!</span><br>";
} else {
$msg[] = "{$line} 环境类型名:【{$value['teaching_envir_type_name']}】 <span style='color:#ce723b'>修改成功!</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 每行数据
*/
public static function importExcelInit(array $value): string
{
// $teaching_envir_type_parent_name = $value['teaching_envir_type_parent_name'];
$teaching_envir_type_name = $value['teaching_envir_type_name'];
$teaching_envir_type_order = $value['teaching_envir_type_order'] ?? 0;
//上级类型验证
// $teaching_envir_type_parent_guid = "0";
// if ($teaching_envir_type_parent_name) {
// $teaching_envir_type_parent = self::where('teaching_envir_type_name', $teaching_envir_type_parent_name)->find();
// if (!$teaching_envir_type_parent) throwErrorMsg('该上级类型名称不存在!');
// $teaching_envir_type_parent_guid = $teaching_envir_type_parent->teaching_envir_type_guid;
// }
//修改/新增
if ($teaching_envir_type = self::where('teaching_envir_type_name', $teaching_envir_type_name)->find()) {
$teaching_envir_type->teaching_envir_type_name = $teaching_envir_type_name;
$teaching_envir_type->teaching_envir_type_order = $teaching_envir_type_order;
// $teaching_envir_type->teaching_envir_type_parent_guid = $teaching_envir_type_parent_guid;
$teaching_envir_type->save();
return 'update';
} else {
self::create([
'teaching_envir_type_name' => $teaching_envir_type_name,
'teaching_envir_type_order' => $teaching_envir_type_order,
// 'teaching_envir_type_parent_guid' => $teaching_envir_type_parent_guid,
]);
return 'create';
}
}
}

View File

@ -110,6 +110,7 @@ class Works extends BaseModel
foreach (array_keys(self::EXPORT_EXCEL_FIELD) as $field_name) {
$value = $val[$field_name];
if ($field_name == 'works_img') $value = Excel::ExportImgFiled($value);
if ($field_name == 'works_likes_count') $value = strval($value);
$data[$key + 1][$field_name] = $value;
}
}

View File

@ -53,7 +53,7 @@ class WorksType extends BaseModel
// excel导入/下载模板表头
public const IMPORT_EXCEL_FIELD = [
'works_type_parent_name' => '上级类型名称',
// 'works_type_parent_name' => '上级类型名称',
'works_type_name' => '*作品类型名称',
'works_type_order' => '作品类型排序',
];
@ -66,11 +66,11 @@ class WorksType extends BaseModel
Validate::unique(self::class, $model->works_type_guid, $model->getData(), [
'works_type_name' => '作品类型名称',
]);
$model->works_type_ancestors_guid = Tool::buildAncestorsGuid(self::class, $model->works_type_parent_guid);
Tool::buildAncestorsGuid($model);
Tool::sortInsertProc(
self::class,
$model->works_type_order,
['works_type_parent_guid' => $model->works_type_parent_guid]
// ['works_type_parent_guid' => $model->works_type_parent_guid]
);
$model->completeCreateField();
}
@ -83,12 +83,13 @@ class WorksType extends BaseModel
Validate::unique(self::class, $model->works_type_guid, $model->getData(), [
'works_type_name' => '作品类型名称',
]);
$model->works_type_ancestors_guid = Tool::buildAncestorsGuid(self::class, $model->works_type_parent_guid);
Tool::handleEthicalRel($model);
Tool::buildAncestorsGuid($model);
Tool::sortEditProc(
self::class,
$model->works_type_guid,
$model->works_type_order,
["works_type_parent_guid" => $model->works_type_parent_guid],
// ["works_type_parent_guid" => $model->works_type_parent_guid],
);
$model->completeUpdateField();
}
@ -109,10 +110,13 @@ class WorksType extends BaseModel
*/
public static function exportExcel(array $select): void
{
$data = [array_values(self::IMPORT_EXCEL_FIELD)];
$data = [[
'作品类型名称',
'作品类型排序'
]];
foreach ($select as $key => $val) {
$data[] = [
$val['works_type_parent_name'] ?? "",
// $val['works_type_parent_name'] ?? "无",
$val['works_type_name'],
strval($val['works_type_order']),
];
@ -166,30 +170,30 @@ class WorksType extends BaseModel
*/
public static function importExcelInit(array $value): string
{
$works_type_parent_name = $value['works_type_parent_name'];
// $works_type_parent_name = $value['works_type_parent_name'];
$works_type_name = $value['works_type_name'];
$works_type_order = $value['works_type_order'];
$works_type_order = $value['works_type_order'] ?? 0;
//上级类型验证
$works_type_parent_guid = "0";
if ($works_type_parent_name) {
$works_type_parent = self::where('works_type_name', $works_type_parent_name)->find();
if (!$works_type_parent) throwErrorMsg('该上级类型名称不存在!');
$works_type_parent_guid = $works_type_parent->works_type_guid;
}
// $works_type_parent_guid = "0";
// if ($works_type_parent_name) {
// $works_type_parent = self::where('works_type_name', $works_type_parent_name)->find();
// if (!$works_type_parent) throwErrorMsg('该上级类型名称不存在!');
// $works_type_parent_guid = $works_type_parent->works_type_guid;
// }
//修改/新增
if ($works_type = self::where('works_type_name', $works_type_name)->find()) {
$works_type->works_type_name = $works_type_name;
$works_type->works_type_order = $works_type_order;
$works_type->works_type_parent_guid = $works_type_parent_guid;
// $works_type->works_type_parent_guid = $works_type_parent_guid;
$works_type->save();
return 'update';
} else {
self::create([
'works_type_name' => $works_type_name,
'works_type_order' => $works_type_order,
'works_type_parent_guid' => $works_type_parent_guid,
// 'works_type_parent_guid' => $works_type_parent_guid,
]);
return 'create';
}