sifangpay/app/Services/SearchServices.php

665 lines
15 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: kongqi
* Date: 2019/1/6
* Time: 16:12
*/
namespace App\Services;
use App\Models\Order;
use Illuminate\Http\Request;
use Jenssegers\Date\Date;
class SearchServices
{
public static $where = [];
public $model;
public $wherePrefix = 'whereBy';
public $timeType = '';
public $param = [];
public function __construct($model, $param = [], $type = '')
{
$this->model = $model;
$this->timeType = $type;
$this->param = [];
$this->param = $param;
//$this->setWhere($param);
//$this->selectSearch();
$this->defaultWhere();
}
public function defaultWhere()
{
switch ($this->timeType) {
case 'order':
$this->model = $this->model->where('merchant_id', '!=', 0);
break;
case 'order_self':
$this->model = $this->model->where('merchant_id', '=', 0);
break;
}
}
public function setWhereTimeSql($start_at, $end_at)
{
$type = $this->timeType;
$data = [];
$start_at = trim($start_at);
$end_at = trim($end_at);
switch ($type) {
case 'order':
case 'order_self':
case 'order_merchant':
$data = [
'order_at' => [
'type' => 'raw',
'value' => ['(order_at>=? and order_at<=?) ', [$start_at, $end_at]]
]
];
break;
case 'draw_money':
$data = [
'order_at' => [
'type' => 'raw',
'value' => ['(created_at>=? and created_at<=?) ', [$start_at, $end_at]]
]
];
case 'created_at':
$data = [
'created_at' => [
'type' => 'raw',
'value' => ['(created_at>=? and created_at<=?) ', [$start_at, $end_at]]
]
];
default:
$data = [
'order_at' => [
'type' => 'raw',
'value' => ['(created_at>=? and created_at<=?) ', [$start_at, $end_at]]
]
];
break;
}
//dump($this->timeType);
$this->addWhere($data);
}
//重载入,清除再载入
public function unsetAllWhere($param = [])
{
self::$where = [];
}
public function getList()
{
$this->render();
return $this->model;
}
public function getModel()
{
$this->render();
return $this->model;
}
//统计数量
public function totalNumber($field = 'id')
{
$this->render();
return $this->model->count($field);
}
//统计增加
public function totalSum($field = 'id')
{
$this->render();
return $this->model->sum($field);
}
/**
* 执行渲染输出
*/
public function render()
{
$this->unsetAllWhere();//清空再获取
$this->setWhere($this->param);
$this->selectSearch();
}
public function selectSearch()
{
if (empty(self::$where)) {
return $this->model;
}
return $this->model = $this->model->search(self::$where, 1);
}
/**
* 设置搜索条件
* @param array $where
*/
public function setWhere(array $where)
{
//dump($where);
foreach ($where as $k => $v) {
if ($v === NULL && $v != 0) {
return false;
}
$str = convert_under_line($k);
$action = $this->wherePrefix . $str;
if (method_exists($this, $action)) {
if ($v !== '' && $v !== NULL) {
self::$action($v);
}
}
}
}
public function addWhere($where)
{
return self::$where = self::$where + $where;
}
public function unsetWhere($key)
{
if (array_key_exists($key, self::$where)) {
unset(self::$where[$key]);
}
}
public function whereByLocation()
{
$this->unsetWhere('out');
$data = [
'merchant_id' => [
'type' => '=',
'value' => 0
]
];
$this->addWhere($data);
}
public function whereByOut()
{
$data = [
'merchant_id' => [
'type' => '<>',
'value' => 0
]
];
$this->addWhere($data);
}
public function echoWhere()
{
return self::$where;
}
public function whereByStatusIn($value)
{
$data = [
'status' => [
'type' => 'in',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByIdIn($value)
{
$data = [
'id' => [
'type' => 'in',
'value' => $value
]
];
$this->addWhere($data);
}
public function prevMonth($value)
{
$timestamp = strtotime();
$firstday = date('Y-m-01', strtotime(date('Y', $timestamp) . '-' . (date('m', $timestamp) - 1) . '-01'));
}
/**
* 前多少月份查询
* @param int $value
*/
public function whereByMonth(int $value)
{
$timestarmp = strtotime(date('Y-m-01'), time());
$date = date('Y-m-d H:i:s', strtotime($value . 'month', $timestarmp));
$month_at = get_month($date);
if (count($month_at) > 0) {
$start_at = $month_at[0];
if ($value == 0) {
$end_at = $month_at[1];
} else {
$end_at = get_month($date, 1, 1);
}
//echo $start_at.'--'.$end_at.'<br/>';
$this->unsetWhere('order_at');
$this->setWhereTimeSql($start_at, $end_at);
}
}
public function whereByYear(int $value)
{
$date = date('Y-m-d', strtotime($value . " year"));
$year = DateServices::year($date);
$start_at = $year['start_at'];
$end_at = $year['end_at'];
$this->setWhereTimeSql($start_at, $end_at);
}
public function whereByWeek(int $value)
{
$date = date('Y-m-d', strtotime($value . 'week'));
$year = DateServices::week($date);
$start_at = $year['start_at'];
$end_at = $year['end_at'];
$this->setWhereTimeSql($start_at, $end_at);
}
public function setTimeType($type)
{
$this->timeType = $type;
$this->defaultWhere();
}
public function whereByIsProxy(int $status)
{
$data = [
'is_proxy' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByCheckStatus(int $status)
{
$data = [
'check_status' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByType(int $status)
{
$data = [
'type' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByOrderSn(string $order_sn)
{
$data = [
'order_sn' => [
'type' => '=',
'value' => $order_sn
]
];
$this->addWhere($data);
}
public function whereByOutOrderSn(string $out_trade_no)
{
$data = [
'out_trade_no' => [
'type' => '=',
'value' => $out_trade_no
]
];
$this->addWhere($data);
}
public function whereByOrderMoney(string $value)
{
$data = [
'order_money' => [
'type' => '=',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByOutTradeNo(string $out_trade_no)
{
$data = [
'out_trade_sn' => [
'type' => '=',
'value' => $out_trade_no
]
];
$this->addWhere($data);
}
public function whereByPayOrderSn(string $pay_order_sn)
{
$data = [
'pay_order_sn' => [
'type' => '=',
'value' => $pay_order_sn
]
];
$this->addWhere($data);
}
public function whereByName(string $value)
{
$data = [
'name' => [
'type' => '=',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByEmail(string $value)
{
$data = [
'email' => [
'type' => '=',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByEwmMark(string $value)
{
$data = [
'ewm_mark' => [
'type' => 'like',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByEwmAccount(string $value)
{
$data = [
'ewm_account' => [
'type' => 'like',
'value' => $value
]
];
$this->addWhere($data);
}
public function whereByUserId(int $user_id)
{
$data = [
'local_user_id' => [
'type' => '=',
'value' => $user_id
]
];
$this->addWhere($data);
}
public function whereByOutUserId(int $user_id)
{
$data = [
'out_user_id' => [
'type' => '=',
'value' => $user_id
]
];
$this->addWhere($data);
}
public function whereByPayStatus(int $status)
{
$data = [
'pay_status' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByGuMa(string $status)
{
$data = [
'pay_type' => [
'type' => 'in',
'value' => config('adconfig.guma_type')
]
];
$this->addWhere($data);
}
/**
* 多少分钟前的数据
* @param int $number
*/
public function whereByMinute(int $number)
{
$type = $this->timeType;
$start_at = DateServices::lastMinute($number);
switch ($type) {
default:
$data = [
'created_at' => [
'type' => 'raw',
'value' => ['created_at<=? ', [$start_at]]
]
];
break;
}
$this->addWhere($data);
}
public function whereByNotifyStatus(int $status)
{
$data = [
'notify_status' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByFromId(int $status)
{
$data = [
'from_id' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByLevel(int $status)
{
$data = [
'level' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByOutNotifyStatus(int $status)
{
$data = [
'out_notify_status' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByStatus(int $status)
{
$data = [
'status' => [
'type' => '=',
'value' => $status
]
];
$this->addWhere($data);
}
public function whereByPayType(string $pay_type)
{
$data = [
'pay_type' => [
'type' => '=',
'value' => $pay_type
]
];
$this->addWhere($data);
}
/**
* 按日期搜索时间
* @param string $time
*/
public function whereByTime(string $time)
{
$time = str_replace('~', ',', $time);
$time = explode(",", $time);
$start_at = isset($time[0]) ? $time[0] : '';
$end_at = isset($time[1]) ? $time[1] : '';
$this->unsetWhere('order_at');
$this->setWhereTimeSql($start_at, $end_at);
}
/**
* 按日期搜索
* @param string $time
*/
public function whereByTimeDay(string $time)
{
$time = str_replace('~', ',', $time);
$time = explode(",", $time);
$start_at = isset($time[0]) ? $time[0] : '';
$end_at = isset($time[1]) ? $time[1] : '';
$start_at .= '00:00:00';
$end_at .= ' 23:59:59';
$this->unsetWhere('order_at');
$this->setWhereTimeSql($start_at, $end_at);
}
public function whereByDay(int $day)
{
if ($day <= 0) {
$day_at = (new Date($day . "day"))->format('Y-m-d');
$start_at = $day_at . ' 00:00:00';
if ($day == -1) {
$end_at = $day_at . ' 23:59:59';
} else {
$end_at = (new Date('0 day'))->format('Y-m-d') . ' 23:59:59';
}
$this->unsetWhere('order_at');
$this->setWhereTimeSql($start_at, $end_at);
}
}
public function whereByDayBefore(int $day)
{
if ($day <= 0) {
$day_at = (new Date($day . "day"))->format('Y-m-d');
$start_at = $day_at . ' 00:00:00';
if ($day == -1) {
$end_at = $day_at . ' ' . date('H:i:s');
} else {
$end_at = (new Date('0 day'))->format('Y-m-d') . ' 23:59:59';
}
$data = [
'order_at' => [
'type' => '<=',
'value' => $end_at
],
];
$this->addWhere($data);
}
}
public function whereByMerchantId(int $value)
{
$data = [
'merchant_id' => [
'type' => '=',
'value' => $value
],
];
$this->addWhere($data);
}
public function whereByFromMerchantId(int $value)
{
$data = [
'from_merchant_id' => [
'type' => '=',
'value' => $value
],
];
$this->addWhere($data);
}
}