from django.db import models
from django.conf import settings

from dingdan.models import Dingdan


# ==================== 板块表 ====================
class Bankuai(models.Model):
    bankuai_id = models.AutoField(primary_key=True, verbose_name='板块ID')
    mingcheng = models.CharField(max_length=50, unique=True, db_index=True, verbose_name='板块名称')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'bankuai'
        verbose_name = '板块'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.mingcheng


# ==================== 打手订单表现记录 ====================
class DashouBiaoxian(models.Model):
    dingdan = models.ForeignKey('dingdan.Dingdan', to_field='dingdan_id', on_delete=models.CASCADE, related_name='biaoxian', verbose_name='关联订单', db_index=True)

    dashou_id = models.CharField(max_length=32, db_index=True, verbose_name='打手ID')
    bankuai = models.ForeignKey(Bankuai, on_delete=models.PROTECT, to_field='bankuai_id', verbose_name='所属板块', db_index=True,null=True)
    shangxian_sudu = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name='上线速度评价(1-5)')
    fuwu_taidu = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name='服务态度评价(1-5)')
    fanche_cishu = models.PositiveIntegerField(null=True, blank=True, verbose_name='翻车次数')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    jiedan_time = models.DateTimeField(null=True, blank=True, verbose_name='接单时间')
    tijiao_time = models.DateTimeField(null=True, blank=True, verbose_name='提交时间')

    class Meta:
        db_table = 'dashou_biaoxian'
        verbose_name = '打手订单表现'
        verbose_name_plural = verbose_name
        indexes = [models.Index(fields=['dashou_id', 'bankuai', 'create_time'])]


# ==================== 经验变动记录 ====================
class JingyanJilu(models.Model):
    yonghu_id = models.CharField(max_length=7, db_index=True, verbose_name='用户ID')
    yonghu_leixing = models.CharField(max_length=10, choices=[('dashou','打手'),('boss','老板'),('shangjia','商家'),('guanshi','管事'),('zuzhang','组长')], verbose_name='用户类型')
    bankuai = models.ForeignKey(Bankuai, on_delete=models.PROTECT, to_field='bankuai_id', verbose_name='所属板块', db_index=True)
    sudu_jingyan = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='上线速度经验')
    shili_jingyan = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='打手实力经验')
    taidu_jingyan = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='服务态度经验')
    zonghe_jingyan = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name='综合经验')
    dingdan_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='关联订单ID')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')


    class Meta:
        db_table = 'jingyan_jilu'
        verbose_name = '经验变动记录'
        verbose_name_plural = verbose_name
        indexes = [models.Index(fields=['yonghu_id', 'bankuai', 'create_time'])]


# ==================== 称号主表 ====================
class Chenghao(models.Model):
    mingcheng = models.CharField(max_length=50, unique=True, verbose_name='称号名称')
    leixing = models.CharField(max_length=10, choices=[('dashou','打手'),('boss','老板'),('shangjia','商家'),('guanshi','管事'),('zuzhang','组长')], verbose_name='称号类型')
    bankuai = models.ForeignKey(Bankuai, on_delete=models.PROTECT, to_field='bankuai_id', null=True, blank=True, verbose_name='所属板块(空=全板块通用)')
    tubiao_url = models.CharField(max_length=200, null=True, blank=True, verbose_name='称号图标相对URL')
    texiao_miaoshu = models.TextField(null=True, blank=True, verbose_name='特效描述(JSON)')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    kaohe_guize = models.TextField(null=True, blank=True, verbose_name='考核规则')  # 新增
    kaioi_jinpai = models.BooleanField(default=False, verbose_name='是否同时开启金牌')

    class Meta:
        db_table = 'chenghao'
        verbose_name = '称号'
        verbose_name_plural = verbose_name
        indexes = [models.Index(fields=['leixing', 'bankuai'])]


# ==================== 用户 - 称号关联 ====================
class YonghuChenghao(models.Model):
    yonghu = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='chenghao_list', verbose_name='用户', db_index=True)
    chenghao = models.ForeignKey(Chenghao, on_delete=models.CASCADE, verbose_name='称号')
    huode_time = models.DateTimeField(auto_now_add=True, verbose_name='获得时间')

    class Meta:
        db_table = 'yonghu_chenghao'
        verbose_name = '用户称号关联'
        verbose_name_plural = verbose_name
        unique_together = ('yonghu', 'chenghao')
        # 修复：只索引本表字段
        indexes = [models.Index(fields=['yonghu', 'chenghao'])]





class DingdanBiaoqian(models.Model):
    """订单需求标签关联表"""
    dingdan = models.ForeignKey(
        Dingdan,
        to_field='dingdan_id',  # 关联到字符串字段 dingdan_id
        on_delete=models.CASCADE,
        verbose_name='订单',
        db_index=True
    )
    chenghao = models.ForeignKey('dengji.Chenghao', on_delete=models.CASCADE, verbose_name='标签')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'dingdan_biaoqian'
        verbose_name = '订单需求标签'
        verbose_name_plural = verbose_name
        unique_together = ('dingdan', 'chenghao')
        indexes = [models.Index(fields=['dingdan'])]





