From 3eec765f4cc86581c36336c15eb8a85ce8215631 Mon Sep 17 00:00:00 2001 From: youda Date: Sat, 12 Apr 2025 17:34:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Goods.php | 116 +++++++++++------------ app/api/controller/Infinite.php | 111 +++++++++++----------- app/common/service/GoodsService.php | 87 +++++++++++++++++ app/common/service/PaymentCalculator.php | 5 +- 4 files changed, 201 insertions(+), 118 deletions(-) create mode 100644 app/common/service/GoodsService.php diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index 312cbf5..3c9f898 100755 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -16,6 +16,7 @@ use app\common\model\User; use app\common\model\Order; use app\common\model\OrderList; use app\common\model\UserVip; +use app\common\service\GoodsService; use think\facade\Db; use \think\Request; use app\common\model\CouponReceive as CouponReceiveModel; @@ -200,11 +201,12 @@ class Goods extends Base */ public function goodsdetail() { - $user_info = $this->getUser(); + $user_id = $this->getUserId(); + $goods_id = request()->param('goods_id/d', 0); $goods_num = request()->param('goods_num/d', 0); - $goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,sale_time,is_shou_zhe,quanju_xiangou') + $goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,sale_time,is_shou_zhe,quanju_xiangou,daily_xiangou,quanju_xiangou') ->where(['id' => $goods_id]) ->find(); if (!$goods) { @@ -240,13 +242,9 @@ class Goods extends Base } } $goods['num'] = $goods_num; - #是否收藏 - $collection_is = Collect::field('id') - ->where(['user_id' => $user_info['id']]) - ->where(['goods_id' => $goods_id]) - ->where(['num' => $goods_num]) - ->find(); - $goods['collection_is'] = $collection_is ? 1 : 0; + $collection_is = 0; + + #本箱子余量 $goods_surplus = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') ->where('goods_id', '=', $goods_id) @@ -326,9 +324,9 @@ class Goods extends Base ->find(); if ($goods['lock_is'] && $goods_lock_info) { $goods_lock_surplus_time = $goods_lock_info['endtime']; - $user_info = User::field('nickname,headimg')->where(['id' => $goods_lock_info['user_id']])->find(); - $goods_lock_user_nickname = $user_info['nickname']; - $goods_lock_user_headimg = imageUrl($user_info['headimg']); + $user_info_lock = User::field('nickname,headimg')->where(['id' => $goods_lock_info['user_id']])->find(); + $goods_lock_user_nickname = $user_info_lock['nickname']; + $goods_lock_user_headimg = imageUrl($user_info_lock['headimg']); } $lock_info = [ 'lock_is' => $goods['lock_is'], @@ -363,28 +361,28 @@ class Goods extends Base $config = getConfig('base'); $goods['three_time'] = $config['three_time']; $goods['five_time'] = $config['five_time']; - $xiangou = null; - $quanju_xiangou = $goods['quanju_xiangou']; - if ($quanju_xiangou > 0 && $goods['type'] == 6) { - //$user_info - $user_xiangou_count = OrderList::field('id')->where('goods_id', '=', $goods_id) - ->where('num', '=', $goods_num) - ->where('shang_id', 'between', self::$shang_count_id) - ->where('order_type', '=', $goods['type']) - ->where('user_id', '=', $user_info['id']) - ->count(); - $xiangou = [ - 'quanju_xiangou' => $quanju_xiangou, - 'user_xiangou_count' => $user_xiangou_count, - ]; + + $goodsService = new GoodsService(); + $limitInfo = $goodsService->getPurchaseLimitInfo($user_id, $goods_num, $goods, self::$shang_count_id); + if ($user_id > 0) { + #是否收藏 + $collection_is = Collect::field('id') + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => $goods_num]) + ->find(); + $goods['collection_is'] = $collection_is ? 1 : 0; + } + + $new_data = [ 'goods' => $goods, 'lock_info' => $lock_info, 'join_user' => $new_join_user, 'join_count' => $join_count, 'goodslist' => $goodslist, - 'xuangou' => $xiangou + 'limitInfo' => $limitInfo ]; return $this->renderSuccess("请求成功", $new_data); @@ -459,7 +457,7 @@ class Goods extends Base #概率 if ($surplus_stock_1 > 0) { //计算概率,保留两位小数 - $pro_num_1 = removeTrailingZeros(round(($surplus_stock_1 / $goodsList_surplus_stock) * 100, 2) ); + $pro_num_1 = removeTrailingZeros(round(($surplus_stock_1 / $goodsList_surplus_stock) * 100, 2)); $pro_1 = '概率:' . $pro_num_1 . '%'; } else { $pro_1 = '概率:' . 0 . '%'; @@ -671,7 +669,7 @@ class Goods extends Base $coupon_id = request()->param('coupon_id/d', 0); //优惠券 #盒子信息 - $goods = Goodsmodel::field('title,imgurl_detail,type,price,status,is_shou_zhe,quanju_xiangou,lock_is,choujiang_xianzhi,lock_time,daily_xiangou')->where(['id' => $goods_id]) + $goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,quanju_xiangou,lock_is,choujiang_xianzhi,lock_time,daily_xiangou')->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); @@ -679,32 +677,32 @@ class Goods extends Base if ($goods['status'] != 1) { return $this->renderError("盒子已下架"); } - + // 获取服务实例 $calculator = new \app\common\service\PaymentCalculator(); - + // 验证抽奖限制 $validationResult = $calculator->validateDrawRestrictions($user, $goods, $prize_num); if ($validationResult['status'] == 0) { return $this->renderError($validationResult['msg']); } - + // 计算订单金额 $result = $calculator->calculateOrderAmount( - $user, - $goods_id, - $prize_num, - $use_money_is, - $use_integral_is, - $use_money2_is, - $coupon_id, + $user, + $goods_id, + $prize_num, + $use_money_is, + $use_integral_is, + $use_money2_is, + $coupon_id, $goods ); - + if ($result['status'] == 0) { return $this->renderError($result['msg']); } - + return $this->renderSuccess("请求成功", $result); } @@ -727,7 +725,7 @@ class Goods extends Base $coupon_id = request()->param('coupon_id/d', 0); //优惠券 #盒子信息 - $goods = Goodsmodel::field('title,imgurl_detail,type,price,status,is_shou_zhe,quanju_xiangou,lock_is,choujiang_xianzhi,lock_time,flw_start_time,flw_end_time,daily_xiangou')->where(['id' => $goods_id]) + $goods = Goodsmodel::field('id,title,imgurl_detail,type,price,status,is_shou_zhe,quanju_xiangou,lock_is,choujiang_xianzhi,lock_time,flw_start_time,flw_end_time,daily_xiangou')->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); @@ -747,7 +745,7 @@ class Goods extends Base if (RegInt($num)) { return $this->renderError("箱号选择错误"); } - + // 使用PaymentCalculator验证抽奖限制(限购、消费门槛等) $paymentCalculator = new \app\common\service\PaymentCalculator(); $validationResult = $paymentCalculator->validateDrawRestrictions($user, $goods, $prize_num); @@ -815,20 +813,20 @@ class Goods extends Base return $this->renderError("剩余数量不足,请刷新重试"); } } - + // 使用PaymentCalculator计算订单金额和支付方式 $paymentResult = $paymentCalculator->calculateOrderAmount( - $user, - $goods_id, - $prize_num, - $use_money_is, - $use_integral_is, - $use_money2_is, - $coupon_id, - $goods, + $user, + $goods_id, + $prize_num, + $use_money_is, + $use_integral_is, + $use_money2_is, + $coupon_id, + $goods, $goods_extend ); - + if ($paymentResult['status'] == 0) { return $this->renderError($paymentResult['msg']); } @@ -1281,7 +1279,7 @@ class Goods extends Base // 删除现有的商品列表数据 GoodsList::where('goods_id', $goodsId)->delete(); - + // 删除现有的扩展表数据 GoodsExtend::where('goods_id', $goodsId)->delete(); } else { @@ -1289,12 +1287,12 @@ class Goods extends Base $goods['async_date'] = date('Y-m-d H:i:s'); $goods['addtime'] = time(); $goods['update_time'] = time(); - $goods['king_user_id'] =0; + $goods['king_user_id'] = 0; $goodsModel = new Goodsmodel(); $goodsModel->save($goods); $goodsId = $goodsModel->id; } - + // 处理盒子扩展数据 if ($goodsExtendData) { if (isset($goodsExtendData['id'])) { @@ -1334,7 +1332,7 @@ class Goods extends Base } $listItem['goods_id'] = $goodsId; - + // 处理奖励ID if (!empty($oldRewardId)) { $newRewardId = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); @@ -1366,7 +1364,7 @@ class Goods extends Base if (isset($idMapping[$originalParentId])) { $listItem['goods_list_id'] = $idMapping[$originalParentId]; } - + // 处理奖励ID if (!empty($oldRewardId)) { $newRewardId = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999); @@ -1378,7 +1376,7 @@ class Goods extends Base $goodsListModel = new GoodsList(); $goodsListModel->save($listItem); } - + // 处理奖励数据 foreach ($rewardsData as $reward) { $oldRewardId = $reward['reward_id']; diff --git a/app/api/controller/Infinite.php b/app/api/controller/Infinite.php index 0459035..299ced0 100755 --- a/app/api/controller/Infinite.php +++ b/app/api/controller/Infinite.php @@ -19,7 +19,7 @@ use think\facade\Db; use app\common\model\CouponReceive as CouponReceiveModel; use app\common\model\GoodsExtend; use \think\Request; - +use app\common\service\GoodsService; /** * 无限赏 */ @@ -32,9 +32,9 @@ class Infinite extends Base */ public function infinite_goodsdetail(Request $request) { - $user_info = $this->getUser(); + $user_id = $this->getUserId(); $goods_id = request()->param('goods_id/d', 0); - $goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,addtime,rage_is,rage,item_card_id,lingzhu_is,is_shou_zhe') + $goods = Goodsmodel::field('id,title,imgurl_detail,price,stock,sale_stock,lock_is,type,status,addtime,rage_is,rage,item_card_id,lingzhu_is,is_shou_zhe,daily_xiangou,quanju_xiangou') ->where(['id' => $goods_id]) ->find(); if (!$goods) { @@ -70,24 +70,6 @@ class Infinite extends Base } } - - #一发 - $goods['price_one'] = (bcmul("{$goods['price']}", "1", 2)) * 1; - #三发 - $goods['price_three'] = (bcmul("{$goods['price']}", "3", 2)) * 1; - #五连发 - $goods['price_five'] = (bcmul("{$goods['price']}", "5", 2)) * 1; - #十连发 - $goods['price_ten'] = (bcmul("{$goods['price']}", "10", 2)) * 1; - #五十发 - $goods['price_five_ten'] = (bcmul("{$goods['price']}", "50", 2)) * 1; - #是否收藏 - $collection_is = Collect::field('id') - ->where(['user_id' => $user_info['id']]) - ->where(['goods_id' => $goods_id]) - ->where(['num' => 0]) - ->find(); - $goods['collection_is'] = $collection_is ? 1 : 0; if ($goods['type'] == 7) { $goods['need_draw_num'] = 1; } @@ -96,7 +78,6 @@ class Infinite extends Base ->append(['userinfo']) ->where('goods_id', '=', $goods_id) ->where('order_type', '=', $goods['type']) - ->order('id desc') ->group('user_id') ->limit(5) @@ -160,13 +141,29 @@ class Infinite extends Base } elseif ($goods['type'] == 17) { $type1 = 8; } + $draw_num = 0; + $goodsService = new GoodsService(); + $limitInfo = $goodsService->getPurchaseLimitInfo($user_id, 0, $goods, self::$shang_prize_id); + if ($user_id > 0) { + //是否收藏 + $collection_is = Collect::field('id') + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => 0]) + ->find(); + $goods['collection_is'] = $collection_is ? 1 : 0; + $user_info = $this->getUser(); + $draw_num = $user_info['draw_num']; + $limitInfo['user_test'] = $user_info['istest']; + } $new_data = [ 'goods' => $goods, 'goodslist' => $goodslist, 'goods_list' => $goods_list, - 'draw_num' => $user_info['draw_num'], #优惠券的数量 - 'danye_id' => $type1 + 'draw_num' => $draw_num, #优惠券的数量 + 'danye_id' => $type1, + 'limitInfo' => $limitInfo ]; return $this->renderSuccess("请求成功", $new_data); } @@ -351,7 +348,7 @@ class Infinite extends Base $where[] = ['goods_id', '=', $goods_id]; $where[] = ['num', '=', 0]; $where[] = ['user_id', '=', $user_id]; - + $data = OrderList::field('user_id,goodslist_title,goodslist_imgurl,addtime') ->where($where) ->where('source', '=', 1) @@ -361,7 +358,7 @@ class Infinite extends Base $item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']); return $item; }); - + return $this->renderSuccess("请求成功", $data); } @@ -387,32 +384,32 @@ class Infinite extends Base if ($goods['status'] != 1) { return $this->renderError("盒子已下架"); } - + // 获取服务实例 $calculator = new \app\common\service\PaymentCalculator(); - + // 验证抽奖限制 $validationResult = $calculator->validateDrawRestrictions($user, $goods, $prize_num); if ($validationResult['status'] == 0) { return $this->renderError($validationResult['msg']); } - + // 计算订单金额 $result = $calculator->calculateOrderAmount( - $user, - $goods_id, - $prize_num, - $use_money_is, - $use_integral_is, - $use_money2_is, - $coupon_id, + $user, + $goods_id, + $prize_num, + $use_money_is, + $use_integral_is, + $use_money2_is, + $coupon_id, $goods ); - + if ($result['status'] == 0) { return $this->renderError($result['msg']); } - + return $this->renderSuccess("请求成功", $result); } @@ -455,7 +452,7 @@ class Infinite extends Base if (!in_array($goods['type'], [2, 8, 9, 10, 16, 17])) { return $this->renderError("非法请求"); } - + // 使用PaymentCalculator验证抽奖限制(限购、消费门槛等) $paymentCalculator = new \app\common\service\PaymentCalculator(); $validationResult = $paymentCalculator->validateDrawRestrictions($user, $goods, $prize_num); @@ -475,21 +472,21 @@ class Infinite extends Base $where[] = ['real_pro', '>', 0]; $where[] = ['shang_id', 'between', self::$shang_prize_id]; } - + #奖品信息 if ($goods['type'] == 9 && $is_mibao == 1) { //秘宝池 $where[] = ['lian_ji_type', '=', 1]; return $this->renderError('请求错误'); } - + $is_goodslist = GoodsList::field('id') ->where($where) ->find(); if (!$is_goodslist) { return $this->renderError('暂无奖品信息'); } - + if ($goods['type'] == 9) { $prize_num = intval($prize_num); if ($prize_num > 20) { @@ -508,21 +505,21 @@ class Infinite extends Base // 使用PaymentCalculator计算订单金额和支付方式 $paymentResult = $paymentCalculator->calculateOrderAmount( - $user, - $goods_id, - $prize_num, - $use_money_is, - $use_integral_is, - $use_money2_is, - $coupon_id, - $goods, + $user, + $goods_id, + $prize_num, + $use_money_is, + $use_integral_is, + $use_money2_is, + $coupon_id, + $goods, $goods_extend ); - + if ($paymentResult['status'] == 0) { return $this->renderError($paymentResult['msg']); } - + $redis = (new \app\common\server\RedisHelper())->getRedis(); $redis_key = "kpw_infinite_orderbuy" . '_' . $user['id']; $redis_key_info = $redis->get($redis_key); @@ -531,13 +528,13 @@ class Infinite extends Base } else { $redis->set($redis_key, 1, 3); } - + Db::startTrans(); $num = 0; if ($goods['type'] == 10) { $num = 1; } - + if ($goods['daily_xiangou'] > 0) { // 查看一天内有没有未支付的订单,未支付的订单要先作废 $order_info = Order::where('goods_id', '=', $goods_id) @@ -551,10 +548,10 @@ class Infinite extends Base $order_info->save(); } } - + $res = []; $order_num = create_order_no('MH_', 'order', 'order_num'); - + #创建订单 $res[] = $order_id = Order::insertGetId([ 'user_id' => $user['id'], @@ -584,7 +581,7 @@ class Infinite extends Base 'ad_id' => $ad_id, 'click_id' => $user['click_id'] ]); - + #微信支付金额大于0 if ($paymentResult['price'] > 0) { $body = '购买盒子' . $goods['title']; diff --git a/app/common/service/GoodsService.php b/app/common/service/GoodsService.php new file mode 100644 index 0000000..1a88ef0 --- /dev/null +++ b/app/common/service/GoodsService.php @@ -0,0 +1,87 @@ + $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; + } +} \ No newline at end of file diff --git a/app/common/service/PaymentCalculator.php b/app/common/service/PaymentCalculator.php index f7c0854..77fb6fd 100644 --- a/app/common/service/PaymentCalculator.php +++ b/app/common/service/PaymentCalculator.php @@ -117,7 +117,7 @@ class PaymentCalculator } else { $coupon_id = 0; } - }else{ + } else { $coupon_id = 0; } } else { @@ -323,10 +323,11 @@ class PaymentCalculator // 验证每日限购 if ($goods['daily_xiangou'] > 0) { $todayMidnight = strtotime('today'); + $goods_id = $goods['id']; $user_toDay_count = \app\common\model\OrderList::field('id') ->where('goods_id', '=', $goods['id']) ->where('user_id', '=', $user_id) - ->where('parent_goods_list_id', '=', 0) + // ->where('parent_goods_list_id', '=', 0) ->where('addtime', '>=', $todayMidnight) ->count();