feat:完成代码类目模块接口及其模型层

This commit is contained in:
xjh 2023-06-29 21:04:34 +08:00
parent d016374151
commit e48a8d4e0f
7 changed files with 517 additions and 3 deletions

View File

@ -1,9 +1,9 @@
<?php
namespace app\admin\controller\CodeModule;
namespace app\admin\controller\Code;
use app\BaseController;
use app\common\model\CodeModule\CodeModule as ModelCodeModule;
use app\common\model\Code\CodeModule as ModelCodeModule;
use app\Request;
use think\Validate;
use think\exception\ValidateException;

View File

@ -0,0 +1,216 @@
<?php
namespace app\admin\controller\Code;
use app\BaseController;
use app\common\model\Code\CodeModuleCategory as ModelCodeModuleCategory;
use app\Request;
use app\common\arw\adjfut\src\Traverse;
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 think\facade\Db;
use think\facade\Env;
class CodeModuleCategory extends BaseController
{
/**
* 获取代码块类目列表接口
*
* @param Request $request
* @return array
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function getCodeModuleCategoryList(Request $request): array
{
$con = Tool::getOptionalQuery(
['cmc.code_module_category_name', 'LIKE'],
['cmc.code_module_category_ps', 'LIKE'],
['cmc.code_module_category_global_mode', '='],
['cmc.code_module_category_library_type', '='],
['cust.customer_name|cust.customer_account', 'LIKE', 'customer'],
['cmc.code_module_category_audit', '='],
);
$tree_data = [];
//只针对主级类目进行的分页处理
$query = ModelCodeModuleCategory::alias('cmc')->leftjoin('customer cust', 'cmc.customer_guid = cust.customer_guid')
->scope('master')->where($con);
$module_category_guids = self::pageWrapper($query)->column('code_module_category_guid');
if ($module_category_guids) {
//分页后的主级类目开始构建树形结构(此时才会获取到各个父级类目所包含的子级类目)
$data = ModelCodeModuleCategory::alias('cmc')
->whereOr([
['cmc.code_module_category_guid', 'in', $module_category_guids],
['cmc.code_module_category_parent_guid', 'in', $module_category_guids],
])->field([
'cmc.code_module_category_id',
'cmc.code_module_category_guid',
'cmc.customer_guid',
'cmc.code_module_category_audit',
'cmc.code_module_category_sort',
'cmc.code_module_category_parent_guid',
'cmc.code_module_category_name',
'cmc.code_module_category_audit',
'cmc.code_module_category_global_mode',
'cmc.code_module_category_library_type',
'cmc.code_module_category_ps',
'cmcp.code_module_category_name' => "code_module_category_parent_name",
'cust.customer_name',
])
->leftjoin('code_module_category cmcp', 'cmc.code_module_category_parent_guid = cmcp.code_module_category_guid')
->leftjoin('customer cust', 'cmc.customer_guid = cust.customer_guid')
->order('code_module_category_sort')
->select()
->toArray();
$Traverse = new Traverse('code_module_category_guid', 'code_module_category_parent_guid');
$tree_data = $Traverse->tree($data, ModelCodeModuleCategory::MASTER_DEFAULT, function ($v) {
return [
'code_module_category_name' => $v['code_module_category_name'],
'code_module_category_parent_name' => $v['code_module_category_parent_name'],
'code_module_category_guid' => $v['code_module_category_guid'],
'code_module_category_parent_guid' => $v['code_module_category_parent_guid'],
'code_module_category_sort' => $v['code_module_category_sort'],
'code_module_category_ps' => $v['code_module_category_ps'],
'code_module_category_global_mode' => $v['code_module_category_global_mode'],
'code_module_category_audit' => $v['code_module_category_audit'],
'code_module_category_library_type' => $v['code_module_category_library_type'],
'customer_guid' => $v['customer_guid'],
'customer_name' => $v['customer_name'],
];
});
}
return msg(0, "获取代码块类目列表成功!", [
'count' => $query->count(),
'data' => $tree_data,
]);
}
/**
* 添加代码块类目接口
*
* @param Request $request
* @return array
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function addCodeModuleCategory(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'code_module_category_name|类目名' => 'require',
'code_module_category_ps|补充' => 'require',
'code_module_category_sort|排序' => 'require',
'code_module_category_global_mode|全局模式' => 'require',
'code_module_category_library_type|库类型' => 'require',
]);
ModelCodeModuleCategory::create($params, [
'code_module_category_name',
'code_module_category_ps',
'code_module_category_parent_guid',
'code_module_category_sort',
'code_module_category_global_mode',
'code_module_category_library_type',
'customer_guid',
'code_module_category_audit',
'code_module_category_guid',
'code_module_category_create_user_guid',
'code_module_category_update_user_guid'
]);
return msg('添加成功!');
}
/**
* 编辑代码块类目接口
*
* @param Request $request
* @return array
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function editCodeModuleCategory(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'code_module_category_name|类目名' => 'require',
'code_module_category_ps|补充' => 'require',
'code_module_category_parent_guid|父级guid' => 'require',
'code_module_category_sort|排序' => 'require',
'code_module_category_global_mode|全局模式' => 'require',
'code_module_category_library_type|库类型' => 'require',
]);
$model = ModelCodeModuleCategory::where('code_module_category_guid', $params['code_module_category_guid'])->find();
if (!$model) throwErrorMsg("该代码块类目不存在", 1);
$model->allowField([
'code_module_category_name',
'code_module_category_ps',
'code_module_category_parent_guid',
'code_module_category_sort',
'code_module_category_global_mode',
'code_module_category_library_type',
'customer_guid',
'code_module_category_audit',
'code_module_category_update_user_guid'
])->save($params);
return msg('编辑成功!');
}
/**
* 删除代码块类目接口
*
* @param Request $request
* @return array
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function deleteCodeModuleCategory(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'code_module_category_guid|类目guid' => 'require',
]);
$code_module_category = ModelCodeModuleCategory::where([
'code_module_category_guid' => explode(',', $params['code_module_category_guid'])
])->select();
$code_module_category->delete();
return msg('删除成功!');
}
/**
* 获取父子类目共用信息返回接口
*
* @param Request $request
* @return array
* @date 2023-06-29
* @author xjh
* @since 1.0.0
*/
public function getCodeModuleCategoryInfo(Request $request): array
{
$params = $request->param();
$this->validate($params, [
'code_module_category_guid|类目guid' => 'require',
]);
$find = ModelCodeModuleCategory::field(ModelCodeModuleCategory::Common_INFO_FIELDS)
->withAttr('code_module_category_global_mode', function (string $value): array {
return explode(',', $value);
})
->find($params['code_module_category_guid']);
if (!$find) {
throwErrorMsg("该类目不存在!");
}
return msg(0, '', ['data' => $find]);
}
}

