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 ;
2024-07-18 15:49:55 +08:00
use think\facade\Db ;
2024-06-18 18:14:25 +08:00
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-18 15:49:55 +08:00
if ( $store_id ) {
$name = SystemStore :: where ( 'id' , $store_id ) -> value ( 'name' );
$title = $name . '农(特)产品交易大数据' ;
} else {
$title = '泸县农(特)产品交易大数据' ;
2024-07-13 16:42:49 +08:00
}
return $this -> success ( 'ok' , [ 'dayPayPrice' => $res , 'title' => $title ]);
}
2024-07-15 10:18:24 +08:00
/**
* 地方商品数量统计
*/
2024-07-13 17:54:48 +08:00
public function product_count_sotre_count ()
{
2024-07-17 11:19:56 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m-d' ));
2024-07-13 17:54:48 +08:00
$data = [
2024-07-18 18:25:15 +08:00
[ 'street_name' => '喻寺镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 1 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
[ 'street_name' => '立石镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 2 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
[ 'street_name' => '百和镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 3 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
[ 'street_name' => '得胜镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 5 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
[ 'street_name' => '玄滩镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 6 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
[ 'street_name' => '云锦镇' , 'product_count' => StoreBranchProduct :: where ( 'store_id' , 7 ) -> where ( 'create_time' , '<=' , strtotime ( $time )) -> count ()],
2024-07-13 16:42:49 +08:00
];
2024-07-18 18:25:15 +08:00
$townProductCount = StoreProduct :: where ( 'create_time' , '<=' , strtotime ( $time )) -> count ();
$product_count = StoreBranchProduct :: where ( 'create_time' , '<=' , strtotime ( $time )) -> group ( 'product_id' ) -> order ( 'total_sales desc' ) -> limit ( 20 ) -> field ( 'image,product_id,store_name,sum(sales) as total_sales' ) -> select ();
2024-07-13 17:54:48 +08:00
$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-07-18 18:25:15 +08:00
$dates_two = [];
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-07-17 11:19:56 +08:00
$dates_two [] = date ( 'n.j' , $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 ()); //获取错误信息并返回错误信息
}
2024-07-18 18:25:15 +08:00
$res [ 'date' ] = $dates_two ;
$money = Db :: name ( 'demo_order_money' ) -> where ( $where ) -> sum ( 'money' );
$where [ 'paid' ] = 1 ;
2024-07-18 17:14:13 +08:00
// $pay_price=Db::connect('demo')->name('store_order')->where($where)->whereDay('create_time')->sum('pay_price');
2024-07-18 18:25:15 +08:00
$price = Db :: connect ( 'demo' ) -> name ( 'user_recharge' ) -> where ( $where ) -> sum ( 'price' );
$res [ 'dayPayPrice' ] = bcadd ( $money , $price , 2 );
2024-06-18 18:14:25 +08:00
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-07-18 18:25:15 +08:00
$where [] = [ 'price' , '>' , 1 ];
2024-06-25 17:28:07 +08:00
// $res = OrderLogic::Count($where,$time);
// if (ProductLogic::hasError()) {
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
// }
2024-07-18 18:05:18 +08:00
$res = Db :: connect ( 'demo' ) -> name ( 'user_recharge' ) -> where ( $where ) -> limit ( 20 ) -> order ( 'id desc' ) -> select () -> each ( function ( $item ) {
2024-07-13 16:42:49 +08:00
if ( $item [ 'uid' ]) {
2024-07-18 17:44:50 +08:00
$find = Db :: connect ( 'demo' ) -> name ( 'user' ) -> where ( 'id' , $item [ 'uid' ]) -> field ( 'real_name,nickname' ) -> find ();
2024-07-18 18:25:15 +08:00
$item [ 'nickname' ] = $find [ 'real_name' ] ? ? '' ;
if ( $item [ 'nickname' ] == '' ) {
$item [ 'nickname' ] = $find [ 'nickname' ] ? ? '' ;
} else {
2024-07-19 09:41:49 +08:00
$nickname = $item [ 'nickname' ];
$item [ 'nickname' ] = $this -> hideMiddleName ( $nickname );
2024-07-18 17:44:50 +08:00
}
2024-07-13 16:42:49 +08:00
} else {
$item [ 'nickname' ] = '' ;
2024-06-25 17:28:07 +08:00
}
2024-07-18 17:42:00 +08:00
$item [ 'create_time' ] = date ( 'Y-m-d H:i:s' , $item [ 'create_time' ]);
2024-07-18 17:40:48 +08:00
return $item ;
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-07-18 17:37:58 +08:00
$where [ 'paid' ] = 1 ;
2024-06-25 17:28:07 +08:00
if ( $store_id ) {
2024-06-24 12:51:58 +08:00
$where [ 'store_id' ] = $store_id ;
}
2024-07-18 17:14:13 +08:00
// $res = OrderLogic::sales($where, $time);
// if (ProductLogic::hasError()) {
// return $this->fail(ProductLogic::getError()); //获取错误信息并返回错误信息
// }
2024-07-18 17:36:37 +08:00
$select = Db :: connect ( 'demo' ) -> name ( 'store_order' ) -> where ( $where ) -> limit ( 20 ) -> order ( 'id desc' )
2024-07-18 18:25:15 +08:00
-> field ( 'id,order_id,pay_price,create_time' ) -> select () -> each ( function ( $item ) {
$item [ 'create_time' ] = date ( 'Y-m-d H:i:s' , $item [ 'create_time' ]);
return $item ;
}) -> toArray ();
2024-07-18 17:36:37 +08:00
return $this -> success ( 'ok' , $select );
2024-06-18 18:14:25 +08:00
}
/**
* 成交用户数据
*/
public function user_trade_count ()
{
2024-07-13 16:42:49 +08:00
$store_id = $this -> request -> get ( 'store_id' , 0 );
2024-07-17 11:19:56 +08:00
$date = $this -> request -> get ( 'date' , '' );
2024-07-18 18:25:15 +08:00
if ( $date != '' ) {
$where = [ 'create_time' => [ 'start_time' => $date , 'end_time' => $date . ' 23:59:59' ]];
$where2 = [ 'create_time' => [ 'start_time' => $date , 'end_time' => $date . ' 23:59:59' ]];
} else {
2024-07-17 11:19:56 +08:00
$where = [ 'create_time' => 'today' ];
$where2 = [ 'create_time' => 'yestoday' ];
}
2024-07-13 16:42:49 +08:00
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 );
2024-07-17 11:19:56 +08:00
2024-07-13 16:42:49 +08:00
$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 ()
{
2024-07-17 11:19:56 +08:00
$time = $this -> request -> get ( 'date' , date ( 'Y-m' ));
2024-07-13 17:54:48 +08:00
$arr = SystemStore :: where ( 'is_show' , 1 ) -> field ( 'id,name street_name' ) -> select ()
2024-07-18 18:25:15 +08:00
-> each ( function ( $item ) use ( $time ) {
$res = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> where ( 'store_id' , $item [ 'id' ]) -> whereDay ( 'create_time' , $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' , $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 ;
});
2024-07-13 17:54:48 +08:00
return $this -> success ( 'ok' , $arr ? -> toArray ());
}
public function store_order_day_two ()
{
$arr = SystemStore :: where ( 'is_show' , 1 ) -> field ( 'id,name street_name' ) -> select ()
2024-07-18 18:25:15 +08:00
-> 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 ;
});
2024-07-13 17:54:48 +08:00
return $this -> success ( 'ok' , $arr ? -> toArray ());
2024-07-17 11:19:56 +08:00
}
2024-07-18 18:25:15 +08:00
/**
2024-07-17 11:19:56 +08:00
* 格式化时间
* @ param $time
* @ return string
*/
public function getDay ( $time )
{
if ( strstr ( $time , '-' ) !== false ) {
[ $startTime , $endTime ] = explode ( '-' , $time );
if ( ! $startTime && ! $endTime ) {
return date ( " Y/m/d " , strtotime ( " -30 days " , time ())) . '-' . date ( " Y/m/d " , time ());
} else {
2024-07-18 18:25:15 +08:00
if ( $startTime == $endTime ) {
return $startTime . '-' . $endTime . ' 23:59:59' ;
} else {
2024-07-17 11:19:56 +08:00
return $startTime . '-' . $endTime ;
}
}
} else {
return date ( " Y/m/d " , strtotime ( " -30 days " , time ())) . '-' . date ( " Y/m/d " , time ());
}
2024-07-13 17:54:48 +08:00
}
2024-07-18 18:30:32 +08:00
2024-07-19 09:41:49 +08:00
function hideMiddleName ( $name )
{
2024-07-18 18:30:32 +08:00
$strlen = mb_strlen ( $name ); // 获取字符串长度
$firstStr = mb_substr ( $name , 0 , 1 ); // 获取名字的第一个字符
$lastStr = mb_substr ( $name , - 1 , 1 ); // 获取名字的最后一个字符
$replaceStr = '' ; // 初始化替换字符串
2024-07-19 09:41:49 +08:00
if ( $strlen == 2 ) {
2024-07-19 09:46:31 +08:00
return $firstStr . $replaceStr ;
2024-07-19 09:41:49 +08:00
} else {
for ( $i = 0 ; $i < $strlen - 2 ; $i ++ ) {
$replaceStr .= '*' ; // 根据需要替换的字符数量,生成相应数量的星号
}
2024-07-18 18:30:32 +08:00
}
2024-07-19 09:41:49 +08:00
return $firstStr . $replaceStr . $lastStr ; // 返回替换后的名字
2024-07-18 18:30:32 +08:00
}
2024-06-18 18:14:25 +08:00
}