from django.db import models

class Dingdan(models.Model):
    """订单主表"""
    # 核心编号：唯一且不可为空[citation:3][citation:9]
    dingdan_id = models.CharField(max_length=32, unique=True,db_index=True, verbose_name='订单ID')
    # 状态与平台：用整数表示，具体含义由业务逻辑定义
    zhuangtai = models.IntegerField(default=1, null=True, blank=True,db_index=True, verbose_name='订单状态')
    fadan_pingtai = models.IntegerField(null=True, blank=True, verbose_name='发单平台')
    # 金额相关
    jine = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='订单金额')
    dashou_fencheng = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='打手分成')
    guanshi_fencheng = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=0, verbose_name='管事分成')
    # 用户与商品关联ID
    jiedan_dashou_id = models.CharField(max_length=32, null=True, db_index=True,blank=True, verbose_name='接单打手ID')
    dashou_liuyan = models.TextField(null=True, blank=True, verbose_name='打手留言')
    zhiding_id = models.CharField(max_length=32, null=True, blank=True, verbose_name='指定ID')
    shangpin_id = models.IntegerField(null=True, blank=True, verbose_name='商品ID')
    leixing_id = models.PositiveIntegerField(null=True, blank=True, verbose_name='商品类型ID')  # 直接存数字
    tupian = models.CharField(max_length=500, null=True, blank=True, verbose_name='商品图片URL')

    yaoqiuleixing = models.PositiveIntegerField(null=True, blank=True, verbose_name='抢单要求类型')
    huiyuan_id = models.CharField(max_length=6, null=True, blank=True, verbose_name='会员ID')
    yongjin = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='佣金要求')
    # 订单描述信息
    jieshao = models.TextField(null=True, blank=True, verbose_name='订单介绍')
    beizhu = models.TextField(null=True, blank=True, verbose_name='订单备注')
    tkly = models.CharField(max_length=500, blank=True, null=True, verbose_name='退款/拒绝理由')
    # 新增字段：游戏昵称（前端提交的游戏昵称）
    nicheng = models.CharField(max_length=50, null=True, blank=True, verbose_name='游戏昵称')
    user1_id = models.CharField(max_length=30,null=True, db_index=True, verbose_name='用户1标识')
    user2_id = models.CharField(max_length=30, null=True,db_index=True, verbose_name='用户2打手标识')
    #liaotian_pindao = models.CharField(max_length=255, blank=True, null=True, verbose_name='聊天频道名')
    # 时间戳

    auto_task_id = models.CharField('延时任务ID', max_length=255, blank=True, default='')
    auto_expire_at = models.DateTimeField('计划过期时间', null=True, blank=True)
    pending_dispatch = models.BooleanField('待调度标记', default=False, db_index=True)
    status_8_time = models.DateTimeField('状态变为8的时间', null=True, blank=True)


    partner_order_id = models.CharField(max_length=32, null=True, blank=True, db_index=True,verbose_name='对方平台订单ID')
    is_cross = models.IntegerField(default=0, verbose_name='是否跨平台订单(0否,1是)')

    dispatch_type = models.IntegerField(default=0, choices=[(1, '我方派单'), (2, '对方派单')], verbose_name='派发情况')
    # 🔥 新增：对方俱乐部ID
    partner_club_id = models.CharField(max_length=6, 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='更新时间')

    clkf = models.CharField(max_length=20, null=True, db_index=True, verbose_name='处理客服')
    wechat_transaction_id = models.CharField(
        max_length=64,
        blank=True,
        null=True,
        db_index=True,
        verbose_name='微信支付订单号'
    )

    class Meta:
        db_table = 'dingdan'
        verbose_name = '订单'
        verbose_name_plural = '订单'
        # 为高频查询字段添加索引
        indexes = [
            models.Index(fields=['zhuangtai']),
            models.Index(fields=['dingdan_id']),
            models.Index(fields=['jiedan_dashou_id']),
            models.Index(fields=['clkf']),
            # 🆕 为新增字段添加索引（可选）
            models.Index(fields=['wechat_transaction_id']),

        ]

    def __str__(self):
        return self.dingdan_id

