84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
from datetime import datetime
|
|
from app import db
|
|
from app.models.version import Version
|
|
|
|
class Product(db.Model):
|
|
"""产品模型"""
|
|
__tablename__ = 'product'
|
|
|
|
product_id = db.Column(db.String(32), primary_key=True)
|
|
product_name = db.Column(db.String(64), nullable=False)
|
|
description = db.Column(db.Text, nullable=True)
|
|
features = db.Column(db.Text, nullable=True) # 产品功能特性
|
|
image_path = db.Column(db.String(255), nullable=True) # 产品图片路径
|
|
status = db.Column(db.Integer, nullable=False, default=1) # 0=禁用, 1=启用
|
|
create_time = db.Column(db.DateTime, default=datetime.utcnow)
|
|
update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# 关联关系
|
|
versions = db.relationship('Version', backref='product', lazy='dynamic', cascade='all, delete-orphan')
|
|
licenses = db.relationship('License', backref='product', lazy='dynamic', cascade='all, delete-orphan')
|
|
devices = db.relationship('Device', backref='product', lazy='dynamic', cascade='all, delete-orphan')
|
|
tickets = db.relationship('Ticket', backref='product', lazy='dynamic', cascade='all, delete-orphan')
|
|
|
|
def __init__(self, product_id=None, **kwargs):
|
|
super(Product, self).__init__(**kwargs)
|
|
if product_id:
|
|
self.product_id = product_id
|
|
elif not self.product_id:
|
|
# 自动生成产品ID
|
|
import uuid
|
|
self.product_id = f"PROD_{uuid.uuid4().hex[:8]}".upper()
|
|
|
|
def get_stats(self):
|
|
"""获取产品统计信息"""
|
|
# 如果已经有缓存的统计信息(来自批量查询),直接使用
|
|
if hasattr(self, '_cached_stats'):
|
|
return self._cached_stats
|
|
|
|
# 否则执行单个查询(用于单个产品详情等场景)
|
|
total_licenses = self.licenses.count()
|
|
active_licenses = self.licenses.filter_by(status=1).count()
|
|
total_devices = self.devices.filter_by(status=1).count()
|
|
|
|
return {
|
|
'total_licenses': total_licenses,
|
|
'active_licenses': active_licenses,
|
|
'total_devices': total_devices,
|
|
'latest_version': self.get_latest_version()
|
|
}
|
|
|
|
def get_latest_version(self):
|
|
"""获取最新版本"""
|
|
latest_version = self.versions.filter_by(publish_status=1).order_by(
|
|
db.desc(Version.update_time), db.desc(Version.create_time)
|
|
).first()
|
|
return latest_version.version_num if latest_version else None
|
|
|
|
def is_enabled(self):
|
|
"""产品是否启用"""
|
|
return self.status == 1
|
|
|
|
def to_dict(self, include_stats=False):
|
|
"""转换为字典"""
|
|
data = {
|
|
'product_id': self.product_id,
|
|
'product_name': self.product_name,
|
|
'description': self.description,
|
|
'features': self.features, # 添加功能特性字段
|
|
'image_path': self.image_path,
|
|
'image_url': self.image_path, # 兼容前端使用image_url的场景
|
|
'status': self.status,
|
|
'status_name': '启用' if self.status == 1 else '禁用',
|
|
'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
|
|
'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
|
|
}
|
|
|
|
if include_stats:
|
|
stats = self.get_stats()
|
|
data.update(stats)
|
|
|
|
return data
|
|
|
|
def __repr__(self):
|
|
return f'<Product {self.product_name}>' |