260 lines
8.0 KiB
PHP
260 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\data;
|
|
|
|
use app\common\controller\Backend;
|
|
/**
|
|
* 订单数据
|
|
*/
|
|
class Order extends Backend
|
|
{
|
|
|
|
/**
|
|
* Course模型对象
|
|
* @var \app\admin\model\order\Course
|
|
*/
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\order\Order;
|
|
$this->trafficModel = new \app\admin\model\data\Traffic();
|
|
$this->timeModel = new \app\admin\model\data\Time();
|
|
}
|
|
|
|
|
|
public function total()
|
|
{
|
|
$time = $this->request->post('time/a',0);
|
|
$unit = $this->request->post('unit','');
|
|
|
|
$data = [
|
|
'order_pay_user_count'=>0,
|
|
'order_pay_price'=>0,
|
|
'order_user_count'=>0,
|
|
'order_price'=>0,
|
|
'user_average_price'=>0,
|
|
'visiter'=>0
|
|
];
|
|
|
|
if(!$time){
|
|
$this->success('获取成功', $data);
|
|
}
|
|
if(count($time) == 1){
|
|
$time = $time[0];
|
|
}
|
|
|
|
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
|
$orderData = $this->model
|
|
->where(['createtime'=>['between',[$time['start'],$time['end']]]])
|
|
->field([
|
|
"sum(real_price) as real_price_sum",
|
|
"count(user_id) as user_count"
|
|
])
|
|
->group("user_id")
|
|
->find();
|
|
|
|
$data['order_user_count'] = $orderData['user_count'];
|
|
$data['order_price'] = $orderData['real_price_sum'];
|
|
|
|
|
|
$payOrderData = $this->model
|
|
->where([
|
|
'status'=>['in',\app\common\constant\order\Status::getSuccessOrderStatus()],
|
|
'createtime'=>['between',[$time['start'],$time['end']]]
|
|
])
|
|
->field([
|
|
"sum(real_price) as pay_real_price_sum",
|
|
"count(user_id) as pay_user_count"
|
|
])
|
|
->group("user_id")
|
|
->find();
|
|
|
|
$data['order_pay_user_count'] = $payOrderData['pay_user_count'];
|
|
$data['order_pay_price'] = $payOrderData['pay_real_price_sum'];
|
|
|
|
if($payOrderData['pay_real_price_sum'] && $payOrderData['pay_user_count']){
|
|
$data['user_average_price'] = number_format($payOrderData['pay_real_price_sum'] / $payOrderData['pay_user_count'],2);
|
|
}
|
|
|
|
$data['visiter'] = $this->trafficModel->where([
|
|
'createtime'=>['between',[$time['start'],$time['end']]]
|
|
])->group('ip')->count();
|
|
|
|
$this->success('获取成功', $data);
|
|
}
|
|
|
|
public function section(){
|
|
$time = $this->request->post('time/a',0);
|
|
$unit = $this->request->post('unit','');
|
|
if(!$time){
|
|
$this->success('获取成功', []);
|
|
}
|
|
if(count($time) == 1){
|
|
$time = $time[0];
|
|
}
|
|
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
|
$result = $this->model
|
|
->field([
|
|
"CASE
|
|
WHEN real_price BETWEEN 0 AND 50 THEN '0-50'
|
|
WHEN real_price BETWEEN 51 AND 100 THEN '51-100'
|
|
WHEN real_price BETWEEN 101 AND 200 THEN '101-200'
|
|
WHEN real_price BETWEEN 201 AND 500 THEN '201-500'
|
|
WHEN real_price BETWEEN 501 AND 1000 THEN '501-1000'
|
|
WHEN real_price BETWEEN 1001 AND 1500 THEN '1001-1500'
|
|
WHEN real_price > 1500 THEN '>1500'
|
|
END AS price_range",
|
|
"COUNT(*) AS order_count"
|
|
])
|
|
->where([
|
|
'createtime'=>['between',[$time['start'],$time['end']]]
|
|
])
|
|
->where('status', 'in', \app\common\constant\order\Status::getSuccessOrderStatus())
|
|
->group('price_range')
|
|
->select();
|
|
|
|
$temp = [];
|
|
foreach ($result as $item){
|
|
$temp[$item['price_range']] = [
|
|
'order_count'=>$item['order_count']
|
|
];
|
|
}
|
|
|
|
$ranges = ['0-50','51-100','101-200','201-500','501-1000','1001-1500'];
|
|
|
|
foreach ($ranges as $range){
|
|
if(!isset($temp[$range])){
|
|
$data[$range] = ['order_count'=>0];
|
|
}else{
|
|
$data[$range] = $temp[$range];
|
|
}
|
|
}
|
|
|
|
$this->success('获取成功', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 交易趋势统计
|
|
* @return void
|
|
*/
|
|
public function trend(){
|
|
$time = $this->request->post('time/a',0);
|
|
if(!$time){
|
|
$this->success('获取成功', []);
|
|
}
|
|
|
|
if(count($time) == 1){
|
|
$time = $time[0];
|
|
}
|
|
$unit = $this->request->post('unit',0);
|
|
$this->timeModel = new \app\admin\model\data\Time();
|
|
$emptyData = [
|
|
'order_price'=>0,
|
|
'order_user_count'=>0,
|
|
'order_pay_price'=>0,
|
|
'order_pay_user_count'=>0,
|
|
'user_average_price'=>0
|
|
];
|
|
|
|
$time = $this->timeModel->getFilterStartEndTime($time,$unit);
|
|
|
|
if($unit == 'date'){
|
|
$interval = 3600;
|
|
$timeFormat = 'H:i';
|
|
$timeFormatSql = '%H';
|
|
}else{
|
|
$interval = 86400;
|
|
$timeFormat = 'Y-m-d';
|
|
$timeFormatSql = '%Y-%m-%d';
|
|
}
|
|
|
|
$result = $this->model
|
|
->where([
|
|
'createtime'=>['between',[$time['start'],$time['end']]]
|
|
])
|
|
->field([
|
|
"FROM_UNIXTIME(createtime, '{$timeFormatSql}') as date", // 转换创建时间为日期
|
|
"SUM(CASE WHEN status IN ('".implode('\',\'',\app\common\constant\order\Status::getSuccessOrderStatus())."') THEN real_price ELSE 0 END) AS order_pay_price",
|
|
"COUNT(DISTINCT CASE WHEN status IN ('".implode('\',\'',\app\common\constant\order\Status::getSuccessOrderStatus())."') THEN user_id ELSE NULL END) AS order_pay_user_count",
|
|
'COUNT(DISTINCT user_id) AS order_user_count',
|
|
'SUM(real_price) AS order_price'
|
|
])
|
|
->group("FROM_UNIXTIME(createtime, '{$timeFormatSql}')")
|
|
->order("createtime", "desc")
|
|
->select();
|
|
|
|
|
|
if($result){
|
|
foreach ($result as $item){
|
|
$timeUnit = $item['date'];
|
|
if($unit == 'date'){
|
|
$timeUnit = $timeUnit.":00";
|
|
}
|
|
$list[$timeUnit] = $item;
|
|
}
|
|
}
|
|
$data = [];
|
|
for($i=$time['start'];$i<$time['end'];$i+=$interval){
|
|
$timeUnit = date($timeFormat,$i);
|
|
if(!isset($data[$timeUnit])){
|
|
$data[$timeUnit] = $emptyData;
|
|
}
|
|
if(isset($list[$timeUnit])){
|
|
$data[$timeUnit] = $list[$timeUnit];
|
|
}
|
|
|
|
$data[$timeUnit]['user_average_price'] = 0;
|
|
|
|
if($data[$timeUnit]['order_pay_price'] && $data[$timeUnit]['order_pay_user_count']){
|
|
$data[$timeUnit]['user_average_price'] = number_format($data[$timeUnit]['order_pay_price'] / $data[$timeUnit]['order_pay_user_count'],2);
|
|
}
|
|
}
|
|
$this->success('获取成功', $data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取交易统计
|
|
* @return void
|
|
*/
|
|
public function getUserTransactionStatistic(){
|
|
$userId = $this->request->post("user_id", 0);
|
|
$this->success("获取成功",$this->model->getTransactionStatistic($userId));
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取订单统计数据
|
|
* @return void
|
|
*/
|
|
public function getOrderData(){
|
|
$data = [];
|
|
|
|
$startTime = strtotime(date("Y-m-d"));
|
|
|
|
//今日支付金额(元)
|
|
$data[__('today_pay_price')] = $this->model->where([
|
|
'createtime'=>['>',$startTime]
|
|
])->sum('real_pay_price');
|
|
|
|
//累计支付金额(元)
|
|
$data[__('total_pay_price')] = $this->model->sum('real_pay_price');
|
|
|
|
//今日有效订单数
|
|
$data[__('today_order_num')] = $this->model->where([
|
|
'real_pay_price'=>['>',0],
|
|
'createtime'=>['>',$startTime]
|
|
])->count();
|
|
|
|
//累计有效订单数
|
|
$data[__('total_order_num')] = $this->model->where([
|
|
'real_pay_price'=>['>',0]
|
|
])->count();
|
|
|
|
|
|
$this->success("获取成功",$data);
|
|
}
|
|
} |