修改首页配送统计

This commit is contained in:
luofei 2024-06-08 17:37:59 +08:00
parent 4e9e6d0410
commit b9c3413336
2 changed files with 123 additions and 39 deletions

View File

@ -3,6 +3,7 @@
namespace app\store\controller;
use app\common\controller\Definitions;
use app\store\lists\store_order\StoreOrderLists;
use app\store\logic\WorkbenchLogic;
use hg\apidoc\annotation as ApiDoc;
@ -49,7 +50,7 @@ class WorkbenchController extends BaseAdminController
}
#[
ApiDoc\Title('配送统计(暂时不用)'),
ApiDoc\Title('配送统计'),
ApiDoc\url('/store/workbench/delivery'),
ApiDoc\Method('GET'),
ApiDoc\NotHeaders(),
@ -61,8 +62,29 @@ class WorkbenchController extends BaseAdminController
]
public function delivery()
{
$storeId = $this->request->adminInfo['store_id'];
$result = WorkbenchLogic::delivery($storeId);
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
$result = WorkbenchLogic::delivery($params);
return $this->data($result);
}
#[
ApiDoc\Title('配送订单统计'),
ApiDoc\url('/store/workbench/deliveryOrder'),
ApiDoc\Method('GET'),
ApiDoc\NotHeaders(),
ApiDoc\Author('中国队长'),
ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'),
ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\Query(ref: [Definitions::class, "page"]),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function deliveryOrder()
{
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
$result = WorkbenchLogic::deliveryOrder($params);
return $this->data($result);
}
@ -120,40 +142,4 @@ class WorkbenchController extends BaseAdminController
return $workbench->get_product_ranking();
}
#[
ApiDoc\Title('用户统计-概况'),
ApiDoc\url('/store/workbench/get_user_basic'),
ApiDoc\Method('GET'),
ApiDoc\NotHeaders(),
ApiDoc\Author('中国队长'),
ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'),
ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function get_user_basic(\app\admin\controller\WorkbenchController $workbench)
{
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
return $workbench->get_user_basic();
}
#[
ApiDoc\Title('用户统计-图表'),
ApiDoc\url('/store/workbench/get_user_trend'),
ApiDoc\Method('GET'),
ApiDoc\NotHeaders(),
ApiDoc\Author('中国队长'),
ApiDoc\Query(name: 'start_time', type: 'string', require: true, desc: '开始时间'),
ApiDoc\Query(name: 'end_time', type: 'string', require: true, desc: '结束时间'),
ApiDoc\Header(ref: [Definitions::class, "token"]),
ApiDoc\ResponseSuccess("data", type: "array"),
]
public function get_user_trend(\app\admin\controller\WorkbenchController $workbench)
{
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
return $workbench->get_user_trend();
}
}

View File

@ -431,4 +431,102 @@ class WorkbenchLogic extends BaseLogic
return $data;
}
public static function delivery($params)
{
$data = [];
$storeId = $params['store_id'];
$startTime = $params['start_time'];
$endTime = $params['end_time'];
$endTime = date('Y-m-d', strtotime($endTime) + 86400);
$dateDiff = (new \DateTime($endTime))->diff(new \DateTime($startTime));
$timeRange = [];
if ($dateDiff->days == 1) {
$group = 'HOUR(pay_time)';
$i = 0;
while ($i < 24) {
$timeRange[] = date('H', strtotime("+$i hours", strtotime($startTime)));
$i++;
}
$field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price,count(id) as order_num';
} elseif ($dateDiff->days <= 31) {
$group = 'DAY(pay_time)';
$i = 0;
while ($i < $dateDiff->days) {
$timeRange[] = date('m-d', strtotime("+$i days", strtotime($startTime)));
$i++;
}
$field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price,count(id) as order_num';
} else {
$group = 'MONTH(pay_time)';
$i = 0;
$month = 0;
if ($dateDiff->y > 0) {
$month = $dateDiff->y * 12;
}
if ($dateDiff->m > 0) {
$month += $dateDiff->m;
}
if ($dateDiff->d > 0) {
$month += 1;
}
while ($i < $month) {
$timeRange[] = date('Y-m', strtotime("+$i months", strtotime($startTime)));
$i++;
}
$field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price,count(id) as order_num';
}
$amountList = StoreOrder::field($field)
->where('store_id', $storeId)
->where('paid', 1)
->where('shipping_type', 1)
->whereBetweenTime('pay_time', $startTime, $endTime)
->group($group)
->select()
->toArray();
$amountList = reset_index($amountList, 'pay_time');
$amountListTmp = [];
$countListTmp = [];
$range = [];
foreach ($timeRange as $item) {
$range[] = $item;
if (!isset($amountList[$item])) {
$amountListTmp[$item] = 0;
} else {
$amountListTmp[$item] = $amountList[$item]['pay_price'];
}
if (!isset($amountList[$item])) {
$countListTmp[$item] = 0;
} else {
$countListTmp[$item] = $amountList[$item]['order_num'];
}
}
$data['statistics'] = [
'range' => $range,
'data' => [
'order_amount' => array_values($amountListTmp),
'order_count' => array_values($countListTmp)
]
];
return $data;
}
public static function deliveryOrder($params)
{
$startTime = $params['start_time'];
$endTime = $params['end_time'];
$endTime = date('Y-m-d', strtotime($endTime) + 86400);
$query = StoreOrder::with('user')->where('store_id', $params['store_id'])
->where('paid', 1)
->where('shipping_type', 1)
->whereBetweenTime('create_time', $startTime, $endTime);
$data['count'] = $query->count();
$data['page_no'] = $params['page_no'];
$data['page_size'] = $params['page_size'];
$data['extend'] = [];
$data['lists'] = $query->order('create_time', 'desc')
->page($params['page_no'], $params['page_size'])
->select()->toArray();
return $data;
}
}