From ef5c7cb5aa828890148148fe53fc85108f3e79c7 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期五, 10 一月 2025 18:09:57 +0800
Subject: [PATCH] 知识库选择返回自己创建的

---
 app/service/organization.py |  146 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 0 deletions(-)

diff --git a/app/service/organization.py b/app/service/organization.py
index e69de29..608cbf2 100644
--- a/app/service/organization.py
+++ b/app/service/organization.py
@@ -0,0 +1,146 @@
+import uuid
+
+from Log import logger
+from app.config.const import DEPT_STATUS_DELETE, DEPT_STATUS_ON, DEPT_STATUS_OFF
+from app.models import OrganizationModel
+from app.models.role_model import RoleModel
+
+
+
+async def get_organization_list(db, deptName: str):
+    query = db.query(OrganizationModel).filter(OrganizationModel.organization_id == None)
+    if deptName:
+        query = query.filter(OrganizationModel.name.like('%{}%'.format(deptName)))
+    depts = query.order_by(OrganizationModel.seq.desc()).all()
+    return {"total": query.count(), "rows":  [dept.to_json() for dept in depts]}
+
+
+async def create_dept(db, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
+    try:
+        dept_model = OrganizationModel(id=str(uuid.uuid4()),name=dept_name, address=address,leader=leader,phone=phone,seq=order_num,status=str(status))
+        if parent_id:
+            dept_model.parent = db.get(OrganizationModel, parent_id)
+        if roles:
+            dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
+        # if groups:
+        #     dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
+        db.add(dept_model)
+        db.commit()
+        db.refresh(dept_model)
+    except Exception as e:
+        logger.error(e)
+        db.rollback()
+        return False
+    return True
+
+
+async def edit_dept_data(db, dept_id, dept_name, leader, phone, address, status, order_num, roles, groups,parent_id):
+    try:
+        dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
+        dept_model.name = dept_name
+        dept_model.address = address
+        dept_model.phone = phone
+        dept_model.leader = leader
+        # dept_model.status = status
+        dept_model.seq = order_num
+        # if parent_id:
+        #     dept_model.parent = db.get(OrganizationModel, parent_id)
+        if roles:
+            dept_model.roles = [db.get(RoleModel, roleId) for roleId in roles]
+        # if groups:
+        #     dept_model.groups = [db.get(GroupModel, groupId) for groupId in groups]
+        # db.add(dept_model)
+        db.commit()
+        db.refresh(dept_model)
+    except Exception as e:
+        logger.error(e)
+        db.rollback()
+        return False
+    return True
+
+
+async def edit_dept_parent(db, dept_id, parent_id, order_num):
+    try:
+        dept_model = db.query(OrganizationModel).filter(OrganizationModel.id == dept_id).first()
+        dept_model.parent = db.get(OrganizationModel, parent_id)
+        dept_model.seq = order_num
+        db.commit()
+        db.refresh(dept_model)
+    except Exception as e:
+        logger.error(e)
+        db.rollback()
+        return False
+    return True
+
+
+async def get_organization_info(db, dept_id: str):
+    dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
+
+    return {"total": 0, "data":  dept.to_dict()}
+
+
+async def delete_organization_info(db, dept_id: str):
+    delete_list = []
+    dept_list = []
+    next_dept_list = []
+    base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
+    dept_list.append(base_dept)
+    while dept_list:
+        for dept in dept_list:
+            delete_list.append(dept.id)
+            if dept.roles:
+                return "閮ㄩ棬锛歿}宸查厤缃鑹诧紝涓嶅厑璁稿垹闄わ紒".format(dept.name)
+            for child_dept in dept.children:
+                next_dept_list.append(child_dept)
+
+        dept_list = next_dept_list
+        next_dept_list = []
+
+
+    try:
+        db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_DELETE})
+        db.commit()
+    except Exception as e:
+        logger.error(e)
+        db.rollback()
+        return '鏈嶅姟寮傚父 锛�'
+    return ""
+
+
+async def edit_organization_status(db, dept_id: str, status:str):
+    delete_list = []
+    dept_list = []
+    next_dept_list = []
+    base_dept = db.query(OrganizationModel).filter(OrganizationModel.id.__eq__(dept_id)).first()
+    if status == DEPT_STATUS_ON:
+        print(11)
+        print(base_dept.name)
+        if base_dept.parent and base_dept.parent.status != DEPT_STATUS_ON:
+            return '涓婄骇鑺傜偣鐘舵�佸紓甯革紒'
+        try:
+            base_dept.status=status
+            db.commit()
+        except Exception as e:
+            logger.error(e)
+            db.rollback()
+            return '鏈嶅姟寮傚父 锛�'
+    else:
+        dept_list.append(base_dept)
+        while dept_list:
+            for dept in dept_list:
+                delete_list.append(dept.id)
+                if dept.roles:
+                    return "閮ㄩ棬锛歿}宸查厤缃鑹诧紝涓嶅厑璁革紒".format(dept.name)
+                for child_dept in dept.children:
+                    next_dept_list.append(child_dept)
+
+            dept_list = next_dept_list
+            next_dept_list = []
+        try:
+            db.query(OrganizationModel).filter(OrganizationModel.id.in_(delete_list)).update({"status": DEPT_STATUS_OFF})
+            db.commit()
+        except Exception as e:
+            logger.error(e)
+            db.rollback()
+            return '鏈嶅姟寮傚父 锛�'
+    return ""
\ No newline at end of file

--
Gitblit v1.8.0