from django.utils import timezone
import datetime
import random
from card.models import (
    CardTemplate, Card, CardBatch, 
    ClothingAttributes, PropAttributes, SongAttributes,
    DanceAttributes, FurnitureAttributes, DecorationAttributes
)
from django.db import transaction

def create_examples():
    """Create example cards for each category"""
    with transaction.atomic():
        # 先清除可能存在的示例数据，避免唯一性冲突
        clear_existing_data()
        
        # 创建多种不同类型的卡牌模板
        clothing_templates = create_clothing_templates()
        prop_templates = create_prop_templates()
        song_templates = create_song_templates()
        dance_templates = create_dance_templates()
        furniture_templates = create_furniture_templates()
        decoration_templates = create_decoration_templates()
        
        print("Successfully created example cards for all categories!")
        return {
            "clothing": clothing_templates,
            "prop": prop_templates,
            "song": song_templates,
            "dance": dance_templates,
            "furniture": furniture_templates,
            "decoration": decoration_templates,
        }

def clear_existing_data():
    """清除已存在的示例数据，避免重复创建导致的错误"""
    print("清除现有示例数据...")
    # 先删除卡片实例，避免外键约束问题
    Card.objects.all().delete()
    # 删除卡片批次
    CardBatch.objects.all().delete()
    # 删除各种卡牌属性
    ClothingAttributes.objects.all().delete()
    PropAttributes.objects.all().delete()
    SongAttributes.objects.all().delete()
    DanceAttributes.objects.all().delete()
    FurnitureAttributes.objects.all().delete()
    DecorationAttributes.objects.all().delete()
    # 最后删除卡牌模板
    CardTemplate.objects.all().delete()
    print("已清除现有示例数据")

def generate_batch_number(prefix, template_id, index=1):
    """生成唯一的批次号，使用时间戳和模板ID+索引确保唯一性"""
    timestamp = timezone.now().strftime('%y%m%d%H%M')
    # 使用模板ID和批次序号作为后缀
    return f"{prefix}-{timestamp}-{template_id}{index:02d}"

