from tortoise import fields
from tortoise.models import Model


class ValuationAssessment(Model):
    """估值评估模型"""
    
    id = fields.IntField(pk=True, description="主键ID")
    
    # 基础信息
    asset_name = fields.CharField(max_length=200, description="资产名称")
    institution = fields.CharField(max_length=200, description="所属机构")
    industry = fields.CharField(max_length=100, description="所属行业")
    
    # 财务状况
    annual_revenue = fields.CharField(max_length=50, null=True, description="近12个月机构营收/万元")
    rd_investment = fields.CharField(max_length=50, null=True, description="近12个月机构研发投入/万元")
    three_year_income = fields.JSONField(null=True, description="近三年机构收益/万元")
    funding_status = fields.CharField(max_length=500, null=True, description="资产受资助情况")
    
    # 非遗等级与技术
    inheritor_level = fields.CharField(max_length=50, null=True, description="非遗传承人等级")
    inheritor_ages = fields.JSONField(null=True, description="传承人年龄列表")
    inheritor_age_count = fields.JSONField(null=True, description="非遗传承人年龄水平及数量")
    inheritor_certificates = fields.JSONField(null=True, description="非遗传承人等级证书")
    heritage_asset_level = fields.CharField(max_length=50, null=True, description="非遗资产等级")
    patent_application_no = fields.CharField(max_length=100, null=True, description="非遗资产所用专利的申请号")
    patent_remaining_years = fields.CharField(max_length=50, null=True, description="专利剩余年限")
    historical_evidence = fields.JSONField(null=True, description="非遗资产历史证明证据及数量")
    patent_certificates = fields.JSONField(null=True, description="非遗资产所用专利的证书")
    pattern_images = fields.JSONField(null=True, description="非遗纹样图片")
    
    # 非遗应用与推广
    implementation_stage = fields.CharField(max_length=100, null=True, description="非遗资产应用成熟度")
    application_coverage = fields.CharField(max_length=100, null=True, description="非遗资产应用覆盖范围")
    cooperation_depth = fields.CharField(max_length=100, null=True, description="非遗资产跨界合作深度")
    offline_activities = fields.CharField(max_length=50, null=True, description="近12个月线下相关宣讲活动次数")
    platform_accounts = fields.JSONField(null=True, description="线上相关宣传账号信息")
    
    # 非遗资产衍生商品信息
    sales_volume = fields.CharField(max_length=50, null=True, description="该商品近12个月销售量")
    link_views = fields.CharField(max_length=50, null=True, description="该商品近12个月的链接浏览量")
    circulation = fields.CharField(max_length=50, null=True, description="该商品的发行量")
    scarcity_level = fields.CharField(max_length=50, null=True, description="稀缺等级")
    last_market_activity = fields.CharField(max_length=100, null=True, description="该商品最近一次市场活动时间")
    market_activity_time = fields.CharField(max_length=100, null=True, description="市场活动的时间")
    monthly_transaction_amount = fields.CharField(max_length=50, null=True, description="月交易额")
    price_fluctuation = fields.JSONField(null=True, description="该商品近30天价格波动区间")
    price_range = fields.JSONField(null=True, description="资产商品的价格波动率")
    market_price = fields.FloatField(null=True, description="市场价格（单位：万元）")
    
    # 内置API计算字段
    infringement_record = fields.CharField(max_length=100, null=True, description="侵权记录")
    patent_count = fields.CharField(max_length=50, null=True, description="专利使用量")
    esg_value = fields.CharField(max_length=50, null=True, description="ESG关联价值")
    policy_matching = fields.CharField(max_length=50, null=True, description="政策匹配度")
    online_course_views = fields.IntField(null=True, description="线上课程点击量")
    pattern_complexity = fields.CharField(max_length=50, null=True, description="结构复杂度")
    normalized_entropy = fields.CharField(max_length=50, null=True, description="归一化信息熵")
    legal_risk = fields.CharField(max_length=100, null=True, description="法律风险-侵权诉讼历史")
    base_pledge_rate = fields.CharField(max_length=50, null=True, description="基础质押率")
    flow_correction = fields.CharField(max_length=50, null=True, description="流量修正系数")
    
    # 计算结果字段
    model_value_b = fields.FloatField(null=True, description="模型估值B（万元）")
    market_value_c = fields.FloatField(null=True, description="市场估值C（万元）")
    final_value_ab = fields.FloatField(null=True, description="最终估值AB（万元）")
    dynamic_pledge_rate = fields.FloatField(null=True, description="动态质押率")
    calculation_result = fields.JSONField(null=True, description="完整计算结果JSON")
    calculation_input = fields.JSONField(null=True, description="计算输入参数JSON")
    
    # 系统字段
    user = fields.ForeignKeyField("models.AppUser", related_name="valuations", description="提交用户")
    status = fields.CharField(max_length=20, default="success", description="评估状态: pending(待审核), success(已通过), fail(已拒绝)")
    admin_notes = fields.TextField(null=True, description="管理员备注")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    is_active = fields.BooleanField(default=True, description="是否激活")
    
    class Meta:
        table = "valuation_assessments"
        table_description = "估值评估表"
        
    def __str__(self):
        return f"估值评估-{self.asset_name}"