87 lines
3.0 KiB
PHP
87 lines
3.0 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;
|
|
/**
|
|
* 盒子服务类
|
|
*
|
|
*/
|
|
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;
|
|
// 初始化返回数据
|
|
$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,
|
|
];
|
|
|
|
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);
|
|
}
|
|
|
|
// 处理每日限购
|
|
if ($dailyLimit > 0) {
|
|
$userDailyPurchased = OrderList::field('id')
|
|
->where($commonConditions)
|
|
->where('shang_id', 'between', $shangCountId)
|
|
->where('addtime', '>=', strtotime(date('Y-m-d')))
|
|
->count();
|
|
$limitInfo['user_daily_purchased'] = $userDailyPurchased;
|
|
$limitInfo['user_daily_remaining'] = max(0, $dailyLimit - $userDailyPurchased);
|
|
$limitInfo['daily_limit'] = $dailyLimit;
|
|
}
|
|
|
|
return $limitInfo;
|
|
}
|
|
} |