<?php


namespace App\Modules\Push\Models;


use Illuminate\Database\Eloquent\Model;

class QappPushTaskLogs extends Model
{
    protected $table = 'qapp_push_task_logs';
    protected $fillable = ['task_id', 'app_id', 'num', 'status', 'push_time', 'push_result'];

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

        $result = self::where('task_id', $taskId)->get();
        return $result ? $result->toArray() : [];
    }

    /**
     * @param $where
     * @param $data
     * @return bool
     */
    public static function updateData($where, $data)
    {
        if (empty($where) || empty($data)) {
            return false;
        }

        return self::where($where)->update($data);
    }

    /**
     * 更新主任务状态
     * @param        $id
     * @param        $status
     * @param        $result
     * @return bool
     */
    public static function updateSubTaskStatus($id, $status, $result = ''): bool
    {
        if (empty($id)) {
            return false;
        }

        $pushResult = is_string($result) ? $result : json_encode(compact('result'), JSON_UNESCAPED_UNICODE);
        $data       = ['status' => $status, 'push_result' => $pushResult, 'updated_at' => date('Y-m-d H:i:s')];
        return self::where('id', $id)->update($data);
    }
}