class ShenheJilu(models.Model):
    """审核记录表"""
    shenqingren_id = models.CharField(max_length=7, verbose_name='申请人用户ID')
    shenheguan_id = models.CharField(max_length=7, null = True,verbose_name='审核官用户ID')
    zhuangtai = models.IntegerField(choices=[(0, '审核中'), (1, '通过'), (2, '未通过'), (3, '待审核')], default=0, verbose_name='审核状态')
    bankuai = models.ForeignKey(Bankuai, on_delete=models.PROTECT, to_field='bankuai_id', null=True, blank=True, verbose_name='审核板块')
    chenghao = models.ForeignKey(Chenghao, on_delete=models.PROTECT, null=True, blank=True, verbose_name='审核标签')
    cishu = models.IntegerField(default=1, verbose_name='第几次审核')
    jiaofei_jine = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='缴纳费用')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    jilu_id = models.CharField(max_length=32, unique=True, db_index=True, verbose_name='记录ID')  # 新增
    guize_neirong = models.TextField(null=True, blank=True, verbose_name='规则内容')
    shenheguan_youxi_id = models.CharField(max_length=50, null=True, blank=True, verbose_name='审核官游戏ID')
    dashou_youxi_id = models.CharField(max_length=50, null=True, blank=True, verbose_name='打手游戏ID')
    dashou_beizhu = models.TextField(null=True, blank=True, verbose_name='打手备注')

    class Meta:
        db_table = 'shenhe_jilu'
        verbose_name = '审核记录'
        verbose_name_plural = verbose_name
        indexes = [
            models.Index(fields=['shenqingren_id', 'zhuangtai']),
            models.Index(fields=['shenheguan_id']),
        ]

class KaoheJiluFeiyong(models.Model):
    """每次考核记录实际缴纳的费用明细"""
    jilu = models.ForeignKey(ShenheJilu, on_delete=models.CASCADE, to_field='jilu_id', verbose_name='考核记录')
    cishu = models.IntegerField(verbose_name='第几次考核')
    jine = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='缴纳金额')

    class Meta:
        db_table = 'kaohe_jilu_feiyong'
        verbose_name = '考核记录费用'
        verbose_name_plural = verbose_name
        unique_together = ('jilu', 'cishu')



class KaohePayTemp(models.Model):
    """考核支付临时上下文（仅用于支付成功后回调取参，不删除数据）"""
    dingdan_id = models.CharField(max_length=50, unique=True, db_index=True, verbose_name='订单ID')
    yonghuid = models.CharField(max_length=7, db_index=True, verbose_name='用户ID')
    tag_id = models.IntegerField(verbose_name='标签ID')
    game_nick = models.CharField(max_length=100, verbose_name='游戏昵称')
    remark = models.TextField(blank=True, verbose_name='备注')
    reapply = models.BooleanField(default=False, verbose_name='是否再次申请')
    jilu_id = models.CharField(max_length=32, null=True, blank=True, verbose_name='原记录ID(再次申请时)')
    shenheguan_id = models.CharField(max_length=7, blank=True, verbose_name='指定考核官ID')
    cishu = models.IntegerField(verbose_name='本次考核次数(预计算)')
    fee = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='本次费用(分账单位)')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'kaohe_pay_temp'
        verbose_name = '考核支付临时表'
        verbose_name_plural = verbose_name

class KaoheCishuFeiyong(models.Model):
    """各标签各考核次数所需缴纳金额"""
    chenghao = models.ForeignKey(Chenghao, on_delete=models.CASCADE, verbose_name='标签')
    cishu = models.IntegerField(verbose_name='第几次考核')
    feiyong = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='需缴纳金额')

    class Meta:
        db_table = 'kaohe_cishu_feiyong'
        verbose_name = '考核次数费用'
        verbose_name_plural = verbose_name
        unique_together = ('chenghao', 'cishu')
        indexes = [models.Index(fields=['chenghao', 'cishu'])]


class KaoheJujueJilu(models.Model):
    """考核拒绝原因记录"""
    jilu = models.ForeignKey(ShenheJilu, on_delete=models.CASCADE, to_field='jilu_id', verbose_name='审核记录')
    cishu = models.IntegerField(verbose_name='对应次数')
    shenheguan_id = models.CharField(max_length=7, verbose_name='考核官用户ID')
    yuanyin = models.TextField(verbose_name='拒绝原因')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'kaohe_jujue_jilu'
        verbose_name = '拒绝原因记录'
        verbose_name_plural = verbose_name
        indexes = [models.Index(fields=['jilu_id', 'cishu'])]



class KaoheguanBankuai(models.Model):
    """考核官与板块多对多关联表"""
    kaoheguan_id = models.CharField(max_length=7, db_index=True, verbose_name='考核官用户ID')
    bankuai = models.ForeignKey(Bankuai, on_delete=models.CASCADE, to_field='bankuai_id', verbose_name='所属板块')

    class Meta:
        db_table = 'kaoheguan_bankuai'
        verbose_name = '考核官板块关联'
        verbose_name_plural = verbose_name
        unique_together = ('kaoheguan_id', 'bankuai')
        indexes = [models.Index(fields=['kaoheguan_id'])]





class ShenheGuize(models.Model):
    """审核规则表"""
    guize_id = models.AutoField(primary_key=True, verbose_name='规则ID')
    neirong = models.TextField(verbose_name='规则内容')

    class Meta:
        db_table = 'shenhe_guize'
        verbose_name = '审核规则'
        verbose_name_plural = verbose_name