manghe/app/common/service/CommonService.php
2025-04-10 03:53:07 +08:00

49 lines
1.2 KiB
PHP
Executable File

<?php
namespace app\common\service;
use app\common\model\User;
use app\common\model\Reward;
use app\common\model\CouponReceive;
use app\common\model\Coupon;
use app\common\model\Order;
/**
* 通用服务类
*/
class CommonService
{
/**
* 获取用户在指定时间范围内的消费情况
*
* @param int $user_id 用户ID
* @param int $start_time 开始时间戳
* @param int $end_time 结束时间戳
* @return array 消费数据
*/
public static function getUserConsumptionByTimeRange($user_id, $start_time, $end_time)
{
// 查询用户在指定时间范围内的订单
$query = Order::where('user_id', '=', $user_id)
->where('status', '=', 1);
// 添加时间范围条件
if ($start_time) {
$query->where('addtime', '>=', $start_time);
}
if ($end_time) {
$query->where('addtime', '<=', $end_time);
}
// 计算总消费金额
$total_consumed = $query->sum('price');
// 获取订单数量
$order_count = $query->count();
return [
'total_consumed' => $total_consumed,
'order_count' => $order_count
];
}
}