feat: 修改了工作台控制器,添加了商品浏览量、支付金额和退款金额的API接口;修复了当订单金额为0时,支付转化率计算错误的bug;重构了代码,使其更加简洁易读。

This commit is contained in:
mkm 2024-06-12 18:16:53 +08:00
parent 06258e7854
commit ac86bf341f
2 changed files with 216 additions and 428 deletions

View File

@ -15,6 +15,8 @@
namespace app\admin\controller;
use app\admin\logic\WorkbenchLogic;
use DateInterval;
use DateTime;
/**
* 工作台
@ -250,186 +252,41 @@ class WorkbenchController extends BaseAdminController
*/
public function get_trend()
{
$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" => [
"2024-05-06",
"2024-05-07",
"2024-05-08",
"2024-05-09",
"2024-05-10",
"2024-05-11",
"2024-05-12",
"2024-05-13",
"2024-05-14",
"2024-05-15",
"2024-05-16",
"2024-05-17",
"2024-05-18",
"2024-05-19",
"2024-05-20",
"2024-05-21",
"2024-05-22",
"2024-05-23",
"2024-05-24",
"2024-05-25",
"2024-05-26",
"2024-05-27",
"2024-05-28",
"2024-05-29",
"2024-05-30",
"2024-05-31",
"2024-06-01",
"2024-06-02",
"2024-06-03",
"2024-06-04"
],
"xAxis" => $dates,
"series" => [
[
"name" => "商品浏览量",
"data" => [
131,
275,
137,
100,
221,
76,
64,
83,
161,
125,
120,
971,
169,
84,
68,
153,
470,
1310,
621,
827,
113,
595,
485,
484,
535,
227,
714,
144,
1159,
731
],
"data" => WorkbenchLogic::store_visit_count($dates),
"type" => "line",
"smooth" => "true",
"yAxisIndex" => 1
],
[
"name" => "商品访客量",
"data" => [
21,
26,
26,
32,
26,
14,
6,
13,
17,
26,
24,
18,
7,
6,
23,
23,
23,
33,
29,
61,
25,
103,
55,
45,
45,
30,
28,
25,
41,
21
],
"data" => WorkbenchLogic::store_visit_user($dates),
"type" => "line",
"smooth" => "true",
"yAxisIndex" => 1
],
[
"name" => "支付金额",
"data" => [
53.6,
0.25,
1231.84,
0.8,
0,
10,
0,
0.66,
7919.2,
0,
8040.38,
0.01,
0,
0,
10.49,
0,
0.01,
599.01,
100100,
908.81,
0,
1887.06,
588,
184.3,
665.11,
0,
0,
219.2,
24.09,
16.99
],
"data" => WorkbenchLogic::payPrice($dates),
"type" => "bar"
],
[
"name" => "退款金额",
"data" => [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
730.8,
49,
219,
0,
1323.01,
0,
0,
0,
0,
0,
0,
0,
0
],
"data" => WorkbenchLogic::refundPrice($dates),
"type" => "bar"
]
]
@ -462,44 +319,6 @@ class WorkbenchController extends BaseAdminController
"stock" => 16777213,
"is_show" => 1
],
[
"product_id" => 1670,
"visit" => "949",
"user" => 161,
"cart" => "20",
"orders" => "47",
"pay" => "34",
"price" => "4347.45",
"cost" => "0.00",
"profit" => "1.00",
"collect" => "2",
"changes" => "0.13",
"repeats" => "0.14",
"store_name" => "ECOK2024年春秋季美式复古美拉德风高级感撞色领拉链夹克外套女潮",
"image" => "https://multi-store.crmeb.net/uploads/attach/2024/02/19/5bf089c30da5bdddd815fbf2cba148e6.jpg",
"product_price" => "169.90",
"stock" => 2980,
"is_show" => 1
],
[
"product_id" => 2033,
"visit" => "743",
"user" => 139,
"cart" => "30",
"orders" => "52",
"pay" => "20",
"price" => "3930.50",
"cost" => "0.00",
"profit" => "1.00",
"collect" => "1",
"changes" => "0.04",
"repeats" => "0.50",
"store_name" => "【618抢先购】CT四色眼影盘新色枕边话哑光亮片粉棕彩妆官方正品",
"image" => "https://multi-store.crmeb.net/uploads/attach/2024/05/24/fc63ee052b9dc66bd70a1e013510ded2.jpg",
"product_price" => "490.00",
"stock" => 139994,
"is_show" => 1
],
];
return $this->data($data);
}
@ -798,6 +617,23 @@ class WorkbenchController extends BaseAdminController
//当日订单金额
public function top_trade()
{
$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');
}
$times = date('Y-m-d');
$yesterday= date('Y-m-d', strtotime('-1 day'));
$dates_two=[];
for ($i = 0; $i < 24; $i++) {
$dates_two[]=[$times." ".$i.":00:00",$times." ".$i.":59:59"];
}
$data = [
"left" => [
"name" => "当日订单金额",
@ -829,66 +665,8 @@ class WorkbenchController extends BaseAdminController
"24"
],
"series" => [
[
"money" => 279.47,
"value" => [
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"4.48",
"257.20",
"0.00",
"16.99",
"0.80",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00"
]
],
[
"money" => 10175.64,
"value" => [
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.00",
"0.01",
"0.00",
"0.00",
"40.00",
"0.10",
"9938.00",
"0.08",
"30.16",
"0.00",
"0.00",
"0.00",
"163.20",
"4.09",
"0.00",
"0.00"
]
]
WorkbenchLogic::day_order_pay_price($dates_two,$times),
WorkbenchLogic::day_order_pay_price($dates_two,$yesterday),
]
],
"right" => [
@ -921,151 +699,13 @@ class WorkbenchController extends BaseAdminController
"24"
],
"series" => [
[
"name" => "今日订单数",
"now_money" => 8,
"last_money" => 13,
"rate" => "-38.46",
"value" => [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
1,
0,
1,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]
],
[
"name" => "今日支付人数",
"now_money" => 5,
"last_money" => 8,
"rate" => "-37.50",
"value" => [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2,
1,
0,
1,
2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]
]
WorkbenchLogic::day_order_count($dates_two,'今日订单数'),
WorkbenchLogic::day_order_user($dates_two,'今日支付人数'),
]
],
"month" => [
[
"name" => "本月订单数",
"now_money" => 30,
"last_money" => 285,
"rate" => "-89.47",
"value" => [
"2024-06-01" => 2,
"2024-06-02" => 7,
"2024-06-03" => 13,
"2024-06-04" => 8,
"2024-06-05" => 0,
"2024-06-06" => 0,
"2024-06-07" => 0,
"2024-06-08" => 0,
"2024-06-09" => 0,
"2024-06-10" => 0,
"2024-06-11" => 0,
"2024-06-12" => 0,
"2024-06-13" => 0,
"2024-06-14" => 0,
"2024-06-15" => 0,
"2024-06-16" => 0,
"2024-06-17" => 0,
"2024-06-18" => 0,
"2024-06-19" => 0,
"2024-06-20" => 0,
"2024-06-21" => 0,
"2024-06-22" => 0,
"2024-06-23" => 0,
"2024-06-24" => 0,
"2024-06-25" => 0,
"2024-06-26" => 0,
"2024-06-27" => 0,
"2024-06-28" => 0,
"2024-06-29" => 0,
"2024-06-30" => 0
]
],
[
"name" => "本月支付人数",
"now_money" => 15,
"last_money" => 79,
"rate" => "-81.01",
"value" => [
"2024-06-01" => 1,
"2024-06-02" => 4,
"2024-06-03" => 8,
"2024-06-04" => 5,
"2024-06-05" => 0,
"2024-06-06" => 0,
"2024-06-07" => 0,
"2024-06-08" => 0,
"2024-06-09" => 0,
"2024-06-10" => 0,
"2024-06-11" => 0,
"2024-06-12" => 0,
"2024-06-13" => 0,
"2024-06-14" => 0,
"2024-06-15" => 0,
"2024-06-16" => 0,
"2024-06-17" => 0,
"2024-06-18" => 0,
"2024-06-19" => 0,
"2024-06-20" => 0,
"2024-06-21" => 0,
"2024-06-22" => 0,
"2024-06-23" => 0,
"2024-06-24" => 0,
"2024-06-25" => 0,
"2024-06-26" => 0,
"2024-06-27" => 0,
"2024-06-28" => 0,
"2024-06-29" => 0,
"2024-06-30" => 0
]
]
WorkbenchLogic::month_order_count($dates_two,'本月订单数'),
WorkbenchLogic::month_order_count($dates_two,'本月支付人数')
]
]
];

