2024-06-01 16:07:53 +08:00
< ? php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台( PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用, 可去除界面版权logo
// | gitee下载: https://gitee.com/likeshop_gitee/likeadmin
// | github下载: https://github.com/likeshop-github/likeadmin
// | 访问官网: https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\store\logic ;
2024-06-20 14:41:04 +08:00
use app\admin\logic\statistic\ProductStatisticLogic ;
2024-06-13 17:25:39 +08:00
use app\common\enum\OrderEnum ;
2024-06-08 12:02:08 +08:00
use app\common\enum\PayEnum ;
2024-06-13 17:25:39 +08:00
use app\common\enum\YesNoEnum ;
2024-06-01 16:07:53 +08:00
use app\common\logic\BaseLogic ;
2024-06-07 17:28:02 +08:00
use app\common\logic\store_order\StoreOrderLogic ;
2024-06-13 17:25:39 +08:00
use app\common\logic\StoreFinanceFlowLogic ;
2024-06-18 12:10:21 +08:00
use app\common\model\finance\CapitalFlow ;
2024-06-08 16:53:08 +08:00
use app\common\model\order\Cart ;
2024-06-08 14:17:05 +08:00
use app\common\model\store_cash_finance_flow\StoreCashFinanceFlow ;
2024-06-13 17:25:39 +08:00
use app\common\model\store_finance_flow\StoreFinanceFlow ;
2024-06-07 17:28:02 +08:00
use app\common\model\store_order\StoreOrder ;
2024-06-08 16:53:08 +08:00
use app\common\model\store_order_cart_info\StoreOrderCartInfo ;
2024-06-21 16:21:00 +08:00
use app\common\model\store_product\StoreProduct ;
use app\common\model\store_product_log\StoreProductLog ;
2024-06-08 16:53:08 +08:00
use app\common\model\store_visit\StoreVisit ;
2024-06-13 17:25:39 +08:00
use app\common\model\system_store\SystemStore ;
2024-06-17 14:09:39 +08:00
use app\common\model\user_recharge\UserRecharge ;
2024-06-01 16:07:53 +08:00
use app\common\service\ConfigService ;
use app\common\service\FileService ;
2024-06-25 16:27:36 +08:00
use app\statistics\logic\OrderLogic ;
2024-06-01 16:07:53 +08:00
/**
* 工作台
* Class WorkbenchLogic
* @ package app\store\logic
*/
class WorkbenchLogic extends BaseLogic
{
2024-06-08 15:07:46 +08:00
2024-06-07 17:28:02 +08:00
public static function index ( $params )
2024-06-01 16:07:53 +08:00
{
2024-06-07 17:28:02 +08:00
$data = [];
2024-06-20 11:50:46 +08:00
$startTime = $params [ 'start_time' ];
$endTime = $params [ 'end_time' ];
$endTime = date ( 'Y-m-d' , strtotime ( $endTime ) + 86400 );
$dateDiff = ( new \DateTime ( $endTime )) -> diff ( new \DateTime ( $startTime ));
2024-06-22 14:16:56 +08:00
$where = [ 'paid' => 1 , 'refund_status' => 0 ];
$userRechargeWhere = [ 'paid' => 1 ];
2024-06-22 10:07:28 +08:00
$cashFinanceWhere = [];
2024-06-22 14:16:56 +08:00
$storeFinanceWhere = [ 'financial_type' => 2 , 'financial_pm' => 1 ];
$storeFinanceWhereTwo = [ 'financial_type' => 11 , 'financial_pm' => 0 ];
2024-06-28 15:03:14 +08:00
$attritionWhere = [];
2024-06-22 10:07:28 +08:00
if ( $params [ 'store_id' ] != 0 ) {
$where [ 'store_id' ] = $params [ 'store_id' ];
2024-06-22 14:16:56 +08:00
$userRechargeWhere [ 'store_id' ] = $params [ 'store_id' ];
2024-06-22 10:07:28 +08:00
$cashFinanceWhere = [ 'store_id' => $params [ 'store_id' ]];
2024-06-22 11:33:36 +08:00
$storeFinanceWhere [ 'store_id' ] = $params [ 'store_id' ];
2024-06-22 14:16:56 +08:00
$storeFinanceWhereTwo [ 'store_id' ] = $params [ 'store_id' ];
2024-06-29 09:24:57 +08:00
$attritionWhere [ 'id' ] = $params [ 'store_id' ];
2024-06-22 10:07:28 +08:00
}
2024-06-08 12:02:08 +08:00
$orderLogic = new StoreOrderLogic ();
2024-06-08 14:17:05 +08:00
//订单总金额
2024-06-22 10:07:28 +08:00
$data [ 'order_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , $where );
2024-06-08 14:17:05 +08:00
//余额支付总金额
2024-06-22 10:07:28 +08:00
$data [ 'balance_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , array_merge ( $where , [ 'pay_type' => PayEnum :: BALANCE_PAY ]));
2024-06-22 11:39:52 +08:00
//微信条码支付总金额
$data [ 'wechat_code_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , array_merge ( $where , [ 'pay_type' => PayEnum :: WECHAT_PAY_BARCODE ]));
//支付条码支付总金额
$data [ 'alipay_code_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , array_merge ( $where , [ 'pay_type' => PayEnum :: ALIPAY_BARCODE ]));
2024-06-08 14:17:05 +08:00
//线下收银总金额
2024-06-22 10:07:28 +08:00
$data [ 'cashier_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , array_merge ( $where , [ 'shipping_type' => 3 ]));
2024-06-08 14:17:05 +08:00
//现金收银总金额
2024-06-22 10:07:28 +08:00
$data [ 'cash_amount' ] = StoreCashFinanceFlow :: where ( $cashFinanceWhere ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> sum ( 'cash_price' );
2024-06-08 14:17:05 +08:00
//核销订单金额
2024-06-22 10:07:28 +08:00
$data [ 'verify_amount' ] = $orderLogic -> storeOrderSumByDate ( $startTime , $endTime , array_merge ( $where , [ 'shipping_type' => 2 ]));
2024-06-08 16:53:08 +08:00
//门店收益金额
2024-06-22 14:16:56 +08:00
$data [ 'income_amount' ] = StoreFinanceFlow :: where ( $storeFinanceWhere ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> sum ( 'number' );
2024-06-17 14:09:39 +08:00
//门店收款金额
2024-06-25 16:27:36 +08:00
$all_where [ 'paid' ] = 1 ;
2024-06-27 16:12:02 +08:00
// $data['receipt_amount'] = OrderLogic::dayPayPrice($all_where,date('Y-m-d',time()));
$data [ 'receipt_amount' ] = OrderLogic :: dealFlexiblePrice ( $all_where , $startTime , $endTime );
2024-06-25 16:27:36 +08:00
// $data['receipt_amount'] = UserRecharge::where($userRechargeWhere)->whereBetweenTime('create_time', $startTime, $endTime)->sum('price');
2024-06-22 14:16:56 +08:00
//保证金金额
$data [ 'deposit_amount' ] = StoreFinanceFlow :: where ( $storeFinanceWhereTwo ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> sum ( 'number' );
2024-06-28 15:03:14 +08:00
/**门店损耗金 */
$data [ 'attrition_amount' ] = SystemStore :: where ( $attritionWhere ) -> sum ( 'attrition' );
2024-06-08 14:17:05 +08:00
//门店成交用户数
2024-06-22 10:07:28 +08:00
$data [ 'user_number' ] = StoreOrder :: where ( $where )
2024-06-20 11:50:46 +08:00
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
2024-06-08 12:02:08 +08:00
-> group ( 'uid' )
-> count ();
if ( $dateDiff -> days == 1 ) {
2024-06-07 17:28:02 +08:00
$group = 'HOUR(pay_time)' ;
2024-06-08 12:02:08 +08:00
$i = 0 ;
while ( $i < 24 ) {
$timeRange [] = date ( 'H' , strtotime ( " + $i hours " , strtotime ( $startTime )));
$i ++ ;
}
2024-06-07 17:28:02 +08:00
$field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price' ;
2024-06-25 18:16:52 +08:00
$recharge_field = 'from_unixtime(pay_time,"%H") as pay_time,sum(price) as pay_price' ;
2024-06-08 12:02:08 +08:00
} elseif ( $dateDiff -> days <= 31 ) {
2024-06-07 17:28:02 +08:00
$group = 'DAY(pay_time)' ;
2024-06-08 12:02:08 +08:00
$i = 0 ;
while ( $i < $dateDiff -> days ) {
$timeRange [] = date ( 'm-d' , strtotime ( " + $i days " , strtotime ( $startTime )));
$i ++ ;
}
2024-06-07 17:28:02 +08:00
$field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price' ;
2024-06-25 18:16:52 +08:00
$recharge_field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(price) as pay_price' ;
2024-06-22 10:07:28 +08:00
} else {
2024-06-07 17:28:02 +08:00
$group = 'MONTH(pay_time)' ;
2024-06-08 12:02:08 +08:00
$i = 0 ;
2024-06-08 15:07:46 +08:00
$month = 0 ;
if ( $dateDiff -> y > 0 ) {
$month = $dateDiff -> y * 12 ;
}
if ( $dateDiff -> m > 0 ) {
$month += $dateDiff -> m ;
}
if ( $dateDiff -> d > 0 ) {
$month += 1 ;
}
while ( $i < $month ) {
2024-06-08 12:02:08 +08:00
$timeRange [] = date ( 'Y-m' , strtotime ( " + $i months " , strtotime ( $startTime )));
$i ++ ;
}
2024-06-07 17:28:02 +08:00
$field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price' ;
2024-06-25 18:16:52 +08:00
$recharge_field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(price) as pay_price' ;
2024-06-07 17:28:02 +08:00
}
2024-06-25 18:16:52 +08:00
$data [ 'order_list' ] = StoreOrder :: with ( 'user' ) -> where ( $where )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> order ( 'pay_time' , 'desc' )
-> limit ( 6 )
-> select () -> each ( function ( $item ){
$item -> pay_time = $item -> pay_time > 0 ? date ( 'Y-m-d H:i:s' , $item -> pay_time ) : '' ;
})
-> toArray ();
//取消单个店铺搜索
unset ( $where [ 'store_id' ]);
$rechargeList = UserRecharge :: field ( $recharge_field )
-> where ([ 'paid' => 1 ])
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group )
-> select ()
-> toArray ();
2024-06-07 17:55:40 +08:00
$orderList = StoreOrder :: field ( $field )
2024-06-22 10:07:28 +08:00
-> where ( $where )
2024-06-07 17:28:02 +08:00
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group )
2024-06-07 17:55:40 +08:00
-> select ()
-> toArray ();
$userList = StoreOrder :: field ( $field . ',count(uid) as user_num' )
2024-06-22 10:07:28 +08:00
-> where ( $where )
2024-06-07 17:55:40 +08:00
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group . ',uid' )
-> select ()
-> toArray ();
$orderList = reset_index ( $orderList , 'pay_time' );
$userList = reset_index ( $userList , 'pay_time' );
$orderListTmp = [];
$userListTmp = [];
2024-06-07 17:58:52 +08:00
$range = [];
2024-06-25 18:16:52 +08:00
$mergedListTmp = [];
2024-06-08 12:02:08 +08:00
foreach ( $timeRange as $item ) {
$range [] = $item ;
if ( ! isset ( $orderList [ $item ])) {
$orderListTmp [ $item ] = 0 ;
2024-06-07 17:55:40 +08:00
} else {
2024-06-08 12:02:08 +08:00
$orderListTmp [ $item ] = $orderList [ $item ][ 'pay_price' ];
2024-06-07 17:55:40 +08:00
}
2024-06-08 12:02:08 +08:00
if ( ! isset ( $userList [ $item ])) {
$userListTmp [ $item ] = 0 ;
2024-06-07 17:55:40 +08:00
} else {
2024-06-08 12:02:08 +08:00
$userListTmp [ $item ] = $userList [ $item ][ 'user_num' ];
2024-06-07 17:55:40 +08:00
}
2024-06-25 18:16:52 +08:00
// 处理 $rechargeList 和 $orderList 的相加
$mergedListTmp [ $item ] = 0 ;
foreach ( $rechargeList as $recharge ) {
if ( $recharge [ 'pay_time' ] == $item ) {
$mergedListTmp [ $item ] += $recharge [ 'pay_price' ];
}
}
foreach ( $orderList as $order ) {
if ( $order [ 'pay_time' ] == $item ) {
$mergedListTmp [ $item ] += $order [ 'pay_price' ];
}
}
2024-06-07 17:55:40 +08:00
}
$data [ 'statistics' ] = [
2024-06-07 17:58:52 +08:00
'range' => $range ,
2024-06-07 17:55:40 +08:00
'data' => [
2024-06-25 18:16:52 +08:00
// 'order_amount' => array_values($orderListTmp),
'user_number' => array_values ( $userListTmp ),
'order_amount' => array_values ( $mergedListTmp )
2024-06-07 17:55:40 +08:00
]
];
2024-06-25 18:16:52 +08:00
2024-06-08 14:17:05 +08:00
$data [ 'pay_type' ] = [
[ 'name' => '线上收银订单' , 'value' => bcsub ( $data [ 'order_amount' ], bcadd ( $data [ 'verify_amount' ], $data [ 'cash_amount' ], 2 ), 2 )],
[ 'name' => '核销订单' , 'value' => $data [ 'verify_amount' ]],
[ 'name' => '现金收银订单' , 'value' => $data [ 'cash_amount' ]],
];
2024-06-07 17:28:02 +08:00
return $data ;
2024-06-01 16:07:53 +08:00
}
/**
* @ notes 常用功能
* @ return array []
* @ author 乔峰
* @ date 2021 / 12 / 29 16 : 40
*/
public static function menu () : array
{
return [
[
'name' => '管理员' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_admin' )),
'url' => '/permission/admin'
],
[
'name' => '角色管理' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_role' )),
'url' => '/permission/role'
],
[
'name' => '部门管理' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_dept' )),
'url' => '/organization/department'
],
[
'name' => '素材中心' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_file' )),
'url' => '/material/index'
],
[
'name' => '菜单权限' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_auth' )),
'url' => '/permission/menu'
],
[
'name' => '网站信息' ,
'image' => FileService :: getFileUrl ( config ( 'project.default_image.menu_web' )),
'url' => '/setting/website/information'
],
];
}
/**
* @ notes 版本信息
* @ return array
* @ author 乔峰
* @ date 2021 / 12 / 29 16 : 08
*/
public static function versionInfo () : array
{
return [
'version' => config ( 'project.version' ),
'website' => config ( 'project.website.url' ),
'name' => ConfigService :: get ( 'website' , 'name' ),
'based' => 'vue3.x、ElementUI、MySQL' ,
'channel' => [
'website' => 'https://gitee.com/MuZJun/gather-admin.git' ,
'gitee' => 'https://gitee.com/MuZJun/gather-vue.git' ,
]
];
}
/**
* @ notes 今日数据
* @ return int []
* @ author 乔峰
* @ date 2021 / 12 / 29 16 : 15
*/
public static function today () : array
{
return [
'time' => date ( 'Y-m-d H:i:s' ),
// 今日销售额
'today_sales' => 100 ,
// 总销售额
'total_sales' => 1000 ,
// 今日访问量
'today_visitor' => 10 ,
// 总访问量
'total_visitor' => 100 ,
// 今日新增用户量
'today_new_user' => 30 ,
// 总用户量
'total_new_user' => 3000 ,
// 订单量 (笔)
'order_num' => 12 ,
// 总订单量
'order_sum' => 255
];
}
/**
* @ notes 访问数
* @ return array
* @ author 乔峰
* @ date 2021 / 12 / 29 16 : 57
*/
public static function visitor () : array
{
$num = [];
$date = [];
for ( $i = 0 ; $i < 15 ; $i ++ ) {
$where_start = strtotime ( " - " . $i . " day " );
$date [] = date ( 'Y/m/d' , $where_start );
$num [ $i ] = rand ( 0 , 100 );
}
return [
'date' => $date ,
'list' => [
[ 'name' => '访客数' , 'data' => $num ]
]
];
}
/**
* @ notes 服务支持
* @ return array []
* @ author 乔峰
* @ date 2022 / 7 / 18 11 : 18
*/
public static function support ()
{
return [
[
'image' => FileService :: getFileUrl ( config ( 'project.default_image.qq_group' )),
'title' => '官方公众号' ,
'desc' => '关注官方公众号' ,
],
[
'image' => FileService :: getFileUrl ( config ( 'project.default_image.customer_service' )),
'title' => '添加企业客服微信' ,
'desc' => '想了解更多请添加客服' ,
]
];
}
2024-06-08 16:53:08 +08:00
public static function product ( $params )
{
$data = [];
$storeId = $params [ 'store_id' ];
$startTime = $params [ 'start_time' ];
$endTime = $params [ 'end_time' ];
$endTime = date ( 'Y-m-d' , strtotime ( $endTime ) + 86400 );
$dateDiff = ( new \DateTime ( $endTime )) -> diff ( new \DateTime ( $startTime ));
$data [ 'visit_count' ] = StoreVisit :: where ( 'store_id' , $storeId ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> count ();
$data [ 'visit_user_count' ] = StoreVisit :: where ( 'store_id' , $storeId ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> group ( 'uid' ) -> count ();
$data [ 'add_cart_count' ] = Cart :: where ( 'store_id' , $storeId ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> withTrashed () -> group ( 'product_id' ) -> count ();
$data [ 'add_order_count' ] = StoreOrderCartInfo :: where ( 'store_id' , $storeId ) -> whereBetweenTime ( 'create_time' , $startTime , $endTime ) -> withTrashed () -> group ( 'product_id' ) -> count ();
$data [ 'pay_count' ] = StoreOrder :: alias ( 't1' )
-> join ( 'store_order_cart_info t2' , 't1.id = t2.oid' )
-> where ( 't1.store_id' , $storeId )
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( 'product_id' )
-> count ();
$data [ 'pay_amount' ] = StoreOrder :: where ( 'store_id' , $storeId )
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> sum ( 'pay_price' );
$data [ 'cost_amount' ] = StoreOrder :: where ( 'store_id' , $storeId )
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> sum ( 'cost' );
$data [ 'refund_amount' ] = StoreOrder :: where ( 'store_id' , $storeId )
-> where ( 'paid' , 1 )
-> where ( 'refund_status' , '>' , 0 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> sum ( 'pay_price' );
$data [ 'refund_count' ] = StoreOrder :: alias ( 't1' )
-> join ( 'store_order_cart_info t2' , 't1.id = t2.oid' )
-> where ( 't1.store_id' , $storeId )
-> where ( 'paid' , 1 )
-> where ( 'refund_status' , '>' , 0 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( 'product_id' )
-> count ();
$payUserCount = StoreOrder :: where ( 'store_id' , $storeId )
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( 'uid' )
-> count ();
if ( $data [ 'visit_user_count' ] == 0 ) {
$data [ 'trans_rate' ] = 0 ;
} else {
$data [ 'trans_rate' ] = bcdiv ( $payUserCount * 100 , $data [ 'visit_user_count' ], 2 );
}
//商品图表统计开始,未完成
if ( $dateDiff -> days == 1 ) {
$group = 'HOUR(pay_time)' ;
$i = 0 ;
while ( $i < 24 ) {
$timeRange [] = date ( 'H' , strtotime ( " + $i hours " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price' ;
} elseif ( $dateDiff -> days <= 31 ) {
$group = 'DAY(pay_time)' ;
$i = 0 ;
while ( $i < $dateDiff -> days ) {
$timeRange [] = date ( 'm-d' , strtotime ( " + $i days " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price' ;
} else {
$group = 'MONTH(pay_time)' ;
$i = 0 ;
$month = 0 ;
if ( $dateDiff -> y > 0 ) {
$month = $dateDiff -> y * 12 ;
}
if ( $dateDiff -> m > 0 ) {
$month += $dateDiff -> m ;
}
if ( $dateDiff -> d > 0 ) {
$month += 1 ;
}
while ( $i < $month ) {
$timeRange [] = date ( 'Y-m' , strtotime ( " + $i months " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price' ;
}
$orderList = StoreOrder :: field ( $field )
-> where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group )
-> select ()
-> toArray ();
$userList = StoreOrder :: field ( $field . ',count(uid) as user_num' )
-> where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group . ',uid' )
-> select ()
-> toArray ();
$orderList = reset_index ( $orderList , 'pay_time' );
$userList = reset_index ( $userList , 'pay_time' );
$orderListTmp = [];
$userListTmp = [];
$range = [];
foreach ( $timeRange as $item ) {
$range [] = $item ;
if ( ! isset ( $orderList [ $item ])) {
$orderListTmp [ $item ] = 0 ;
} else {
$orderListTmp [ $item ] = $orderList [ $item ][ 'pay_price' ];
}
if ( ! isset ( $userList [ $item ])) {
$userListTmp [ $item ] = 0 ;
} else {
$userListTmp [ $item ] = $userList [ $item ][ 'user_num' ];
}
}
$data [ 'statistics' ] = [
'range' => $range ,
'data' => [
'order_amount' => array_values ( $orderListTmp ),
'user_number' => array_values ( $userListTmp )
]
];
//商品图表统计结束
// 商品排行榜,未完成
$data [ 'rank_list' ] = StoreOrder :: with ( 'user' ) -> where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'paid' , 1 )
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> order ( 'create_time' , 'desc' )
-> limit ( 10 )
-> select () -> toArray ();
return $data ;
}
2024-06-08 17:37:59 +08:00
public static function delivery ( $params )
{
$data = [];
$storeId = $params [ 'store_id' ];
$startTime = $params [ 'start_time' ];
$endTime = $params [ 'end_time' ];
$endTime = date ( 'Y-m-d' , strtotime ( $endTime ) + 86400 );
$dateDiff = ( new \DateTime ( $endTime )) -> diff ( new \DateTime ( $startTime ));
$timeRange = [];
if ( $dateDiff -> days == 1 ) {
$group = 'HOUR(pay_time)' ;
$i = 0 ;
while ( $i < 24 ) {
$timeRange [] = date ( 'H' , strtotime ( " + $i hours " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%H") as pay_time,sum(pay_price) as pay_price,count(id) as order_num' ;
} elseif ( $dateDiff -> days <= 31 ) {
$group = 'DAY(pay_time)' ;
$i = 0 ;
while ( $i < $dateDiff -> days ) {
$timeRange [] = date ( 'm-d' , strtotime ( " + $i days " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%m-%d") as pay_time,sum(pay_price) as pay_price,count(id) as order_num' ;
} else {
$group = 'MONTH(pay_time)' ;
$i = 0 ;
$month = 0 ;
if ( $dateDiff -> y > 0 ) {
$month = $dateDiff -> y * 12 ;
}
if ( $dateDiff -> m > 0 ) {
$month += $dateDiff -> m ;
}
if ( $dateDiff -> d > 0 ) {
$month += 1 ;
}
while ( $i < $month ) {
$timeRange [] = date ( 'Y-m' , strtotime ( " + $i months " , strtotime ( $startTime )));
$i ++ ;
}
$field = 'from_unixtime(pay_time,"%Y-%m") as pay_time,sum(pay_price) as pay_price,count(id) as order_num' ;
}
$amountList = StoreOrder :: field ( $field )
-> where ( 'store_id' , $storeId )
-> where ( 'paid' , 1 )
-> where ( 'shipping_type' , 1 )
-> whereBetweenTime ( 'pay_time' , $startTime , $endTime )
-> group ( $group )
-> select ()
-> toArray ();
$amountList = reset_index ( $amountList , 'pay_time' );
$amountListTmp = [];
$countListTmp = [];
$range = [];
foreach ( $timeRange as $item ) {
$range [] = $item ;
if ( ! isset ( $amountList [ $item ])) {
$amountListTmp [ $item ] = 0 ;
} else {
$amountListTmp [ $item ] = $amountList [ $item ][ 'pay_price' ];
}
if ( ! isset ( $amountList [ $item ])) {
$countListTmp [ $item ] = 0 ;
} else {
$countListTmp [ $item ] = $amountList [ $item ][ 'order_num' ];
}
}
$data [ 'statistics' ] = [
'range' => $range ,
'data' => [
'order_amount' => array_values ( $amountListTmp ),
'order_count' => array_values ( $countListTmp )
]
];
return $data ;
}
public static function deliveryOrder ( $params )
{
$startTime = $params [ 'start_time' ];
$endTime = $params [ 'end_time' ];
$endTime = date ( 'Y-m-d' , strtotime ( $endTime ) + 86400 );
$query = StoreOrder :: with ( 'user' ) -> where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'paid' , 1 )
-> where ( 'shipping_type' , 1 )
-> whereBetweenTime ( 'create_time' , $startTime , $endTime );
$data [ 'count' ] = $query -> count ();
$data [ 'page_no' ] = $params [ 'page_no' ];
$data [ 'page_size' ] = $params [ 'page_size' ];
$data [ 'extend' ] = [];
$data [ 'lists' ] = $query -> order ( 'create_time' , 'desc' )
-> page ( $params [ 'page_no' ], $params [ 'page_size' ])
-> select () -> toArray ();
return $data ;
}
2024-06-13 17:25:39 +08:00
public static function revenueStatistics ( $params )
{
2024-06-18 10:47:26 +08:00
//当日营业额的统计
2024-06-22 10:07:28 +08:00
$today = StoreOrder :: where ([ 'paid' => YesNoEnum :: YES , 'store_id' => $params [ 'store_id' ]]);
2024-06-13 17:25:39 +08:00
$turnover_today = $today
-> whereDay ( 'create_time' )
-> sum ( 'pay_price' );
2024-06-18 10:47:26 +08:00
//当日利润的统计
2024-06-13 17:25:39 +08:00
$profit_today = $today
-> whereDay ( 'create_time' )
-> sum ( 'profit' );
2024-06-18 10:47:26 +08:00
//当日成本合集的统计
2024-06-13 17:25:39 +08:00
$cost_today = $today
-> whereDay ( 'create_time' )
-> sum ( 'cost' );
2024-06-18 10:47:26 +08:00
//当日加到保证金的
2024-06-22 10:07:28 +08:00
$deposit = StoreFinanceFlow :: where ([ 'store_id' => $params [ 'store_id' ], 'status' => YesNoEnum :: YES ])
-> where ( 'financial_type' , OrderEnum :: ORDER_MARGIN );
$deposit_today = $deposit
-> whereDay ( 'create_time' )
-> sum ( 'number' );
2024-06-18 10:47:26 +08:00
//当日的现金收银
2024-06-22 10:07:28 +08:00
$cash_today = StoreCashFinanceFlow :: where ( 'store_id' , $params [ 'store_id' ])
2024-06-13 17:25:39 +08:00
-> whereDay ( 'create_time' )
2024-06-22 10:07:28 +08:00
-> where ( 'status' , YesNoEnum :: YES )
2024-06-18 11:16:48 +08:00
-> sum ( 'receivable' );
2024-06-13 17:25:39 +08:00
//总的营业额的统计 总的利润的统计 总的成本合集的统计 总的加到保证金的
2024-06-22 10:07:28 +08:00
$all = StoreOrder :: where ([ 'paid' => YesNoEnum :: YES , 'store_id' => $params [ 'store_id' ]]);
$deposit_all = StoreFinanceFlow :: where ([ 'store_id' => $params [ 'store_id' ], 'status' => YesNoEnum :: YES ])
2024-06-18 10:47:26 +08:00
-> sum ( 'number' );
2024-06-14 17:55:56 +08:00
2024-06-22 10:07:28 +08:00
$cash_all = StoreCashFinanceFlow :: where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'status' , YesNoEnum :: YES )
2024-06-18 12:10:21 +08:00
-> sum ( 'receivable' );
2024-06-22 10:07:28 +08:00
if ( isset ( $params [ 'month' ]) && $params [ 'month' ]) {
$all = StoreOrder :: where ([ 'paid' => YesNoEnum :: YES , 'store_id' => $params [ 'store_id' ]])
-> whereMonth ( 'create_time' , $params [ 'month' ]);
$deposit_all = SystemStore :: where ( 'id' , $params [ 'store_id' ])
2024-06-14 17:55:56 +08:00
-> whereMonth ( 'create_time' , $params [ 'month' ])
-> value ( 'paid_deposit' );
2024-06-22 10:07:28 +08:00
$cash_all = StoreCashFinanceFlow :: where ( 'store_id' , $params [ 'store_id' ])
-> where ( 'status' , YesNoEnum :: YES )
2024-06-14 17:55:56 +08:00
-> whereMonth ( 'create_time' , $params [ 'month' ])
2024-06-18 11:16:48 +08:00
-> sum ( 'receivable' );
2024-06-14 17:53:31 +08:00
}
2024-06-13 17:25:39 +08:00
$turnover_all = $all
-> sum ( 'pay_price' );
$profit_all = $all
-> sum ( 'profit' );
2024-06-18 12:10:21 +08:00
//消耗余额 V2.0
2024-06-22 10:07:28 +08:00
$cost_all = CapitalFlow :: where ([ 'category' => 'user_order_balance_pay' , 'store_id' => $params [ 'store_id' ]])
2024-06-18 12:10:21 +08:00
-> sum ( 'amount' );
2024-06-13 17:25:39 +08:00
$time = self :: getLastSevenDays ();
$newArr = [];
2024-06-22 10:07:28 +08:00
foreach ( $time as $value ) {
$data = self :: dealSearch ( $params [ 'store_id' ], $value );
2024-06-13 17:25:39 +08:00
$newArr [ $value ] = $data ;
}
return [
2024-06-22 10:07:28 +08:00
'today' => [
'turnover_today' => $turnover_today ,
'profit_today' => $profit_today ,
'cost_today' => $cost_today ,
'deposit_today' => $deposit_today ,
'cash_today' => $cash_today ,
2024-06-13 17:25:39 +08:00
],
2024-06-22 10:07:28 +08:00
'all' => [
'turnover_all' => $turnover_all ,
'profit_all' => $profit_all ,
'cost_all' => $cost_all ,
'deposit_all' => $deposit_all ,
'cash_all' => $cash_all ,
2024-06-13 17:25:39 +08:00
],
2024-06-22 10:07:28 +08:00
'time' => $newArr
2024-06-13 17:25:39 +08:00
];
// 可以选时间 当日订单数 本月订单数 拆分线上和线下的方式 ---v.01
/* if ( $params [ 'start_time' ] && $params [ 'end_time' ]){
$startTime = $params [ 'start_time' ];
$endTime = $params [ 'end_time' ];
$endTime = date ( 'Y-m-d' , strtotime ( $endTime ) + 86400 );
} else {
$startTime = date ( 'Y-m-d' , time ());
$endTime = date ( 'Y-m-d' , strtotime ( $startTime ) + 86400 );
}
//当日订单和当月订单
$base = StoreOrder :: where ( 'paid' , YesNoEnum :: YES );
$offline_order_today_num = $base
-> whereIn ( 'shipping_type' , OrderEnum :: OFFLINE )
-> whereDay ( 'create_time' )
-> count ();
$offline_order_today_month = $base
-> whereIn ( 'shipping_type' , OrderEnum :: OFFLINE )
-> whereMonth ( 'create_time' )
-> count ();
//条码收银的-->微信和支付宝的没那个
//线下现金收银
whereIn ( 'pay_type' ,[ OrderEnum :: CASHIER_ORDER_PAY ,
OrderEnum :: CASHIER_ORDER_ALI_PAY , OrderEnum :: CASHIER_FACE_PAY ])
$cash = StoreCashFinanceFlow :: where ( 'store_id' , $params [ 'store_id' ])
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> where ( 'status' , YesNoEnum :: YES );
$cash_count = $cash -> count ();
$cash_receipt = $cash -> sum ( 'receipts' ); */
}
public static function getLastSevenDays ()
{
$dates = [];
$today = new \DateTime (); // 获取当前日期
for ( $i = 0 ; $i < 30 ; $i ++ ) {
$date = clone $today ; // 克隆当前日期对象,以避免修改原始对象
$date -> modify ( " - $i day " ); // 减去相应的天数
$dates [] = $date -> format ( 'Y-m-d' ); // 格式化日期并添加到数组中
}
return $dates ;
}
2024-06-22 10:07:28 +08:00
public static function dealSearch ( $store_id , $startTime )
2024-06-13 17:25:39 +08:00
{
$endTime = date ( 'Y-m-d' , strtotime ( $startTime ) + 86400 );
//当日营业额的统计 当日利润的统计 当日成本合集的统计 当日加到保证金的 当日的现金收银
2024-07-01 15:06:55 +08:00
$today = StoreOrder :: where ([ 'paid' => YesNoEnum :: YES , 'store_id' => $store_id , 'refund_status' => 0 ]);
2024-06-13 17:25:39 +08:00
$turnover_today = $today
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> sum ( 'pay_price' );
$profit_today = $today
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> sum ( 'profit' );
$cost_today = $today
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> sum ( 'cost' );
2024-06-22 10:07:28 +08:00
$deposit = StoreFinanceFlow :: where ([ 'store_id' => $store_id ])
-> where ( 'financial_type' , OrderEnum :: ORDER_MARGIN );
$deposit_today = $deposit
2024-06-13 17:25:39 +08:00
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
-> sum ( 'number' );
2024-06-22 10:07:28 +08:00
$cash_today = StoreCashFinanceFlow :: where ( 'store_id' , $store_id )
2024-06-13 17:25:39 +08:00
-> whereBetweenTime ( 'create_time' , $startTime , $endTime )
2024-06-22 10:07:28 +08:00
-> where ( 'status' , YesNoEnum :: YES )
2024-06-13 17:25:39 +08:00
-> sum ( 'receipts' );
return [
2024-06-22 10:07:28 +08:00
'turnover_today' => $turnover_today ,
'profit_today' => $profit_today ,
'cost_today' => $cost_today ,
'deposit_today' => $deposit_today ,
'cash_today' => $cash_today ,
2024-06-13 17:25:39 +08:00
];
}
2024-06-17 15:50:05 +08:00
public static function rechargeData ( $params )
{
$data [ 'receipt_amount' ] = UserRecharge :: where ([
2024-06-22 10:07:28 +08:00
'store_id' => $params [ 'store_id' ],
'paid' => YesNoEnum :: YES
2024-06-17 15:50:05 +08:00
]) -> sum ( 'price' );
2024-06-22 10:07:28 +08:00
return $data ? ? [];
2024-06-17 15:50:05 +08:00
}
2024-06-13 17:25:39 +08:00
2024-06-20 14:41:04 +08:00
/**
* 商品趋势
*/
public static function get_trend ( $store_id )
{
$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 " => $dates ,
" series " => [
[
" name " => " 商品浏览量 " ,
2024-06-22 10:07:28 +08:00
" data " => self :: store_visit_count ( $dates , $store_id ),
2024-06-20 14:41:04 +08:00
" type " => " line " ,
" smooth " => " true " ,
" yAxisIndex " => 1
],
[
" name " => " 商品访客量 " ,
2024-06-22 10:07:28 +08:00
" data " => self :: store_visit_user ( $dates , $store_id ),
2024-06-20 14:41:04 +08:00
" type " => " line " ,
" smooth " => " true " ,
" yAxisIndex " => 1
],
[
" name " => " 支付金额 " ,
2024-06-22 10:07:28 +08:00
" data " => self :: payPrice ( $dates , $store_id ),
2024-06-20 14:41:04 +08:00
" type " => " bar "
],
[
" name " => " 退款金额 " ,
2024-06-22 10:07:28 +08:00
" data " => self :: refundPrice ( $dates , $store_id ),
2024-06-20 14:41:04 +08:00
" type " => " bar "
]
]
];
return $data ;
}
/**
* 商品浏览量
*/
2024-06-22 10:07:28 +08:00
public static function store_visit_count ( $dates , $store_id )
2024-06-20 14:41:04 +08:00
{
$data = [];
foreach ( $dates as $date ) {
2024-06-22 10:07:28 +08:00
$data [] = StoreVisit :: whereDay ( 'create_time' , $date ) -> where ( 'store_id' , $store_id ) -> cache ( 'store_visit_count_' . $date , 300 ) -> sum ( 'count' );
2024-06-20 14:41:04 +08:00
}
return $data ;
}
/**
* 商品浏览量
*/
2024-06-22 10:07:28 +08:00
public static function store_visit_user ( $dates , $store_id )
2024-06-20 14:41:04 +08:00
{
$data = [];
foreach ( $dates as $date ) {
2024-06-22 10:07:28 +08:00
$data [] = StoreVisit :: whereDay ( 'create_time' , $date ) -> where ( 'store_id' , $store_id ) -> cache ( 'store_visit_user_' . $date , 300 ) -> count ( 'uid' );
2024-06-20 14:41:04 +08:00
}
return $data ;
}
/**
* 支付金额
*/
2024-06-22 10:07:28 +08:00
public static function payPrice ( $dates , $store_id )
2024-06-20 14:41:04 +08:00
{
$data = [];
foreach ( $dates as $date ) {
2024-06-22 10:07:28 +08:00
$data [] = StoreOrder :: whereDay ( 'create_time' , $date ) -> where ( 'store_id' , $store_id ) -> cache ( 'payPrice_' . $date , 300 ) -> where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> sum ( 'pay_price' );
2024-06-20 14:41:04 +08:00
}
return $data ;
}
/**
* 退款金额
*/
2024-06-22 10:07:28 +08:00
public static function refundPrice ( $dates , $store_id )
2024-06-20 14:41:04 +08:00
{
$data = [];
foreach ( $dates as $date ) {
2024-06-22 10:07:28 +08:00
$data [] = StoreOrder :: whereDay ( 'create_time' , $date ) -> where ( 'store_id' , $store_id ) -> where ( 'status' , 'in' , [ - 1 , - 2 ]) -> cache ( 'refundPrice_' . $date , 300 ) -> where ( 'paid' , 1 ) -> sum ( 'pay_price' );
2024-06-20 14:41:04 +08:00
}
return $data ;
}
/**
* 获取商品排名数据
*/
2024-06-22 10:07:28 +08:00
public static function get_product_ranking ( $where , $store_id )
2024-06-20 14:41:04 +08:00
{
2024-06-22 10:07:28 +08:00
return self :: product_ranking ( $where , $store_id );
2024-06-21 16:21:00 +08:00
}
2024-06-22 10:07:28 +08:00
public static function product_ranking ( $where , $store_id )
2024-06-21 16:21:00 +08:00
{
$time = explode ( '-' , $where [ 'create_time' ]);
$time = [ strtotime ( $time [ 0 ]), strtotime ( $time [ 1 ])];
2024-06-22 10:07:28 +08:00
$list = StoreProductLog :: whereBetweenTime ( 'create_time' , $time [ 0 ], $time [ 1 ])
-> where ( 'store_id' , $store_id )
2024-06-21 16:21:00 +08:00
-> field ([
'store_id' ,
'product_id' ,
'SUM(visit_num) as visit' ,
'COUNT(distinct(uid)) as user' ,
'SUM(cart_num) as cart' ,
'SUM(order_num) as orders' ,
'SUM(pay_num) as pay' ,
'SUM(pay_price * pay_num) as price' ,
'SUM(cost_price) as cost' ,
'ROUND((SUM(pay_price)-SUM(cost_price))/SUM(pay_price),2) as profit' ,
'SUM(collect_num) as collect' ,
'ROUND((COUNT(distinct(pay_uid))-1)/COUNT(distinct(uid)),2) as changes' ,
'COUNT(distinct(pay_uid))-1 as repeats'
2024-06-22 10:07:28 +08:00
]) -> group ( 'product_id' ) -> order ( 'pay' , ' desc' ) -> limit ( 20 ) -> select () -> toArray ();
2024-06-21 16:21:00 +08:00
foreach ( $list as $key => & $item ) {
2024-06-22 10:07:28 +08:00
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> field ( 'store_name,image' ) -> find ();
$item [ 'store_name' ] = $find [ 'store_name' ];
$item [ 'image' ] = $find [ 'image' ];
2024-06-21 16:21:00 +08:00
if ( $item [ 'profit' ] == null ) $item [ 'profit' ] = 0 ;
if ( $item [ 'changes' ] == null ) $item [ 'changes' ] = 0 ;
if ( $item [ 'repeats' ] == null ) {
$item [ 'repeats' ] = 0 ;
} else {
2024-06-22 10:07:28 +08:00
$item [ 'repeats' ] = bcdiv ( count ( StoreProductLog :: where ( $where ) -> where ( 'type' , 'pay' ) -> where ( 'product_id' , $item [ 'product_id' ]) -> where ( 'store_id' , $store_id ) -> field ( 'count(pay_uid) as p' ) -> group ( 'pay_uid' ) -> having ( 'p>1' ) -> select ()), $item [ 'repeats' ], 2 );
2024-06-21 16:21:00 +08:00
}
}
return array_merge ( $list );
2024-06-20 14:41:04 +08:00
}
2024-06-01 16:07:53 +08:00
}