|
@@ -0,0 +1,126 @@
|
|
|
+import sys,os
|
|
|
+current_path = os.getcwd()
|
|
|
+sys.path.append(current_path)
|
|
|
+
|
|
|
+from math import ceil
|
|
|
+from sqlalchemy import text
|
|
|
+from typing import Optional
|
|
|
+from pydantic import BaseModel
|
|
|
+from db.database import get_db
|
|
|
+from sqlalchemy.orm import Session
|
|
|
+from fastapi import APIRouter, Depends
|
|
|
+from agent.libs.response import resp_200
|
|
|
+from agent.libs.sys import SysUserRoleOrganBusiness
|
|
|
+from agent.libs.auth import verify_session_id, SessionValues
|
|
|
+from agent.models.web.response import StandardResponse,FAILED,SUCCESS
|
|
|
+
|
|
|
+class SysUserRoleOrganRequest(BaseModel):
|
|
|
+ id: Optional[int] = None
|
|
|
+ user_id: Optional[int] = None
|
|
|
+ role_id: Optional[int] = None
|
|
|
+ organ_id: Optional[int] = None
|
|
|
+ data_type: Optional[int] = None
|
|
|
+ user_name: Optional[str] = None
|
|
|
+ role_name: Optional[str] = None
|
|
|
+ page: Optional[int] = None
|
|
|
+ page_size: Optional[int] = None
|
|
|
+
|
|
|
+router = APIRouter(prefix="/userRoleOrgan", tags=["用户权限管理接口"])
|
|
|
+
|
|
|
+@router.post("/data-list")
|
|
|
+def dataList(request: SysUserRoleOrganRequest, db: Session = Depends(get_db)):
|
|
|
+ if request.page is None :
|
|
|
+ return StandardResponse(code=FAILED, message="页码不能为空")
|
|
|
+ if request.page_size is None :
|
|
|
+ return StandardResponse(code=FAILED, message="一页记录数不能为空")
|
|
|
+
|
|
|
+ COUNT_SQL = f"select count(1) from sys_user_role_organ a,users b,roles c,sys_organ d where a.user_id =b.id and a.role_id=c.id and a.organ_id=d.id"
|
|
|
+ DATA_SQL = f"select a.*,b.username user_name,c.name role_name,d.name organ_name from sys_user_role_organ a,users b,roles c,sys_organ d where a.user_id =b.id and a.role_id=c.id and a.organ_id=d.id"
|
|
|
+
|
|
|
+ if request.role_name is not None: #模糊查询角色名称
|
|
|
+ COUNT_SQL = COUNT_SQL + f" and c.name like '%" + request.role_name + f"%'"
|
|
|
+ DATA_SQL = DATA_SQL + f" and c.name like '%" + request.role_name + f"%'"
|
|
|
+ if request.user_name is not None: #模糊查询用户名称
|
|
|
+ COUNT_SQL = COUNT_SQL + f" and b.username like '%" + request.user_name + f"%'"
|
|
|
+ DATA_SQL = DATA_SQL + f" and b.username like '%" + request.user_name + f"%'"
|
|
|
+ if request.user_id is not None: #查询用户编号
|
|
|
+ COUNT_SQL = COUNT_SQL + f" and a.user_id = " + str(request.user_id)
|
|
|
+ DATA_SQL = DATA_SQL + f" and a.user_id = " + str(request.user_id)
|
|
|
+ if request.organ_id is not None: #模糊机构名称
|
|
|
+ COUNT_SQL = COUNT_SQL + f" and a.organ_id =" + str(request.organ_id)
|
|
|
+ DATA_SQL = DATA_SQL + f" and a.organ_id =" + str(request.organ_id)
|
|
|
+
|
|
|
+ result = db.execute(text(COUNT_SQL))
|
|
|
+ count = result.scalar()
|
|
|
+ total_page = ceil(count / request.page_size)
|
|
|
+ start = 1
|
|
|
+ if request.page <= total_page:
|
|
|
+ start = (request.page - 1) * request.page_size
|
|
|
+
|
|
|
+ DATA_SQL = DATA_SQL + f" order by id limit "+ str(request.page_size) + f" OFFSET " + str(start)
|
|
|
+
|
|
|
+ results = db.execute(text(DATA_SQL))
|
|
|
+ datList = []
|
|
|
+ for row in results:
|
|
|
+ datList.append({
|
|
|
+ "id":row.id,
|
|
|
+ 'user_id':row.user_id,
|
|
|
+ 'role_id':row.role_id,
|
|
|
+ 'organ_id':row.organ_id,
|
|
|
+ 'data_type':row.data_type,
|
|
|
+ 'last_use_time':row.last_use_time,
|
|
|
+ 'create_time':row.create_time,
|
|
|
+ 'create_by':row.create_by,
|
|
|
+ 'update_time':row.update_time,
|
|
|
+ 'update_by':row.update_by,
|
|
|
+ 'user_name':row.user_name,
|
|
|
+ 'role_name':row.role_name,
|
|
|
+ 'organ_name':row.organ_name
|
|
|
+ })
|
|
|
+
|
|
|
+ return resp_200(data={"total": count, "pages": request.page, "size": request.page_size, "records": datList})
|
|
|
+
|
|
|
+@router.post("/insert", response_model=StandardResponse)
|
|
|
+def insertData(request: SysUserRoleOrganRequest, db: Session = Depends(get_db), sess:SessionValues = Depends(verify_session_id)):
|
|
|
+ biz = SysUserRoleOrganBusiness(db)
|
|
|
+ if request.user_id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="用户编号不能为空")
|
|
|
+ if request.role_id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="角色编号不能为空")
|
|
|
+ if request.organ_id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="机构编号不能为空")
|
|
|
+ if request.data_type is None:
|
|
|
+ return StandardResponse(code=FAILED, message="数据权限类型不能为空")
|
|
|
+
|
|
|
+ data = biz.get_SURO(request.user_id,request.organ_id)
|
|
|
+ if data is not None:
|
|
|
+ return StandardResponse(code=FAILED, message="用户角色机构已经存在")
|
|
|
+
|
|
|
+ data = biz.insert_data(request.user_id, request.role_id,request.organ_id,request.data_type,sess.full_name)
|
|
|
+ if data is None:
|
|
|
+ return StandardResponse(code=FAILED, message="创建用户角色机构失败")
|
|
|
+ return StandardResponse(code=SUCCESS, message="创建用户角色机构成功")
|
|
|
+
|
|
|
+@router.post("/update", response_model=StandardResponse)
|
|
|
+def updateData(request: SysUserRoleOrganRequest, db: Session = Depends(get_db), sess:SessionValues = Depends(verify_session_id)):
|
|
|
+ biz = SysUserRoleOrganBusiness(db)
|
|
|
+
|
|
|
+ if request.id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="编号不能为空")
|
|
|
+ if request.user_id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="用户编号不能为空")
|
|
|
+ if request.role_id is None:
|
|
|
+ return StandardResponse(code=FAILED, message="角色编号不能为空")
|
|
|
+ if request.data_type is None:
|
|
|
+ return StandardResponse(code=FAILED, message="数据权限类型不能为空")
|
|
|
+
|
|
|
+ biz.update_data(request.id,request.user_id, request.role_id,request.organ_id,request.data_type,sess.full_name)
|
|
|
+ return StandardResponse(code=SUCCESS, message="更新用户角色机构成功")
|
|
|
+
|
|
|
+@router.post("/delete/{id}", response_model=StandardResponse)
|
|
|
+def deleteData(id:int, db: Session = Depends(get_db)):
|
|
|
+ biz = SysUserRoleOrganBusiness(db)
|
|
|
+ biz.delete_data(id)
|
|
|
+ return StandardResponse(code=SUCCESS, message="删除用户角色机构成功")
|
|
|
+
|
|
|
+sys_user_role_organ = router
|