zhanglinxin 1 year ago
parent
commit
6108ed746d

+ 1 - 1
application/api/controller/Approve.php

@@ -51,7 +51,7 @@ class Approve extends Base
         $search = input('search');
         $offset = $this->off_set;
         $length = $this->page_num;
-        $data = ApproveService::get_list( $status,$module, $start_time, $end_time,$search, $offset, $length, $this->user);
+        $data = ApproveService::get_list($status,$module, $start_time, $end_time,$search, $offset, $length, $this->user);
         $this->success('审批列表', $data);
     }
 

+ 1 - 1
application/api/controller/Approveinfo.php

@@ -92,7 +92,7 @@ class Approveinfo extends Base
      *     @Apidoc\Returned("info_id", type="integer", desc="申请ID"),
      *     @Apidoc\Returned("name", type="string", desc="姓名"),
      * )
-     * @Apidoc\Returned("approve_info", type="object", desc="审批信息",
+     * @Apidoc\Returned("approve_one", type="object", desc="审批信息 status=2,4",
      *     @Apidoc\Returned("id", type="integer", desc="审批ID"),
      *     @Apidoc\Returned("info_id", type="integer", desc="申请ID"),
      *     @Apidoc\Returned("approve_user", type="string", desc="审批人userid"),

+ 7 - 1
application/common/model/Approve.php

@@ -17,7 +17,7 @@ class Approve extends Model
     // 关联用户
     public function user()
     {
-        return $this->belongsTo(User::class, 'approve_user', 'userid')->where('is_deleted',CommonConstant::IS_DELETED_0);
+        return $this->belongsTo(User::class, 'approve_user', 'userid');
     }
 
     // 关联审批申请关联用户 (审批列表)
@@ -26,4 +26,10 @@ class Approve extends Model
         return $this->belongsTo(User::class, 'user_id', 'userid');
     }
 
+    // 关联审批 (审批列表)
+    public function approveOne()
+    {
+        return $this->hasOne(Approve::class, 'info_id', 'info_id')->where('is_deleted',CommonConstant::IS_DELETED_0);
+    }
+
 }

+ 1 - 1
application/common/model/ApproveInfo.php

@@ -43,7 +43,7 @@ class ApproveInfo extends Model
     }
 
     // 关联审批 (我的申请记录)
-    public function approveInfo()
+    public function approveOne()
     {
         return $this->hasOne(Approve::class, 'info_id', 'id')->where('is_deleted',CommonConstant::IS_DELETED_0);
     }

+ 67 - 35
application/common/service/ApproveInfoService.php

@@ -157,6 +157,7 @@ class ApproveInfoService
         $flow_num = 0;
         $copy_num = 0;
         $create_at = date('Y-m-d H:i:s');
+
         // 审批人
         foreach ($approve_user as $key => $value) {
             $flow_num++;
@@ -179,6 +180,7 @@ class ApproveInfoService
                 'approve_user' => $value,
             ];
         }
+
         if ($data) {
             Approve::insertAll($data);
         }
@@ -222,24 +224,6 @@ class ApproveInfoService
         return $list;
     }
 
