<?php
/**
 *   订单列表
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-12 08:32:14
 *
 */
namespace App\Repositories\Order;

use App\Repositories\Base\Repository;


class InfoRepository extends Repository {

    public function model() {
        return \App\Models\OrderInfoModel::class;
    }

    public function searchOrder(array $search,array $orderby=['id'=>'desc'],$pagesize=25)
    {
        $currentQuery = $this->model;
        if(isset($search['keyword']) && ! empty($search['keyword'])) {
            $keywords = '%' . $search['keyword'] . '%';
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('out_trade_no'  , 'like', $keywords)
                    ->orwhere('username', 'like', $keywords)
                    ->orwhere('email', 'like', $keywords)
                    ->orwhere('phone', 'like', $keywords);
            });
        }

        if(isset($search['status'])) {
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('status', $search['status']);
            });

        }else{
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('status','>' ,0);
            });
        }

        if($orderby && is_array($orderby)){
            foreach ($orderby AS $field => $value){
                //dd($orderby);
                $currentQuery = $currentQuery -> orderBy($field, $value);
            }
        }
//        $currentQuery = $currentQuery->paginate($pagesize);
        return $currentQuery;
    }
    
}
