|  | @@ -1,7 +1,7 @@
 | 
	
		
			
				|  |  |  import json
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  from django.contrib.auth.models import User
 | 
	
		
			
				|  |  | -from django.db.models import Q
 | 
	
		
			
				|  |  | +from django.db.models import Q, Sum
 | 
	
		
			
				|  |  |  from django.forms.models import model_to_dict
 | 
	
		
			
				|  |  |  from rest_framework import status
 | 
	
		
			
				|  |  |  from rest_framework.response import Response
 | 
	
	
		
			
				|  | @@ -12,6 +12,8 @@ from even_table.models import Dept, Jobs, Dept2Jobs
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  # Create your views here.
 | 
	
		
			
				|  |  |  # 部门增删查改
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  class deptView(APIView):
 | 
	
		
			
				|  |  |      """
 | 
	
		
			
				|  |  |      request {name名字},parent_id{父级id,可空}
 | 
	
	
		
			
				|  | @@ -125,7 +127,8 @@ class jobsView(APIView):
 | 
	
		
			
				|  |  |      # 查 {dept_id:父级部门id(可空,空则查全部)}
 | 
	
		
			
				|  |  |      def get(self, request):
 | 
	
		
			
				|  |  |          # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name
 | 
	
		
			
				|  |  | -        names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name")
 | 
	
		
			
				|  |  | +        names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name").annotate(
 | 
	
		
			
				|  |  | +            jog_sum=Sum('job'))
 | 
	
		
			
				|  |  |          # print(ids)
 | 
	
		
			
				|  |  |          return Response(data={'result': names, 'message': '查询成功'}, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -145,18 +148,18 @@ class jobsView(APIView):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  # 用户增删查改
 | 
	
		
			
				|  |  |  class userView(APIView):
 | 
	
		
			
				|  |  | -    def post(self, request):
 | 
	
		
			
				|  |  | -        pass
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      # 用户查询 {job_id:岗位id(可空,有则选这个岗位下的用户)}
 | 
	
		
			
				|  |  |      def get(self, request):
 | 
	
		
			
				|  |  |          data = request.GET
 | 
	
		
			
				|  |  | +        res = []
 | 
	
		
			
				|  |  |          if 'job_id' in data:
 | 
	
		
			
				|  |  | -            job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values()
 | 
	
		
			
				|  |  | +            job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values('user_id')
 | 
	
		
			
				|  |  |              ids = []
 | 
	
		
			
				|  |  |              for id in job_ids:
 | 
	
		
			
				|  |  | -                ids.append(id)
 | 
	
		
			
				|  |  | -            res = User.objects.filter(id__in=ids).values()
 | 
	
		
			
				|  |  | +                ids.append(id['user_id'])
 | 
	
		
			
				|  |  | +            if len(ids) != 0:
 | 
	
		
			
				|  |  | +                res = User.objects.filter(id__in=ids).values()
 | 
	
		
			
				|  |  |          else:
 | 
	
		
			
				|  |  |              res = User.objects.values()
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -179,3 +182,15 @@ class userView(APIView):
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def delete(self, request):
 | 
	
		
			
				|  |  |          pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 部门岗位关联表增删查改
 | 
	
		
			
				|  |  | +class Dept2JobsView(APIView):
 | 
	
		
			
				|  |  | +    # 传入岗位ID,用户ID数组,data{'dept_id': '34', 'job_id': '25', 'user_id': [1, 2]}
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        # user_id 不为空的删除
 | 
	
		
			
				|  |  | +        Dept2Jobs.objects.filter(~Q(user_id=None), dept_id=request.data['dept_id'],
 | 
	
		
			
				|  |  | +                                 job_id=request.data['job_id']).delete()
 | 
	
		
			
				|  |  | +        for user_id in request.data['user_id']:
 | 
	
		
			
				|  |  | +            Dept2Jobs(dept_id=request.data['dept_id'], job_id=request.data['job_id'], user_id=user_id).save()
 | 
	
		
			
				|  |  | +        return Response(data=None, status=status.HTTP_200_OK)
 |