Files
aiform_dev/backend/app/services/policy_service.py
AI Assistant 3b08916c22 fix: Исправлены MySQL креды - подключение к локальной БД
Проблема: Backend пытался подключиться к удаленной БД turistpr_erv
Решение: Обновлены креды на локальную БД ci20465_erv

Изменения:
- MySQL Host: localhost (было: 141.8.194.131)
- MySQL DB: ci20465_erv (было: turistpr_erv)
- MySQL User: ci20465_erv (было: root)
- MySQL Password: c7vOXbmG (было: пустой)
- MySQL Table: lexrpiority (было: erv_vouchers в коде)

Результат:
 MySQL Policy DB подключена успешно
 API /api/v1/policy/check работает
 Валидация полисов работает (33963 полисов в БД)

Тестирование:
- E1000-302372730 → found: true 
- E9999-999999999 → found: false 
2025-10-24 21:02:20 +03:00

81 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Policy Service - Проверка полисов в MySQL БД
"""
import aiomysql
from typing import Optional, Dict, Any
from ..config import settings
import logging
logger = logging.getLogger(__name__)
class PolicyService:
"""Сервис для проверки полисов ERV"""
def __init__(self):
self.pool: Optional[aiomysql.Pool] = None
async def connect(self):
"""Подключение к MySQL БД с полисами"""
try:
# Используем credentials из .env через settings
self.pool = await aiomysql.create_pool(
host=settings.mysql_host,
port=settings.mysql_port,
user=settings.mysql_user,
password=settings.mysql_password,
db=settings.mysql_db,
autocommit=True,
minsize=1,
maxsize=5
)
logger.info(f"✅ MySQL Policy DB connected: {settings.mysql_host}/{settings.mysql_db}")
except Exception as e:
logger.error(f"❌ MySQL Policy DB connection error: {e}")
raise
async def check_policy(self, voucher: str) -> Optional[Dict[str, Any]]:
"""
Проверить полис в БД
Args:
voucher: Номер полиса вида E1000-302538524
Returns:
Dict с данными полиса или None если не найден
"""
if not self.pool:
await self.connect()
try:
async with self.pool.acquire() as conn:
async with conn.cursor(aiomysql.DictCursor) as cursor:
# Запрос поиска по номеру полиса в таблице lexrpiority
query = "SELECT * FROM lexrpiority WHERE voucher = %s LIMIT 1"
await cursor.execute(query, [voucher])
result = await cursor.fetchone()
if result:
logger.info(f"✅ Policy found: {voucher}")
return dict(result)
else:
logger.warning(f"⚠️ Policy not found: {voucher}")
return None
except Exception as e:
logger.error(f"Error checking policy: {e}")
return None
async def close(self):
"""Закрыть пул подключений"""
if self.pool:
self.pool.close()
await self.pool.wait_closed()
logger.info("MySQL Policy DB pool closed")
# Глобальный экземпляр
policy_service = PolicyService()