177 lines
7.5 KiB
PHP
177 lines
7.5 KiB
PHP
<?php
|
||
|
||
|
||
namespace Yunshop\AreaDividend\models;
|
||
|
||
use app\common\models\PayTypeGroup;
|
||
|
||
/**
|
||
* Class Order
|
||
* @package Yunshop\AreaDividend\models
|
||
* @property AreaDividend areaDividend
|
||
*/
|
||
class Order extends \app\backend\modules\order\models\Order
|
||
{
|
||
public function areaDividend()
|
||
{
|
||
return $this->hasMany(AreaDividend::class,'order_sn','order_sn');
|
||
}
|
||
|
||
public function hasOneAgentOrder()
|
||
{
|
||
return $this->hasOne(AgentOrder::class, 'order_id', 'id');
|
||
}
|
||
public function scopeSearch($order_builder,$params){
|
||
if ($params['shop_name'] != '') {
|
||
$order_builder->where('shop_name', 'like', '%' . $params['shop_name'] . '%');
|
||
}
|
||
|
||
if (array_get($params, 'ambiguous.field', '') && array_get($params, 'ambiguous.string', '')) {
|
||
//订单.支付单号
|
||
if ($params['ambiguous']['field'] == 'order') {
|
||
call_user_func(function () use (&$order_builder, $params) {
|
||
list($field, $value) = explode(':', $params['ambiguous']['string']);
|
||
if (isset($value)) {
|
||
return $order_builder->where($field, $value);
|
||
} else {
|
||
return $order_builder->where(function ($query)use ($params){
|
||
$query->searchLike($params['ambiguous']['string']);
|
||
|
||
$query->orWhereHas('hasOneOrderPay', function ($query) use ($params) {
|
||
$query->where('pay_sn','like',"%{$params['ambiguous']['string']}%");
|
||
});
|
||
});
|
||
|
||
}
|
||
});
|
||
|
||
}
|
||
//用户
|
||
if ($params['ambiguous']['field'] == 'member') {
|
||
call_user_func(function () use (&$order_builder, $params) {
|
||
list($field, $value) = explode(':', $params['ambiguous']['string']);
|
||
if (isset($value)) {
|
||
return $order_builder->where($field, $value);
|
||
} else {
|
||
return $order_builder->whereHas('belongsToMember', function ($query) use ($params) {
|
||
return $query->searchLike($params['ambiguous']['string']);
|
||
});
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
//增加地址,姓名,手机号搜索
|
||
if ($params['ambiguous']['field'] == 'address') {
|
||
call_user_func(function () use (&$order_builder, $params) {
|
||
list($field, $value) = explode(':', $params['ambiguous']['string']);
|
||
if (isset($value)) {
|
||
return $order_builder->where($field, $value);
|
||
} else {
|
||
return $order_builder->whereHas('address', function ($query) use ($params) {
|
||
return $query->where('address','like', '%' . $params['ambiguous']['string'] . '%')
|
||
->orWhere('mobile','like','%' . $params['ambiguous']['string'] . '%')
|
||
->orWhere('realname','like','%' . $params['ambiguous']['string'] . '%');
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
//增加根据优惠券名称搜索订单
|
||
if($params['ambiguous']['field'] == 'coupon'){
|
||
call_user_func(function () use (&$order_builder, $params) {
|
||
list($field, $value) = explode(':', $params['ambiguous']['string']);
|
||
if (isset($value)) {
|
||
return $order_builder->where($field, $value);
|
||
} else {
|
||
return $order_builder->whereHas('coupons', function ($query) use ($params) {
|
||
return $query->where('name','like','%' . $params['ambiguous']['string'] . '%');
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
//订单商品
|
||
if ($params['ambiguous']['field'] == 'order_goods') {
|
||
$order_builder->whereHas('hasManyOrderGoods', function ($query) use ($params) {
|
||
$query->searchLike($params['ambiguous']['string']);
|
||
});
|
||
}
|
||
|
||
//商品id
|
||
if ($params['ambiguous']['field'] == 'goods_id') {
|
||
// print_r($order_builder->whereHas('hasManyOrderGoods', function ($query) use ($params) {
|
||
// $query->where('goods_id',$params['ambiguous']['string']);
|
||
// })->toSql());exit;
|
||
$order_builder->whereHas('hasManyOrderGoods', function ($query) use ($params) {
|
||
$query->where('goods_id',$params['ambiguous']['string']);
|
||
});
|
||
}
|
||
//快递单号
|
||
if ($params['ambiguous']['field'] == 'dispatch') {
|
||
$order_builder->whereHas('express', function ($query) use ($params) {
|
||
$query->searchLike($params['ambiguous']['string']);
|
||
});
|
||
}
|
||
|
||
}
|
||
//支付方式
|
||
if (array_get($params, 'pay_type', '')) {
|
||
/* 改为按支付分组方式查询后,该部分被替换
|
||
$order_builder->where('pay_type_id', $params['pay_type']);
|
||
*/
|
||
//改为支付分组查询,前端传入支付分组id,在该处通过分组id获取组中所有成员,这些成员就是确切的支付方式
|
||
//如前端传入的分组id为2,对应的是支付宝支付分组,然后查找属于支付宝支付组的支付方式,找到如支付宝,支付宝-yz这些具体支付方式
|
||
//获取到确切的支付方式后,对查询条件进行拼接
|
||
$payTypeGroup = PayTypeGroup::with('hasManyPayType')->find($params['pay_type']);
|
||
|
||
if($payTypeGroup)
|
||
{
|
||
$payTypes = $payTypeGroup->toArray();
|
||
if($payTypes['has_many_pay_type']) {
|
||
$pay_type_ids = array_column($payTypes['has_many_pay_type'], 'id');
|
||
$order_builder->whereIn('yz_order.pay_type_id', $pay_type_ids);
|
||
}
|
||
}
|
||
}
|
||
//操作时间范围
|
||
if (array_get($params, 'time_range.field', '') && array_get($params, 'time_range.start', 0) && array_get($params, 'time_range.end', 0)) {
|
||
$range = [strtotime($params['time_range']['start']), strtotime($params['time_range']['end'])];
|
||
$order_builder->whereBetween($params['time_range']['field'], $range);
|
||
}
|
||
return $order_builder;
|
||
|
||
|
||
}
|
||
|
||
public static function agentOrder($orderModel)
|
||
{
|
||
$orderAgent = self::getOrderAgent($orderModel->address);
|
||
if (!$orderAgent) {
|
||
return;
|
||
}
|
||
|
||
AgentOrder::create([
|
||
'order_id' => $orderModel->id,
|
||
'agent_id' => $orderAgent->id,
|
||
'plugin_id' => $orderModel->plugin_id
|
||
]);
|
||
}
|
||
|
||
private static function getOrderAgent($orderAddress)
|
||
{
|
||
if ($orderAddress->street_id) {
|
||
$agent = AreaDividendAgent::getSpecifiedAgent($orderAddress->street_id, 'street_id', 4);
|
||
}
|
||
if ($orderAddress->district_id && !$agent) {
|
||
$agent = AreaDividendAgent::getSpecifiedAgent($orderAddress->district_id, 'district_id', 3);
|
||
}
|
||
if ($orderAddress->city_id && !$agent) {
|
||
$agent = AreaDividendAgent::getSpecifiedAgent($orderAddress->city_id, 'city_id', 2);
|
||
}
|
||
if ($orderAddress->province_id && !$agent) {
|
||
$agent = AreaDividendAgent::getSpecifiedAgent($orderAddress->province_id, 'province_id', 1);
|
||
}
|
||
return $agent;
|
||
}
|
||
} |