<?php
/**
 *   预约管理
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-18 01:53:05
 *
 */
namespace App\Repositories\User;

use App\Repositories\Base\Repository;
use Carbon\Carbon;


class ScheduleRepository extends Repository {

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

    public function searchSchedule(array $search,array $orderby=['time'=>'desc'],$pagesize=10)
    {
        $currentQuery = $this->model;
        if(isset($search['keyword']) && ! empty($search['keyword'])) {
            $keywords = '%' . $search['keyword'] . '%';
            $currentQuery = $currentQuery->leftJoin('store_info','store_info.id','=','user_schedule.store_id')->where(function ($query) use ($keywords) {
                $query->where('username'  , 'like', $keywords)
                    ->orwhere('store_info.name'  , 'like', $keywords)
                    ->orwhere('user_schedule.phone', 'like', $keywords);
            });
        }


        if(isset($search['expire']) && ! empty($search['expire'])) {
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('time','<=',Carbon::now('Asia/Shanghai'));
            });
            if($orderby && is_array($orderby)){
                foreach ($orderby AS $field => $value){
                    $currentQuery = $currentQuery -> orderBy($field, $value);
                }
            }


        }else{
            $currentQuery = $currentQuery->where(function ($query) use ($search) {
                $query->where('time','>',Carbon::now('Asia/Shanghai'));
            });

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

        }

        $currentQuery = $currentQuery->paginate($pagesize);
        return $currentQuery;


    }

    
}