class DingdanPingtai(models.Model):
    """平台订单扩展表 (当 fadan_pingtai=1 时使用)"""
    dingdan = models.OneToOneField(Dingdan, on_delete=models.CASCADE, related_name='pingtai_kuozhan', verbose_name='订单')
    laoban_id = models.CharField(max_length=32, null=True, blank=True, db_index=True,verbose_name='老板ID')
    laoban_pingjia = models.TextField(null=True, blank=True, verbose_name='老板评价')
    jjjs_biaoshi = models.IntegerField(default=0, verbose_name='拒绝结算标记(1表示拒绝结算)')

    # ========== 新增字段 ==========
    pingtai_shouyi = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True,default=0.00,verbose_name='平台收益')
    dianpu_shouyi = models.DecimalField(max_digits=10,decimal_places=2, null=True,blank=True,default=0.00,verbose_name='店铺收益')
    dianpu_id = models.PositiveIntegerField(null=True,blank=True,db_index=True,verbose_name='所属店铺ID')

    # ---------- 上级分红快照 ----------
    shangji_dianpu_id = models.PositiveIntegerField(null=True, blank=True, db_index=True,
                                                    verbose_name='该订单的上级店铺ID')
    shangji_fenhong = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=0.00,
                                          verbose_name='上级分红金额')

    # =============================


    class Meta:
        db_table = 'dingdan_pingtai'
        verbose_name = '平台订单扩展'
        verbose_name_plural = '平台订单扩展'
        indexes = [
            models.Index(fields=['laoban_id']),
            models.Index(fields=['dianpu_id']),  # 为新字段加索引


        ]

    def __str__(self):
        return f"平台扩展-{self.dingdan.dingdan_id}"

class DingdanShangjia(models.Model):
    """商家发单扩展表 (当 fadan_pingtai=2 时使用)"""
    dingdan = models.OneToOneField(Dingdan, on_delete=models.CASCADE, related_name='shangjia_kuozhan', verbose_name='订单')
    shangjia_id = models.CharField(max_length=32, null=True, blank=True,db_index=True, verbose_name='商家ID')
    shangjia_pingjia = models.TextField(null=True, blank=True, verbose_name='商家评价')
    cfliyou = models.TextField(null=True, blank=True, verbose_name='商家处罚理由')
    sqzhuangtai =models.IntegerField(default=0, null=True, blank=True, verbose_name='处罚申请状态:0待处理,1成功,2驳回')
    bhliyou = models.TextField(null=True, blank=True, verbose_name='驳回商家处罚理由')
    sjnicheng =models.CharField(max_length=50,null=True, verbose_name='商家昵称')
    partner_yonghu_id = models.CharField(max_length=32, blank=True, null=True, verbose_name='对方平台用户ID')

    # 在 dingdan/models.py 的 DingdanShangjia 类中添加
    shangyige_dingdan_id = models.CharField(
        max_length=32,
        null=True,
        blank=True,
        verbose_name='上一个订单ID'
    )

    class Meta:
        db_table = 'dingdan_shangjia'
        verbose_name = '商家订单扩展'
        verbose_name_plural = '商家订单扩展'
        indexes = [
            models.Index(fields=['shangjia_id']),

        ]

    def __str__(self):
        return f"商家扩展-{self.dingdan.dingdan_id}"