def create_clothing_templates():
    """创建服装类卡牌模板"""
    templates = []
    
    # 1. 夏日清凉T恤
    clothing_template = CardTemplate.objects.create(
        name="夏日清凉T恤",
        category="clothing",
        description="轻薄透气的T恤，适合夏季穿着",
        card_type="regular",
        rarity="uncommon",
        price=69.99,
        status="published",
        published_at=timezone.now()
    )
    
    ClothingAttributes.objects.create(
        template=clothing_template,
        style="休闲",
        size="M",
        color="天蓝色",
        season="夏季",
        material="棉质",
        fit_type="标准",
        care_instructions="机洗冷水，低温烘干"
    )
    
    create_batch_and_cards(clothing_template, "CLT", 100, "2023年夏季T恤卡片批次", 5)
    templates.append(clothing_template)
    
    # 2. 星光限定连衣裙
    limited_clothing = CardTemplate.objects.create(
        name="星光限定连衣裙",
        category="clothing",
        description="限量发行的星光元素连衣裙，闪耀夺目",
        card_type="limited",
        rarity="limited",
        price=299.99,
        status="published",
        published_at=timezone.now()
    )
    
    ClothingAttributes.objects.create(
        template=limited_clothing,
        style="华丽",
        size="S/M/L",
        color="星空蓝",
        season="春秋",
        material="聚酯纤维、亮片",
        fit_type="修身",
        care_instructions="仅限干洗，禁止机洗"
    )
    
    create_batch_and_cards(limited_clothing, "CLT-SPECIAL", 20, "特别版星光限定连衣裙卡片批次", 3)
    templates.append(limited_clothing)
    
    # 3. 复古牛仔夹克
    clothing3 = CardTemplate.objects.create(
        name="复古牛仔夹克",
        category="clothing",
        description="经典复古风格牛仔夹克，耐穿百搭",
        card_type="regular",
        rarity="rare",
        price=199.99,
        status="published",
        published_at=timezone.now()
    )
    
    ClothingAttributes.objects.create(
        template=clothing3,
        style="复古",
        size="L",
        color="深蓝色",
        season="春秋",
        material="牛仔布",
        fit_type="宽松",
        care_instructions="冷水手洗，避免用漂白剂"
    )
    
    create_batch_and_cards(clothing3, "CLT", 80, "复古牛仔夹克卡片批次", 5)
    templates.append(clothing3)
    
    # 4. 运动健身背心
    clothing4 = CardTemplate.objects.create(
        name="运动健身背心",
        category="clothing",
        description="弹性透气的健身背心，适合各种运动场景",
        card_type="regular",
        rarity="common",
        price=49.99,
        status="published",
        published_at=timezone.now()
    )
    
    ClothingAttributes.objects.create(
        template=clothing4,
        style="运动",
        size="均码",
        color="黑色",
        season="四季",
        material="聚酯纤维，氨纶",
        fit_type="贴身",
        care_instructions="冷水洗涤，避免烘干机"
    )
    
    create_batch_and_cards(clothing4, "CLT", 150, "运动健身背心卡片批次", 5)
    templates.append(clothing4)
    
    # 5. 冬季保暖羽绒服
    clothing5 = CardTemplate.objects.create(
        name="冬季保暖羽绒服",
        category="clothing",
        description="轻量保暖的高级羽绒服，抵御严寒",
        card_type="seasonal",
        rarity="epic",
        price=499.99,
        status="published",
        published_at=timezone.now()
    )
    
    ClothingAttributes.objects.create(
        template=clothing5,
        style="户外",
        size="XL",
        color="军绿色",
        season="冬季",
        material="防水面料，白鹅绒填充",
        fit_type="适中",
        care_instructions="专业干洗，避免长期挤压"
    )
    
    create_batch_and_cards(clothing5, "CLT", 60, "冬季羽绒服卡片批次", 5)
    templates.append(clothing5)
    
    return templates

def create_prop_templates():
    """创建道具类卡牌模板"""
    templates = []
    
    # 1. 梦幻魔法棒
    prop_template = CardTemplate.objects.create(
        name="梦幻魔法棒",
        category="prop",
        description="华丽的魔法棒道具，可发光",
        card_type="seasonal",
        rarity="rare",
        price=129.99,
        status="published",
        published_at=timezone.now()
    )
    
    PropAttributes.objects.create(
        template=prop_template,
        prop_type="手持道具",
        material="塑料、LED灯",
        size="45cm长",
        weight=150.0,
        durability="高",
        usage_instructions="按下底部按钮开启灯效，长按3秒关闭"
    )
    
    create_batch_and_cards(prop_template, "PROP", 50, "魔法棒道具卡片批次", 5)
    templates.append(prop_template)
    
    # 2. 古风折扇
    legendary_prop = CardTemplate.objects.create(
        name="古风折扇",
        category="prop",
        description="精美古风折扇，采用传统工艺制作",
        card_type="regular",
        rarity="legendary",
        price=399.99,
        status="published",
        published_at=timezone.now()
    )
    
    PropAttributes.objects.create(
        template=legendary_prop,
        prop_type="折扇",
        material="竹、丝绸",
        size="21cm长(展开后)",
        weight=80.0,
        durability="中等",
        usage_instructions="轻柔开合，避免暴力折叠"
    )
    
    create_batch_and_cards(legendary_prop, "PROP-SPECIAL", 20, "特别版古风折扇卡片批次", 3)
    templates.append(legendary_prop)
    
    # 3. 战士护腕
    prop3 = CardTemplate.objects.create(
        name="战士护腕",
        category="prop",
        description="科幻风格的战士护腕，带有炫酷灯效",
        card_type="event",
        rarity="epic",
        price=159.99,
        status="published",
        published_at=timezone.now()
    )
    
    PropAttributes.objects.create(
        template=prop3,
        prop_type="腕部装饰",
        material="合成皮革、LED灯、3D打印件",
        size="可调节，适合大多数尺寸",
        weight=220.0,
        durability="高",
        usage_instructions="长按中央按钮3秒开机，短按切换模式"
    )
    
    create_batch_and_cards(prop3, "PROP", 40, "战士护腕卡片批次", 5)
    templates.append(prop3)
    
    # 4. 复古怀表
    prop4 = CardTemplate.objects.create(
        name="复古怀表",
        category="prop",
        description="精致的复古怀表，走时精准",
        card_type="regular",
        rarity="uncommon",
        price=89.99,
        status="published",
        published_at=timezone.now()
    )
    
    PropAttributes.objects.create(
        template=prop4,
        prop_type="饰品",
        material="合金",
        size="直径4cm",
        weight=60.0,
        durability="中等",
        usage_instructions="需定期上发条，避免水浸和磁场"
    )
    
    create_batch_and_cards(prop4, "PROP", 100, "复古怀表卡片批次", 5)
    templates.append(prop4)
    
    # 5. 魔法书道具
    prop5 = CardTemplate.objects.create(
        name="魔法书道具",
        category="prop",
        description="精美制作的魔法书道具，内页可打开",
        card_type="seasonal",
        rarity="epic",
        price=199.99,
        status="published",
        published_at=timezone.now()
    )
    
    PropAttributes.objects.create(
        template=prop5,
        prop_type="书籍道具",
        material="皮革、特种纸、LED灯",
        size="25cm×18cm×5cm",
        weight=350.0,
        durability="中等",
        usage_instructions="轻拿轻放，避免水浸"
    )
    
    create_batch_and_cards(prop5, "PROP", 30, "魔法书道具卡片批次", 5)
    templates.append(prop5)
    
    return templates

