<?php


namespace App\Modules\AdPosition\Models;


use Illuminate\Database\Eloquent\Model;

class AdPositionDispatch extends Model
{
    protected $table = 'qapp_ad_position_dispatch';
    protected $fillable = ['title', 'ad_position_id', 'link_url', 'link_type', 'image_url', 'desc', 'type', 'content_type',
        'priority', 'status', 'filter_condition', 'channel_id', 'qapp_account_id', 'start_time', 'end_time'];

    /**
     * 获取有效的广告位分配
     * @param $adPositionIds
     * @param $qappAccountId
     * @return array
     */
    public static function getValidPositionDispatches($adPositionIds, $qappAccountId): array
    {
        if (empty($adPositionIds) || empty($qappAccountId)) {
            return [];
        }

        $result = self::whereIn('ad_position_id', $adPositionIds)
            ->where('qapp_account_id', $qappAccountId)
            ->where('status', 1)
            ->where('start_time', '<=', date('Y-m-d H:i:s'))
            ->where('end_time', '>=', date('Y-m-d H:i:s'))
            ->orderBy('priority', 'DESC')
            ->orderBy('end_time', 'ASC')
            ->get();

        return $result ? $result->toArray() : [];
    }
}