添加统计逻辑
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
use app\admin\model\OrderGoods;
|
||||
use app\admin\model\OrderRecharge;
|
||||
use app\admin\model\OrderYearcard;
|
||||
use app\admin\model\User;
|
||||
use app\common\server\UrlServer;
|
||||
use think\facade\Url;
|
||||
use app\admin\model\Order;
|
||||
|
||||
class BulletinBoard extends AdminBase
|
||||
{
|
||||
@@ -11,6 +15,108 @@ class BulletinBoard extends AdminBase
|
||||
//看板统计
|
||||
public function index()
|
||||
{
|
||||
//查询商品订单
|
||||
$orderMoney = Order::where('status','<>', 0)->sum('order_amount');
|
||||
//年卡订单
|
||||
$yearCardOrderMoney = OrderYearcard::where('status', 1)->sum('pay_fee');
|
||||
//订单管理
|
||||
$orderGoods = OrderGoods::where('status', 1)->sum('total_fee');
|
||||
//充值订单
|
||||
$rechargeOrderMoney = OrderRecharge::where('pay_status', 1)->sum('order_amount');
|
||||
|
||||
// 计算月销售总额(所有订单类型的总和)
|
||||
$monthlySales = ($orderMoney ?: 0) + ($yearCardOrderMoney ?: 0) + ($orderGoods ?: 0) + ($rechargeOrderMoney ?: 0);
|
||||
|
||||
// 计算月订单量(本月订单数量)
|
||||
$monthlyOrders = Order::where('status','<>', 0)
|
||||
->whereTime('create_time', 'month')
|
||||
->count();
|
||||
$monthlyOrders += OrderYearcard::where('status', 1)
|
||||
->whereTime('createtime', 'month')
|
||||
->count();
|
||||
$monthlyOrders += OrderGoods::where('status', 1)
|
||||
->whereTime('createtime', 'month')
|
||||
->count();
|
||||
$monthlyOrders += OrderRecharge::where('pay_status', 1)
|
||||
->whereTime('create_time', 'month')
|
||||
->count();
|
||||
|
||||
// 计算月客户数(本月新增用户)
|
||||
$monthlyCustomers = User::whereTime('create_time', 'month')
|
||||
->where('del', 0)
|
||||
->count();
|
||||
|
||||
// 计算同比(上个月的数据)
|
||||
$lastMonthSales = Order::where('status','<>', 0)
|
||||
->whereTime('create_time', 'last month')
|
||||
->sum('order_amount');
|
||||
$lastMonthSales += OrderYearcard::where('status', 1)
|
||||
->whereTime('createtime', 'last month')
|
||||
->sum('pay_fee');
|
||||
$lastMonthSales += OrderGoods::where('status', 1)
|
||||
->whereTime('createtime', 'last month')
|
||||
->sum('total_fee');
|
||||
$lastMonthSales += OrderRecharge::where('pay_status', 1)
|
||||
->whereTime('create_time', 'last month')
|
||||
->sum('order_amount');
|
||||
|
||||
$lastMonthOrders = Order::where('status','<>', 0)
|
||||
->whereTime('create_time', 'last month')
|
||||
->count();
|
||||
$lastMonthOrders += OrderYearcard::where('status', 1)
|
||||
->whereTime('createtime', 'last month')
|
||||
->count();
|
||||
$lastMonthOrders += OrderGoods::where('status', 1)
|
||||
->whereTime('createtime', 'last month')
|
||||
->count();
|
||||
$lastMonthOrders += OrderRecharge::where('pay_status', 1)
|
||||
->whereTime('create_time', 'last month')
|
||||
->count();
|
||||
|
||||
$lastMonthCustomers = User::whereTime('create_time', 'last month')
|
||||
->where('del', 0)
|
||||
->count();
|
||||
|
||||
// 计算同比变化率
|
||||
$salesCompare = $lastMonthSales > 0 ? round((($monthlySales - $lastMonthSales) / $lastMonthSales) * 100, 2) : 0;
|
||||
$ordersCompare = $lastMonthOrders > 0 ? round((($monthlyOrders - $lastMonthOrders) / $lastMonthOrders) * 100, 2) : 0;
|
||||
$customersCompare = $lastMonthCustomers > 0 ? round((($monthlyCustomers - $lastMonthCustomers) / $lastMonthCustomers) * 100, 2) : 0;
|
||||
|
||||
// 统计本月客户分析(老客户 vs 新客户)
|
||||
// 本月所有用户ID列表
|
||||
$monthUsers = User::whereTime('create_time', 'month')
|
||||
->where('del', 0)
|
||||
->column('id');
|
||||
|
||||
// 如果本月有用户,查询这些用户中哪些有订单(老客户)
|
||||
$oldCustomerCount = 0;
|
||||
$newCustomerCount = 0;
|
||||
if (!empty($monthUsers)) {
|
||||
// 查询本月注册的用户中,在Order表中有订单的用户(老客户)
|
||||
// 只要有订单就算老客户,不限制订单时间
|
||||
$oldCustomerIds = Order::where('status','<>', 0)
|
||||
->where('user_id', 'in', $monthUsers)
|
||||
->where('user_id', '>', 0)
|
||||
->distinct(true)
|
||||
->column('user_id');
|
||||
|
||||
$oldCustomerCount = count($oldCustomerIds);
|
||||
// 本月注册但没下过单的(新客户)
|
||||
$newCustomerCount = count($monthUsers) - $oldCustomerCount;
|
||||
}
|
||||
|
||||
// 传递数据到视图
|
||||
$this->assign([
|
||||
'monthlySales' => $monthlySales,
|
||||
'monthlyOrders' => $monthlyOrders,
|
||||
'monthlyCustomers' => $monthlyCustomers,
|
||||
'salesCompare' => $salesCompare,
|
||||
'ordersCompare' => $ordersCompare,
|
||||
'customersCompare' => $customersCompare,
|
||||
'oldCustomers' => $oldCustomerCount, // 老客户数量(本月注册且下过单的)
|
||||
'newCustomers' => $newCustomerCount, // 新客户数量(本月注册但没下过单的)
|
||||
]);
|
||||
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
@@ -18,6 +124,14 @@ class BulletinBoard extends AdminBase
|
||||
//地图上显示注册用户位置
|
||||
public function map()
|
||||
{
|
||||
//查询商品订单
|
||||
$orderMoney = Order::where('status','<>', 0)->sum('order_amount');
|
||||
//年卡订单
|
||||
$yearCardOrderMoney = OrderYearcard::where('status', 1)->sum('pay_fee');
|
||||
//订单管理
|
||||
$orderGoods = OrderGoods::where('status', 1)->sum('total_fee');
|
||||
//充值订单
|
||||
$rechargeOrderMoney = OrderRecharge::where('pay_status', 1)->sum('order_amount');
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
|
||||
@@ -130,11 +130,6 @@ class Invoice extends AdminBase
|
||||
}
|
||||
}
|
||||
|
||||
// 处理文件路径(转换为完整URL用于显示)
|
||||
if (!empty($invoice['document'])) {
|
||||
$invoice['document'] = UrlServer::getFileUrl($invoice['document']);
|
||||
}
|
||||
|
||||
// 查询订单列表
|
||||
$orders = Db::name('order')
|
||||
->where('order_status', '>', 0)
|
||||
@@ -143,6 +138,14 @@ class Invoice extends AdminBase
|
||||
->limit(100)
|
||||
->select();
|
||||
|
||||
|
||||
|
||||
$invoice = InvoiceLog::with(['invoice'])->where('id',$id)->find();
|
||||
// 处理文件路径(转换为完整URL用于显示)
|
||||
if (!empty($invoice['document'])) {
|
||||
$currentDomain = $_SERVER['HTTP_HOST'];
|
||||
$invoice['document'] = 'http://'.$currentDomain.'/public/'.$invoice['document'];
|
||||
}
|
||||
$this->assign([
|
||||
'invoice' => $invoice,
|
||||
'orders' => $orders,
|
||||
|
||||
@@ -16,3 +16,4 @@ class Invoice extends Model
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
10
application/admin/model/OrderGoods.php
Normal file
10
application/admin/model/OrderGoods.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class OrderGoods extends Model
|
||||
{
|
||||
protected $name = 'order_service';
|
||||
}
|
||||
10
application/admin/model/OrderRecharge.php
Normal file
10
application/admin/model/OrderRecharge.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class OrderRecharge extends Model
|
||||
{
|
||||
protected $name = 'recharge_order';
|
||||
}
|
||||
10
application/admin/model/OrderYearcard.php
Normal file
10
application/admin/model/OrderYearcard.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class OrderYearcard extends Model
|
||||
{
|
||||
protected $name = 'order_yearcard';
|
||||
}
|
||||
@@ -429,6 +429,21 @@ layui.config({
|
||||
var $ = layui.$
|
||||
,laydate = layui.laydate;
|
||||
|
||||
// 统计数据(从后端传递)
|
||||
var statisticsData = {
|
||||
monthlySales: parseFloat('{$monthlySales|default=0}') || 0,
|
||||
monthlyOrders: parseInt('{$monthlyOrders|default=0}') || 0,
|
||||
monthlyCustomers: parseInt('{$monthlyCustomers|default=0}') || 0,
|
||||
salesCompare: parseFloat('{$salesCompare|default=0}') || 0,
|
||||
ordersCompare: parseFloat('{$ordersCompare|default=0}') || 0,
|
||||
customersCompare: parseFloat('{$customersCompare|default=0}') || 0,
|
||||
oldCustomers: parseInt('{$oldCustomers|default=0}') || 0,
|
||||
newCustomers: parseInt('{$newCustomers|default=0}') || 0
|
||||
};
|
||||
|
||||
// 渲染统计数据
|
||||
renderStatistics();
|
||||
|
||||
// 日期选择器
|
||||
laydate.render({
|
||||
elem: '#date-filter',
|
||||
@@ -439,6 +454,38 @@ layui.config({
|
||||
// 初始化所有图表(仅样式,不填充数据)
|
||||
initCharts();
|
||||
|
||||
// 渲染统计数据
|
||||
function renderStatistics() {
|
||||
// 格式化金额(保留两位小数,添加千分位)
|
||||
function formatMoney(amount) {
|
||||
if (!amount) return '0.00';
|
||||
return parseFloat(amount).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
}
|
||||
|
||||
// 格式化同比显示
|
||||
function formatCompare(compare) {
|
||||
if (compare > 0) {
|
||||
return '<span style="color: #52c41a;">↑ ' + Math.abs(compare).toFixed(2) + '%</span>';
|
||||
} else if (compare < 0) {
|
||||
return '<span style="color: #ff4d4f;">↓ ' + Math.abs(compare).toFixed(2) + '%</span>';
|
||||
} else {
|
||||
return '<span style="color: #999;">持平</span>';
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染月销售总额
|
||||
$('#monthly-sales').text('¥' + formatMoney(statisticsData.monthlySales));
|
||||
$('#sales-compare').html(formatCompare(statisticsData.salesCompare));
|
||||
|
||||
// 渲染月订单量
|
||||
$('#monthly-orders').text(statisticsData.monthlyOrders);
|
||||
$('#orders-compare').html(formatCompare(statisticsData.ordersCompare));
|
||||
|
||||
// 渲染月客户数
|
||||
$('#monthly-customers').text(statisticsData.monthlyCustomers);
|
||||
$('#customers-compare').html(formatCompare(statisticsData.customersCompare));
|
||||
}
|
||||
|
||||
function initCharts() {
|
||||
// 月客户分析 - 饼图
|
||||
var customerChart = echarts.init(document.getElementById('customer-analysis-chart'));
|
||||
@@ -466,8 +513,8 @@ layui.config({
|
||||
formatter: '{b}: {d}%'
|
||||
},
|
||||
data: [
|
||||
{value: 28, name: '老客', itemStyle: {color: '#FFB6C1'}},
|
||||
{value: 72, name: '新客', itemStyle: {color: '#FF69B4'}}
|
||||
{value: statisticsData.oldCustomers, name: '老客', itemStyle: {color: '#FFB6C1'}},
|
||||
{value: statisticsData.newCustomers, name: '新客', itemStyle: {color: '#FF69B4'}}
|
||||
]
|
||||
}]
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
<div id="file-list">
|
||||
{if $invoice.document}
|
||||
<div style="padding: 10px; background: #f5f5f5; border-radius: 4px; margin-bottom: 10px;">
|
||||
<a href="{$invoice.document_url}" target="_blank">{$invoice.document}</a>
|
||||
<a href="{$invoice.document}" target="_blank">{$invoice.document}</a>
|
||||
<span style="color: #5FB878;">已上传</span>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
@@ -175,7 +175,7 @@ class UserAddressLogic
|
||||
'district_id' => $post['district_id'],
|
||||
'address' => $post['address'],
|
||||
'is_default' => $post['is_default'],
|
||||
'update_time' => time()
|
||||
'updatetime' => time()
|
||||
];
|
||||
|
||||
$result = Db::name('user_address')
|
||||
@@ -203,7 +203,7 @@ class UserAddressLogic
|
||||
|
||||
$data = [
|
||||
'del' => 1,
|
||||
'update_time' => time()
|
||||
'updatetime' => time()
|
||||
];
|
||||
|
||||
return Db::name('user_address')
|
||||
|
||||
@@ -180,7 +180,6 @@ class CityService
|
||||
|
||||
// 缓存结果(缓存1小时)
|
||||
Cache::store('redis')->set($cacheKey, $data, 3600);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'msg' => '获取成功',
|
||||
|
||||
Reference in New Issue
Block a user