108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
use think\Model;
 | 
						|
 | 
						|
class AdGroup extends Model
 | 
						|
{
 | 
						|
    // 设置当前模型对应的完整数据表名称
 | 
						|
    protected $table = 'bps.bps_google_ads_ad_group';
 | 
						|
 | 
						|
    // 设置主键
 | 
						|
    protected $pk = 'ad_group_id';
 | 
						|
 | 
						|
    // 设置自动时间戳
 | 
						|
    protected $autoWriteTimestamp = true;
 | 
						|
 | 
						|
    // 定义时间戳字段
 | 
						|
    protected $createTime = 'create_at';
 | 
						|
    protected $updateTime = 'update_at';
 | 
						|
 | 
						|
    // 设置字段类型和默认值
 | 
						|
    protected $casts = [
 | 
						|
        'ad_group_id' => 'int',
 | 
						|
        'campaign_id' => 'int',
 | 
						|
        'customer_id' => 'int',
 | 
						|
        'cpc_bid_micros' => 'int',
 | 
						|
    ];
 | 
						|
 | 
						|
    // 默认值设置
 | 
						|
    protected $defaults = [
 | 
						|
        'cpc_bid_micros' => 0, // 每次点击出价默认值为0
 | 
						|
    ];
 | 
						|
 | 
						|
    // 状态判断常量
 | 
						|
    const STATUS_UNSPECIFIED = 0;
 | 
						|
    const STATUS_UNKNOWN = 1;
 | 
						|
    const STATUS_ENABLED = 2;
 | 
						|
    const STATUS_PAUSED = 3;
 | 
						|
    const STATUS_REMOVED = 4;
 | 
						|
 | 
						|
    // 获取广告组状态
 | 
						|
    public function getStatusTextAttr($value, $data)
 | 
						|
    {
 | 
						|
        $statusMap = [
 | 
						|
            self::STATUS_UNSPECIFIED => 'UNSPECIFIED',
 | 
						|
            self::STATUS_UNKNOWN => 'UNKNOWN',
 | 
						|
            self::STATUS_ENABLED => 'ENABLED',
 | 
						|
            self::STATUS_PAUSED => 'PAUSED',
 | 
						|
            self::STATUS_REMOVED => 'REMOVED',
 | 
						|
        ];
 | 
						|
        return $statusMap[$data['status']] ?? 'UNKNOWN';
 | 
						|
    }
 | 
						|
 | 
						|
    // 更新广告组状态
 | 
						|
//    public function updateStatus($status)
 | 
						|
//    {
 | 
						|
//        if (!in_array($status, [self::STATUS_ENABLED, self::STATUS_PAUSED, self::STATUS_REMOVED])) {
 | 
						|
////            throw new \think\exception\ValidateException('Invalid status');
 | 
						|
//        }
 | 
						|
//
 | 
						|
//        $this->status = $status;
 | 
						|
//        return $this->save();
 | 
						|
//    }
 | 
						|
 | 
						|
    // 判断广告组状态
 | 
						|
    public function isEnabled()
 | 
						|
    {
 | 
						|
        return $this->status == self::STATUS_ENABLED;
 | 
						|
    }
 | 
						|
 | 
						|
    public function isPaused()
 | 
						|
    {
 | 
						|
        return $this->status == self::STATUS_PAUSED;
 | 
						|
    }
 | 
						|
 | 
						|
    public function isRemoved()
 | 
						|
    {
 | 
						|
        return $this->status == self::STATUS_REMOVED;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    // 关联 Campaign 模型(广告组属于广告活动)
 | 
						|
    public function campaign()
 | 
						|
    {
 | 
						|
        return $this->belongsTo(Campaign::class, 'campaign_id', 'campaign_id');
 | 
						|
    }
 | 
						|
 | 
						|
    // 关联 Customer 模型(广告组属于客户)
 | 
						|
//    public function customer()
 | 
						|
//    {
 | 
						|
//        return $this->belongsTo(GoogleAdsCustomer::class, 'customer_id', 'customer_id');
 | 
						|
//    }
 | 
						|
 | 
						|
    // 关联 Ad 模型(广告组包含多个广告)
 | 
						|
    public function ads()
 | 
						|
    {
 | 
						|
        return $this->hasMany(Ad::class, 'ad_group_id', 'ad_group_id');
 | 
						|
    }
 | 
						|
 | 
						|
    // 关联到素材关系
 | 
						|
    public function assetRelations()
 | 
						|
    {
 | 
						|
        return $this->hasMany(AssetRelation::class, 'ad_group_id', 'ad_group_id');
 | 
						|
    }
 | 
						|
}
 | 
						|
 |