def create_song_templates():
    """创建音乐类卡牌模板"""
    templates = []
    
    # 1. 星空之梦
    song_template = CardTemplate.objects.create(
        name="星空之梦",
        category="song",
        description="舒缓的电子音乐，适合夜晚聆听",
        card_type="regular",
        rarity="epic",
        price=39.99,
        status="published",
        published_at=timezone.now()
    )
    
    SongAttributes.objects.create(
        template=song_template,
        genre="电子",
        duration=datetime.timedelta(minutes=3, seconds=45),
        bpm=120,
        composer="星辰音乐",
        lyricist="月光诗人",
        arrangement="电子工作室",
        audio_file="songs/starry_dream.mp3",
        lyrics="在静谧的夜空下，星光点缀着梦想..."
    )
    
    create_batch_and_cards(song_template, "SONG", 200, "电子音乐卡片批次", 5)
    templates.append(song_template)
    
    # 2. 夏日恋曲
    song2 = CardTemplate.objects.create(
        name="夏日恋曲",
        category="song",
        description="活力四射的夏日流行歌曲",
        card_type="seasonal",
        rarity="rare",
        price=29.99,
        status="published",
        published_at=timezone.now()
    )
    
    SongAttributes.objects.create(
        template=song2,
        genre="流行",
        duration=datetime.timedelta(minutes=4, seconds=12),
        bpm=128,
        composer="阳光作曲",
        lyricist="海滩诗人",
        arrangement="夏日工作室",
        audio_file="songs/summer_love.mp3",
        lyrics="阳光沙滩，微风轻拂，那个夏天我们的故事..."
    )
    
    create_batch_and_cards(song2, "SONG", 150, "夏日流行歌曲卡片批次", 5)
    templates.append(song2)
    
    # 3. 古风水袖
    song3 = CardTemplate.objects.create(
        name="古风水袖",
        category="song",
        description="古典韵味的传统曲风，适合舞蹈伴奏",
        card_type="regular",
        rarity="legendary",
        price=49.99,
        status="published",
        published_at=timezone.now()
    )
    
    SongAttributes.objects.create(
        template=song3,
        genre="古风",
        duration=datetime.timedelta(minutes=5, seconds=30),
        bpm=90,
        composer="古韵大师",
        lyricist="水墨词家",
        arrangement="国乐团",
        audio_file="songs/classical_sleeve_dance.mp3",
        lyrics="水袖轻挥，倾城一笑，花落知多少..."
    )
    
    create_batch_and_cards(song3, "SONG", 100, "古风音乐卡片批次", 5)
    templates.append(song3)
    
    # 4. 电音节拍
    song4 = CardTemplate.objects.create(
        name="电音节拍",
        category="song",
        description="强劲的电子舞曲，适合派对氛围",
        card_type="event",
        rarity="uncommon",
        price=24.99,
        status="published",
        published_at=timezone.now()
    )
    
    SongAttributes.objects.create(
        template=song4,
        genre="EDM",
        duration=datetime.timedelta(minutes=3, seconds=15),
        bpm=140,
        composer="节拍大师",
        lyricist="",
        arrangement="混音工作室",
        audio_file="songs/edm_beats.mp3",
        lyrics=""
    )
    
    create_batch_and_cards(song4, "SONG", 180, "电音舞曲卡片批次", 5)
    templates.append(song4)
    
    # 5. 轻松爵士
    song5 = CardTemplate.objects.create(
        name="轻松爵士",
        category="song",
        description="悠扬的爵士乐，适合咖啡馆氛围",
        card_type="regular",
        rarity="common",
        price=19.99,
        status="published",
        published_at=timezone.now()
    )
    
    SongAttributes.objects.create(
        template=song5,
        genre="爵士",
        duration=datetime.timedelta(minutes=4, seconds=45),
        bpm=95,
        composer="蓝调乐队",
        lyricist="",
        arrangement="爵士工作室",
        audio_file="songs/cafe_jazz.mp3",
        lyrics=""
    )
    
    create_batch_and_cards(song5, "SONG", 200, "爵士音乐卡片批次", 5)
    templates.append(song5)
    
    return templates

