2024-06-18 18:14:25 +08:00
< ? php
namespace app\statistics\controller ;
2024-06-25 10:53:25 +08:00
use app\admin\logic\statistic\TradeStatisticLogic ;
2024-06-18 18:14:25 +08:00
use app\common\controller\BaseLikeController ;
2024-07-13 16:42:49 +08:00
use app\common\model\store_branch_product\StoreBranchProduct ;
2024-07-13 17:54:48 +08:00
use app\common\model\store_order\StoreOrder ;
2024-07-13 16:42:49 +08:00
use app\common\model\store_order_cart_info\StoreOrderCartInfo ;
use app\common\model\store_product\StoreProduct ;
2024-07-13 17:54:48 +08:00
use app\common\model\system_store\SystemStore ;
2024-06-25 17:28:07 +08:00
use app\common\model\user\User ;
use app\common\model\user_recharge\UserRecharge ;
2024-06-18 18:14:25 +08:00
use app\statistics\logic\OrderLogic ;
use app\statistics\logic\ProductLogic ;
use app\statistics\logic\UserLogic ;
use DateTime ;
class IndexController extends BaseLikeController
{
2024-07-13 16:42:49 +08:00
// public $store_id = 0;
2024-06-22 15:07:41 +08:00
2024-06-18 18:14:25 +08:00
public function index ()
{
2024-06-25 17:28:07 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 17:28:07 +08:00
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
$where [ 'paid' ] = 1 ;
2024-06-25 17:28:07 +08:00
$res = OrderLogic :: dayPayPrice ( $where , $time );
2024-06-18 18:14:25 +08:00
if ( OrderLogic :: hasError ()) {
return $this -> fail ( OrderLogic :: getError ()); //获取错误信息并返回错误信息
}
2024-07-13 16:42:49 +08:00
switch ( $store_id ) {
case 1 :
$title = '喻寺镇农(特)产品交易大数据' ;
break ;
case 2 :
$title = '立石镇农(特)产品交易大数据' ;
break ;
case 3 :
$title = '百和镇农(特)产品交易大数据' ;
break ;
case 4 :
$title = '得胜镇农(特)产品交易大数据' ;
break ;
case 5 :
$title = '玄滩镇农(特)产品交易大数据' ;
break ;
case 6 :
$title = '牛滩镇农(特)产品交易大数据' ;
break ;
case 7 :
$title = '云锦镇农(特)产品交易大数据' ;
break ;
default :
2024-07-13 18:00:44 +08:00
$title = '泸县农(特)产品交易大数据' ;
2024-07-13 16:42:49 +08:00
break ;
}
return $this -> success ( 'ok' , [ 'dayPayPrice' => $res , 'title' => $title ]);
}
/**
* 门店统计
*/
2024-07-13 17:54:48 +08:00
public function sotre_count ()
{
$data = [
[ 'street_name' => '喻寺镇' , 'merchant_count' => 1 ],
[ 'street_name' => '立石镇' , 'merchant_count' => 1 ],
[ 'street_name' => '百和镇' , 'merchant_count' => 1 ],
[ 'street_name' => '得胜镇' , 'merchant_count' => 1 ],
[ 'street_name' => '玄滩镇' , 'merchant_count' => 1 ],
[ 'street_name' => '云锦镇' , 'merchant_count' => 1 ],
2024-07-13 16:42:49 +08:00
];
2024-07-13 17:54:48 +08:00
return $this -> success ( 'ok' , [ 'merchatCountList' => $data , 'merchantTotalCount' => count ( $data )]);
2024-07-13 16:42:49 +08:00
}
2024-07-13 17:54:48 +08:00
public function product_count_sotre_count ()
{
$data = [
[ 'street_name' => '喻寺镇' , 'product_count' => 1 ],
[ 'street_name' => '立石镇' , 'product_count' => 1 ],
[ 'street_name' => '百和镇' , 'product_count' => 1 ],
[ 'street_name' => '得胜镇' , 'product_count' => 1 ],
[ 'street_name' => '玄滩镇' , 'product_count' => 1 ],
[ 'street_name' => '云锦镇' , 'product_count' => 1 ],
2024-07-13 16:42:49 +08:00
];
2024-07-13 17:54:48 +08:00
$townProductCount = StoreProduct :: count ();
$product_count = StoreBranchProduct :: group ( 'product_id' ) -> order ( 'total_sales desc' ) -> limit ( 20 ) -> field ( 'image,product_id,store_name,sum(sales) as total_sales' ) -> select ();
$productRankingTotal = 0 ;
foreach ( $product_count as $item ) {
$productRankingTotal += $item [ 'total_sales' ];
2024-07-13 16:42:49 +08:00
}
2024-07-13 17:54:48 +08:00
return $this -> success ( 'ok' , [ 'townProductCountList' => $data , 'productRankingList' => $product_count , 'townProductCount' => $townProductCount , 'productRankingTotal' => $productRankingTotal ]);
2024-06-18 18:14:25 +08:00
}
public function user ()
{
2024-06-25 17:28:07 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 10:53:25 +08:00
$today = strtotime ( $time );
2024-06-25 17:28:07 +08:00
$dates = [];
2024-06-18 18:14:25 +08:00
// 循环输出前5天的日期
for ( $i = 0 ; $i <= 4 ; $i ++ ) {
// 计算前第$i天的日期时间戳
$timestamp = $today - ( $i * 86400 ); // 86400秒等于1天
// 将时间戳格式化为日期
$date = date ( 'Y-m-d' , $timestamp );
2024-06-25 17:28:07 +08:00
$dates [] = $date ;
2024-06-18 18:14:25 +08:00
}
2024-06-25 17:28:07 +08:00
$where = [];
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-06-25 17:28:07 +08:00
$res = UserLogic :: userCount ( $where , $dates );
2024-06-18 18:14:25 +08:00
if ( UserLogic :: hasError ()) {
return $this -> fail ( UserLogic :: getError ()); //获取错误信息并返回错误信息
}
return $this -> success ( 'ok' , $res );
}
/**
* 中间商品统计
*/
public function product_count ()
{
2024-06-25 17:28:07 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 17:28:07 +08:00
$where = [];
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-06-25 17:28:07 +08:00
$res = ProductLogic :: Count ( $where , $time );
2024-06-18 18:14:25 +08:00
if ( ProductLogic :: hasError ()) {
return $this -> fail ( ProductLogic :: getError ()); //获取错误信息并返回错误信息
}
return $this -> success ( 'ok' , $res );
}
/**
* 订单统计
*/
public function order_user_num_count ()
{
2024-06-25 17:28:07 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 17:28:07 +08:00
$where = [];
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-07-13 16:42:49 +08:00
$where [ 'recharge_type' ] = 'INDUSTRYMEMBERS' ;
$where [ 'status' ] = 1 ;
$where [ 'paid' ] = 1 ;
2024-06-25 17:28:07 +08:00
// $res = OrderLogic::Count($where,$time);
// if (ProductLogic::hasError()) {
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
// }
$res = UserRecharge :: where ( $where ) -> whereTime ( 'create_time' , $time ) -> select () -> each ( function ( $item ) {
2024-07-13 16:42:49 +08:00
if ( $item [ 'uid' ]) {
$item [ 'nickname' ] = User :: where ( 'id' , $item [ 'uid' ]) -> value ( 'nickname' );
} else {
$item [ 'nickname' ] = '' ;
2024-06-25 17:28:07 +08:00
}
});
return $this -> success ( 'ok' , $res ? -> toArray ());
2024-06-18 18:14:25 +08:00
}
/**
* 商品销量排行榜统计
*/
public function sales_ranking ()
{
2024-07-06 16:43:12 +08:00
$time = $this -> request -> get ( 'date' );
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 17:28:07 +08:00
$where = [];
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-06-25 17:28:07 +08:00
$res = OrderLogic :: sales ( $where , $time );
2024-06-18 18:14:25 +08:00
if ( ProductLogic :: hasError ()) {
return $this -> fail ( ProductLogic :: getError ()); //获取错误信息并返回错误信息
}
return $this -> success ( 'ok' , $res );
}
/**
* 成交用户数据
*/
public function user_trade_count ()
{
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
$where = [ 'create_time' => 'today' ];
$where2 = [ 'create_time' => 'yestoday' ];
if ( $store_id ) {
$where [ 'store_id' ] = $store_id ;
$where2 [ 'store_id' ] = $store_id ;
}
2024-06-25 17:28:07 +08:00
$logic = ( new TradeStatisticLogic ());
2024-07-13 16:42:49 +08:00
$leftToday = $logic -> getTopLeftTrade ( $where );
$leftyestoday = $logic -> getTopLeftTrade ( $where2 );
2024-06-25 10:53:25 +08:00
$totalleft = [ $leftToday , $leftyestoday ];
foreach ( $totalleft as $k => $v ) {
$left [ 'name' ] = " 当日订单金额 " ;
$left [ 'x' ] = $v [ 'curve' ][ 'x' ];
$left [ 'series' ][ $k ][ 'money' ] = round ( $v [ 'total_money' ], 2 );
$left [ 'series' ][ $k ][ 'value' ] = array_values ( $v [ 'curve' ][ 'y' ]);
2024-06-24 12:51:58 +08:00
}
2024-06-25 10:53:25 +08:00
return $this -> data ( $left );
2024-06-18 18:14:25 +08:00
}
/**
* 当日订单金额
*/
public function street_currday_order_count ()
{
2024-06-25 17:28:07 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-06-25 17:28:07 +08:00
$where = [];
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-06-25 17:28:07 +08:00
$res = OrderLogic :: Currday ( $where , $time );
2024-06-18 18:14:25 +08:00
if ( ProductLogic :: hasError ()) {
return $this -> fail ( ProductLogic :: getError ()); //获取错误信息并返回错误信息
}
return $this -> success ( 'ok' , $res );
}
2024-07-13 17:54:48 +08:00
public function store_order_day ()
{
$arr = SystemStore :: where ( 'is_show' , 1 ) -> field ( 'id,name street_name' ) -> select ()
-> each ( function ( $item ){
$res = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> where ( 'store_id' , $item [ 'id' ]) -> whereDay ( 'create_time' ) -> field ( 'count(id) count,sum(pay_price) price' ) -> find ();
$month = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> where ( 'store_id' , $item [ 'id' ]) -> whereMonth ( 'create_time' ) -> field ( 'count(id) count,sum(pay_price) price' ) -> find ();
$item [ 'dayOrderAmount' ] = $res [ 'price' ] ? ? 0 ;
$item [ 'dayOrderCount' ] = $res [ 'count' ] ? ? 0 ;
$item [ 'monthOrderAmount' ] = $month [ 'price' ] ? ? 0 ;
$item [ 'monthOrderCount' ] = $month [ 'count' ] ? ? 0 ;
return $item ;
});
return $this -> success ( 'ok' , $arr ? -> toArray ());
}
public function store_order_day_two ()
{
$arr = SystemStore :: where ( 'is_show' , 1 ) -> field ( 'id,name street_name' ) -> select ()
-> each ( function ( $item ){
$today_order_amount = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> where ( 'store_id' , $item [ 'id' ]) -> whereDay ( 'create_time' ) -> sum ( 'pay_price' );
$yesterday_order_amount = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> where ( 'store_id' , $item [ 'id' ]) -> whereMonth ( 'create_time' , 'yesterday' ) -> sum ( 'pay_price' );
$item [ 'today_order_amount' ] = $today_order_amount ;
$item [ 'yesterday_order_amount' ] = $yesterday_order_amount ;
return $item ;
});
return $this -> success ( 'ok' , $arr ? -> toArray ());
}
2024-06-18 18:14:25 +08:00
}