194 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			194 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace app\model;
 | |
| 
 | |
| use think\Model;
 | |
| 
 | |
| class Ad extends Model
 | |
| {
 | |
|     // 设置当前模型对应的完整数据表名称
 | |
|     protected $table = 'bps.bps_google_ads_ad';
 | |
| 
 | |
|     // 设置主键
 | |
|     protected $pk = 'ad_id';
 | |
| 
 | |
|     // 设置自动时间戳
 | |
|     protected $autoWriteTimestamp = true;
 | |
| 
 | |
|     // 定义时间戳字段
 | |
|     protected $createTime = 'create_at';
 | |
|     protected $updateTime = 'update_at';
 | |
| 
 | |
|     // 设置字段类型和默认值
 | |
|     protected $casts = [
 | |
|         'ad_id' => 'int',
 | |
|         'ad_group_id' => 'int',
 | |
|         'customer_id' => 'int',
 | |
|     ];
 | |
| 
 | |
|     // 设置json类型字段
 | |
| 	protected $json = ['metadata'];
 | |
| 
 | |
| 
 | |
|     // 默认值设置
 | |
|     protected $defaults = [
 | |
|         'status' => 1, // 广告状态默认值为 'ENABLED'
 | |
|     ];
 | |
| 
 | |
|     // 状态判断常量
 | |
|     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';
 | |
|     }
 | |
| 
 | |
|     // 定义广告类型枚举常量
 | |
|     const UNSPECIFIED = 0;
 | |
|     const UNKNOWN = 1;
 | |
|     const TEXT_AD = 2;
 | |
|     const EXPANDED_TEXT_AD = 3;
 | |
|     const EXPANDED_DYNAMIC_SEARCH_AD = 7;
 | |
|     const HOTEL_AD = 8;
 | |
|     const SHOPPING_SMART_AD = 9;
 | |
|     const SHOPPING_PRODUCT_AD = 10;
 | |
|     const VIDEO_AD = 12;
 | |
|     const IMAGE_AD = 14;
 | |
|     const RESPONSIVE_SEARCH_AD = 15;
 | |
|     const LEGACY_RESPONSIVE_DISPLAY_AD = 16;
 | |
|     const APP_AD = 17;
 | |
|     const LEGACY_APP_INSTALL_AD = 18;
 | |
|     const RESPONSIVE_DISPLAY_AD = 19;
 | |
|     const LOCAL_AD = 20;
 | |
|     const HTML5_UPLOAD_AD = 21;
 | |
|     const DYNAMIC_HTML5_AD = 22;
 | |
|     const APP_ENGAGEMENT_AD = 23;
 | |
|     const SHOPPING_COMPARISON_LISTING_AD = 24;
 | |
|     const VIDEO_BUMPER_AD = 25;
 | |
|     const VIDEO_NON_SKIPPABLE_IN_STREAM_AD = 26;
 | |
|     const VIDEO_OUTSTREAM_AD = 27;
 | |
|     const VIDEO_TRUEVIEW_IN_STREAM_AD = 29;
 | |
|     const VIDEO_RESPONSIVE_AD = 30;
 | |
|     const SMART_CAMPAIGN_AD = 31;
 | |
|     const CALL_AD = 32;
 | |
|     const APP_PRE_REGISTRATION_AD = 33;
 | |
|     const IN_FEED_VIDEO_AD = 34;
 | |
|     const DEMAND_GEN_MULTI_ASSET_AD = 40;
 | |
|     const DEMAND_GEN_CAROUSEL_AD = 41;
 | |
|     const TRAVEL_AD = 37;
 | |
|     const DEMAND_GEN_VIDEO_RESPONSIVE_AD = 42;
 | |
|     const DEMAND_GEN_PRODUCT_AD = 39;
 | |
| 
 | |
