<?php

namespace app\manager\model;

use think\Model;

class Agent extends Model
{
    protected $name = 'cursor_agents';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 设置字段类型
    protected $type = [
        'balance' => 'float',
        'total_income' => 'float',
        'commission_rate' => 'float',
        'last_login_time' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    // 获取父级代理
    public function parent()
    {
        return $this->belongsTo(Agent::class, 'parent_id');
    }

    // 获取子级代理
    public function children()
    {
        return $this->hasMany(Agent::class, 'parent_id');
    }

    // 获取代理的激活码
    public function codes()
    {
        return $this->hasMany('app\manager\model\AgentCode', 'agent_id');
    }

    // 更新余额
    public function updateBalance($amount)
    {
        $this->balance += $amount;
        $this->total_income += max(0, $amount);
        return $this->save();
    }
} 