from django.db import models from django.utils.translation import gettext_lazy as _ from userapp.models import ParadiseUser from .storage import OSSStorage from django.conf import settings from django.utils import timezone # Create your models here. # Initialize OSS storage with cards directory oss_storage = OSSStorage(base_dir='cards') class CardTemplate(models.Model): """Card template model for creating card batches""" CATEGORY_CHOICES = [ ('clothing', '服装'), ('prop', '道具'), ('song', '音乐'), ('dance', '舞蹈'), ('furniture', '家具'), ('decoration', '装饰'), ] RARITY_CHOICES = [ ('common', '普通'), ('uncommon', '不常见'), ('rare', '稀有'), ('epic', '史诗'), ('legendary', '传说'), ('limited', '限定'), ] TYPE_CHOICES = [ ('regular', '常规'), ('seasonal', '季节限定'), ('event', '活动限定'), ] PUBLISH_STATUS_CHOICES = [ ('draft', '草稿'), ('published', '已发布'), ('archived', '已归档'), ] # Template basic information name = models.CharField('名称', max_length=100) category = models.CharField('类别', max_length=20, choices=CATEGORY_CHOICES) description = models.TextField('描述', blank=True) # Type and rarity card_type = models.CharField('类型', max_length=20, choices=TYPE_CHOICES, default='regular') rarity = models.CharField('稀有度', max_length=20, choices=RARITY_CHOICES, default='common') # Display image image = models.ImageField( '展示图片', upload_to='templates/images/', storage=oss_storage, blank=True, null=True ) # 3D model information model_url = models.URLField('模型包链接', max_length=255, blank=True, null=True) model_version = models.CharField('模型版本', max_length=20, blank=True, null=True) # Publishing status status = models.CharField('状态', max_length=20, choices=PUBLISH_STATUS_CHOICES, default='draft') published_at = models.DateTimeField('发布时间', null=True, blank=True) # Commercial information price = models.DecimalField('价格', max_digits=10, decimal_places=2, default=0) # Timestamps created_at = models.DateTimeField('创建时间', auto_now_add=True) updated_at = models.DateTimeField('更新时间', auto_now=True) def publish(self): """发布卡牌模板""" self.status = 'published' self.published_at = timezone.now() self.save() def archive(self): """归档卡牌模板""" self.status = 'archived' self.save() def __str__(self): return f"{self.name} ({self.get_rarity_display()}, {self.get_card_type_display()})" class Meta: verbose_name = '卡片模板' verbose_name_plural = '卡片模板' ordering = ['-created_at'] class Card(models.Model): """Card model for various categories including clothing, props, songs, dances, furniture, and decorations""" STATUS_CHOICES = [ ('active', '可用'), ('used', '已使用'), ('void', '已作废'), ('inactive', '未激活'), ] # Unique identifier for the card, used for NFC scanning unique_id = models.CharField('唯一标识', max_length=100, unique=True) # Card template and basic information template = models.ForeignKey(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='cards', null=True, blank=True) name = models.CharField('名称', max_length=100) category = models.CharField('类别', max_length=20, choices=CardTemplate.CATEGORY_CHOICES) description = models.TextField('描述', blank=True) # Card batch batch = models.ForeignKey('CardBatch', verbose_name='批次', on_delete=models.SET_NULL, null=True, related_name='cards') # Card image - now using OSS storage image = models.ImageField( upload_to='images/', storage=oss_storage, blank=True, null=True, verbose_name=_('Image') ) # Card commercial information price = models.DecimalField('价格', max_digits=10, decimal_places=2, default=0) # Card status status = models.CharField('状态', max_length=20, choices=STATUS_CHOICES, default='inactive') # Card user binding user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='使用者', on_delete=models.SET_NULL, null=True, blank=True) used_at = models.DateTimeField('使用时间', null=True, blank=True) # Production information manufactured = models.BooleanField('已生产', default=False) manufactured_at = models.DateTimeField('生产时间', null=True, blank=True) # Timestamps created_at = models.DateTimeField('创建时间', auto_now_add=True) updated_at = models.DateTimeField('更新时间', auto_now=True) def __str__(self): return f"{self.name} ({self.unique_id})" class Meta: verbose_name = '卡片' verbose_name_plural = '卡片' ordering = ['-created_at'] class CardBatch(models.Model): """Model to track batches of cards generated for production""" STATUS_CHOICES = [ ('draft', '草稿'), ('exported', '已导出'), ('in_production', '生产中'), ('produced', '已生产'), ('published', '已发布'), ] batch_number = models.CharField('批次号', max_length=50, unique=True) template = models.ForeignKey(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='batches', null=True, blank=True) category = models.CharField('类别', max_length=20, choices=CardTemplate.CATEGORY_CHOICES) quantity = models.PositiveIntegerField('数量') description = models.TextField('描述', blank=True) # Status information status = models.CharField('状态', max_length=20, choices=STATUS_CHOICES, default='draft') # Export status exported = models.BooleanField('已导出', default=False) exported_at = models.DateTimeField('导出时间', null=True, blank=True) excel_file = models.FileField('Excel文件', upload_to='card_batches/', null=True, blank=True) # Production status sent_to_production = models.BooleanField('已发送生产', default=False) production_date = models.DateTimeField('生产日期', null=True, blank=True) # Publishing info published = models.BooleanField('已发布', default=False) published_at = models.DateTimeField('发布时间', null=True, blank=True) # Timestamps created_at = models.DateTimeField('创建时间', auto_now_add=True) updated_at = models.DateTimeField('更新时间', auto_now=True) start_id = models.CharField('起始ID', max_length=100, blank=True, null=True) end_id = models.CharField('结束ID', max_length=100, blank=True, null=True) def publish(self): """发布卡牌批次""" self.published = True self.status = 'published' self.published_at = timezone.now() self.save() # 将批次中的所有卡片设置为已激活 self.cards.filter(status='inactive').update(status='active') def mark_produced(self): """标记批次为已生产""" self.sent_to_production = True self.status = 'produced' self.production_date = timezone.now() self.save() # 更新卡片的生产状态 self.cards.update(manufactured=True, manufactured_at=timezone.now()) def contains_card_id(self, card_id): """判断某个卡片ID是否属于该批次""" if not (self.start_id and self.end_id): return False if not card_id.startswith(self.batch_number): return False card_index = card_id.replace(self.batch_number + '-', '') start_index = self.start_id.replace(self.batch_number + '-', '') end_index = self.end_id.replace(self.batch_number + '-', '') return start_index <= card_index <= end_index def __str__(self): template_name = self.template.name if self.template else "未指定模板" return f"{self.batch_number} ({template_name})" class Meta: verbose_name = '卡片批次' verbose_name_plural = '卡片批次' ordering = ['-created_at'] class CardUsageLog(models.Model): """Log to track card usage and status changes""" ACTION_CHOICES = [ ('scan', '扫描'), ('use', '使用'), ('void', '作废'), ('publish', '发布'), ('manufacture', '生产'), ] card = models.ForeignKey(Card, verbose_name='卡片', on_delete=models.CASCADE, related_name='usage_logs') user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='用户', on_delete=models.CASCADE) action = models.CharField('操作', max_length=20, choices=ACTION_CHOICES) details = models.TextField('详细信息', blank=True) old_status = models.CharField('原状态', max_length=20) new_status = models.CharField('新状态', max_length=20) ip_address = models.GenericIPAddressField('IP地址', null=True, blank=True) created_at = models.DateTimeField('创建时间', auto_now_add=True) def __str__(self): return f"{self.card.name} - {self.get_action_display()} by {self.user.username}" class Meta: verbose_name = '使用记录' verbose_name_plural = '使用记录' ordering = ['-created_at'] class ClothingAttributes(models.Model): """服装卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='clothing_attrs') style = models.CharField('风格', max_length=50) size = models.CharField('尺码', max_length=20) color = models.CharField('颜色', max_length=50) season = models.CharField('季节', max_length=20) material = models.CharField('材质', max_length=100) fit_type = models.CharField('版型', max_length=50) care_instructions = models.TextField('保养说明', blank=True) class Meta: verbose_name = '服装属性' verbose_name_plural = '服装属性' class PropAttributes(models.Model): """道具卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='prop_attrs') prop_type = models.CharField('道具类型', max_length=50) material = models.CharField('材质', max_length=100) size = models.CharField('尺寸', max_length=50) weight = models.FloatField('重量(g)', null=True, blank=True) durability = models.CharField('耐久度', max_length=50) usage_instructions = models.TextField('使用说明', blank=True) class Meta: verbose_name = '道具属性' verbose_name_plural = '道具属性' class SongAttributes(models.Model): """音乐卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='song_attrs') genre = models.CharField('曲风', max_length=50) duration = models.DurationField('时长') bpm = models.IntegerField('曲速(BPM)', null=True, blank=True) composer = models.CharField('作曲', max_length=100) lyricist = models.CharField('作词', max_length=100, blank=True) arrangement = models.CharField('编曲', max_length=100, blank=True) audio_file = models.URLField('音频文件', blank=True, max_length=1000) lyrics = models.TextField('歌词', blank=True) class Meta: verbose_name = '音乐属性' verbose_name_plural = '音乐属性' class DanceAttributes(models.Model): """舞蹈卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='dance_attrs') style = models.CharField('舞蹈风格', max_length=50) difficulty = models.CharField('难度等级', max_length=20) duration = models.DurationField('时长') choreographer = models.CharField('编舞', max_length=100) required_space = models.CharField('所需空间', max_length=50) calories_burn = models.IntegerField('消耗卡路里', null=True, blank=True) tutorial_video = models.URLField('教学视频链接', blank=True) class Meta: verbose_name = '舞蹈属性' verbose_name_plural = '舞蹈属性' class FurnitureAttributes(models.Model): """家具卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='furniture_attrs') furniture_type = models.CharField('家具类型', max_length=50) style = models.CharField('风格', max_length=50) material = models.CharField('材质', max_length=100) dimensions = models.CharField('尺寸', max_length=100) weight = models.FloatField('重量(kg)', null=True, blank=True) assembly_required = models.BooleanField('需要组装', default=False) max_weight_capacity = models.FloatField('最大承重(kg)', null=True, blank=True) care_instructions = models.TextField('保养说明', blank=True) class Meta: verbose_name = '家具属性' verbose_name_plural = '家具属性' class DecorationAttributes(models.Model): """装饰卡牌的特殊属性""" template = models.OneToOneField(CardTemplate, verbose_name='卡片模板', on_delete=models.CASCADE, related_name='decoration_attrs') decoration_type = models.CharField('装饰类型', max_length=50) style = models.CharField('风格', max_length=50) material = models.CharField('材质', max_length=100) size = models.CharField('尺寸', max_length=50) placement = models.CharField('摆放位置', max_length=50) indoor_outdoor = models.CharField('室内/室外', max_length=20) installation_required = models.BooleanField('需要安装', default=False) care_instructions = models.TextField('保养说明', blank=True) class Meta: verbose_name = '装饰属性' verbose_name_plural = '装饰属性'