-    public static function get_with($list, $module, $status)
-    {
-        switch ($module) {
-            case CommonConstant::MODULE_5:
-                $list = $list->with(['peer_user' => function ($query) {
-                    $query->field('id,info_id,name');
-                }, 'approve_info' => function ($query) use ($status) {
-                    $query->field('id,info_id,status,approve_user')
-                        ->where('status', $status)
-                        ->with(['user' => function ($query) {
-                            $query->field('userid,name');
-                        }]);
-                }]);
-                break;
-        }
-        return $list;
-    }
-
     /**
      * 详情/信息
      *
@@ -250,28 +234,76 @@ class ApproveInfoService
     public static function get_detail($id, $user, $type)
     {
         $userid = $user['userid'];
+        $info = ApproveInfo::field('user_id,is_deleted', true)
+            ->where('user_id', $userid)
+            ->where('is_deleted', CommonConstant::IS_DELETED_0);
         if ($type == 'detail') {
-            $info = ApproveInfo::field('user_id,is_deleted', true)
-                ->where('user_id', $userid)
-                ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                ->with(['module_info', 'peer_user' => function ($query) {
-                    $query->field('id,info_id,name');
-                }, 'approve' => function ($query) {
-                    $query->field('is_deleted,create_at', true)
-                        ->with(['user' => function ($query) {
-                            $query->field('userid,headimg,name');
-                        }]);
-                }])
-                ->find($id);
+            $info->with(['module_info', 'approve' => function ($query) {
+                $query->field('is_deleted,create_at', true)
+                    ->with(['user' => function ($query) {
+                        $query->field('userid,headimg,name');
+                    }]);
+            }]);
+            $info = $info->find($id);
             if ($info) {
                 $info['department'] = Department::where('dept_id', 'in', $user['department'])->field('dept_id,name')->select();
             }
         } else {
-            $info = ApproveInfo::field('user_id,is_deleted', true)
-                ->where('user_id', $userid)
-                ->where('is_deleted', CommonConstant::IS_DELETED_0)
-                ->with(['module_info', 'peer_user'])
-                ->find($id);
+            $info->with(['module_info']);
+            $info = $info->find($id);
+        }
+        $info = self::get_item($info, $type);
+        return $info;
+    }
+
+    public static function get_with($list, $module, $status)
+    {
+        switch ($module) {
+            case CommonConstant::MODULE_1:
+                break;
+            case CommonConstant::MODULE_3:
+                break;
+            case CommonConstant::MODULE_4:
+                break;
+            case CommonConstant::MODULE_5:
+                if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
+                    // 审批中或审批驳回 才关联 审批信息
+                    $list = $list->with(['peer_user' => function ($query) {
+                        $query->field('id,info_id,name');
+                    }, 'approveOne' => function ($query) use ($status) {
+                        $query->field('id,info_id,status,approve_user')
+                            ->where('status', $status)
+                            ->with(['user' => function ($query) {
+                                $query->field('userid,name');
+                            }]);
+                    }]);
+                } else {
+                    $list = $list->with(['peer_user' => function ($query) {
+                        $query->field('id,info_id,name');
+                    }]);
+                }
+                break;
+        }
+        return $list;
+    }
+
+    public static function get_item($info, $type)
+    {
+        $module = $info['module'];
+        switch ($module) {
+            case CommonConstant::MODULE_1:
+                break;
+            case CommonConstant::MODULE_3:
+                break;
+            case CommonConstant::MODULE_4:
+                break;
+            case CommonConstant::MODULE_5:
+                if ($type == 'detail') {
+                    $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
+                } else {
+                    $info->peerUser;
+                }
+                break;
         }
         return $info;
     }

+ 53 - 44
application/common/service/ApproveService.php

@@ -35,6 +35,8 @@ class ApproveService
             return [];
         }
 
+        $userids = $search && !is_numeric($search) ? User::where('name', 'like', '%' . $search . '%')->column('userid') : [];
+
         $userid = $user['userid'];
         $aliasName = 'approve.';
         $jobName = 'approveInfo.';
@@ -53,64 +55,71 @@ class ApproveService
                     $query->where($aliasName . 'status', 'in', [CommonConstant::STATUS_3, CommonConstant::STATUS_4]);
                 }
             })
-            ->when($module, function ($query) use ($jobName, $module) {
-                $query->where($jobName . 'module', 'in', $module);
-            })
             ->where($aliasName . 'is_deleted', CommonConstant::IS_DELETED_0)
             ->where($aliasName . 'approve_user', $userid)
             ->where($jobName . 'is_deleted', CommonConstant::IS_DELETED_0)
-            ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName,$start_time, $end_time) {
-                $query->where($aliasName.'create_at', 'BETWEEN', [$start_time, $end_time]);
+            ->when($module, function ($query) use ($jobName, $module) {
+                $query->where($jobName . 'module', 'in', $module);
+            })
+            ->when(!empty($start_time) && !empty($end_time), function ($query) use ($aliasName, $start_time, $end_time) {
+                $query->where($aliasName . 'create_at', 'BETWEEN', [$start_time, $end_time]);
+            })
+            ->when($userids, function ($query) use ($jobName, $userids) {
+                $query->where($jobName . 'user_id', 'in', $userids);
             })
-            ->when($search, function ($query) use ($search) {
-                if(!is_numeric($search)){
-                    $query->where('order_no|reason', 'like', '%' . $search . '%');
-                } else{
-                    $query->where('order_no', 'like', '%' . $search . '%');
+            ->when($search, function ($query) use ($search, $userids) {
+                if (!$userids) {
+                    if (!is_numeric($search)) {
+                        $query->where('order_no|reason', 'like', '%' . $search . '%');
+                    } else {
+                        $query->where('order_no', 'like', '%' . $search . '%');
+                    }
                 }
             })
-            ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER')
-            ->with(['approve_info_user' => function ($query) {
+            ->join('__APPROVE_INFO__ approveInfo', 'approve.info_id = approveInfo.id', 'INNER');
+        if ($status == 1) {
+            // 待处理 才关联 审批信息
+            $list->with(['approveInfoUser' => function ($query) {
+                $query->field('userid,name');
+            }, 'approveOne' => function ($query) {
+                $query->field('id,info_id,status,approve_user')
+                    ->where('status', CommonConstant::STATUS_2)
+                    ->with(['user' => function ($query) {
+                        $query->field('userid,name');
+                    }]);
+            }]);
+        } else {
+            $list->with(['approveInfoUser' => function ($query) {
                 $query->field('userid,name');
             }]);
-//        $list = self::get_with($list, $module, $status);
+        }
         $list = $list->limit($offset, $length)
             ->order('id desc')
             ->select();
-//            ->map(function($return){
-//                if($return['status'] == CommonConstant::STATUS_1){
-//                    $return['approve_user'] = Approve::field('id,approve_user')
-//                        ->where('info_id',$return['info_id'])
-//                        ->where('status',CommonConstant::STATUS_2)
-//                        ->where('is_deleted', CommonConstant::IS_DELETED_0)
-//                        ->find();
-//                }
-//                return $return;
-//            });
-//        $array = [];
+
+//        $info_ids = [];
 //        foreach ($list as $value){
-//            $array[$value['info_id']] = $value['user_id'];
+//            if($value['status'] == CommonConstant::STATUS_1) {
+//                $info_ids[] = $value['info_id'];
+//            }
+//        }
+//        if($info_ids){
+//            $approve_one_list = Approve::field('id,info_id,status,approve_user')
+//                ->where('info_id','in',$info_ids)
+//                ->where('status',CommonConstant::STATUS_2)
+//                ->where('is_deleted', CommonConstant::IS_DELETED_0)
+//                ->with(['user' => function ($query) {
+//                    $query->field('userid,name');
+//                }])
+//                ->select();
+//            $approve_one_data = array_column($approve_one_list, null, 'info_id');
+//            foreach ($list as $val){
+//                if(array_key_exists($val['info_id'],$approve_one_data)){
+//                    $val['approve_one'] = $approve_one_data[$val['info_id']];
+//                }
+//            }
 //        }
-//        p($array);exit;
-
-        return $list;
-    }
 
-    public static function get_with($list, $module, $status)
-    {
-        switch ($module) {
-            case CommonConstant::MODULE_5:
-                $list = $list->with(['peer_user' => function ($query) {
-                    $query->field('id,info_id,name');
-                }, 'approve_info' => function ($query) use ($status) {
-                    $query->field('id,info_id,status,approve_user')
-                        ->where('status', $status)
-                        ->with(['user' => function ($query) {
-                            $query->field('userid,name');
-                        }]);
-                }]);
-                break;
-        }
         return $list;
     }