class DingdanPeihu(models.Model):
    """陪护订单表（一个订单最多2个陪护）"""
    # 关联订单（一对一，陪护订单依附于原订单）
    dingdan_id = models.CharField(max_length=32, unique=True, db_index=True, verbose_name='订单ID')
    # 主打手ID（冗余，方便查询，必须与订单的 jiedan_dashou_id 一致）
    zhudashou_id = models.CharField(max_length=32, db_index=True, verbose_name='主打手ID')
    # 实际接单陪护
    peihu1_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='陪护1打手ID')
    peihu2_id = models.CharField(max_length=32, null=True, blank=True, db_index=True, verbose_name='陪护2打手ID')
    # 订单状态（1=待抢单,2=已抢待确认,3=进行中,4=已完成,5=已取消,7=指定状态...）
    zhuangtai = models.IntegerField(default=1, verbose_name='陪护订单状态')
    # 需求配置
    peihu_xuqiu_shuliang = models.IntegerField(default=1, verbose_name='陪护需求数量')
    yongjin_yaoqiu = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='抢单佣金要求')
    # 等级需求（关联称号ID，可空表示无限制）
    peihu1_dengji_xuqiu = models.PositiveIntegerField(null=True, blank=True, verbose_name='陪护1等级需求(称号ID)')
    peihu2_dengji_xuqiu = models.PositiveIntegerField(null=True, blank=True, verbose_name='陪护2等级需求(称号ID)')
    # 处罚与分红比例（格式："1/5" 或 "2/5"）
    peihu1_zhadan_danze = models.CharField(max_length=10, null=True, blank=True, verbose_name='陪护1炸单担责比例')
    peihu2_zhadan_danze = models.CharField(max_length=10, null=True, blank=True, verbose_name='陪护2炸单担责比例')
    peihu1_fenhong = models.CharField(max_length=10, null=True, blank=True, verbose_name='陪护1分红比例')
    peihu2_fenhong = models.CharField(max_length=10, null=True, blank=True, verbose_name='陪护2分红比例')
    # 指定陪护（可预设打手ID，可为空）
    zhiding_peihu1 = models.CharField(max_length=32, null=True, blank=True, verbose_name='指定陪护1 ID')
    zhiding_peihu2 = models.CharField(max_length=32, null=True, blank=True, verbose_name='指定陪护2 ID')
    # 时间戳
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'dingdan_peihu'
        verbose_name = '陪护订单'
        verbose_name_plural = verbose_name
        indexes = [
            models.Index(fields=['zhudashou_id']),
            models.Index(fields=['peihu1_id']),
            models.Index(fields=['peihu2_id']),
            models.Index(fields=['zhuangtai']),
        ]





class CrossPlatformOrderData(models.Model):
    """
    跨平台订单扩展数据表
    存储我方订单与对方平台订单的关联及对方订单状态
    """
    dingdan_id = models.CharField(max_length=32, unique=True, db_index=True, verbose_name='我方订单ID')
    partner_order_id = models.CharField(max_length=32, db_index=True, verbose_name='对方平台订单ID')
    partner_club_id = models.CharField(max_length=6, db_index=True, verbose_name='对方俱乐部ID')
    partner_order_status = models.IntegerField(default=0, verbose_name='对方平台订单状态')  # 对方订单当前状态码
    partner_dashou_id = models.CharField(max_length=32, blank=True, default='', verbose_name='对方平台接单打手ID')
    partner_amount = 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='更新时间')

    class Meta:
        db_table = 'cross_platform_order_data'
        verbose_name = '跨平台订单扩展'
        verbose_name_plural = '跨平台订单扩展'
        indexes = [
            models.Index(fields=['dingdan_id']),
            models.Index(fields=['partner_order_id']),
            models.Index(fields=['partner_club_id']),
        ]

    def __str__(self):
        return f"{self.dingdan_id} -> {self.partner_order_id}"



# 建议路径：dingdan/models.py 或 独立 fadan/models.py
from django.db import models

