<?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 OrderParam extends Model
{

    protected $table = 'order_params';

    public static function getChargeAmount($params = [])
    {

        \Log::info($params);

        $search_object = self::join('orders', 'order_params.order_id', '=', 'orders.id');
        $search_object->where('status', 'PAID');

        if (isset($params['start_time']) && $params['start_time']) {
            $search_object->where('orders.pay_end_at', '>=', $params['start_time']);
        }

        if (isset($params['end_time']) && $params['end_time']) {
            $search_object->where('orders.pay_end_at', '<=', $params['end_time']);
        }

        if (isset($params['promotion_id']) && $params['promotion_id']) {
            $search_object->where('order_params.gxhp', $params['promotion_id']);
        }

        \Log::info($search_object->toSql());
        return $search_object->sum('price');
    }
}