<?php


namespace App\Modules\Push\Models;


use App\Consts\PushConst;
use Illuminate\Database\Eloquent\Model;

class QappPushApp extends Model
{
    protected $table = 'qapp_push_app';
    protected $fillable = ['uid', 'package_id', 'package', 'app_id', 'app_secret', 'app_key', 'master_key',
        'name', 'config_json', 'provider', 'status'];

    public static function getPushAppByAppId($appId)
    {
        if (empty($appId)) {
            return [];
        }

        return self::where('app_id', $appId)->where('status', PushConst::APP_WORKING)->first();
    }

    /**
     * @param $package
     * @param $provider
     * @return array
     */
    public static function getPushAppByPackageAndProvider($package, $provider)
    {
        if (empty($package) || empty($provider)) {
            return [];
        }

        return self::where('package', $package)->where('provider', strtolower($provider))->where('status', 1)->first();
    }

    /**
     * @param $packageId
     * @param $provider
     * @return array
     */
    public static function getPushAppByPackageIdAndProvider($packageId, $provider)
    {
        if (empty($packageId) || empty($provider)) {
            return [];
        }

        return self::where('package_id', $packageId)->where('provider', strtolower($provider))->where('status', 1)->first();
    }

    /**
     * @param $packageId
     * @return array
     */
    public static function getPushAppByPackageId($packageId): array
    {
        if (empty($packageId)) {
            return [];
        }

        $result = self::where('package_id', $packageId)->where('status', 1)->get();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $appIds
     * @return array
     */
    public static function getPushAppByAppIds($appIds): array
    {
        if (empty($appIds)) {
            return [];
        }

        $result = self::whereIn('app_id', $appIds)->where('status', PushConst::APP_WORKING)->get();
        return $result ? $result->toArray() : [];
    }
}