drag-create-api/app/common/logic/Code/CodeModuleCategory.php

107 lines
3.9 KiB
PHP

<?php
namespace app\common\logic\Code;
use app\common\model\Code\CodeModuleCategory as ModelCodeModuleCategory;
use app\common\model\Token as ModelToken;
class CodeModuleCategory
{
/**
* 父子数据共用信息数据同步处理
*
* @param string $code_module_category_guid 类目guid
* @return void
* @date 2023-07-07
* @author xjh
* @since 1.0.0
*/
public static function hanldCommonInfoFieldsDataSync(string $code_module_category_guid): void
{
$category = ModelCodeModuleCategory::find($code_module_category_guid);
if (!$category) {
throwErrorMsg('该类目不存在!');
}
//为主级类目时才执行
//该方法目的是保持父子类目的共用信息一致
if ($category->code_module_category_parent_guid == ModelCodeModuleCategory::MASTER_DEFAULT) {
$update_data = [];
foreach (ModelCodeModuleCategory::COMMON_INFO_FIELDS as $field) {
$update_data[$field] = $category->$field;
}
ModelCodeModuleCategory::where('code_module_category_parent_guid', $code_module_category_guid)->select()->update($update_data);
}
}
/**
* 新增/编辑前父级(上级类目)guid处理
*
* @param ModelCodeModuleCategory $model
* @return void
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public static function onBeforeOpParentGuid(ModelCodeModuleCategory $model): void
{
if (isset($model->code_module_category_parent_guid) && $model->code_module_category_parent_guid) {
if ($model->code_module_category_parent_guid == $model->code_module_category_guid) {
throwErrorMsg('不可成为自己的子级类目!');
}
$code_module_category_parent = ModelCodeModuleCategory::find($model->code_module_category_parent_guid);
if (!$code_module_category_parent) {
throwErrorMsg('上级类目不存在!');
}
if ($code_module_category_parent->code_module_category_parent_guid != ModelCodeModuleCategory::MASTER_DEFAULT) {
throwErrorMsg('不可选择已拥有上级类目的类目作为自己的上级类目!');
}
if (ModelCodeModuleCategory::where('code_module_category_parent_guid', $model->code_module_category_guid)->find()) {
throwErrorMsg('当前类目已有子类目,不可更换上级类目!');
}
} else {
$model->code_module_category_parent_guid = ModelCodeModuleCategory::MASTER_DEFAULT;
}
}
/**
* 父子类目共用信息验证
*
* @param ModelCodeModuleCategory $model
* @return void
* @date 2023-06-29
* @author xjh
* @since 1.0.0
*/
public static function validateCategoryInfo(ModelCodeModuleCategory $model): void
{
if ($model->code_module_category_parent_guid != ModelCodeModuleCategory::MASTER_DEFAULT) {
$parent_category = ModelCodeModuleCategory::find($model->code_module_category_parent_guid);
if (!$parent_category) {
throwErrorMsg("上级类目不存在!");
}
foreach (ModelCodeModuleCategory::COMMON_INFO_FIELDS as $field) {
if ($model->$field != $parent_category->$field) {
throwErrorMsg("父子类目共用信息{$field}未完全相同! ");
}
}
}
}
/**
* 验证是否为子类目
*
* @param string $code_module_category_guid 类目guid
* @return void
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public static function isSonCategory(string $code_module_category_guid): void
{
$module_category = ModelCodeModuleCategory::scope('notMaster')->find($code_module_category_guid);
if (!$module_category) {
throwErrorMsg("当前类目不为子类目!");
}
}
}