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

115 lines
5.1 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 handleQueryPaging(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_email', '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】特殊情况-当正常情况的查询分页处理无任何数据时,则通过符合查询条件的子级类目来反推出主级类目)
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;
}
}