class Fadan(models.Model):
    """罚单表——平台各类处罚信息"""
    id = models.AutoField(primary_key=True, verbose_name='自增主键')
    beichufa_id = models.CharField(max_length=20, db_index=True, verbose_name='被处罚用户ID')               # 直接用yonghuid，后续可改为外键
    shenqing_chufa = models.CharField(max_length=20, blank=True, null=True, verbose_name='申请处罚者ID')
    shenfen = models.IntegerField(choices=[(1, '打手'), (2, '管事'), (3, '商家'), (4, '组长')], default=1, verbose_name='被处罚者身份')
    chufaliyou = models.TextField(blank=True, null=True, verbose_name='处罚理由')
    fakuanjine = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='罚款金额')
    guanliandingdan_id = models.CharField(max_length=32, blank=True, null=True, db_index=True, verbose_name='关联订单ID')
    zhuangtai = models.IntegerField(choices=[(1, '待缴纳'), (2, '已缴纳'), (3, '申诉中'), (4, '已驳回')], default=1, db_index=True, verbose_name='罚单状态')
    yingxiang_qiangdan = models.IntegerField(choices=[(1, '影响抢单'), (0, '不影响抢单')], default=1, db_index=True, verbose_name='是否影响抢单')
    shensuliyou = models.TextField(blank=True, null=True, verbose_name='申诉理由')
    tongyi_chufaliyou = models.TextField(blank=True, null=True, verbose_name='同意处罚理由')
    bohuiliyou = models.TextField(blank=True, null=True, verbose_name='驳回理由')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    # ========== 新增字段 ==========
    shenqingren_fenhong_jine = models.DecimalField(
        max_digits=12, decimal_places=2, default=0.00,
        verbose_name='申请人分红金额'
    )
    # 在 Fadan 模型中添加
    partner_club_id = models.CharField(max_length=6, blank=True, null=True, db_index=True, verbose_name='对方俱乐部ID')
    # 在 class Fadan 中添加
    shenqingren_shenfen = models.IntegerField(
        choices=[(1, '客服'), (2, '售后'), (3, '管理员'),(4,'店铺'),(5,'商家')],  # 按实际扩展
        default=1,
        verbose_name='申请处罚人身份'
    )
    chulizhe_shenfen = models.IntegerField(
        choices=[(1, '客服'), (2, '售后'), (3, '管理员')],
        default=2,
        verbose_name='处理者身份'
    )
    chulizhe = models.CharField(max_length=20, blank=True, null=True, verbose_name='处理者')

    class Meta:
        db_table = 'fadan'
        verbose_name = '罚单'
        verbose_name_plural = '罚单'
        indexes = [
            models.Index(fields=['beichufa_id', 'yingxiang_qiangdan', 'zhuangtai'], name='fadan_qiangdan_check'),
            models.Index(fields=['guanliandingdan_id'], name='fadan_dingdan_idx'),
        ]

    def __str__(self):
        return f"{self.beichufa_id} - 罚款{self.fakuanjine}元 - 状态{self.zhuangtai}"


# 与 Fadan 放在同一个文件或独立的 fadan/models.py
class FadanFenhong(models.Model):
    """罚款分红表——记录罚款产生的分红信息"""
    id = models.AutoField(primary_key=True, verbose_name='自增主键')
    fenhongzhe_id = models.CharField(max_length=20, db_index=True, verbose_name='分红者ID')
    zonge = models.DecimalField(max_digits=12, decimal_places=2, default=0.00, verbose_name='分红总额')
    ketixian_fenhong = models.DecimalField(max_digits=12, decimal_places=2, default=0.00, verbose_name='可提现分红')
    shenfen = models.IntegerField(choices=[(1, '商家'), (2, '客服'), (3, '平台管理者'),(4,'店铺'),(5, '售后')], default=1, verbose_name='分红者身份')
    fenhong_cishu = models.IntegerField(default=0, verbose_name='分红次数')
    # ✅ 新增字段：是否单独开启分红利率（若开启，则使用本记录的 yingde_lilv；否则使用全局配置）
    is_individual_rate = models.BooleanField(default=False, verbose_name='是否单独开启分红利率')
    yingde_lilv = models.DecimalField(max_digits=5, decimal_places=4, default=0.0000, verbose_name='分红应得利率')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'fadan_fenhong'
        verbose_name = '罚款分红'
        verbose_name_plural = '罚款分红'
        indexes = [
            models.Index(fields=['fenhongzhe_id'], name='fadan_fenhong_zhe_idx'),
            models.Index(fields=['shenfen'], name='fadan_fenhong_sf_idx'),
        ]

    def __str__(self):
        return f"{self.fenhongzhe_id} - 分红{self.ketixian_fenhong}元"


