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

42 lines
1.6 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 $userId 用户ID
* @param int $startTime 开始时间戳
* @param int $endTime 结束时间戳
* @return array 包含余额消费和微信支付消费的数组
*/
public static function getUserConsumptionByTimeRange($userId, $startTime, $endTime)
{
// 查询用户在指定时间范围内的订单消费情况
$consumptionData = Order::where('user_id', '=', $userId)
->where(function ($query) {
$query->where('price', '>', 0)
->whereOr('use_money', '>', 0);
})
->where('status', '=', 1)
->where('pay_time', '>=', $startTime)
->where('pay_time', '<=', $endTime)
->field('sum(use_money) as balance_consumed, sum(price) as wechat_consumed')
->find();
// 处理查询结果
$result = [
'money_consumed' => round(floatval($consumptionData['balance_consumed'] ?? 0), 2), // 余额消费
'wechat_consumed' => round(floatval($consumptionData['wechat_consumed'] ?? 0), 2), // 微信支付消费
'total_consumed' => round(floatval(($consumptionData['balance_consumed'] ?? 0) + ($consumptionData['wechat_consumed'] ?? 0)), 2) // 总消费
];
return $result;
}
}