<?php
/**
 *   订单管理
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-03 16:51:03
 *
 */
namespace App\Repositories\Album;

use App\Repositories\Base\Repository;


class OrderRepository extends Repository {

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

    public function searchOrder(array $search,array $orderby=['id'=>'desc'],$pagesize=10)
    {
        $currentQuery = $this->model;
        if(isset($search['keyword']) && ! empty($search['keyword'])) {
            $keywords = '%' . $search['keyword'] . '%';
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('sno'  , 'like', $keywords)
                    ->orwhere('category', 'like', $keywords)
                    ->orwhere('type', 'like', $keywords)
                    ->orwhere('customer_name', 'like', $keywords)
                    ->orwhere('customer_phone', 'like', $keywords)
                    ->orwhere('customer_address', 'like', $keywords);
            });
        }

        if(isset($search['start']) && ! empty($search['start'])) {
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('created_at','>=', $search['start']);
            });
        }
        if(isset($search['end']) && ! empty($search['end'])) {
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('created_at','<=', $search['end']);
            });

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

        }

        if(isset($search['expected']) && ! empty($search['expected'])) {
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('expected_time','<', now());
            });

        }

        // dd($orderby);
        $currentQuery = $currentQuery->where(function ($query) use ($search) {
            $query->where('store_id',$search['storeid']);
        });

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

    
}