class FadanFenhongLilv(models.Model):
    shenfen = models.IntegerField(primary_key=True, choices=[(1, '客服'), (2, '售后'), (3, '管理员'), (4, '店铺'), (5, '商家')], verbose_name='申请处罚人身份')
    lilv = models.DecimalField(max_digits=5, decimal_places=4, default=0.0000, verbose_name='分红费率')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'fadan_fenhong_lilv'
        verbose_name = '罚款分红费率配置'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"身份{self.shenfen} - 费率{self.lilv}"


# ==================== 罚款申诉图片表 ====================
class FadanShensuTupian(models.Model):
    """罚款申诉图片，关联罚单"""
    fadan = models.ForeignKey(Fadan, on_delete=models.CASCADE, related_name='shensu_tupians', verbose_name='关联罚单')
    beichufa_id = models.CharField(max_length=7, db_index=True, verbose_name='被处罚用户ID')      # 冗余，便于直接按用户查
    tupian_url = models.CharField(max_length=500, verbose_name='图片相对URL')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    # 在 FadanShensuTupian 模型中添加
    yongtu = models.IntegerField(
        choices=[(1, '处罚证据图片'), (2, '申诉图片')],
        default=2,
        verbose_name='图片用途'
    )

    class Meta:
        db_table = 'fadan_shensu_tupian'
        verbose_name = '罚款申诉图片'
        verbose_name_plural = '罚款申诉图片'
        indexes = [
            models.Index(fields=['fadan_id']),
            models.Index(fields=['beichufa_id']),
        ]



# apps/dingdan/models.py 中添加