def create_dance_templates():
    """创建舞蹈类卡牌模板"""
    templates = []
    
    # 1. 流行街舞基础
    dance_template = CardTemplate.objects.create(
        name="流行街舞基础",
        category="dance",
        description="适合初学者的街舞动作组合",
        card_type="regular",
        rarity="common",
        price=49.99,
        status="published",
        published_at=timezone.now()
    )
    
    DanceAttributes.objects.create(
        template=dance_template,
        style="街舞",
        difficulty="初级",
        duration=datetime.timedelta(minutes=2, seconds=30),
        choreographer="舞动工作室",
        required_space="3平方米",
        calories_burn=150,
        tutorial_video="https://example.com/dance/tutorial/streetdance101"
    )
    
    create_batch_and_cards(dance_template, "DANCE", 150, "街舞基础动作卡片批次", 5)
    templates.append(dance_template)
    
    # 2. 古典芭蕾组合
    dance2 = CardTemplate.objects.create(
        name="古典芭蕾组合",
        category="dance",
        description="优雅的古典芭蕾舞蹈组合",
        card_type="regular",
        rarity="epic",
        price=79.99,
        status="published",
        published_at=timezone.now()
    )
    
    DanceAttributes.objects.create(
        template=dance2,
        style="芭蕾",
        difficulty="高级",
        duration=datetime.timedelta(minutes=3, seconds=15),
        choreographer="芭蕾舞团",
        required_space="4平方米",
        calories_burn=200,
        tutorial_video="https://example.com/dance/tutorial/ballet_classic"
    )
    
    create_batch_and_cards(dance2, "DANCE", 80, "古典芭蕾卡片批次", 5)
    templates.append(dance2)
    
    # 3. 现代爵士舞
    dance3 = CardTemplate.objects.create(
        name="现代爵士舞",
        category="dance",
        description="充满活力的现代爵士舞组合",
        card_type="regular",
        rarity="uncommon",
        price=59.99,
        status="published",
        published_at=timezone.now()
    )
    
    DanceAttributes.objects.create(
        template=dance3,
        style="爵士",
        difficulty="中级",
        duration=datetime.timedelta(minutes=2, seconds=45),
        choreographer="节奏舞团",
        required_space="3平方米",
        calories_burn=180,
        tutorial_video="https://example.com/dance/tutorial/modern_jazz"
    )
    
    create_batch_and_cards(dance3, "DANCE", 120, "爵士舞卡片批次", 5)
    templates.append(dance3)
    
    # 4. 拉丁探戈
    dance4 = CardTemplate.objects.create(
        name="拉丁探戈",
        category="dance",
        description="热情奔放的双人探戈舞",
        card_type="regular",
        rarity="rare",
        price=69.99,
        status="published",
        published_at=timezone.now()
    )
    
    DanceAttributes.objects.create(
        template=dance4,
        style="拉丁",
        difficulty="中高级",
        duration=datetime.timedelta(minutes=3, seconds=0),
        choreographer="拉丁舞团",
        required_space="5平方米",
        calories_burn=220,
        tutorial_video="https://example.com/dance/tutorial/latin_tango"
    )
    
    create_batch_and_cards(dance4, "DANCE", 100, "拉丁舞卡片批次", 5)
    templates.append(dance4)
    
    # 5. 民族舞蹈
    dance5 = CardTemplate.objects.create(
        name="民族舞蹈",
        category="dance",
        description="传统民族特色舞蹈组合",
        card_type="regular",
        rarity="legendary",
        price=89.99,
        status="published",
        published_at=timezone.now()
    )
    
    DanceAttributes.objects.create(
        template=dance5,
        style="民族舞",
        difficulty="中级",
        duration=datetime.timedelta(minutes=4, seconds=0),
        choreographer="民族舞研究所",
        required_space="4平方米",
        calories_burn=190,
        tutorial_video="https://example.com/dance/tutorial/folk_dance"
    )
    
    create_batch_and_cards(dance5, "DANCE", 90, "民族舞卡片批次", 5)
    templates.append(dance5)
    
    return templates

