manghe/app/command/UserStatisticsDay.php
2025-03-21 18:43:39 +08:00

170 lines
7.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\command;
use app\common\model\User;
use app\common\model\UserAccount;
use app\common\model\UserStatistics;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use app\common\model\Order as OrderModel;
use app\common\model\OrderList;
use app\common\model\OrderListRecovery;
use app\common\model\ProfitExpenses;
use app\common\model\ProfitRvenue;
class UserStatisticsDay extends Command
{
// php think UserStatisticsDay
protected function configure()
{
$this->setName('UserStatisticsHour')->setDescription('用户每天数据统计');
}
protected function execute(Input $input, Output $output)
{
//# 统计表 user_statistics
// ELECT `id`, `login_count`, `register_count`, `created_at`, `updated_at`, `record_date` FROM `user_statistics`
//# 用户登录记录表 user_accountlast_login_time 最后登录时间时间戳int(11) 。user_id 用户id
//SELECT `id`, `user_id`, `account_token`, `token_num`, `token_time`, `last_login_time`, `last_login_ip`, `last_login_ip1`, `ip_adcode`, `ip_province`, `ip_city` FROM `user_account`
$userList = User::where('istest', '>=', 1)->field('id')->select();
$userNotArray = array_column($userList->toArray(), 'id');
// 获取当天日期
$today = date('Y-m-d');
// 检查当天的统计记录是否存在
$existingRecord = UserStatistics::where('record_date', $today)
->find();
$today_start = strtotime($today);
$today_end = strtotime($today . ' +1 day');
// 查询当天的登录人数
$loginCount = UserAccount::where('last_login_time', '>=', $today_start)
->where('last_login_time', '<', $today_end)
->count('DISTINCT user_id');
//当天注册人数
$registerCount = User::where('addtime', '>=', $today_start)
->count('id');
// 本日充值金额
$order_today_price = OrderModel::where('status', '=', 1)->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)->sum('price');
// 消费人数统计
$consume_today = OrderModel::where('status', '=', 1)->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)
->field('user_id')->group('user_id')->count('user_id');
// 消费RMB人数统计
$consume_today_price = OrderModel::where('status', '=', 1)
->where('price', '>', '0')
->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)
->field('user_id')->group('user_id')->count('user_id');
//今日余额消费
$money_today = OrderModel::where('status', '=', 1)->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)->sum('use_money');
//今日消费
$order_zhe_total = OrderModel::where('status', '=', 1)->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)->sum('order_zhe_total');
//今日消费
$consume_order_count = OrderModel::where('status', '=', 1)->whereNotIn('user_id', $userNotArray)
->whereBetweenTime(
'addtime',
$today_start,
$today_end
)->count();
//发放价值
$shipment_money = OrderList::whereBetweenTime('addtime', $today_start, $today_end)->whereNotIn('user_id', $userNotArray)->sum('goodslist_money');
//回收价值
$shipment_money1 = OrderList::where('status', '=', 1)->whereBetweenTime('choice_time', $today_start, $today_end)->whereNotIn('user_id', $userNotArray)->sum('goodslist_money');
$send_money = OrderList::where('status', '=', 2)->whereBetweenTime('choice_time', $today_start, $today_end)->whereNotIn('user_id', $userNotArray)->sum('goodslist_money');
$profit_expenses = ProfitExpenses::getTotalAmountByDate(date('Y-m-d'));
$profit_rvenue = ProfitRvenue::getTotalAmountByDate(date('Y-m-d'));
$profit_money = $order_zhe_total - $shipment_money;
//今日余额发放
$money_recovery_today = OrderListRecovery::
whereBetweenTime(
'addtime',
$today_start,
$today_end
)->whereNotIn('user_id', $userNotArray)->sum('money');
if ($existingRecord) {
// 更新记录
UserStatistics::where('record_date', $today)
->update([
'login_count' => $loginCount,
'register_count' => $registerCount,
'recharge_amount' => $order_today_price,
'consume_user_count' => $consume_today,
'balance_consume' => $money_today,
'balance_issue' => $money_recovery_today,
'consume_rmb_count' => $consume_today_price,
'recharge_sum' => $order_zhe_total,
'consume_order_count' => $consume_order_count,
'shipment_money' => $shipment_money,
'send_money' => $send_money,
'recycle_money' => $shipment_money1,
'profit_money' => $profit_money,
'all_shipment_money' => $shipment_money + $profit_expenses,
'all_recycle_money' => $order_zhe_total + $profit_rvenue,
'all_money' => round(($order_zhe_total + $profit_rvenue) - ($shipment_money + $profit_expenses), 2),
'updated_at' => date('Y-m-d H:i:s')
]);
} else {
// 插入新记录
UserStatistics::insert([
'login_count' => $loginCount,
'register_count' => $registerCount, // 假设注册人数统计在其他地方处理
'record_date' => $today,
'recharge_amount' => $order_today_price,
'balance_consume' => $money_today,
'balance_issue' => $money_recovery_today,
'consume_user_count' => $consume_today,
'consume_rmb_count' => $consume_today_price,
'recharge_sum' => $order_zhe_total,
'consume_order_count' => $consume_order_count,
'shipment_money' => $shipment_money,
'send_money' => $send_money,
'recycle_money' => $shipment_money1,
'profit_money' => $profit_money,
'all_shipment_money' => $shipment_money + $profit_expenses,
'all_recycle_money' => $order_zhe_total + $profit_rvenue,
'all_money' => round(($order_zhe_total + $profit_rvenue) - ($shipment_money + $profit_expenses), 2),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
}
}
}