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

119 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\logic\Code;
use app\common\model\Code\CodeModuleCategory as ModelCodeModuleCategory;
use app\common\exception\Tool;
use app\common\arw\adjfut\src\Traverse;
class CodeModuleCategory
{
/**
* 类目分页处理
*
* @param array $params 请求参数
* @return array ['count' => 总数量 ,'guids' => 分页后的主级类目guid数组]
* @date 2023-07-05
* @author xjh
* @since 1.0.0
*/
public static function handleCodeModuleCategoryPaging(array $params): array
{
$con = Tool::getOptionalQuery(
['cmc.code_module_category_name', 'LIKE'],
['cmc.code_module_category_ps', 'LIKE'],
['cmc.code_module_category_library_type', '='],
['cust.customer_name|cust.customer_account', 'LIKE', 'customer'],
['cmc.code_module_category_audit', '='],
);
if (isset($params['code_module_category_global_mode']) && $params['code_module_category_global_mode']) {
$con[] = ['cmc.code_module_category_global_mode', 'REGEXP', implode('|', $params['code_module_category_global_mode'])];
}
$module_category_guids = [];
//【分页处理1】正常情况
$query = ModelCodeModuleCategory::alias('cmc')->leftjoin('customer cust', 'cmc.customer_guid = cust.customer_guid')->scope('master')->where($con);
$module_category_guids = Tool::pageWrapper($query)->column('code_module_category_guid');
//【分页处理2】(当前查询未审核的类目时,原正常情况分页处理查询不到子类目未审核的数据,此时要走这条查询)
//情况:目前查询未审核的类目数据并且分页处理1未能查出任何数据
//该查询会将未审核子类目的所属主类目给查询出来并分页处理
if ($params['code_module_category_audit'] == ModelCodeModuleCategory::AUDIT_UNAUDITED) {
if (!$module_category_guids) {
$query = ModelCodeModuleCategory::alias('cmc')->leftjoin('customer cust', 'cmc.customer_guid = cust.customer_guid')->where($con)->group('code_module_category_parent_guid');
$module_category_guids = Tool::pageWrapper($query)->column('code_module_category_parent_guid');
}
}
return [
'count' => $query->count(),
'guids' => $module_category_guids
];
}
/**
* 构建代码块类目树形结构数据
* 分页后的主级类目开始构建树形结构(此时才会获取到各个父级类目所包含的子级类目)
*
* @param array $module_category_guids 主类目guid数组
* @return array
* @date 2023-07-05
* @author xjh
* @since 1.0.0
*/
public static function buildTreeData(array $module_category_guids): array
{
if (!$module_category_guids) {
return [];
}
$tree_data = [];
$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 $tree_data;
}
}