From 91062dda27e06bf29eaa78eff47ba505ad19b7a2 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期三, 18 十二月 2024 16:00:31 +0800
Subject: [PATCH] 机构接口优化

---
 app/service/organization.py |   63 +++++++++++++++++++++++++++++--
 1 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/app/service/organization.py b/app/service/organization.py
index 13b9d08..db4152c 100644
--- a/app/service/organization.py
+++ b/app/service/organization.py
@@ -1,6 +1,7 @@
 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
 
@@ -40,7 +41,7 @@
         dept_model.address = address
         dept_model.phone = phone
         dept_model.leader = leader
-        dept_model.status = status
+        # dept_model.status = status
         dept_model.seq = order_num
         # if parent_id:
         #     dept_model.parent = db.get(OrganizationModel, parent_id)
@@ -75,15 +76,67 @@
 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_json()}
+    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.__eq__(dept_id)).delete()
+        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 False
-    return True
\ No newline at end of file
+        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:
+        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