def create_furniture_templates():
    """创建家具类卡牌模板"""
    templates = []
    
    # 1. 北欧简约书桌
    furniture_template = CardTemplate.objects.create(
        name="北欧简约书桌",
        category="furniture",
        description="简约风格的实木书桌，适合学习工作",
        card_type="regular",
        rarity="rare",
        price=599.99,
        status="published",
        published_at=timezone.now()
    )
    
    FurnitureAttributes.objects.create(
        template=furniture_template,
        furniture_type="书桌",
        style="北欧简约",
        material="橡木",
        dimensions="120cm×60cm×75cm",
        weight=25.5,
        assembly_required=True,
        max_weight_capacity=80.0,
        care_instructions="避免阳光直射，定期上蜡保养"
    )
    
    create_batch_and_cards(furniture_template, "FURN", 30, "北欧风格家具卡片批次", 5)
    templates.append(furniture_template)
    
    # 2. 现代休闲沙发
    furniture2 = CardTemplate.objects.create(
        name="现代休闲沙发",
        category="furniture",
        description="舒适现代的三人座沙发，适合客厅使用",
        card_type="regular",
        rarity="epic",
        price=1299.99,
        status="published",
        published_at=timezone.now()
    )
    
    FurnitureAttributes.objects.create(
        template=furniture2,
        furniture_type="沙发",
        style="现代",
        material="布艺，实木框架",
        dimensions="220cm×90cm×85cm",
        weight=65.0,
        assembly_required=False,
        max_weight_capacity=350.0,
        care_instructions="定期吸尘，避免阳光直射，可拆卸布套清洗"
    )
    
    create_batch_and_cards(furniture2, "FURN", 20, "现代沙发卡片批次", 5)
    templates.append(furniture2)
    
    # 3. 复古实木餐桌
    furniture3 = CardTemplate.objects.create(
        name="复古实木餐桌",
        category="furniture",
        description="古朴风格的实木餐桌，可容纳6人用餐",
        card_type="regular",
        rarity="uncommon",
        price=899.99,
        status="published",
        published_at=timezone.now()
    )
    
    FurnitureAttributes.objects.create(
        template=furniture3,
        furniture_type="餐桌",
        style="复古",
        material="松木",
        dimensions="180cm×90cm×76cm",
        weight=45.0,
        assembly_required=True,
        max_weight_capacity=120.0,
        care_instructions="避免水渍，定期打蜡保养"
    )
    
    create_batch_and_cards(furniture3, "FURN", 25, "复古餐桌卡片批次", 5)
    templates.append(furniture3)
    
    # 4. 工业风书架
    furniture4 = CardTemplate.objects.create(
        name="工业风书架",
        category="furniture",
        description="金属框架与实木搁板结合的工业风格书架",
        card_type="regular",
        rarity="common",
        price=399.99,
        status="published",
        published_at=timezone.now()
    )
    
    FurnitureAttributes.objects.create(
        template=furniture4,
        furniture_type="书架",
        style="工业风",
        material="金属，实木",
        dimensions="120cm×40cm×180cm",
        weight=30.0,
        assembly_required=True,
        max_weight_capacity=150.0,
        care_instructions="注意均匀分布重物，避免潮湿环境"
    )
    
    create_batch_and_cards(furniture4, "FURN", 40, "工业风书架卡片批次", 5)
    templates.append(furniture4)
    
    # 5. 日式榻榻米床
    furniture5 = CardTemplate.objects.create(
        name="日式榻榻米床",
        category="furniture",
        description="日式风格的低矮实木床，含榻榻米垫",
        card_type="regular",
        rarity="legendary",
        price=1599.99,
        status="published",
        published_at=timezone.now()
    )
    
    FurnitureAttributes.objects.create(
        template=furniture5,
        furniture_type="床",
        style="日式",
        material="柏木，天然草垫",
        dimensions="200cm×180cm×35cm",
        weight=70.0,
        assembly_required=True,
        max_weight_capacity=300.0,
        care_instructions="保持干燥，定期通风，草垫需3-5年更换一次"
    )
    
    create_batch_and_cards(furniture5, "FURN", 15, "日式榻榻米卡片批次", 5)
    templates.append(furniture5)
    
    return templates

