import os import pyodbc from flask import current_app class OASQLServerService: @staticmethod def _pick_driver(): drivers = pyodbc.drivers() preferred = [ "ODBC Driver 18 for SQL Server", "ODBC Driver 17 for SQL Server", "SQL Server", ] for name in preferred: if name in drivers: return name if not drivers: raise RuntimeError("未找到可用的 SQL Server ODBC 驱动") return drivers[-1] @classmethod def _connect(cls): driver = os.getenv("OA_SQLSERVER_DRIVER", "").strip() or cls._pick_driver() server = os.getenv("OA_SQLSERVER_HOST", "192.168.100.225").strip() port = os.getenv("OA_SQLSERVER_PORT", "1433").strip() database = os.getenv("OA_SQLSERVER_DATABASE", "ecology").strip() username = os.getenv("OA_SQLSERVER_USERNAME", "AI_Knowledge_Base_OA").strip() password = os.getenv("OA_SQLSERVER_PASSWORD", "hoo-thai@123_AI_OA").strip() encrypt = os.getenv("OA_SQLSERVER_ENCRYPT", "no").strip().lower() trust_cert = os.getenv("OA_SQLSERVER_TRUST_CERT", "yes").strip().lower() timeout = int(os.getenv("OA_SQLSERVER_TIMEOUT", "10")) conn_str = ( f"DRIVER={{{driver}}};" f"SERVER={server},{port};" f"DATABASE={database};" f"UID={username};" f"PWD={password};" f"Encrypt={encrypt};" f"TrustServerCertificate={trust_cert};" ) return pyodbc.connect(conn_str, timeout=timeout) @classmethod def search_employees(cls, keyword: str, page: int, page_size: int): connection = cls._connect() cursor = connection.cursor() try: offset = (page - 1) * page_size params = [] # Count total count_sql = "SELECT COUNT(1) FROM userInfo_view WHERE 1=1" if keyword: count_sql += " AND (workcode LIKE ? OR lastname LIKE ?)" like_kw = f"%{keyword}%" params.extend([like_kw, like_kw]) cursor.execute(count_sql, params) total = cursor.fetchone()[0] # Fetch page # Compatible with older SQL Server versions (2008/2012) query_sql = """ SELECT id, workcode, lastname FROM ( SELECT id, workcode, lastname, ROW_NUMBER() OVER(ORDER BY id) as rn FROM userInfo_view WHERE 1=1 """ if keyword: query_sql += " AND (workcode LIKE ? OR lastname LIKE ?)" query_sql += """ ) as T WHERE rn > ? AND rn <= ? """ page_params = params + [offset, offset + page_size] cursor.execute(query_sql, page_params) items = [] for row in cursor.fetchall(): items.append({ "uid": str(row.id), "employeeId": str(row.workcode), "name": str(row.lastname) }) return { "items": items, "total": total, "page": page, "page_size": page_size } finally: try: cursor.close() finally: connection.close() @classmethod def get_employee_by_name_and_workcode(cls, real_name: str, employee_id: str): connection = cls._connect() cursor = connection.cursor() try: normalized_name = (real_name or "").strip() normalized_employee_id = (employee_id or "").strip() if not normalized_name or not normalized_employee_id: raise RuntimeError("缺少姓名或工号,无法查询 OA 用户") query_sql = """ SELECT TOP 1 id, workcode, lastname FROM userInfo_view WHERE workcode = ? AND lastname = ? ORDER BY id """ cursor.execute(query_sql, [normalized_employee_id, normalized_name]) row = cursor.fetchone() if not row: return None return { "uid": str(row.id), "employeeId": str(row.workcode), "name": str(row.lastname), } finally: try: cursor.close() finally: connection.close()