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

136 lines
5.1 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);
}
}
/**
* 新增前审核状态处理
*
* @param ModelCodeModuleCategory &$model
* @return void
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public static function onBeforeInsertAudit(ModelCodeModuleCategory &$model): void
{
switch (ModelToken::getCurrent()->token_type) {
case ModelToken::USER_TYPE: //用户
//保证用户新增的任意代码类目审核状态为已审核
$model->code_module_category_audit = ModelCodeModuleCategory::AUDIT_PASS;
break;
case ModelToken::CUSTOMER_TYPE: //客户
//客户新增代码类目时只有库类为私用的权限时,才可默认审核状态为已审核,其余都为待审核
if ($model->code_module_category_library_type == ModelCodeModuleCategory::LIBRARY_PRIVATE) {
$model->code_module_category_audit = ModelCodeModuleCategory::AUDIT_PASS;
} else {
$model->code_module_category_audit = ModelCodeModuleCategory::AUDIT_UNAUDITED;
}
break;
default:
throwErrorMsg("token所有者身份类型不正确!");
}
}
/**
* 新增/编辑前父级(上级类目)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("当前类目不为子类目!");
}
}
}