Browse Source

提供部门id,可以查询到部门下的所有人员

DYaiu 4 years ago
parent
commit
6ac651f4a1
1 changed files with 50 additions and 0 deletions
  1. 50 0
      treeAndTable/views.py

+ 50 - 0
treeAndTable/views.py

@@ -158,3 +158,53 @@ class DeptJobUserView(ModelViewSet):
         except IntegrityError:
             return Response(data={'msg': '数据已经存在,不要添加重复数据'}, status=status.HTTP_400_BAD_REQUEST)
         return Response(data={'msg': 'success'}, status=status.HTTP_200_OK)
+
+    '''
+    返回的数据案例
+    [
+        {
+            "id": 14,
+            "user_id": 3,
+            "user_name": "zangsan",
+            "jobs": [
+                {
+                    "job_id": 4,
+                    "job_name": "测试岗位"
+                },
+                {
+                    "job_id": 6,
+                    "user_name": "测试岗位1"
+                }
+            ]
+        }
+    ]
+    根据部门id获取部门下的所有人员和人员所分配到的岗位信息
+    '''
+
+    def list(self, request, *args, **kwargs):
+        deptAndJob = Dept_job.objects.filter(dept_id=request.GET.get('dept_id', -1)).all().values()
+        # 使用列表生成器生成该部门的所有岗位id数组查询所有人员
+        deptAndJobAndUser = Dept_job_user.objects.filter(dept_job_id__in=[DJ['id'] for DJ in deptAndJob]).all()
+        # 返回前端的数据集
+        data = {}
+        # 部门岗位人员中间表的数据集
+        for DJU in deptAndJobAndUser:
+            # 如果该用户有多个岗位
+            if DJU.user.id in data:
+                data[DJU.user.id]['jobs'].append({'job_id': DJU.dept_job.job.id, 'job_name': DJU.dept_job.job.name})
+            else:
+                # 第一次包装用户的岗位信息
+                data[DJU.user.id] = {
+                    'id': DJU.id,
+                    'user_id': DJU.user.id,
+                    'user_name': DJU.user.username,
+                    'jobs': [
+                        {
+                            'job_id': DJU.dept_job.job.id,
+                            'job_name': DJU.dept_job.job.name,
+                        }
+                    ]
+                }
+        # 前端table不接收有id的object,需转换成list
+        data = [i for i in data.values()]
+        return Response(data=data, status=status.HTTP_200_OK)