views.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import json
  2. from django.contrib.auth.models import User
  3. from django.db.models import Q, Sum
  4. from django.forms.models import model_to_dict
  5. from rest_framework import status
  6. from rest_framework.response import Response
  7. from rest_framework.views import APIView
  8. from even_table.models import Dept, Jobs, Dept2Jobs
  9. # Create your views here.
  10. # 部门增删查改
  11. class deptView(APIView):
  12. """
  13. request {name名字},parent_id{父级id,可空}
  14. 创建一个节点
  15. """
  16. def post(self, request):
  17. data = request.data
  18. res = []
  19. if 'parent_id' in data:
  20. # try:
  21. # 创建部门表,返回创建数据成功
  22. # print(data['parent_id'])
  23. # print(model_to_dict(Dept.objects.get(id=data['parent_id'])))
  24. res = Dept.objects.create(
  25. name=data['name'],
  26. parent_id=data['parent_id']
  27. )
  28. res = model_to_dict(res)
  29. res = {'node': res, 'message': data['name'] + ' 新增叶子成功'}
  30. return Response(data=res, status=status.HTTP_200_OK)
  31. # except Exception:
  32. # print(json.dump(Exception))
  33. # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  34. else:
  35. # try:
  36. res = Dept.objects.create(
  37. name=data['name'],
  38. )
  39. res = model_to_dict(res)
  40. res = {'node': res, 'message': data['name'] + ' 新增父级成功'}
  41. return Response(data=res, status=status.HTTP_200_OK)
  42. # except Exception:
  43. # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  44. """
  45. 得到节点
  46. request{parent_id(节点id,可空)}
  47. """
  48. def get(self, request):
  49. data = request.GET
  50. res = []
  51. if 'parent_id' in data:
  52. # 给parent_id 拿到子集
  53. res = Dept.objects.filter(parent_id=data['parent_id']).values().all()
  54. else:
  55. # 没有传name 则给第0层
  56. res = Dept.objects.filter(parent_id=None).values().all()
  57. return Response(data=res, status=status.HTTP_200_OK)
  58. """
  59. 删除节点
  60. {id:部门id}
  61. """
  62. def delete(self, request):
  63. data = request.data
  64. # 岗位 删除这个部门下面的所有岗位
  65. # 这个部门下面的所有岗位
  66. jobs = Dept2Jobs.objects.filter(dept_id=data['id']).values('job_id').all()
  67. job_ids = []
  68. for job in jobs:
  69. job_ids.append(job['job_id'])
  70. Jobs.objects.filter(id__in=job_ids).delete()
  71. # 部门 删除这个部门
  72. res = Dept.objects.filter(id=data['id']).delete()
  73. # 关联表 删除跟这个部门关联的
  74. Dept2Jobs.objects.filter(dept_id=data['id']).delete()
  75. return Response(data="删除成功", status=status.HTTP_200_OK)
  76. """
  77. 更新节点
  78. {id:节点id,name:节点名字,parent_id:父节点id(可空)}
  79. """
  80. def put(self, request):
  81. data = request.data
  82. if 'parent_id' in data:
  83. res = Dept.objects.filter(id=data['id']).update(name=data['name'], parent_id=data['parent_id'])
  84. else:
  85. res = Dept.objects.filter(id=data['id']).update(name=data['name'])
  86. return Response(data=res, status=status.HTTP_200_OK)
  87. # 岗位增删查改
  88. class jobsView(APIView):
  89. # 增 {name:岗位名字,dept_id:父级部门id}
  90. def post(self, request):
  91. data = request.data
  92. # 创建岗位
  93. res = Jobs.objects.create(
  94. name=data['name']
  95. )
  96. res = model_to_dict(res)
  97. if res:
  98. # 创建关联
  99. Dept2Jobs.objects.create(
  100. dept_id=data['dept_id'],
  101. job_id=res['id']
  102. )
  103. return Response(data={'result': res, 'message': '创建岗位成功'}, status=status.HTTP_200_OK)
  104. # 查 {dept_id:父级部门id(可空,空则查全部)}
  105. def get(self, request):
  106. # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name
  107. names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name").annotate(
  108. jog_sum=Sum('job'))
  109. # print(ids)
  110. return Response(data={'result': names, 'message': '查询成功'}, status=status.HTTP_200_OK)
  111. # 更新 {id:岗位id,name:岗位新名字}
  112. def put(self, request):
  113. pass
  114. # 删 {id:岗位id}
  115. def delete(self, request):
  116. # 删除关联表数据
  117. data = request.data
  118. Dept2Jobs.objects.filter(job_id=data['id']).delete()
  119. # 删除岗位
  120. Jobs.objects.filter(id=data['id']).delete()
  121. return Response(data={'message': data['id'] + '删除成功'}, status=status.HTTP_200_OK)
  122. # 用户增删查改
  123. class userView(APIView):
  124. # 用户查询 {job_id:岗位id(可空,有则选这个岗位下的用户)}
  125. def get(self, request):
  126. data = request.GET
  127. res = []
  128. if 'job_id' in data:
  129. job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values('user_id')
  130. ids = []
  131. for id in job_ids:
  132. ids.append(id['user_id'])
  133. if len(ids) != 0:
  134. res = User.objects.filter(id__in=ids).values()
  135. else:
  136. res = User.objects.values()
  137. return Response(data={'result': res, 'message': '查询用户基础数据'}, status=status.HTTP_200_OK)
  138. # 用户替换部门 {job_id:岗位id,user_ids:用户id数组,type:移除用户还是新增用户(0移除,1新增)}
  139. def put(self, request):
  140. data = request.data
  141. res = {}
  142. if data['type'] == 0:
  143. # 岗位下删除这个用户
  144. Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__in=data['user_ids']).delete()
  145. res = {'message': '删除用户'}
  146. elif data['type'] == 1:
  147. # 岗位下新增用户
  148. for user_id in data['user_ids']:
  149. Dept2Jobs.objects.create(job_id=data['job_id'], user_id=user_id)
  150. res = {'message': '新增用户'}
  151. return Response(data=res, status=status.HTTP_200_OK)
  152. def delete(self, request):
  153. pass
  154. # 部门岗位关联表增删查改
  155. class Dept2JobsView(APIView):
  156. # 传入岗位ID,用户ID数组,data{'dept_id': '34', 'job_id': '25', 'user_id': [1, 2]}
  157. def post(self, request):
  158. # user_id 不为空的删除
  159. Dept2Jobs.objects.filter(~Q(user_id=None), dept_id=request.data['dept_id'],
  160. job_id=request.data['job_id']).delete()
  161. for user_id in request.data['user_id']:
  162. Dept2Jobs(dept_id=request.data['dept_id'], job_id=request.data['job_id'], user_id=user_id).save()
  163. return Response(data=None, status=status.HTTP_200_OK)