|  | @@ -0,0 +1,156 @@
 | 
	
		
			
				|  |  | +import json
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from django.contrib.auth.models import User
 | 
	
		
			
				|  |  | +from django.db.models import Q
 | 
	
		
			
				|  |  | +from django.forms.models import model_to_dict
 | 
	
		
			
				|  |  | +from rest_framework import status
 | 
	
		
			
				|  |  | +from rest_framework.response import Response
 | 
	
		
			
				|  |  | +from rest_framework.views import APIView
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from even_table.models import Dept, Jobs, Dept2Jobs
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# Create your views here.
 | 
	
		
			
				|  |  | +# 部门增删查改
 | 
	
		
			
				|  |  | +class deptView(APIView):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    request {name名字},parent_id{父级id,可空}
 | 
	
		
			
				|  |  | +    创建一个节点
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        res = []
 | 
	
		
			
				|  |  | +        if 'parent_id' in data:
 | 
	
		
			
				|  |  | +            # try:
 | 
	
		
			
				|  |  | +            # 创建部门表,返回创建数据成功
 | 
	
		
			
				|  |  | +            # print(data['parent_id'])
 | 
	
		
			
				|  |  | +            # print(model_to_dict(Dept.objects.get(id=data['parent_id'])))
 | 
	
		
			
				|  |  | +            res = Dept.objects.create(
 | 
	
		
			
				|  |  | +                name=data['name'],
 | 
	
		
			
				|  |  | +                parent_id=data['parent_id']
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +            res = model_to_dict(res)
 | 
	
		
			
				|  |  | +            res = {'node': res, 'message': data['name'] + ' 新增叶子成功'}
 | 
	
		
			
				|  |  | +            return Response(data=res, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +        # except Exception:
 | 
	
		
			
				|  |  | +        #     print(json.dump(Exception))
 | 
	
		
			
				|  |  | +        #     return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            # try:
 | 
	
		
			
				|  |  | +            res = Dept.objects.create(
 | 
	
		
			
				|  |  | +                name=data['name'],
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +            res = model_to_dict(res)
 | 
	
		
			
				|  |  | +            res = {'node': res, 'message': data['name'] + ' 新增父级成功'}
 | 
	
		
			
				|  |  | +            return Response(data=res, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +        # except Exception:
 | 
	
		
			
				|  |  | +        #     return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    得到节点
 | 
	
		
			
				|  |  | +    request{parent_id(节点id,可空)}
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def get(self, request):
 | 
	
		
			
				|  |  | +        data = request.GET
 | 
	
		
			
				|  |  | +        res = []
 | 
	
		
			
				|  |  | +        if 'parent_id' in data:
 | 
	
		
			
				|  |  | +            # 给parent_id 拿到子集
 | 
	
		
			
				|  |  | +            res = Dept.objects.filter(parent_id=data['parent_id']).values().all()
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            # 没有传name 则给第0层
 | 
	
		
			
				|  |  | +            res = Dept.objects.filter(parent_id=None).values().all()
 | 
	
		
			
				|  |  | +        return Response(data=res, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    删除节点
 | 
	
		
			
				|  |  | +    {id:节点id}
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def delete(self, request):
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        # 部门 删除这个部门
 | 
	
		
			
				|  |  | +        res = Dept.objects.filter(id=data['id']).delete()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # 岗位 删除这个部门下面的所有岗位
 | 
	
		
			
				|  |  | +        # Jobs.objects.filter()
 | 
	
		
			
				|  |  | +        # Dept2Jobs.objects.filter(dept_id=data['id'])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # 关联表 删除跟这个部门关联的
 | 
	
		
			
				|  |  | +        Dept2Jobs.objects.filter(dept_id=data['id']).delete()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return Response(data="删除成功", status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    更新节点
 | 
	
		
			
				|  |  | +    {id:节点id,name:节点名字,parent_id:父节点id(可空)}
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def put(self, request):
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        if 'parent_id' in data:
 | 
	
		
			
				|  |  | +            res = Dept.objects.filter(id=data['id']).update(name=data['name'], parent_id=data['parent_id'])
 | 
	
		
			
				|  |  | +        else:
 | 
	
		
			
				|  |  | +            res = Dept.objects.filter(id=data['id']).update(name=data['name'])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return Response(data=res, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 岗位增删查改
 | 
	
		
			
				|  |  | +class jobsView(APIView):
 | 
	
		
			
				|  |  | +    # 增 {name:岗位名字,dept_id:父级部门id}
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        # 创建岗位
 | 
	
		
			
				|  |  | +        res = Jobs.objects.create(
 | 
	
		
			
				|  |  | +            name=data['name']
 | 
	
		
			
				|  |  | +        )
 | 
	
		
			
				|  |  | +        res = model_to_dict(res)
 | 
	
		
			
				|  |  | +        if res:
 | 
	
		
			
				|  |  | +            # 创建关联
 | 
	
		
			
				|  |  | +            Dept2Jobs.objects.create(
 | 
	
		
			
				|  |  | +                dept_id=data['dept_id'],
 | 
	
		
			
				|  |  | +                job_id=res['id']
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return Response(data={'result': res, 'message': '创建岗位成功'}, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 查 {dept_id:父级部门id(可空,空则查全部)}
 | 
	
		
			
				|  |  | +    def get(self, request):
 | 
	
		
			
				|  |  | +        # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name
 | 
	
		
			
				|  |  | +        names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name")
 | 
	
		
			
				|  |  | +        # print(ids)
 | 
	
		
			
				|  |  | +        return Response(data={'result': names, 'message': '查询成功'}, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 更新 {id:岗位id,name:岗位新名字}
 | 
	
		
			
				|  |  | +    def put(self, request):
 | 
	
		
			
				|  |  | +        pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 删 {id:岗位id}
 | 
	
		
			
				|  |  | +    def delete(self, request):
 | 
	
		
			
				|  |  | +        # 删除关联表数据
 | 
	
		
			
				|  |  | +        data = request.data
 | 
	
		
			
				|  |  | +        Dept2Jobs.objects.filter(job_id=data['id']).delete()
 | 
	
		
			
				|  |  | +        # 删除岗位
 | 
	
		
			
				|  |  | +        Jobs.objects.filter(id=data['id']).delete()
 | 
	
		
			
				|  |  | +        return Response(data={'message': data['id'] + '删除成功'}, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# 用户增删查改
 | 
	
		
			
				|  |  | +class userView(APIView):
 | 
	
		
			
				|  |  | +    def post(self, request):
 | 
	
		
			
				|  |  | +        pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # 用户查询
 | 
	
		
			
				|  |  | +    def get(self, request):
 | 
	
		
			
				|  |  | +        data = request.GET
 | 
	
		
			
				|  |  | +        res = User.objects.values('id', 'username', 'email')
 | 
	
		
			
				|  |  | +        return Response(data={'result': res, 'message': '查询所有用户基础数据'}, status=status.HTTP_200_OK)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def put(self, request):
 | 
	
		
			
				|  |  | +        pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def delete(self, request):
 | 
	
		
			
				|  |  | +        pass
 |