修改门店商品排行相关的查询逻辑

This commit is contained in:
liu 2024-06-20 14:41:04 +08:00
parent 24fd63e880
commit 1051e0c7a9
2 changed files with 134 additions and 8 deletions

View File

@ -130,11 +130,12 @@ class WorkbenchController extends BaseAdminController
// ApiDoc\Header(ref: [Definitions::class, "token"]),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function get_trend(\app\admin\controller\WorkbenchController $workbench)
public function get_trend()
{
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
return $workbench->get_trend();
// $params = $this->request->get();
$store_id = $this->request->adminInfo['store_id'];
$workbench = WorkbenchLogic::get_trend($store_id);
return $this->data($workbench);
}
// #[
@ -148,11 +149,25 @@ class WorkbenchController extends BaseAdminController
// ApiDoc\Header(ref: [Definitions::class, "token"]),
// ApiDoc\ResponseSuccess("data", type: "array"),
// ]
public function get_product_ranking(\app\admin\controller\WorkbenchController $workbench)
public function get_product_ranking()
{
$params = $this->request->get();
$params['store_id'] = $this->request->adminInfo['store_id'];
return $workbench->get_product_ranking();
// $params = $this->request->get();
$dateRange = $this->request->get('date');
// 拆分日期范围
list($startDate, $endDate) = explode('-', $dateRange);
$startTime = str_replace('/', '-', $startDate);
$endTime = str_replace('/', '-', $endDate);
if (empty($startTime)) {
$startTime = strtotime(date('Y-m-d'));
$endTime = $startTime + 86400;
}
$where = [
['create_time', 'between', [$startTime, $endTime]],
['store_id','=',$this->request->adminInfo['store_id']]
];
$workbench = WorkbenchLogic::get_product_ranking($where);
return $this->data($workbench);
}

View File

@ -15,6 +15,7 @@
namespace app\store\logic;
use app\admin\logic\statistic\ProductStatisticLogic;
use app\common\enum\OrderEnum;
use app\common\enum\PayEnum;
use app\common\enum\YesNoEnum;
@ -725,6 +726,116 @@ class WorkbenchLogic extends BaseLogic
/**
* 商品趋势
*/
public static function get_trend($store_id)
{
$dates = [];
$today = new \DateTime();
$thirtyDaysAgo = new \DateTime($today->format('Y-m-d'));
$thirtyDaysAgo->modify('-30 days');
for ($i = 0; $i < 31; $i++) {
$date = new \DateTime($thirtyDaysAgo->format('Y-m-d'));
$date->modify('+' . $i . ' days');
$dates[] = $date->format('Y-m-d');
}
$data = [
"xAxis" => $dates,
"series" => [
[
"name" => "商品浏览量",
"data" => self::store_visit_count($dates,$store_id),
"type" => "line",
"smooth" => "true",
"yAxisIndex" => 1
],
[
"name" => "商品访客量",
"data" => self::store_visit_user($dates,$store_id),
"type" => "line",
"smooth" => "true",
"yAxisIndex" => 1
],
[
"name" => "支付金额",
"data" => self::payPrice($dates,$store_id),
"type" => "bar"
],
[
"name" => "退款金额",
"data" => self::refundPrice($dates,$store_id),
"type" => "bar"
]
]
];
return $data;
}
/**
* 商品浏览量
*/
public static function store_visit_count($dates,$store_id)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_count_' . $date, 300)->sum('count');
}
return $data;
}
/**
* 商品浏览量
*/
public static function store_visit_user($dates,$store_id)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->where('store_id',$store_id)->cache('store_visit_user_' . $date, 300)->count('uid');
}
return $data;
}
/**
* 支付金额
*/
public static function payPrice($dates,$store_id)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
}
return $data;
}
/**
* 退款金额
*/
public static function refundPrice($dates,$store_id)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->where('store_id',$store_id)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price');
}
return $data;
}
/**
* 获取商品排名数据
*/
public static function get_product_ranking($where)
{
return (new ProductStatisticLogic())->get_product_ranking($where);
}
}