def create_decoration_templates():
    """创建装饰类卡牌模板"""
    templates = []
    
    # 1. 梦幻星空投影灯
    decoration_template = CardTemplate.objects.create(
        name="梦幻星空投影灯",
        category="decoration",
        description="可投影星空的LED灯，营造浪漫氛围",
        card_type="seasonal",
        rarity="epic",
        price=159.99,
        status="published",
        published_at=timezone.now()
    )
    
    DecorationAttributes.objects.create(
        template=decoration_template,
        decoration_type="投影灯",
        style="梦幻",
        material="塑料、LED",
        size="15cm×15cm×20cm",
        placement="桌面/床头",
        indoor_outdoor="室内",
        installation_required=False,
        care_instructions="请用干布擦拭，避免进水"
    )
    
    create_batch_and_cards(decoration_template, "DECO", 80, "投影灯装饰卡片批次", 5)
    templates.append(decoration_template)
    
    # 2. 北欧风格挂画
    decoration2 = CardTemplate.objects.create(
        name="北欧风格挂画",
        category="decoration",
        description="简约北欧风格装饰画，适合客厅卧室",
        card_type="regular",
        rarity="uncommon",
        price=89.99,
        status="published",
        published_at=timezone.now()
    )
    
    DecorationAttributes.objects.create(
        template=decoration2,
        decoration_type="壁画",
        style="北欧简约",
        material="帆布，实木框",
        size="60cm×45cm",
        placement="墙面",
        indoor_outdoor="室内",
        installation_required=True,
        care_instructions="避免阳光直射，定期除尘"
    )
    
    create_batch_and_cards(decoration2, "DECO", 100, "北欧挂画卡片批次", 5)
    templates.append(decoration2)
    
    # 3. 复古台灯
    decoration3 = CardTemplate.objects.create(
        name="复古铜艺台灯",
        category="decoration",
        description="复古风格铜制台灯，温暖柔和的灯光",
        card_type="regular",
        rarity="rare",
        price=129.99,
        status="published",
        published_at=timezone.now()
    )
    
    DecorationAttributes.objects.create(
        template=decoration3,
        decoration_type="台灯",
        style="复古",
        material="铜，布艺灯罩",
        size="25cm×25cm×45cm",
        placement="桌面",
        indoor_outdoor="室内",
        installation_required=False,
        care_instructions="定期擦拭，避免潮湿环境"
    )
    
    create_batch_and_cards(decoration3, "DECO", 60, "复古台灯卡片批次", 5)
    templates.append(decoration3)
    
    # 4. 多肉植物组合
    decoration4 = CardTemplate.objects.create(
        name="多肉植物组合",
        category="decoration",
        description="精选多肉植物组合，带精美陶瓷花盆",
        card_type="regular",
        rarity="common",
        price=59.99,
        status="published",
        published_at=timezone.now()
    )
    
    DecorationAttributes.objects.create(
        template=decoration4,
        decoration_type="植物",
        style="自然",
        material="陶瓷，植物",
        size="15cm×15cm×10cm",
        placement="窗台，桌面",
        indoor_outdoor="室内",
        installation_required=False,
        care_instructions="避免阳光直射，一周浇水一次，冬季减少浇水"
    )
    
    create_batch_and_cards(decoration4, "DECO", 120, "多肉植物卡片批次", 5)
    templates.append(decoration4)
    
    # 5. 创意壁挂置物架
    decoration5 = CardTemplate.objects.create(
        name="创意壁挂置物架",
        category="decoration",
        description="几何造型的墙面置物架，既实用又装饰",
        card_type="regular",
        rarity="legendary",
        price=199.99,
        status="published",
        published_at=timezone.now()
    )
    
    DecorationAttributes.objects.create(
        template=decoration5,
        decoration_type="置物架",
        style="现代创意",
        material="金属，实木",
        size="60cm×20cm×60cm",
        placement="墙面",
        indoor_outdoor="室内",
        installation_required=True,
        care_instructions="注意承重，均匀分布物品重量"
    )
    
    create_batch_and_cards(decoration5, "DECO", 40, "壁挂置物架卡片批次", 5)
    templates.append(decoration5)
    
    return templates

