闫增涛
2025-04-14 729e47e5aa77d73914de4353e95f56350611f820
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
+-----------------------------------------------------------------------------------------------
* GouGuOPEN [ 左手研发,右手开源,未来可期!]
+-----------------------------------------------------------------------------------------------
* @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
+-----------------------------------------------------------------------------------------------
* @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
+-----------------------------------------------------------------------------------------------
* @Author 勾股工作室 <hdm58@qq.com>
+-----------------------------------------------------------------------------------------------
*/
 
declare (strict_types = 1);
 
namespace app\adm\controller;
 
use app\base\BaseController;
use app\adm\validate\FlowCateCheck;
use app\adm\validate\FlowCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
 
class Flow extends BaseController
{    
 
    public function datalist()
    {
        if (request()->isAjax()) {
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name('Flow')
                ->field('f.*,fc.title as cate,fm.title as module')
                ->alias('f')
                ->join('FlowCate fc', 'fc.id = f.cate_id', 'left')
                ->join('FlowModule fm', 'fm.id = fc.module_id', 'left')
                ->order('id desc')->paginate(['list_rows'=> $rows])
                ->each(function($item, $key){
                    if(!empty($item['department_ids'])){
                        $item['departments']=get_department_name($item['department_ids']);
                    }
                    else{
                        $item['departments']='全部';
                    }
                    if(!empty($item['copy_uids'])){
                        $copy_unames=Db::name('Admin')->where([['status','=',1],['id','in',$item['copy_uids']]])->column('name');
                        $item['copy_unames']=implode(',',$copy_unames);
                    }
                    else{
                        $item['copy_unames']='-';
                    }
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
 
    //添加新增/编辑
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param['flow_list'] = '';
            $flow_list=[];
            //固定审批流
            if($param['check_type']==2){
                $roleData = isset($param['check_role']) ? $param['check_role'] : '';
                $positionIdData = isset($param['check_position_id']) ? $param['check_position_id'] : '';
                $checkUidsData = isset($param['check_uids_a']) ? $param['check_uids_a'] : '';
                $checkTypesData = isset($param['check_types']) ? $param['check_types'] : '';
                foreach ($roleData as $key => $value) {
                    if (!$value) {
                        continue;
                    }
                    if($value==3 && $positionIdData[$key]==''){
                        return to_assign(1, '第'.($key+1).'行的指定岗位职称未选择');
                        break;
                    }
                    if($value>3 && $checkUidsData[$key]==''){
                        return to_assign(1, '第'.($key+1).'行的指定人未选择');
                        break;
                    }
                    $item = [];
                    $item['check_role'] = $value;
                    $item['check_position_id'] = $positionIdData[$key];
                    $item['check_uids'] = $checkUidsData[$key];
                    $item['check_types'] = $checkTypesData[$key];
                    $flow_list[]=$item;    
                }
                $param['flow_list'] = serialize($flow_list);
            }
            //可回退审批流
            if($param['check_type']==3){
                $flowNameData = isset($param['flow_name']) ? $param['flow_name'] : '';
                $checkUidsData = isset($param['check_uids_b']) ? $param['check_uids_b'] : '';
                foreach ($flowNameData as $key => $value) {
                    if (!$value) {
                        continue;
                    }
                    if($checkUidsData[$key]==''){
                        return to_assign(1, '第'.($key+1).'行的指定人未选择');
                        break;
                    }
                    $item = [];
                    $item['check_role'] = 5;
                    $item['flow_name'] = $value;
                    $item['check_uids'] = $checkUidsData[$key];
                    $flow_list[]=$item;    
                }
                if(empty($flow_list)){
                    return to_assign(1, '审批流程信息未完善');
                }
                $param['flow_list'] = serialize($flow_list);
            }
            
            if ($param['id'] > 0) {
                try {
                    validate(FlowCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['update_time'] = time();
                Db::name('Flow')->strict(false)->field(true)->update($param);
                add_log('edit', $param['id'], $param);
            } else {
                try {
                    validate(FlowCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['admin_id'] = $this->uid;
                $param['create_time'] = time();
                $mid = Db::name('Flow')->strict(false)->field(true)->insertGetId($param);
                add_log('add', $mid, $param);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $check_type = 1;
            if($id>0){
                $detail = Db::name('Flow')->where('id',$id)->find();
                $flow_list = unserialize($detail['flow_list']);
                if(!empty($flow_list)){
                    foreach ($flow_list as $key => &$val) {
                        $val['check_unames'] ='';
                        $val['check_position_title'] ='';
                        if($val['check_role']==3){
                            $val['check_position_title'] = Db::name('Position')->where('id', '=', $val['check_position_id'])->value('title');
                        }
                        if($val['check_role']>3){
                            $check_unames = Db::name('Admin')->where('id', 'in', $val['check_uids'])->column('name');
                            $val['check_unames'] = implode(',', $check_unames);
                        }
                    }
                }
                $detail['flow_list'] = $flow_list;
                $detail['copy_unames'] ='';
                if($detail['copy_uids']!=''){
                    $copy_unames = Db::name('Admin')->where('id', 'in', $detail['copy_uids'])->column('name');
                    $detail['copy_unames'] = implode(',', $copy_unames);
                }
                $check_type = $detail['check_type'];
                //var_dump($flow_list);exit;
                View::assign('detail', $detail);
            }
            View::assign('check_type', $check_type);
            View::assign('id', $id);
            return view();
        }
    }
 
    //禁用/启用
    public function check()
    {
        $param = get_params();
        $param['update_time']= time();
        $res = Db::name('Flow')->strict(false)->field('status,update_time')->update($param);
        if($res!==false){
            if($param['status'] == 0){
                add_log('disable', $param['id'], $param);
            }
            else if($param['status'] == 1){
                add_log('recovery', $param['id'], $param);
            }
            return to_assign();
        }
        else{
            return to_assign(1,'操作失败');
        }
    }
    //审批模块
    public function modulelist()
    {
        if (request()->isAjax()) {
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name('FlowModule')
                ->order('sort desc,id desc')
                ->paginate(['list_rows'=> $rows]);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
    //添加模块
    public function module_add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                $param['update_time'] = time();
                $res = Db::name('FlowModule')->strict(false)->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                }
                return to_assign();
            } else {
                $param['create_time'] = time();
                $insertId = Db::name('FlowModule')->strict(false)->field(true)->insertGetId($param);
                if ($insertId) {
                    add_log('add', $insertId, $param);
                }
                return to_assign();
            }
        }else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = Db::name('FlowModule')->find($id);
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            return view();
        }
    }
    
    //设置
    public function module_check()
    {
        $param = get_params();
        $res = Db::name('FlowModule')->strict(false)->field('id,status')->update($param);
        if ($res) {
            if($param['status'] == 0){
                add_log('disable', $param['id'], $param);
            }
            else if($param['status'] == 1){
                add_log('recovery', $param['id'], $param);
            }
            return to_assign();
        }
        else{
            return to_assign(0, '操作失败');
        }
    }   
    
    //审批类型列表
    public function catelist()
    {
        if (request()->isAjax()) {
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name('FlowCate')
                ->field('fc.*,fm.title as module')
                ->alias('fc')
                ->join('FlowModule fm', 'fm.id = fc.module_id', 'left')
                ->order('fc.sort desc,fc.id desc')
                ->paginate(['list_rows'=> $rows])
                ->each(function($item, $key){
                    if(!empty($item['department_ids'])){
                        $item['departments']=get_department_name($item['department_ids']);
                    }
                    else{
                        $item['departments']='全部';
                    }
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }
    //添加审批类型
    public function cate_add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                try {
                    validate(FlowCateCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['update_time'] = time();
                $res = Db::name('FlowCate')->strict(false)->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                }
                return to_assign();
            } else {
                try {
                    validate(FlowCateCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                $param['create_time'] = time();
                $insertId = Db::name('FlowCate')->strict(false)->field(true)->insertGetId($param);
                if ($insertId) {
                    add_log('add', $insertId, $param);
                }
                return to_assign();
            }
        }else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = Db::name('FlowCate')->find($id);
                if(!empty($detail['department_ids'])){
                    $detail['departments']=get_department_name($detail['department_ids']);
                }
                else{
                    $detail['departments']='全部';
                }
                if($detail['template_id']>0){
                    $detail['template_title'] = Db::name('Template')->where('id',$detail['template_id'])->value('title');
                }
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            return view();
        }
    }
    
    //审批类型设置
    public function item_check()
    {
        $param = get_params();
        $res = Db::name('FlowCate')->strict(false)->field('id,status')->update($param);
        if ($res) {
            if($param['status'] == 0){
                add_log('disable', $param['id'], $param);
            }
            else if($param['status'] == 1){
                add_log('recovery', $param['id'], $param);
            }
            return to_assign();
        }
        else{
            return to_assign(0, '操作失败');
        }
    }   
}