Extension3915ModuleController.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @remark :
* @name :Extension3915ModuleController.php
* @author :lyh
* @method :post
* @time :2025/11/20 11:42
*/
namespace App\Http\Controllers\Bside\ExtensionModule;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Models\ExtentModule\ExtensionModuleField;
use App\Models\ExtentModule\ExtensionModuleValue;
use Illuminate\Support\Facades\Cache;
class Extension3915ModuleController extends BaseController
{
/**
* @remark :获取列表数据
* @name :lists
* @author :lyh
* @method :post
* @time :2025/11/20 14:38
*/
public function lists(){
$this->request->validate([
'module_id'=>'required',
],[
'module_id.required' => '模块id不能为空',
]);
$searchParam = [
'module_id'=>$this->param['module_id'],
];
//先暂时去掉缓存
// $resultData = Cache::get('extension_module_list_3915_'.$this->param['module_id']);
$resultData = [];
if(empty($resultData)){
$data = [];
$moduleValueModel = new ExtensionModuleValue();
if(isset($this->param['field_id']) && ($this->param['field_id'] != 0) && isset($this->param['value'])){
$uuidArr = $moduleValueModel->formatQuery(['field_id'=>$this->param['field_id'],'value'=>$this->param['value'],'module_id'=>$this->param['module_id']])->distinct()->pluck('uuid')->toArray();
if(!empty($uuidArr)){
$searchParam['uuid'] = ['in',$uuidArr];
}
}
if(isset($this->param['start_time']) && !empty($this->param['start_time']) && isset($this->param['end_time']) && !empty($this->param['end_time'])){
$searchParam['created_at'] = ['between',[$this->param['start_time'],$this->param['end_time']]];
}
$lists = $moduleValueModel->list($searchParam);
if(!empty($lists)){
foreach ($lists as $k => $v){
$data[$v['uuid']][$v['field_id']] = $v['value'];
$data[$v['uuid']]['created_at'] = $v['created_at'];
}
}
$resultData = [];
foreach ($data as $k => $v){
$v['uuid'] = $k;
$resultData[] = $v;
}
Cache::add('extension_module_list_3915_'.$this->param['module_id'],$resultData);
}
$resultData = $this->formatPaginate($resultData,$this->row,$this->page);
//执行分页
$this->response('success',Code::SUCCESS,$resultData);
}
/**
* @remark :分页
* @name :simplePaginate
* @author :lyh
* @method :post
* @time :2025/11/20 14:50
*/
public function formatPaginate($data, $size = 20, $page = 1)
{
$collection = collect($data);
$total = $collection->count();
$totalPage = max(1, ceil($total / $size));
// 确保页码在有效范围内
$page = max(1, min($page, $totalPage));
$list = $collection->forPage($page, $size)->values()->toArray();
return [
'list' => $list,
'page' => (int)$page,
'size' => (string)$size,
'total' => $total,
'total_page' => $totalPage
];
}
/**
* @remark :导入数据
* @name :importData
* @author :lyh
* @method :post
* @time :2025/11/20 14:40
*/
public function importData()
{
$this->request->validate([
'module_id'=>'required',
],[
'module_id.required' => '模块id不能为空',
]);
if($this->user['project_id'] != 3915){
$this->fail('当前项目不能调用当前方法');
}
$moduleFieldModel = new ExtensionModuleField();
$filedList = $moduleFieldModel->where(['module_id' => $this->param['module_id']])->pluck( 'id','field_name')->toArray();
if(empty($filedList)){
$this->fail('请先设置字段,再添加数据');
}
$data = $this->param['data'];
$moduleValueModel = new ExtensionModuleValue();
$key_filedId = [];
$resultData = [];
try {
foreach ($data as $k => $v){
foreach ($v as $k1 => $v1){
if($k == 0){
if(isset($filedList[$v1])){
$key_filedId[$k1] = $filedList[$v1];
}
continue;
}
$resultData[] = [
'uuid'=>$k + 1,
'module_id'=>$this->param['module_id'],
'field_id'=>$key_filedId[$k1],
'value'=>$v1 ?? '',
];
}
}
if(!empty($resultData)){
//todo::优先截断表
$moduleValueModel->truncate();
$moduleValueModel->insertAll($resultData);
}
}catch (\Exception $e){
$this->fail('当前文件不能导入,请修改后再导入');
}
$this->response('success',Code::SUCCESS,$resultData);
}
}