HaniBlindBox/server/php/app/common/service/GoodsService.php
2026-01-01 20:46:07 +08:00

111 lines
4.1 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\Goods as GoodsModel;
use app\common\model\Order;
use app\common\model\GoodsExtend;
use app\common\model\CouponReceive as CouponReceiveModel;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\OrderList;
use app\common\model\CouponReceive;
use app\common\helper\ConfigHelper;
/**
* 盒子服务类
*
*/
class GoodsService
{
/**
* 获取商品限购信息
* @param int $userId
* @param int $goodsNum
* @param array $goods
* @param array $shangCountId
* @return array|array{daily_limit: int, global_limit: int, user_daily_purchased: int, user_daily_remaining: int, user_global_purchased: int, user_global_remaining: int}
*/
function getPurchaseLimitInfo(
int $userId,
int $goodsNum,
$goods,
array $shangCountId
): array {
$globalLimit = 0;
$dailyLimit = 0;
$daily_coupon_limit = ConfigHelper::getAppSettingKey("daily_coupon_limit");
if ($daily_coupon_limit == "") {
$daily_coupon_limit = 0;
} else {
$daily_coupon_limit = intval($daily_coupon_limit);
}
// 初始化返回数据
$limitInfo = [
'global_limit' => $globalLimit, // 全局限购数量
'user_global_purchased' => 0, // 用户已购买总数
'user_global_remaining' => $globalLimit, // 用户剩余可购买总数
'daily_limit' => $dailyLimit, // 每日限购数量
'user_daily_purchased' => 0, // 用户今日已购买数
'user_daily_remaining' => $dailyLimit, // 用户今日剩余可购买数
'user_test' => 0,
'coupon_limit' => $daily_coupon_limit,
'user_coupon_purchased' => 0, // 用户今日已购买数
'user_coupon_remaining' => $daily_coupon_limit, // 用户今日剩余可购买数
];
if ($userId <= 0 || $goods == null) {
return $limitInfo;
}
$goodsId = $goods['id'];
$goodsType = $goods['type'];
$dailyLimit = $goods['daily_xiangou'];
$globalLimit = $goods['quanju_xiangou'];
// 公共查询条件
$commonConditions = [
'goods_id' => $goodsId,
'num' => $goodsNum,
'order_type' => $goodsType,
'user_id' => $userId,
'parent_goods_list_id' => 0
];
// 处理全局限购
if ($globalLimit > 0) {
$userGlobalPurchased = OrderList::field('id')
->where($commonConditions)
->where('shang_id', 'between', $shangCountId)
->count();
$limitInfo['global_limit'] = $globalLimit;
$limitInfo['user_global_purchased'] = $userGlobalPurchased;
$limitInfo['user_global_remaining'] = max(0, $globalLimit - $userGlobalPurchased);
}
$day = strtotime(date('Y-m-d'));
// 处理每日限购
if ($dailyLimit > 0) {
$userDailyPurchased = OrderList::field('id')
->where($commonConditions)
->where('shang_id', 'between', $shangCountId)
->where('addtime', '>=', $day)
->count();
$limitInfo['user_daily_purchased'] = $userDailyPurchased;
$limitInfo['user_daily_remaining'] = max(0, $dailyLimit - $userDailyPurchased);
$limitInfo['daily_limit'] = $dailyLimit;
}
if ($daily_coupon_limit > 0) {
$user_coupon_purchased = Order::field('id')
->where('status', '=', 1)
->where('coupon_id', '>', 0)
->where('user_id', '=', $userId)
->where('pay_time', '>=', $day)
->count();
$limitInfo['user_coupon_purchased'] = $user_coupon_purchased;
$limitInfo['user_coupon_remaining'] = max(0, $daily_coupon_limit - $user_coupon_purchased);
$limitInfo['coupon_limit'] = $daily_coupon_limit;
}
return $limitInfo;
}
}