98 lines
3.5 KiB
PHP
Executable File
98 lines
3.5 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;
|
|
}
|
|
|
|
/**
|
|
* 获取用户总消费金额
|
|
*
|
|
* @param int $userId 用户ID
|
|
* @return array 包含钻石消费和RMB支付消费的数组
|
|
*/
|
|
public static function getUserTotalConsumption($userId)
|
|
{
|
|
// 查询用户的总订单消费情况
|
|
$consumptionData = Order::where('user_id', '=', $userId)
|
|
->where(function ($query) {
|
|
$query->where('price', '>', 0)
|
|
->whereOr('use_money', '>', 0);
|
|
})
|
|
->where('status', '=', 1)
|
|
->field('sum(use_money) as diamond_consumed, sum(price) as rmb_consumed')
|
|
->find();
|
|
|
|
// 处理查询结果
|
|
$result = [
|
|
'diamond_consumed' => round(floatval($consumptionData['diamond_consumed'] ?? 0), 2), // 钻石消费
|
|
'rmb_consumed' => round(floatval($consumptionData['rmb_consumed'] ?? 0), 2), // RMB支付消费
|
|
'total_consumed' => round(floatval(($consumptionData['diamond_consumed'] ?? 0) + ($consumptionData['rmb_consumed'] ?? 0)), 2) // 总消费
|
|
];
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 获取用户总消费金额(只返回总金额数值)
|
|
*
|
|
* @param int $userId 用户ID
|
|
* @return float 用户总消费金额
|
|
*/
|
|
public static function getTotalConsumptionAmount($userId)
|
|
{
|
|
// 查询用户的总订单消费情况
|
|
$consumptionData = Order::where('user_id', '=', $userId)
|
|
->where(function ($query) {
|
|
$query->where('price', '>', 0)
|
|
->whereOr('use_money', '>', 0);
|
|
})
|
|
->where('status', '=', 1)
|
|
->field('sum(use_money) as diamond_consumed, sum(price) as rmb_consumed')
|
|
->find();
|
|
|
|
// 计算总消费金额
|
|
$totalAmount = round(floatval(($consumptionData['diamond_consumed'] ?? 0) + ($consumptionData['rmb_consumed'] ?? 0)), 2);
|
|
|
|
return $totalAmount;
|
|
}
|
|
} |