class PreSettlement(models.Model):
    """
    预结算表
    用于记录对方平台派单订单的待结算款项，等待对方确认后才能真正入账
    """
    STATUS_CHOICES = (
        (0, '待结算'),
        (1, '已结算'),
        (2, '已拒绝'),
    )
    # 我方订单ID
    dingdan_id = models.CharField(max_length=32,  db_index=True, verbose_name='我方订单ID')
    # 对方订单ID
    partner_order_id = models.CharField(max_length=32, db_index=True, verbose_name='对方订单ID')
    # 对方俱乐部ID
    partner_club_id = models.CharField(max_length=6, db_index=True, verbose_name='对方俱乐部ID')
    # 接单打手ID
    dashou_id = models.CharField(max_length=7, db_index=True, verbose_name='接单打手ID')
    # 打手应分金额
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='打手应分金额')
    # 订单金额（原金额，可选）
    order_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='订单原金额')
    # 结算状态
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=0, verbose_name='结算状态')
    # 处理时间（结算或拒绝的时间）
    process_time = models.DateTimeField(null=True, blank=True, verbose_name='处理时间')
    # 处理客服/对方操作人（可选）
    operator = models.CharField(max_length=32, blank=True, default='', verbose_name='操作人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'pre_settlement'
        verbose_name = '预结算表'
        verbose_name_plural = verbose_name
        # 添加这个联合唯一约束
        unique_together = [['dingdan_id', 'dashou_id']]
        indexes = [
            models.Index(fields=['dingdan_id']),
            models.Index(fields=['partner_order_id']),
            models.Index(fields=['partner_club_id']),
            models.Index(fields=['dashou_id']),
            models.Index(fields=['status']),
        ]

    def __str__(self):
        return f"预结算-{self.dingdan_id} -> {self.partner_order_id}"


# apps/dingdan/models.py 中添加

class OrderDashouHistory(models.Model):
    """
    订单打手变更历史表
    记录订单每次更换打手的操作
    """
    dingdan_id = models.CharField(max_length=32, db_index=True, verbose_name='订单ID')
    dashou_id = models.CharField(max_length=7, db_index=True, verbose_name='打手ID')
    times = models.PositiveSmallIntegerField(verbose_name='第几次接单（从1开始）')
    operator = models.CharField(max_length=11, verbose_name='操作客服ID')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'order_dashou_history'
        verbose_name = '订单打手变更历史'
        verbose_name_plural = '订单打手变更历史'
        indexes = [
            models.Index(fields=['dingdan_id']),
            models.Index(fields=['dashou_id']),
        ]

    def __str__(self):
        return f"{self.dingdan_id} -> {self.dashou_id} 第{self.times}次"


class Dashoutupian(models.Model):
    dingdan_id = models.CharField(max_length=32,db_index=True, null=True, verbose_name='订单ID')
    dashou = models.CharField(max_length=32, null=True, blank=True,db_index=True, verbose_name='接单打手ID')
    tupian = models.CharField(max_length=500, null=True, blank=True, verbose_name='打手提交图片URL')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table = 'dashoutupian'

        indexes = [
            models.Index(fields=['dashou']),
            models.Index(fields=['dingdan_id']),

        ]



class Pingfen(models.Model):
    dashou = models.CharField(max_length=32, null=True, blank=True, db_index=True,verbose_name='接单打手ID')
    zongfen = models.PositiveIntegerField(default=0, verbose_name='总分')
    cishu = models.PositiveIntegerField(default=0, verbose_name='评分次数')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'pingfenbiao'
        verbose_name = '评分表'
        indexes = [
            models.Index(fields=['dashou']),
        ]
    def __str__(self):
        return f"{self.dashou} - 平均分: {self.pingjun_fen():.2f}"

    def pingjun_fen(self):
        """计算平均分"""
        if self.cishu > 0:
            return float(self.zongfen) / self.cishu
        return 0.0



class Lilubiao(models.Model):
    lilu = models.DecimalField(max_digits=5, decimal_places=4,null=True, verbose_name='平台分成利率，1是平台,3是商家派单抽成，5是打手提现抽，6管事提现抽，8.组长提现抽9.审核官提现抽，10.商家提现抽，11打手保证金提现抽，12.押金分红,13.管事分红')
    #sahngjialilu = models.DecimalField(max_digits=5, decimal_places=4,null=True, verbose_name='商家利率')
    fadanpingtai = models.CharField(max_length=10,null=True,verbose_name='利率类型,平台分成利率，1是平台订单打手分成,3是商家订单打手分成，5：打手提现佣金抽成6：管事提现抽成，8：组长提现抽成')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table = 'lilubiao'




class Chufajilu(models.Model):
    dashouid = models.CharField(max_length=7, null=True, db_index=True, verbose_name='被罚打手ID')
    qingqiuid = models.CharField(max_length=7, null=True, db_index=True, verbose_name='请求处罚ID')
    chuliid = models.CharField(max_length=11, null=True, db_index=True, verbose_name='处理处罚ID')
    cfliyou = models.TextField(null=True, blank=True, verbose_name='处罚理由')
    sqzhuangtai = models.IntegerField(default=0, null=True, blank=True, verbose_name='处罚申请状态:0待处理,1成功,2驳回，3申诉')
    bhliyou = models.TextField(null=True, blank=True, verbose_name='驳回处罚理由')
    ssliyou = models.TextField(null=True, blank=True, verbose_name='打手申诉理由')
    jifen = models.IntegerField(default=0, verbose_name='扣除打手积分')
    dingdan_id = models.CharField(max_length=32,null=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 = 'chufajilu'
        indexes = [
            models.Index(fields=['dashouid']),
            models.Index(fields=['qingqiuid']),
            models.Index(fields=['chuliid']),
            models.Index(fields=['dingdan_id']),
        ]


class Chufatupian(models.Model):
    dingdan_id = models.CharField(max_length=32,db_index=True, null=True, verbose_name='订单ID')
    yonghuid = models.CharField(max_length=32, null=True, blank=True,db_index=True, verbose_name='用户ID')
    tupian = models.CharField(max_length=500, null=True, blank=True, verbose_name='证据图片URL')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table = 'chufatupian'

        indexes = [
            models.Index(fields=['yonghuid']),
            models.Index(fields=['dingdan_id']),

        ]


class Tuikuanjilu(models.Model):
    dingdan_id = models.CharField(max_length=32, unique=True, verbose_name='订单ID')
    dashouid = models.CharField(max_length=7, null=True, verbose_name='接单打手ID')
    qingqiuid = models.CharField(max_length=7, null=True, verbose_name='请求用户ID')
    chuliid = models.CharField(max_length=7, null=True, db_index=True, verbose_name='处理人ID')
    liyou = models.TextField(null=True, blank=True, verbose_name='理由')
    sqzhuangtai = models.IntegerField(default=0, null=True, blank=True,db_index=True, verbose_name='申请状态:0待处理,1成功,2驳回')
    bhliyou = models.TextField(null=True, blank=True, verbose_name='驳回理由')
    jine = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='订单金额')
    dashou_fencheng = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='打手分成')
    jieshao = models.TextField(null=True, blank=True, verbose_name='订单介绍')
    beizhu = models.TextField(null=True, blank=True, verbose_name='订单备注')
    # 新增字段：游戏昵称（前端提交的游戏昵称）
    nicheng = models.CharField(max_length=50, null=True, blank=True, verbose_name='游戏昵称')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table = 'tuikuanjilu'
        indexes = [
            models.Index(fields=['chuliid']),
            models.Index(fields=['sqzhuangtai']),
        ]


