You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
8.5 KiB
203 lines
8.5 KiB
<?php
|
|
|
|
|
|
namespace App\Admin\Common;
|
|
|
|
|
|
use App\Models\ImsCjdcOrderMain;
|
|
use App\Models\LanzuUserRelationBind;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class Order
|
|
{
|
|
/**
|
|
* 查询结果
|
|
* @param $params
|
|
* @param $option
|
|
* @return int
|
|
*/
|
|
public static function getOrderData($params,$option)
|
|
{
|
|
//如果市场id存在
|
|
$buildData = self::buildData();
|
|
if ($params['market_id']) {//是否存在市场id
|
|
if (is_array($params['market_id'])) {
|
|
$buildData = $buildData->whereIn('lanzu_order_main.market_id',$params['market_id']);
|
|
} else {
|
|
$buildData = $buildData->where('lanzu_order_main.market_id',$params['market_id']);
|
|
}
|
|
}
|
|
if ($params['user_type']=='lanzu_cs'||$params['user_type']=='lanzu_biz'){//社区角色
|
|
if ($params['condition']==1){//获取时间范围
|
|
list($beginTime,$endTime) = self::beginAndEnd($option);
|
|
//>>2.构建数据模型
|
|
if ($params['user_ids']){
|
|
$buildData = $buildData->whereIn('lanzu_order_main.user_id',$params['user_ids']);
|
|
}else{
|
|
return 0;
|
|
}
|
|
$buildData = $buildData
|
|
->leftJoin('lanzu_user_relation_bind','lanzu_user_relation_bind.user_id','lanzu_order_main.user_id')
|
|
->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
|
|
->whereBetween('lanzu_order_main.created_at',[$beginTime,$endTime]);
|
|
}
|
|
}elseif ($params['user_type']=='market_service'||$params['user_type']=='lanzu_mp'){//市场服务站角色 及服务商
|
|
if ($params['condition']==1){//获取时间范围
|
|
list($beginTime,$endTime) = self::beginAndEnd($option);
|
|
//>>2.构建数据模型
|
|
$buildData = $buildData->whereBetween('created_at',[$beginTime,$endTime]);
|
|
}
|
|
}
|
|
if ($params['data_type']=='count_user'){//用户数
|
|
$result = $buildData
|
|
->groupBy('lanzu_order_main.user_id')
|
|
->pluck('lanzu_order_main.user_id')
|
|
->toArray();
|
|
$result = count($result);
|
|
}elseif ($params['data_type']=='number'){//订单数
|
|
$result = $buildData->count();
|
|
}elseif ($params['data_type']=='amount'){//订单金额
|
|
$result = $buildData->sum('money');
|
|
}elseif ($params['data_type']=='shipping'){//配送方式
|
|
$data = $buildData->selectRaw("count(*) as shipping,shipping_type")->whereIn('shipping_type',[1,3])->groupBy('shipping_type')->orderBy('shipping_type')->get()->toArray();
|
|
|
|
$result[0] = isset($data[0]['shipping'])&&($data[0]['shipping_type']=='1')?$data[0]['shipping']:0;
|
|
$result[1] = isset($data[1]['shipping'])&&($data[1]['shipping_type']=='3')?$data[1]['shipping']:0;
|
|
}elseif ($params['data_type']=='distance'){
|
|
$data = $buildData->selectRaw(
|
|
"ELT(INTERVAL(lanzu_order_main.delivery_distance,0,1000,3000,5000),'d1','d2','d3','d4') as delivery,count(lanzu_order_main.id) as num"
|
|
)->groupBy('delivery')->orderBy('delivery')->get()->toArray();
|
|
|
|
$result[0] = isset($data[0]['delivery'])&&($data[0]['delivery']=='d1')?$data[0]['num']:0;
|
|
$result[1] = isset($data[1]['delivery'])&&($data[1]['delivery']=='d2')?$data[1]['num']:0;
|
|
$result[2] = isset($data[2]['delivery'])&&($data[2]['delivery']=='d3')?$data[2]['num']:0;
|
|
$result[3] = isset($data[3]['delivery'])&&($data[3]['delivery']=='d4')?$data[3]['num']:0;
|
|
}elseif ($params['data_type']=='order_count_user'){
|
|
$result = DB::table('lanzu_order_main')
|
|
->whereIn('state', [3, 4, 5, 10,11])
|
|
->where('type',1)
|
|
->pluck('user_id')
|
|
->toArray();
|
|
$result = count(array_unique($result));
|
|
}elseif ($params['data_type']=='num'){
|
|
$result = $buildData->pluck('user_id');
|
|
if ($result){
|
|
$result = $result->toArray();
|
|
}
|
|
}elseif ($params['data_type']=='market'){
|
|
$data = $buildData
|
|
->selectRaw("count(*) as num,case market_id when 2 then 'dg' when 3 then 'tg' when 4 then 'hy' when 5 then 'px' end 'name'")
|
|
->groupBy('market_id')
|
|
->orderBy('market_id')
|
|
->get()->toArray();
|
|
$arr = ['dg','tg','hy','px'];
|
|
$result = [];
|
|
foreach ($arr as $key=>$item){
|
|
foreach ($data as $val){
|
|
if ($item==$val['name']){
|
|
$result[$key] = $val['num'];
|
|
break;
|
|
}
|
|
}
|
|
if (!isset($result[$key])){
|
|
$result[$key] = 0;
|
|
}
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 查询时间范围
|
|
* @param $option
|
|
* @return array
|
|
*/
|
|
public static function beginAndEnd($option)
|
|
{
|
|
switch ($option) {
|
|
case '1':
|
|
$beginTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
|
|
$endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
|
|
break;
|
|
case '-1':
|
|
$beginTime = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
|
|
$endTime = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
|
|
break;
|
|
case '7':
|
|
$beginTime = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
|
|
$endTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
|
|
break;
|
|
case 'w':
|
|
//$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
|
|
//$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
|
|
|
|
//当前日期
|
|
$sdefaultDate = date("Y-m-d");
|
|
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
|
|
$first=1;
|
|
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
|
|
$w=date('w',strtotime($sdefaultDate));
|
|
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
|
|
$beginTime=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
|
|
//本周结束日期
|
|
$start = date('Y-m-d',$beginTime);
|
|
$endTime=strtotime("$start +6 days")+86399;
|
|
break;
|
|
case 'lw':
|
|
$beginTime = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y"));
|
|
$endTime = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y"));
|
|
break;
|
|
case '30':
|
|
$beginTime = mktime(0, 0, 0, date('m'), 1, date('Y'));
|
|
$endTime = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
|
|
break;
|
|
case '-30':
|
|
$beginTime = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
|
|
$endTime = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y"))));
|
|
break;
|
|
default:
|
|
$searchTime = request()->input('created_at');
|
|
if ($searchTime){
|
|
$beginTime = strtotime($searchTime['start']);
|
|
$endTime = strtotime($searchTime['end']);
|
|
}else{
|
|
$beginTime = 0;
|
|
$endTime = 999999999999;
|
|
}
|
|
break;
|
|
}
|
|
return [$beginTime,$endTime];
|
|
}
|
|
|
|
/**
|
|
* 构建数据模型
|
|
* @param $condition
|
|
* @return mixed
|
|
*/
|
|
public static function buildData()
|
|
{
|
|
$build = ImsCjdcOrderMain
|
|
::whereIn('state', [3, 4, 5, 10,11])
|
|
->where('type',1);
|
|
return $build;
|
|
}
|
|
|
|
/**
|
|
* 查询用户绑定后下过单的用户id
|
|
* @param $adminUserId
|
|
* @return mixed
|
|
*/
|
|
public static function getUserOrder($adminUserId)
|
|
{
|
|
$uid = LanzuUserRelationBind::where('source_id',$adminUserId)
|
|
->leftJoin('lanzu_order_main','lanzu_order_main.user_id','lanzu_user_relation_bind.user_id')
|
|
->whereRaw('lanzu_order_main.created_at > lanzu_user_relation_bind.created_at')
|
|
->groupBy('lanzu_order_main.user_id')
|
|
->pluck('lanzu_order_main.user_id')
|
|
->toArray();//获取绑定社区的所有用户
|
|
return $uid;
|
|
}
|
|
|
|
|
|
|
|
}
|