|
@@ -3,40 +3,76 @@ import datetime
|
|
|
from sqlalchemy import or_
|
|
|
|
|
|
from extensions.ext_database import db
|
|
|
-from models.account import Account
|
|
|
+from models.account import Account, TenantAccountJoin
|
|
|
from models.dept import Dept
|
|
|
from services.account_service import AccountService
|
|
|
|
|
|
|
|
|
class DeptService:
|
|
|
@staticmethod
|
|
|
- def get_dept_account_list():
|
|
|
+ def get_dept_account_list(self):
|
|
|
dept_list = []
|
|
|
- account_list = []
|
|
|
|
|
|
- dept_results = db.session.query(Dept.dept_id, Dept.dept_name).filter(Dept.status == "active").all()
|
|
|
+ condition = or_(Dept.parent_dept_id == None, Dept.parent_dept_id == "")
|
|
|
+ dept_results = db.session.query(Dept).filter(Dept.status == "active", condition).all()
|
|
|
|
|
|
- account_results = (
|
|
|
- db.session.query(Account.dept_id, Account.id, Account.email).filter(Account.status == "active").all()
|
|
|
- )
|
|
|
for dept_row in dept_results:
|
|
|
- for account_row in account_results:
|
|
|
- if account_row.dept_id == dept_row.dept_id:
|
|
|
- account_list.append({"account_id": account_row.id, "email": account_row.email})
|
|
|
- dept_list.append({"dept_id": dept_row.dept_id, "dept_name": dept_row.dept_name, "accounts": account_list})
|
|
|
+ children_dept_list = []
|
|
|
+ children_depts = (
|
|
|
+ db.session.query(Dept).filter(Dept.status == "active", Dept.parent_dept_id == dept_row.dept_id).all()
|
|
|
+ )
|
|
|
|
|
|
+ for children_dept in children_depts:
|
|
|
+ if self == 0:
|
|
|
+ account_list = DeptService.get_dept_account(children_dept.dept_id)
|
|
|
+ else:
|
|
|
+ account_list = DeptService.get_dept_edit_account(children_dept.dept_id)
|
|
|
+ children_dept_list.append(
|
|
|
+ {
|
|
|
+ "dept_id": children_dept.dept_id,
|
|
|
+ "dept_name": children_dept.dept_name,
|
|
|
+ "parent_dept_id": children_dept.parent_dept_id,
|
|
|
+ "account_list": account_list,
|
|
|
+ }
|
|
|
+ )
|
|
|
+ if self == 0:
|
|
|
+ account_list = DeptService.get_dept_account(dept_row.dept_id)
|
|
|
+ else:
|
|
|
+ account_list = DeptService.get_dept_edit_account(dept_row.dept_id)
|
|
|
+ dept_list.append(
|
|
|
+ {
|
|
|
+ "parent_dept_id": "",
|
|
|
+ "dept_id": dept_row.dept_id,
|
|
|
+ "dept_name": dept_row.dept_name,
|
|
|
+ "account_list": account_list,
|
|
|
+ "children": children_dept_list,
|
|
|
+ }
|
|
|
+ )
|
|
|
return dept_list
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_dept_account(dept_id):
|
|
|
+ def get_dept_account(self):
|
|
|
dept_account = []
|
|
|
account_results = (
|
|
|
- db.session.query(Account.dept_id, Account.id, Account.email)
|
|
|
- .filter(Account.status == "active", Account.dept_id == str(dept_id))
|
|
|
+ db.session.query(Account.dept_id, Account.id, Account.email, Account.name)
|
|
|
+ .filter(Account.status == "active", Account.dept_id == str(self))
|
|
|
.all()
|
|
|
)
|
|
|
for row in account_results:
|
|
|
- dept_account.append({"account_id": row.id, "email": row.email})
|
|
|
+ dept_account.append({"account_id": row.id, "email": row.email, "name": row.name})
|
|
|
+ return dept_account
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_dept_edit_account(self):
|
|
|
+ dept_account = []
|
|
|
+ account_results = (
|
|
|
+ db.session.query(Account.dept_id, Account.id, Account.email, Account.name)
|
|
|
+ .join(TenantAccountJoin, Account.id == TenantAccountJoin.account_id)
|
|
|
+ .filter(Account.status == "active", Account.dept_id == str(self), TenantAccountJoin.role != "normal")
|
|
|
+ .all()
|
|
|
+ )
|
|
|
+ for row in account_results:
|
|
|
+ dept_account.append({"account_id": row.id, "email": row.email, "name": row.name})
|
|
|
return dept_account
|
|
|
|
|
|
@staticmethod
|
|
@@ -46,13 +82,10 @@ class DeptService:
|
|
|
condition = or_(Dept.parent_dept_id == None, Dept.parent_dept_id == "")
|
|
|
dept_results = db.session.query(Dept).filter(Dept.status == "active", condition).all()
|
|
|
|
|
|
- print(str(dept_results))
|
|
|
for dept_row in dept_results:
|
|
|
children_dept_list = []
|
|
|
children_depts = (
|
|
|
- db.session.query(Dept)
|
|
|
- .filter(Dept.status == "active", Dept.parent_dept_id == dept_row.dept_id)
|
|
|
- .all()
|
|
|
+ db.session.query(Dept).filter(Dept.status == "active", Dept.parent_dept_id == dept_row.dept_id).all()
|
|
|
)
|
|
|
for children_dept in children_depts:
|
|
|
children_dept_list.append(
|
|
@@ -118,7 +151,6 @@ class DeptService:
|
|
|
}
|
|
|
)
|
|
|
)
|
|
|
- print(str(sql))
|
|
|
db.session.commit()
|
|
|
|
|
|
@staticmethod
|
|
@@ -138,3 +170,37 @@ class DeptService:
|
|
|
for dept_account in dept_account_list:
|
|
|
account_id = dept_account.get("account_id")
|
|
|
AccountService.update_account_dept("", account_id)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_dept_account_edit_list():
|
|
|
+ dept_list = []
|
|
|
+
|
|
|
+ condition = or_(Dept.parent_dept_id == None, Dept.parent_dept_id == "")
|
|
|
+ dept_results = db.session.query(Dept).filter(Dept.status == "active", condition).all()
|
|
|
+
|
|
|
+ for dept_row in dept_results:
|
|
|
+ children_dept_list = []
|
|
|
+ children_depts = (
|
|
|
+ db.session.query(Dept).filter(Dept.status == "active", Dept.parent_dept_id == dept_row.dept_id).all()
|
|
|
+ )
|
|
|
+ for children_dept in children_depts:
|
|
|
+ account_list = DeptService.get_dept_account(children_dept.dept_id)
|
|
|
+ children_dept_list.append(
|
|
|
+ {
|
|
|
+ "dept_id": children_dept.dept_id,
|
|
|
+ "dept_name": children_dept.dept_name,
|
|
|
+ "parent_dept_id": children_dept.parent_dept_id,
|
|
|
+ "account_list": account_list,
|
|
|
+ }
|
|
|
+ )
|
|
|
+ account_list = DeptService.get_dept_account(dept_row.dept_id.dept_id)
|
|
|
+ dept_list.append(
|
|
|
+ {
|
|
|
+ "parent_dept_id": "",
|
|
|
+ "dept_id": dept_row.dept_id,
|
|
|
+ "dept_name": dept_row.dept_name,
|
|
|
+ "account_list": account_list,
|
|
|
+ "children": children_dept_list,
|
|
|
+ }
|
|
|
+ )
|
|
|
+ return dept_list
|