View File

@ -156,4 +156,23 @@ class Customer extends BaseController
$model->allowField(['customer_password'])->save($params);
return msg('编辑成功!');
}
/**
* 获取客户下拉选项数据接口
*
* @param Request $request
* @return array
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function getCustomerOptionList(Request $request): array
{
$select = ModelCustomer::scope('blacklist')
->field(['customer_guid', 'customer_name', 'customer_account'])
->append(['customer_show_text'])
->order('customer_update_time', 'desc')
->select();
return msg(0, '获取客户下拉选项数据成功!', ['data' => $select]);
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace app\common\logic\Code;
use app\common\model\Code\CodeModuleCategory as ModelCodeModuleCategory;
use app\common\model\Token as ModelToken;
class CodeModuleCategory
{
/**
* 新增前审核状态处理
*
* @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 == ModelToken::USER_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 onBeforeInsertParentGuid(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}未完全相同! ");
}
};
}
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace app\common\model\CodeModule;
namespace app\common\model\Code;
use app\common\arw\adjfut\src\Validate;
use app\BaseModel;

View File

@ -0,0 +1,145 @@
<?php
namespace app\common\model\Code;
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;
use app\common\model\Token as ModelToken;
use app\common\logic\Code\CodeModuleCategory as CommonLogicCodeModuleCategory;
class CodeModuleCategory extends BaseModel
{
use SoftDelete;
// 删除字段
protected $deleteTime = 'code_module_category_delete_time';
// 设置主键名
protected $pk = 'code_module_category_guid';
// 设置废弃字段
protected $disuse = [];
// 设置字段信息
protected $schema = [
'code_module_category_id' => 'int',
'code_module_category_guid' => 'string',
'code_module_category_name' => 'string',
'code_module_category_ps' => 'string',
'code_module_category_parent_guid' => 'string',
'code_module_category_sort' => 'int',
'code_module_category_global_mode' => 'int',
'code_module_category_library_type' => 'int',
'customer_guid' => 'string',
'code_module_category_audit' => 'int',
'code_module_category_create_time' => 'datetime',
'code_module_category_create_user_guid' => 'string',
'code_module_category_update_time' => 'datetime',
'code_module_category_update_user_guid' => 'string',
'code_module_category_delete_time' => 'datetime',
'code_module_category_delete_user_guid' => 'string',
];
// 设置json类型字段
protected $json = [''];
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'datetime';
// 创建时间
protected $createTime = 'code_module_category_create_time';
// 修改时间
protected $updateTime = 'code_module_category_update_time';
// 字典
public static $dictionaryMap = [
'code_module_category_audit' => [
self::AUDIT_UNAUDITED => '未审核',
self::AUDIT_PASS => '审核通过',
self::AUDIT_FAILED => '审核未通过'
],
'code_module_category_library_type' => [
self::LIBRARY_DEFAULT => '默认',
self::LIBRARY_COMMON => '公共',
self::LIBRARY_PRIVATE => '私人'
],
];
// 审核状态 未审核
const AUDIT_UNAUDITED = 1;
// 审核状态 审核通过
const AUDIT_PASS = 2;
// 审核状态 审核未通过
const AUDIT_FAILED = 3;
//库类型 默认
const LIBRARY_DEFAULT = 1;
//库类型 公共
const LIBRARY_COMMON = 2;
//库类型 私人
const LIBRARY_PRIVATE = 3;
//主级类目guid默认值
const MASTER_DEFAULT = '0';
//父级主键字段
public $parent_guid_field = 'code_module_category_parent_guid';
//父子类目共用信息字段
const Common_INFO_FIELDS = [
'code_module_category_global_mode',
'code_module_category_library_type',
'customer_guid'
];
/**
* 新增前
*/
public static function onBeforeInsert(self $model): void
{
CommonLogicCodeModuleCategory::onBeforeInsertParentGuid($model);
CommonLogicCodeModuleCategory::onBeforeInsertAudit($model);
CommonLogicCodeModuleCategory::validateCategoryInfo($model);
$model->completeCreateField();
}
/**
* 更新前
*/
public static function onBeforeUpdate(self $model): void
{
CommonLogicCodeModuleCategory::onBeforeInsertParentGuid($model);
CommonLogicCodeModuleCategory::validateCategoryInfo($model);
Tool::handleEthicalRel($model);
$model->completeUpdateField();
}
/**
* 删除前
*/
public static function onBeforeDelete(self $model): void
{
$model->completeDeleteField();
}
/**
* 设置库类型
*
* @param $value
* @return string
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function setCodeModuleCategoryGlobalModeAttr($value): string
{
return is_array($value) ? implode(',', $value) : $value;
}
/**
* 查询范围-主级类目
*
* @param $query
* @return void
* @date 2023-06-29
* @author xjh
* @since 1.0.0
*/
public function scopeMaster($query): void
{
$query->where('code_module_category.code_module_category_parent_guid', self::MASTER_DEFAULT);
}
}

View File

@ -47,6 +47,18 @@ class Customer extends BaseModel
protected $createTime = 'customer_create_time';
// 修改时间
protected $updateTime = 'customer_update_time';
// 字典
public static $dictionaryMap = [
'customer_blacklist' => [
self::BLACKLIST_ENABLE => '是',
self::BLACKLIST_DISABLE => '否'
],
];
// 是否黑名单 是
const BLACKLIST_ENABLE = 1;
// 是否黑名单 否
const BLACKLIST_DISABLE = 2;
/**
* 新增前
@ -109,4 +121,33 @@ class Customer extends BaseModel
{
return LogicCustomer::getCustomerMenu();
}
/**
* 查询范围-非客户黑名单的客户
*
* @param $query
* @return void
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function scopeBlacklist($query): void
{
$query->where('customer.customer_blacklist', self::BLACKLIST_DISABLE);
}
/**
* 获取客户选项展示文字
*
* @param $value
* @param $data
* @return string
* @date 2023-06-28
* @author xjh
* @since 1.0.0
*/
public function getCustomerShowTextAttr($value, $data): string
{
return "客户名称:【{$data['customer_name']}】 账号:【{$data['customer_account']}";
}
}