View File

@ -57,8 +57,8 @@ class WorkbenchLogic extends BaseLogic
$user = 0;
$cart = Cart::where($where)->where('is_fail', 0)->sum('cart_num');
$order = StoreOrder::where($where)->count();
$pay=StoreOrder::where($where)->where('paid',1)->count();
$payPrice=StoreOrder::where($where)->where('paid',1)->count('pay_price');
$pay = StoreOrder::where($where)->where('paid', 1)->where('refund_status', 0)->count();
$payPrice = StoreOrder::where($where)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
$cost = StoreOrder::where($where)->where('paid', 1)->sum('cost');
$refundPrice = StoreOrder::where($where)->where('status', 'in', [-1, -2])->sum('refund_price');
$refund = StoreOrder::where($where)->where('status', 'in', [-1, -2])->count();
@ -76,4 +76,152 @@ class WorkbenchLogic extends BaseLogic
'payPercent' => ['num' => $payPercent, 'title' => '支付转化率'], //支付转化率
];
}
/**
* 商品浏览量
*/
public static function store_visit_count($dates)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->cache('store_visit_count_' . $date, 300)->sum('count');
}
return $data;
}
/**
* 商品浏览量
*/
public static function store_visit_user($dates)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreVisit::whereDay('create_time', $date)->cache('store_visit_user_' . $date, 300)->count('uid');
}
return $data;
}
/**
* 支付金额
*/
public static function payPrice($dates)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->cache('payPrice_' . $date, 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
}
return $data;
}
/**
* 退款金额
*/
public static function refundPrice($dates)
{
$data = [];
foreach ($dates as $date) {
$data[] = StoreOrder::whereDay('create_time', $date)->where('status', 'in', [-1, -2])->cache('refundPrice_' . $date, 300)->where('paid', 1)->sum('pay_price');
}
return $data;
}
/**
* 当日订单金额
*/
public static function day_order_pay_price($where, $time)
{
$money = StoreOrder::whereDay('create_time', $time)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
$data = [];
foreach ($where as $date) {
$data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_pay_price_' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->sum('pay_price');
}
return ['money' => $money, 'value' => $data];
}
/**
* 今日订单数
*/
public static function day_order_count($where,$name)
{
$now_money = StoreOrder::whereDay('create_time')->where('paid', 1)->where('refund_status', 0)->count();
$last_money = StoreOrder::whereDay('create_time', 'yesterday')->where('paid', 1)->where('refund_status', 0)->count();
$data = [];
foreach ($where as $date) {
$data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_count' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->count();
}
if ($now_money > $last_money) {
if($last_money == 0){
$rate = bcmul($now_money, '100');
}else{
$rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
}
} else {
$rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
}
return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
}
/**
* 今日支付人数
*/
public static function day_order_user($where,$name)
{
$now_money = StoreOrder::whereDay('create_time')->where('paid', 1)->where('refund_status', 0)->count('uid');
$last_money = StoreOrder::whereDay('create_time', 'yesterday')->where('paid', 1)->where('refund_status', 0)->count('uid');
$data = [];
foreach ($where as $date) {
$data[] = StoreOrder::where('create_time','between', $date)->cache('day_order_count' . $date[0], 300)->where('paid', 1)->where('refund_status', 0)->count('uid');
}
if ($now_money > $last_money) {
if($last_money == 0){
$rate = bcmul($now_money, '100');
}else{
$rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
}
} else {
$rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
}
return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
}
/**
* 本月订单数
*/
public static function month_order_count($name)
{
$now_money = StoreOrder::whereMonth('create_time')->where('paid', 1)->where('refund_status', 0)->count();
$last_money = StoreOrder::whereMonth('create_time', 'last month')->where('paid', 1)->where('refund_status', 0)->count();
$data = [];
if ($now_money > $last_money) {
if($last_money == 0){
$rate = bcmul($now_money, '100');
}else{
$rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
}
} else {
$rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
}
return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
}
/**
* 本月支付人数
*/
public static function month_order_user($name)
{
$now_money = StoreOrder::whereMonth('create_time')->where('paid', 1)->where('refund_status', 0)->count('uid');
$last_money = StoreOrder::whereMonth('create_time','last month')->where('paid', 1)->where('refund_status', 0)->count('uid');
$data = [];
if ($now_money > $last_money) {
if($last_money == 0){
$rate = bcmul($now_money, '100');
}else{
$rate = bcmul(bcmul($now_money, '100'), bcdiv($last_money, $now_money, 2), 2);
}
} else {
$rate = bcmul(bcmul($last_money, '100'), bcdiv($now_money, $last_money, 2), 2);
}
return ['name' => $name, 'now_money' => $now_money, 'last_money' => $last_money, 'rate' => $rate, 'value' => $data];
}
}