|     private static $valueToName = [
 | |
|         self::UNSPECIFIED => 'UNSPECIFIED',
 | |
|         self::UNKNOWN => 'UNKNOWN',
 | |
|         self::TEXT_AD => 'TEXT_AD',
 | |
|         self::EXPANDED_TEXT_AD => 'EXPANDED_TEXT_AD',
 | |
|         self::EXPANDED_DYNAMIC_SEARCH_AD => 'EXPANDED_DYNAMIC_SEARCH_AD',
 | |
|         self::HOTEL_AD => 'HOTEL_AD',
 | |
|         self::SHOPPING_SMART_AD => 'SHOPPING_SMART_AD',
 | |
|         self::SHOPPING_PRODUCT_AD => 'SHOPPING_PRODUCT_AD',
 | |
|         self::VIDEO_AD => 'VIDEO_AD',
 | |
|         self::IMAGE_AD => 'IMAGE_AD',
 | |
|         self::RESPONSIVE_SEARCH_AD => 'RESPONSIVE_SEARCH_AD',
 | |
|         self::LEGACY_RESPONSIVE_DISPLAY_AD => 'LEGACY_RESPONSIVE_DISPLAY_AD',
 | |
|         self::APP_AD => 'APP_AD',
 | |
|         self::LEGACY_APP_INSTALL_AD => 'LEGACY_APP_INSTALL_AD',
 | |
|         self::RESPONSIVE_DISPLAY_AD => 'RESPONSIVE_DISPLAY_AD',
 | |
|         self::LOCAL_AD => 'LOCAL_AD',
 | |
|         self::HTML5_UPLOAD_AD => 'HTML5_UPLOAD_AD',
 | |
|         self::DYNAMIC_HTML5_AD => 'DYNAMIC_HTML5_AD',
 | |
|         self::APP_ENGAGEMENT_AD => 'APP_ENGAGEMENT_AD',
 | |
|         self::SHOPPING_COMPARISON_LISTING_AD => 'SHOPPING_COMPARISON_LISTING_AD',
 | |
|         self::VIDEO_BUMPER_AD => 'VIDEO_BUMPER_AD',
 | |
|         self::VIDEO_NON_SKIPPABLE_IN_STREAM_AD => 'VIDEO_NON_SKIPPABLE_IN_STREAM_AD',
 | |
|         self::VIDEO_OUTSTREAM_AD => 'VIDEO_OUTSTREAM_AD',
 | |
|         self::VIDEO_TRUEVIEW_IN_STREAM_AD => 'VIDEO_TRUEVIEW_IN_STREAM_AD',
 | |
|         self::VIDEO_RESPONSIVE_AD => 'VIDEO_RESPONSIVE_AD',
 | |
|         self::SMART_CAMPAIGN_AD => 'SMART_CAMPAIGN_AD',
 | |
|         self::CALL_AD => 'CALL_AD',
 | |
|         self::APP_PRE_REGISTRATION_AD => 'APP_PRE_REGISTRATION_AD',
 | |
|         self::IN_FEED_VIDEO_AD => 'IN_FEED_VIDEO_AD',
 | |
|         self::DEMAND_GEN_MULTI_ASSET_AD => 'DEMAND_GEN_MULTI_ASSET_AD',
 | |
|         self::DEMAND_GEN_CAROUSEL_AD => 'DEMAND_GEN_CAROUSEL_AD',
 | |
|         self::TRAVEL_AD => 'TRAVEL_AD',
 | |
|         self::DEMAND_GEN_VIDEO_RESPONSIVE_AD => 'DEMAND_GEN_VIDEO_RESPONSIVE_AD',
 | |
|         self::DEMAND_GEN_PRODUCT_AD => 'DEMAND_GEN_PRODUCT_AD',
 | |
|     ];
 | |
| 
 | |
|     // 检查广告类型是否有效
 | |
|     public static function isValidAdType($adType) {
 | |
|         return in_array($adType, array_keys(self::$valueToName));
 | |
|     }
 | |
| 
 | |
|     // 获取广告类型名称
 | |
|     public static function getAdTypeName($adType) {
 | |
|         return self::$valueToName[$adType] ?? 'UNKNOWN';
 | |
|     }
 | |
| 
 | |
|     // 在模型中使用这些常量
 | |
|     public function setAdType($adType) {
 | |
|         if (!self::isValidAdType($adType)) {
 | |
|             throw new Exception("Invalid ad type");
 | |
|         }
 | |
|         $this->ad_type = $adType;
 | |
|     }
 | |
| 
 | |
|     // 更新广告状态
 | |
| //    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;
 | |
|     }
 | |
| 
 | |
|     // 关联 AdGroup 模型(广告属于广告组)
 | |
|     // 即使没有外键约束,依然可以使用 belongsTo 访问 AdGroup 数据
 | |
|     public function adGroup()
 | |
|     {
 | |
|         return $this->belongsTo(AdGroup::class, 'ad_group_id', 'ad_group_id');
 | |
|     }
 | |
| 
 | |
|     // 关联到素材关系
 | |
|     public function assetRelations()
 | |
|     {
 | |
|         return $this->hasMany(AssetRelation::class, 'ad_id', 'ad_id');
 | |
|     }
 | |
| 
 | |
|     // 关联 Customer 模型(广告属于客户)
 | |
| //    public function customer()
 | |
| //    {
 | |
| //        return $this->belongsTo(GoogleAdsCustomer::class, 'customer_id', 'customer_id');
 | |
| //    }
 | |
| } |