<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/23
 * Time: 9:25
 */

namespace App\Modules\MediaPush\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class HeadlineStat extends Model
{

    protected $table = 'media_push_headline_stats';
    protected $fillable = ['id', 'headline', 'seq', 'push_num', 'click_num', 'created_at', 'updated_at'];

    public static function getHeadlineStat($params = [], $isAll = false)
    {
        \Log::info($params);
        $search_object = self::select('id', 'headline', 'seq', 'push_num', DB::raw('click_num/push_num clickRate'),'click_num', 'created_at', 'updated_at');
        if (isset($params['headline']) && $params['headline']) {
            $search_object->where('headline', 'like', '%' . $params['headline'] . '%');
        }

         if (isset($params['seq']) && $params['seq']) {
             $search_object->where('seq', $params['seq']);
         }

        $orderBy = 'id';
        $orderType = 'desc';

        if (isset($params['orderType']) && $params['orderType']) {
            $orderType = $params['orderType'];
        }

        if (isset($params['orderBy']) && $params['orderBy']) {
            $orderBy = $params['orderBy'];
        }

        $search_object->orderBy($orderBy, $orderType);
        \Log::info($search_object->toSql());
        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }
}