def create_batch_and_cards(template, prefix, quantity, description, index=1):
    """创建批次和卡片实例"""
    batch_number = generate_batch_number(prefix, template.id, index)
    
    batch = CardBatch.objects.create(
        batch_number=batch_number,
        template=template,
        category=template.category,
        quantity=quantity,
        description=description,
        status="draft",
        published=False,
        published_at=None
    )
    
    # 根据需要创建实际的卡片样本(默认创建少量样本，不是全部quantity)
    sample_count = min(5, quantity)  # 最多创建5张样本卡片
    
    card_ids = []
    # 创建样本卡片
    for i in range(1, sample_count + 1):
        unique_id = f"{batch_number}-{i:03d}"
        Card.objects.create(
            unique_id=unique_id,
            template=template,
            name=template.name,
            category=template.category,
            batch=batch,
            price=template.price,
            status="inactive"  # 默认为未激活状态
        )
        card_ids.append(unique_id)
    
    # 设置批次的起始ID和结束ID
    if card_ids:
        # 起始ID是第一张卡的ID
        batch.start_id = f"{batch_number}-001"
        # 结束ID是根据quantity计算的最后一张卡的ID
        batch.end_id = f"{batch_number}-{quantity:03d}"
        batch.save(update_fields=["start_id", "end_id"])
    
    return batch 