<?php
/**
 *   通话纪录
 * @author  system
 * @version    1.0
 * @date 2018-11-27 03:15:47
 *
 */

namespace App\Repositories\Call;

use App\Repositories\Base\Repository;


class RecordsRepository extends Repository
{

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

    public function searchRecords(array $search, array $orderby = ['id' => 'desc'])
    {
        $currentQuery = $this->model;
        /*企业名称*/
        if (isset($search['ip']) && !empty($search['ip'])) {
            $keywords = $search['ip'];
            \Log::info($search['ip']);
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('ip', $keywords);
            });
        };
        if (isset($search['term_status']) && $search['term_status'] == 200) {
            $keywords = $search['term_status'];
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('term_status', $keywords);
            });
        };

        if (isset($search['term_status']) && $search['term_status'] != 200) {
            $keywords = $search['term_status'];
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('term_status', '!=', 200);
            });
        };

        if (isset($search['phone']) && !empty($search['phone'])) {
            $keywords = '%' . $search['phone'] . '%';
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('phone', 'like', $keywords)
                ->orWhere('tag','like',$keywords) ;
            });
        };

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

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



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

        return $currentQuery;
    }

}
