6 Commits 14a9879f78 ... 6ac651f4a1

Autore SHA1 Messaggio Data
  DYaiu 6ac651f4a1 提供部门id,可以查询到部门下的所有人员 4 anni fa
  DYaiu 8e7776e072 修复DeptJobUserView的查询源和序列化对象 4 anni fa
  DYaiu 03e7832f69 DeptJobUserView的新增成功,postman测试正确 4 anni fa
  DYaiu b14d81b29f 7-22作业第二步:DeptJobUserView初始化 4 anni fa
  DYaiu 0dd3747ab5 修复bug,获取的数据没有中间表id,无法满足删除功能,现已修复 4 anni fa
  DYaiu 16973f684c 7-22作业第二步:覆盖job岗位的list逻辑 4 anni fa
3 ha cambiato i file con 125 aggiunte e 5 eliminazioni
  1. 8 1
      treeAndTable/serializer.py
  2. 2 1
      treeAndTable/urls.py
  3. 115 3
      treeAndTable/views.py

+ 8 - 1
treeAndTable/serializer.py

@@ -1,6 +1,6 @@
 from rest_framework import serializers
 
-from treeAndTable.models import Dept_job
+from treeAndTable.models import Dept_job, Dept_job_user
 
 
 class Dept_Job_Serializer(serializers.ModelSerializer):
@@ -8,3 +8,10 @@ class Dept_Job_Serializer(serializers.ModelSerializer):
         model = Dept_job
         # 全部都包含的意思
         fields = '__all__'
+
+
+class DeptJobUserSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = Dept_job_user
+        # 全部都包含的意思
+        fields = '__all__'

+ 2 - 1
treeAndTable/urls.py

@@ -1,10 +1,11 @@
 from django.conf.urls import url
 from rest_framework_bulk.routes import BulkRouter
 
-from treeAndTable.views import DepartmentView, DeptAndJobView
+from treeAndTable.views import DepartmentView, DeptAndJobView,DeptJobUserView
 
 router = BulkRouter()
 router.register(r'job', DeptAndJobView)
+router.register(r'user', DeptJobUserView)
 
 urlpatterns = router.urls
 

+ 115 - 3
treeAndTable/views.py

@@ -1,10 +1,13 @@
+from django.db import IntegrityError
 from django.db.models import F
+from django.forms import model_to_dict
+from django.contrib.auth.models import User
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
 from rest_framework.viewsets import ModelViewSet
-from treeAndTable.models import Department, Dept_job, Job
-from treeAndTable.serializer import Dept_Job_Serializer
+from treeAndTable.models import Department, Dept_job, Job, Dept_job_user
+from treeAndTable.serializer import Dept_Job_Serializer, DeptJobUserSerializer
 
 
 class DepartmentView(APIView):
@@ -51,6 +54,7 @@ class DepartmentView(APIView):
             return Response(data={}, status=status.HTTP_400_BAD_REQUEST)
         return Response(data=data, status=status.HTTP_200_OK)
 
+
 '''
 post
 {
@@ -67,6 +71,8 @@ Put
     "job":6
 }
 '''
+
+
 class DeptAndJobView(ModelViewSet):
     queryset = Dept_job.objects.all()
     serializer_class = Dept_Job_Serializer
@@ -86,7 +92,7 @@ class DeptAndJobView(ModelViewSet):
             deptId = request.data['dept_id']
             name = request.data['name']
         except Exception:
-            return Response(data={'msg':'请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
+            return Response(data={'msg': '请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
         try:
             # 如果给定的岗位名称不存在岗位表直接新建一个岗位
             job = Job.objects.get(name=name)
@@ -96,3 +102,109 @@ class DeptAndJobView(ModelViewSet):
         Dept_job.objects.create(dept_id=deptId, job_id=job.id)
 
         return Response(data={'msg': 'success'}, status=status.HTTP_200_OK)
+
+    '''
+    根据部门id获取部门岗位中间表的该部门所有岗位信息
+    返回中间表id和岗位name
+    '''
+
+    def list(self, request, *args, **kwargs):
+        deptId = request.GET.get('dept_id')
+        print(deptId)
+        deptAndJob = Dept_job.objects.filter(dept_id=deptId).all()
+        data = []
+        for DJ in deptAndJob:
+            data.append({
+                'id': DJ.id,
+                'name': DJ.job.name
+            })
+        return Response(data=data, status=status.HTTP_200_OK)
+
+
+'''
+部门岗位中间表与user建立的中间表
+'''
+
+
+class DeptJobUserView(ModelViewSet):
+    queryset = Dept_job_user.objects.all()
+    serializer_class = DeptJobUserSerializer
+
+    '''
+    仅支持list类型的部门岗位id集
+    批量创建部门岗位的人员
+    '''
+
+    def create(self, request, *args, **kwargs):
+        try:
+            # 如果传入的部门必须包装成list
+            deptJobId = request.data['dept_job_id']
+            if not isinstance(deptJobId, list):
+                raise Exception
+            name = request.data['name']
+        except Exception:
+            return Response(data={'msg': '请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
+        try:
+            # 传入的用户名不能重复,如果没有当前用户就新建一个
+            user = User.objects.get(username=name)
+        except Exception:
+            user = User.objects.create_user(username=name, password='2020txts')
+        # 批量创建
+        listinsert = list()
+        for id in deptJobId:
+            listinsert.append(Dept_job_user(dept_job_id=id, user_id=user.id))
+        try:
+            Dept_job_user.objects.bulk_create(listinsert)
+        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)