class Ptliushui(models.Model):
    zonge = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='收取总金额')
    zhichuzonge = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='支出总金额')
    zongshouyi = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='总收益')
    rizonge = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='日收取')
    rizhichu = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='日支出')
    rishouyi = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, verbose_name='日收益')
    zongshoucishu = models.IntegerField(default=0, verbose_name='总收取次数')
    rishoucishu = models.IntegerField(default=0, verbose_name='日收取次数')
    zongzhichucishu = models.IntegerField(default=0, verbose_name='总支出次数')
    rizhichucishu = models.IntegerField(default=0, verbose_name='日支出次数')
    class Meta:
        db_table = 'ptliushui'


# models.py - ChatRelation 模型



class Liaotian(models.Model):
    """聊天关系表 - 极简设计，不设置唯一约束"""
    # 主键
    id = models.BigAutoField(primary_key=True, verbose_name='主键ID')

    # 聊天双方的用户标识（GoEasy的user_id）
    user1_id = models.CharField(max_length=30, db_index=True, verbose_name='用户1标识')
    user2_id = models.CharField(max_length=30, db_index=True, verbose_name='用户2标识')

    # 关联的订单（可以为空，但实际业务中应该关联）
    dingdan_id = models.CharField(max_length=32, unique=True, verbose_name='订单ID')
    # 状态与平台：用整数表示，具体含义由业务逻辑定义
    zhuangtai = models.IntegerField(default=1,db_index=True,choices=[(1, '开启中'), (0, '已关闭')],verbose_name='聊天状态')
    # 时间戳
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'liaotian'
        indexes = [
            models.Index(fields=['user1_id']),
            models.Index(fields=['user2_id']),
            models.Index(fields=['zhuangtai']),
        ]

    def __str__(self):
        return f'{self.user1_id} ↔ {self.user2_id} ({self.zhuangtai})'



