From fd64700ba5ffeb6a614324ebe8c81d8282d43329 Mon Sep 17 00:00:00 2001 From: manghe Date: Thu, 3 Apr 2025 15:59:22 +0000 Subject: [PATCH] =?UTF-8?q?=E7=A6=8F=E5=88=A9=E5=B1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Goods.php | 6 +- app/api/controller/FuLiWu.php | 339 ++++++++++++++++++++++++ app/api/controller/Goods.php | 383 +++++++-------------------- app/api/controller/Notify.php | 174 ++++++------ app/api/controller/Sign.php | 24 +- app/api/route/app.php | 57 ++-- app/command/FlwOpen.php | 262 ++++++++++-------- app/common/service/CommonService.php | 42 +++ config/console.php | 1 + 9 files changed, 772 insertions(+), 516 deletions(-) create mode 100644 app/api/controller/FuLiWu.php create mode 100644 app/common/service/CommonService.php diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 2f24b8b..bf6eb1c 100755 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -778,6 +778,7 @@ class Goods extends Base } elseif ($type == 15) { $prize_code = getPrizeCode() . '_' . time(); $data['prize_code'] = $prize_code; + $data['surplus_stock'] = $data['stock']; if (RegInt($data['stock'])) { return $this->renderError('奖品数量设置错误,请设置大于0的整数'); } @@ -906,6 +907,8 @@ class Goods extends Base if (RegInt($data['prize_num']) && $data['shang_id'] == 5) { return $this->renderError("擂台赏抽全局赏数量设置错误,请设置大于0的整数"); } + } elseif ($type == 15) { + $data['surplus_stock'] = $data['stock']; } else { return $this->err('请求参数错误'); } @@ -1480,7 +1483,7 @@ class Goods extends Base $goodsData['status'] = 2; // 默认下架状态 $goodsData['addtime'] = time(); $goodsData['async_code'] = ''; // 清空同步代码,避免混淆 - + $goodsData['is_open'] = 0; // 插入新盒子 $newGoods = new GoodsModel(); $newGoods->save($goodsData); @@ -1493,6 +1496,7 @@ class Goods extends Base unset($listData['id']); $listData['goods_id'] = $newGoodsId; $listData['addtime'] = time(); + $listData['surplus_stock'] = $listData['stock']; if ($listData['reward_id'] && $listData['reward_id'] != '') { $reward = Reward::where(['reward_id' => $listData['reward_id']])->select(); $listData['reward_id'] = 'MHHZ' . date('YmdHis') . mt_rand(1000, 9999);//$this->generateUUID(); diff --git a/app/api/controller/FuLiWu.php b/app/api/controller/FuLiWu.php new file mode 100644 index 0000000..81e0aaf --- /dev/null +++ b/app/api/controller/FuLiWu.php @@ -0,0 +1,339 @@ +param('type', 1); + if ($type_str != 1 && $type_str != 3) { + return $this->renderError('参数错误'); + } + $whe = []; + $whe[] = ['status', '=', $type_str]; + $paginate = 15; + $whe[] = ['type', '=', 15]; + $whe[] = ['is_open', '=', $type_str == 1 ? 0 : 1]; + $user_id = $this->getUserId(); + if ($user_id == 0) { + //充值金额 + $whe[] = ['unlock_amount', '=', 0]; + } else { + $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price'); + $userInfo = User::where('id', '=', $user_id)->field('istest')->find(); + if ($userInfo && $userInfo['istest'] > 0) { + //推广账号,门槛计算是全部的 + $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total'); + } + $whe[] = ['unlock_amount', '<=', $order_money]; + } + #盒子 + $goods = GoodsModel::where($whe) + ->field("id,title,imgurl,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open") + ->order("sort desc,id desc")->paginate($paginate)->each(function ($itme) { + $itme['imgurl'] = imageUrl($itme['imgurl']); + $itme['flw_start_time'] = date('Y-m-d H:i:s', $itme['flw_start_time']); + $itme['flw_end_time'] = date('Y-m-d H:i:s', $itme['flw_end_time']); + $itme['open_time'] = date('Y-m-d H:i:s', $itme['open_time']); + $goodslist = GoodsList::where('goods_id', '=', $itme['id']) + ->where('num', '=', 0) + ->field('title,imgurl,stock,price,sc_money') + ->select() + ->toArray(); + $itme['goodslist'] = $goodslist; + #参与次数 + $join_count = OrderList::field('id') + ->where('goods_id', '=', $itme['id']) + ->where('order_type', '=', $itme['type']) + ->count(); + $itme['join_count'] = $join_count; + + }); + $new_data = [ + 'data' => $goods->items(), + 'last_page' => $goods->lastPage(), + ]; + return $this->renderSuccess('请求成功', $new_data); + } + + /** + * 福利屋详情 + * @param \think\Request $request + * @return \think\response\Json + */ + public function fuliwu_detail(Request $request) + { + $goods_id = request()->param('goods_id/d', 0); + $goods = GoodsModel::where(['id' => $goods_id])->find(); + if (!$goods) { + return $this->renderError("盒子不存在"); + } + if ($goods['status'] != 1 && $goods['status'] != 3) { + return $this->renderError("盒子已下架"); + } + + // 检查类型是否为福利屋 + if ($goods['type'] != 15) { + return $this->renderError("该盒子不是福利屋类型"); + } + + // 获取用户信息 + $user_id = $this->getUserId(); + // 检查用户是否可以查看该福利屋(解锁金额限制) + if ($user_id > 0) { + $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price'); + $userInfo = User::where('id', '=', $user_id)->field('istest')->find(); + if ($userInfo && $userInfo['istest'] > 0) { + // 推广账号,门槛计算是全部的 + $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total'); + } + + if ($goods['unlock_amount'] > $order_money) { + return $this->renderError("您需要充值满" . $goods['unlock_amount'] . "元才能查看此福利屋"); + } + } else if ($goods['unlock_amount'] > 0) { + return $this->renderError("您需要登录并充值满" . $goods['unlock_amount'] . "元才能查看此福利屋"); + } + + // 获取福利屋详细信息 + $goods_detail = GoodsModel::field("id,title,imgurl,imgurl_detail,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open,unlock_amount,sort") + ->where(['id' => $goods_id]) + ->find(); + + // 处理图片地址和时间格式 + $goods_detail['imgurl'] = imageUrl($goods_detail['imgurl']); + if (!empty($goods_detail['imgurl_detail'])) { + $goods_detail['imgurl_detail'] = imageUrl($goods_detail['imgurl_detail']); + } + + // 获取福利屋中的奖品列表 + $goodslist = GoodsList::where('goods_id', '=', $goods_id) + ->where('num', '=', 0) + ->field('id,title,imgurl,imgurl_detail,stock,price,sc_money,shang_id,surplus_stock,sort') + ->order('sort asc') + ->select() + ->toArray(); + + // 处理奖品列表,添加图片地址 + foreach ($goodslist as &$item) { + $item['imgurl'] = imageUrl($item['imgurl']); + $item['imgurl_detail'] = imageUrl($item['imgurl_detail']); + // 获取奖品类型信息 + $shang_info = Shang::field('title,color')->where(['id' => $item['shang_id']])->find(); + $item['shang_title'] = $shang_info ? $shang_info['title'] : ''; + $item['shang_color'] = $shang_info ? $shang_info['color'] : ''; + } + + // 统计参与人数 + $join_count = OrderList::field('id') + ->where('goods_id', '=', $goods_id) + ->where('order_type', '=', $goods_detail['type']) + ->count(); + // + $user_count = OrderList:: + where('goods_id', '=', $goods_id) + ->where('user_id', '=', $user_id) + ->where('order_type', '=', $goods_detail['type']) + ->field('id') + ->count(); + // 获取用户在该福利屋活动期间的消费情况 + $consumptionData = CommonService::getUserConsumptionByTimeRange( + $user_id, + $goods_detail['flw_start_time'], + $goods_detail['flw_end_time'] + ); + + $goods_detail['flw_start_time'] = date('Y-m-d H:i', $goods_detail['flw_start_time']); + $goods_detail['flw_end_time'] = date('Y-m-d H:i', $goods_detail['flw_end_time']); + $goods_detail['open_time'] = date('Y-m-d H:i', $goods_detail['open_time']); + + + // 组装返回数据 + $data = [ + 'goods' => $goods_detail, + 'goodslist' => $goodslist, + 'join_count' => $join_count, + 'current_time' => date('Y-m-d H:i:s'), + 'user_count' => $user_count, + 'user_consumption' => $consumptionData + ]; + + // 判断福利屋状态 + $now = time(); + $start_time = strtotime($goods_detail['flw_start_time']); + $end_time = strtotime($goods_detail['flw_end_time']); + $open_time = strtotime($goods_detail['open_time']); + + if ($now < $start_time) { + $data['status'] = 'waiting'; // 等待开始 + $data['status_text'] = '即将开始'; + } else if ($now >= $start_time && $now < $end_time) { + $data['status'] = 'ongoing'; // 进行中 + $data['status_text'] = '进行中'; + } else if ($now >= $end_time && $now < $open_time) { + $data['status'] = 'ended'; // 已结束,等待开奖 + $data['status_text'] = '已结束,等待开奖'; + } else if ($now >= $open_time) { + if ($goods_detail['is_open'] == 1) { + $data['status'] = 'opened'; // 已开奖 + $data['status_text'] = '已开奖'; + } else { + $data['status'] = 'to_open'; // 待开奖(超过开奖时间但未开奖) + $data['status_text'] = '待开奖'; + } + } + + return $this->renderSuccess('请求成功', $data); + } + + /** + * 福利屋参与人员 + * @param Request $request + * @return \think\response\Json + */ + public function fuliwu_participants(Request $request) + { + $goods_id = request()->param('goods_id/d', 0); + + $list = OrderList::where('goods_id', '=', $goods_id) + ->where('order_type', '=', 15) + ->field('user_id,addtime') + ->order('addtime desc') + ->limit(1000) + ->select(); + foreach ($list as $item) { + $user_id = $item['user_id']; + $user_info = User::where('id', '=', $user_id)->field('nickname,headimg')->find(); + unset($item['user_id']); + if ($user_info) { + $item['nickname'] = $user_info['nickname']; + $item['avatar'] = imageUrl($user_info['headimg']); + } + $item['create_time'] = date('Y-m-d H:i', $item['addtime']); + + } + $data = [ + 'list' => $list, + ]; + return $this->renderSuccess('请求成功', $data); + } + + /** + * 福利屋开奖记录 + * @param Request $request + * @return \think\response\Json + */ + public function fuliwu_records(Request $request) + { + $goods_id = request()->param('goods_id/d', 0); + + $list = OrderList::where('goods_id', '=', $goods_id) + ->where('order_type', '=', 15) + ->where('shang_id', '>', 0) + ->field('user_id,addtime,goodslist_title,shang_id') + ->order('shang_id desc,addtime desc') + ->limit(1000) + ->select(); + foreach ($list as $item) { + $user_id = $item['user_id']; + $user_info = User::where('id', '=', $user_id)->field('nickname,headimg')->find(); + unset($item['user_id']); + if ($user_info) { + $item['nickname'] = $user_info['nickname']; + $item['avatar'] = imageUrl($user_info['headimg']); + } + $item['create_time'] = date('Y-m-d H:i', $item['addtime']); + + } + $data = [ + 'list' => $list, + ]; + return $this->renderSuccess('请求成功', $data); + } + /** + * 福利屋用户参与记录 + * @param \think\Request $request + * @return \think\response\Json + */ + public function fuliwu_user_records(Request $request) + { + $user = $this->getUser(); + $user_id = $user['user_id']; + + $list = OrderList::where(' user_id', '=', $user_id) + ->where('order_type', '=', 15) + ->field('user_id,addtime,goodslist_title,shang_id,goods_id') + ->order('addtime desc') + ->limit(1000) + ->select(); + + if ($list) { + foreach ($list as $item) { + $goods_id = $item['goods_id']; + $goods = GoodsModel::where('id', '=', $goods_id)->find(); + $item['goods_title'] = $goods['title']; + $item['create_time'] = date('Y-m-d H:i', $item['addtime']); + } + } + + $data = [ + 'list' => $list, + ]; + return $this->renderSuccess('请求成功', $data); + } + /** + * 福利屋用户参与记录 + * @param \think\Request $request + * @return \think\response\Json + */ + public function fuliwu_user_winning_records(Request $request) + { + $user = $this->getUser(); + $user_id = $user['user_id']; + + $list = OrderList::where(' user_id', '=', $user_id) + ->where('order_type', '=', 15) + ->where('shang_id', '>', 0) + ->field('user_id,addtime,goodslist_title,shang_id,goods_id') + ->order('addtime desc') + ->limit(1000) + ->select(); + + if ($list) { + foreach ($list as $item) { + $goods_id = $item['goods_id']; + $goods = GoodsModel::where('id', '=', $goods_id)->find(); + $item['goods_title'] = $goods['title']; + $item['create_time'] = date('Y-m-d H:i', $item['addtime']); + } + } + + $data = [ + 'list' => $list, + ]; + return $this->renderSuccess('请求成功', $data); + } +} diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index daab79e..616c9dd 100755 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -21,7 +21,7 @@ use \think\Request; use app\common\model\CouponReceive as CouponReceiveModel; use app\common\model\UserCoupon; use app\common\model\GoodsType; - +use app\common\service\CommonService; class Goods extends Base { @@ -168,197 +168,9 @@ class Goods extends Base } - /** - * 福利屋 - * @param \think\Request $request - * @return \think\response\Json - */ - public function goods_fuliwu(Request $request) - { - $type_str = request()->param('type', 1); - if ($type_str != 1 && $type_str != 3) { - return $this->renderError('参数错误'); - } - $whe = []; - $whe[] = ['status', '=', $type_str]; - $paginate = 15; - $whe[] = ['type', '=', 15]; - $user_id = $this->getUserId(); - if ($user_id == 0) { - //充值金额 - $whe[] = ['unlock_amount', '=', 0]; - } else { - $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price'); - $userInfo = User::where('id', '=', $user_id)->field('istest')->find(); - if ($userInfo && $userInfo['istest'] > 0) { - //推广账号,门槛计算是全部的 - $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total'); - } - $whe[] = ['unlock_amount', '<=', $order_money]; - } - #盒子 - $goods = GoodsModel::where($whe) - ->field("id,title,imgurl,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open") - ->order("sort desc,id desc")->paginate($paginate)->each(function ($itme) { - $itme['imgurl'] = imageUrl($itme['imgurl']); - $itme['flw_start_time'] = date('Y-m-d H:i:s', $itme['flw_start_time']); - $itme['flw_end_time'] = date('Y-m-d H:i:s', $itme['flw_end_time']); - $itme['open_time'] = date('Y-m-d H:i:s', $itme['open_time']); - $goodslist = GoodsList::where('goods_id', '=', $itme['id']) - ->where('num', '=', 0) - ->field('title,imgurl,stock,price,sc_money') - ->select() - ->toArray(); - $itme['goodslist'] = $goodslist; - #参与次数 - $join_count = OrderList::field('id') - ->where('goods_id', '=', $itme['id']) - ->where('order_type', '=', $itme['type']) - ->count(); - $itme['join_count'] = $join_count; - - }); - $new_data = [ - 'data' => $goods->items(), - 'last_page' => $goods->lastPage(), - ]; - return $this->renderSuccess('请求成功', $new_data); - } /** - * 福利屋详情 - * @param \think\Request $request - * @return \think\response\Json - */ - public function goods_fuliwu_detail(Request $request) - { - $goods_id = request()->param('goods_id/d', 0); - $goods = GoodsModel::where(['id' => $goods_id])->find(); - if (!$goods) { - return $this->renderError("盒子不存在"); - } - if ($goods['status'] != 1 && $goods['status'] != 3) { - return $this->renderError("盒子已下架"); - } - - // 检查类型是否为福利屋 - if ($goods['type'] != 15) { - return $this->renderError("该盒子不是福利屋类型"); - } - - // 获取用户信息 - $user_id = $this->getUserId(); - // 检查用户是否可以查看该福利屋(解锁金额限制) - if ($user_id > 0) { - $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('price'); - $userInfo = User::where('id', '=', $user_id)->field('istest')->find(); - if ($userInfo && $userInfo['istest'] > 0) { - // 推广账号,门槛计算是全部的 - $order_money = Order::where('status', '=', 1)->where('user_id', '=', $user_id)->sum('order_zhe_total'); - } - - if ($goods['unlock_amount'] > $order_money) { - return $this->renderError("您需要充值满" . $goods['unlock_amount'] . "元才能查看此福利屋"); - } - } else if ($goods['unlock_amount'] > 0) { - return $this->renderError("您需要登录并充值满" . $goods['unlock_amount'] . "元才能查看此福利屋"); - } - - // 获取福利屋详细信息 - $goods_detail = GoodsModel::field("id,title,imgurl,imgurl_detail,price,type,new_is,quanju_xiangou,choujiang_xianzhi,flw_start_time,flw_end_time,open_time,goods_describe,is_open,unlock_amount,sort") - ->where(['id' => $goods_id]) - ->find(); - - // 处理图片地址和时间格式 - $goods_detail['imgurl'] = imageUrl($goods_detail['imgurl']); - if (!empty($goods_detail['imgurl_detail'])) { - $goods_detail['imgurl_detail'] = imageUrl($goods_detail['imgurl_detail']); - } - - // 获取福利屋中的奖品列表 - $goodslist = GoodsList::where('goods_id', '=', $goods_id) - ->where('num', '=', 0) - ->field('id,title,imgurl,imgurl_detail,stock,price,sc_money,shang_id,surplus_stock,sort') - ->order('sort asc') - ->select() - ->toArray(); - - // 处理奖品列表,添加图片地址 - foreach ($goodslist as &$item) { - $item['imgurl'] = imageUrl($item['imgurl']); - $item['imgurl_detail'] = imageUrl($item['imgurl_detail']); - // 获取奖品类型信息 - $shang_info = Shang::field('title,color')->where(['id' => $item['shang_id']])->find(); - $item['shang_title'] = $shang_info ? $shang_info['title'] : ''; - $item['shang_color'] = $shang_info ? $shang_info['color'] : ''; - } - - // 统计参与人数 - $join_count = OrderList::field('id') - ->where('goods_id', '=', $goods_id) - ->where('order_type', '=', $goods_detail['type']) - ->count(); - // 统计参与人数 - $user_count = OrderList:: - where('goods_id', '=', $goods_id) - ->where('user_id', '=', $user_id) - ->where('order_type', '=', $goods_detail['type']) - ->field('id') - ->count(); - //余额充值 - - // 获取用户在该福利屋活动期间的消费情况 - $consumptionData = $this->getUserConsumptionByTimeRange( - $user_id, - $goods_detail['flw_start_time'], - $goods_detail['flw_end_time'] - ); - - $goods_detail['flw_start_time'] = date('Y-m-d H:i', $goods_detail['flw_start_time']); - $goods_detail['flw_end_time'] = date('Y-m-d H:i', $goods_detail['flw_end_time']); - $goods_detail['open_time'] = date('Y-m-d H:i', $goods_detail['open_time']); - - - // 组装返回数据 - $data = [ - 'goods' => $goods_detail, - 'goodslist' => $goodslist, - 'join_count' => $join_count, - 'current_time' => date('Y-m-d H:i:s'), - 'user_status' => $user_count > 0 ? true : false, - 'user_consumption' => $consumptionData - ]; - - // 判断福利屋状态 - $now = time(); - $start_time = strtotime($goods_detail['flw_start_time']); - $end_time = strtotime($goods_detail['flw_end_time']); - $open_time = strtotime($goods_detail['open_time']); - - if ($now < $start_time) { - $data['status'] = 'waiting'; // 等待开始 - $data['status_text'] = '即将开始'; - } else if ($now >= $start_time && $now < $end_time) { - $data['status'] = 'ongoing'; // 进行中 - $data['status_text'] = '进行中'; - } else if ($now >= $end_time && $now < $open_time) { - $data['status'] = 'ended'; // 已结束,等待开奖 - $data['status_text'] = '已结束,等待开奖'; - } else if ($now >= $open_time) { - if ($goods_detail['is_open'] == 1) { - $data['status'] = 'opened'; // 已开奖 - $data['status_text'] = '已开奖'; - } else { - $data['status'] = 'to_open'; // 待开奖(超过开奖时间但未开奖) - $data['status_text'] = '待开奖'; - } - } - - return $this->renderSuccess('请求成功', $data); - } - - /** - * 商品详情 + * 商品详情 * @param $goods_id 盒子id * @param $goods_num 盒子箱号 */ @@ -717,7 +529,6 @@ class Goods extends Base if (!$goods) { return $this->renderError("盒子不存在"); } - # 获取盒子类型配置 $goodsType = \app\common\model\GoodsType::where('value', $goods['type'])->find(); if (!$goodsType) { @@ -742,33 +553,44 @@ class Goods extends Base if ($goods['quanju_xiangou'] <= $user_xiangou_count) { return $this->renderError('当前箱子限购' . $goods['quanju_xiangou'] . '次'); } + } elseif ($goods['type'] == 15 && $goods['quanju_xiangou'] > 0) { + //限购 + $user_xiangou_count = OrderList::field('id')->where('goods_id', '=', $goods_id) + ->where('user_id', '=', $user['id']) + ->count(); + //已经达到限购先上限 + if ($goods['quanju_xiangou'] <= $user_xiangou_count) { + return $this->renderError('当前活动限购' . $goods['quanju_xiangou'] . '次'); + } } - #奖品信息 - $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('shang_id', 'between', self::$shang_prize_id) - ->find(); - if (!$goodslist) { - return $this->renderError('暂无奖品信息'); - } - $surplus_stock = $goodslist['surplus_stock']; - if ($surplus_stock <= 0) { - return $this->renderError('库存剩余不足,请刷新重试'); - } - #判断库存 - if (RegInt($prize_num)) { - return $this->renderError("抽奖数量选择错误,请刷新重试"); - } - if ($prize_num > $surplus_stock) { - return $this->renderError("剩余数量不足,请刷新重试"); + if ($goods['type'] != 15) { + #奖品信息 + $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) + ->find(); + if (!$goodslist) { + return $this->renderError('暂无奖品信息'); + } + $surplus_stock = $goodslist['surplus_stock']; + if ($surplus_stock <= 0) { + return $this->renderError('库存剩余不足,请刷新重试'); + } + #判断库存 + if (RegInt($prize_num)) { + return $this->renderError("抽奖数量选择错误,请刷新重试"); + } + if ($prize_num > $surplus_stock) { + return $this->renderError("剩余数量不足,请刷新重试"); + } } #盒子单价 $box_price = $goods['price']; //是否首抽五折 $shou_zhe_price = 0; $is_shou_zhe = 0; - if ($goods['type'] != 5 && $goods['type'] != 10) { + if ($goods['type'] != 5 && $goods['type'] != 10 && $goods['type'] != 15) { $is_chou = Order::field('id')->where([['user_id', '=', $user['id']], ['status', '=', 1]])->find(); $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find(); if (!$is_chou && !$is_chou2 && $goods['is_shou_zhe'] == 1) { @@ -921,6 +743,7 @@ class Goods extends Base } + /** * 创建订单 */ @@ -940,7 +763,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')->where(['id' => $goods_id]) + $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')->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); @@ -959,24 +782,44 @@ class Goods extends Base return $this->renderError("箱号选择错误"); } $user_id = $this->getuserid(); + $choujiang_xianzhi = $goods['choujiang_xianzhi']; + if ($choujiang_xianzhi && $choujiang_xianzhi > 0) { // SELECT sum(price) FROM xinglanmh_shequt_test.`order` where user_id=4445 and status=1 - $user_price = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('price'); - if ($user_price < $choujiang_xianzhi) { - return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "元"); + if ($goods['type'] == 15) { + + // 获取用户在该福利屋活动期间的消费情况 + $consumptionData = CommonService::getUserConsumptionByTimeRange( + $user_id, + $goods['flw_start_time'], + $goods['flw_end_time'] + ); + if ($consumptionData['total_consumed'] < $choujiang_xianzhi) { + return $this->renderError(" 需在指定时间" . date('Y-m-d H:i:s', $goods['flw_start_time']) . "-" + . date('Y-m-d H:i:s', $goods['flw_end_time']) . "消耗达到" . $choujiang_xianzhi . "钻石,即可加入房间,还需" . round(($choujiang_xianzhi - $consumptionData['total_consumed']), 2) . "钻石."); + } + + } else { + $user_price = order::where('user_id', '=', $user_id)->where('status', '=', 1)->sum('price'); + if ($user_price < $choujiang_xianzhi) { + return $this->renderError("消费满" . $choujiang_xianzhi . "元可参与 已消费" . round($user_price, 2) . "元"); + } } } - #奖品信息 - $is_goodslist = GoodsList::field('id') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('shang_id', 'between', self::$shang_prize_id) - ->find(); - if (!$is_goodslist) { - return $this->renderError('暂无奖品信息'); + if ($goods['type'] != 15) { + #奖品信息 + $is_goodslist = GoodsList::field('id') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) + ->find(); + if (!$is_goodslist) { + return $this->renderError('暂无奖品信息'); + } + } else { + $num = 0; } - #锁箱信息=============== if ($goods['type'] == 1 || $goods['type'] == 6 || $goods['type'] == 11) { $goods_id_num = $goods_id . '_' . $num; @@ -1005,30 +848,31 @@ class Goods extends Base } } #擂台赏限购=============== - - #奖品信息 - $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('shang_id', 'between', self::$shang_prize_id) - ->find(); - $surplus_stock = $goodslist['surplus_stock']; - if ($surplus_stock <= 0) { - return $this->renderError('库存剩余不足,请刷新重试'); - } - #判断库存 - if (RegInt($prize_num)) { - return $this->renderError("抽奖数量选择错误,请刷新重试"); - } - if ($prize_num > $surplus_stock) { - return $this->renderError("剩余数量不足,请刷新重试"); + if ($goods['type'] != 15) { + #奖品信息 + $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) + ->find(); + $surplus_stock = $goodslist['surplus_stock']; + if ($surplus_stock <= 0) { + return $this->renderError('库存剩余不足,请刷新重试'); + } + #判断库存 + if (RegInt($prize_num)) { + return $this->renderError("抽奖数量选择错误,请刷新重试"); + } + if ($prize_num > $surplus_stock) { + return $this->renderError("剩余数量不足,请刷新重试"); + } } #盒子单价 $box_price = $goods['price']; //是否首抽五折 $shou_zhe_price = 0; $is_shou_zhe = 0; - if ($goods['type'] != 5 && $goods['type'] != 10) { + if ($goods['type'] != 5 && $goods['type'] != 10 && $goods['type'] != 15) { $is_chou = Order::field('id')->where([['user_id', '=', $user['id']], ['status', '=', 1]])->find(); $is_chou2 = Order::field('id')->where([['is_shou_zhe', '=', 1], ['status', '=', 1], ['user_id', '=', $user['id']]])->find(); if (!$is_chou && !$is_chou2 && $goods['is_shou_zhe'] == 1) { @@ -1258,16 +1102,18 @@ class Goods extends Base ->where('num', '=', $num) ->lock(true)->select(); #奖品信息 - $goodslist_lock = GoodsList::field('sum(`surplus_stock`) as surplus_stock') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('shang_id', 'between', self::$shang_prize_id) - ->find(); - #判断库存 - if ($goodslist_lock['surplus_stock'] <= 0) { - Db::rollback(); - $redis->del($redis_key); - return $this->renderError("已售空,请刷新重试"); + if ($goods['type'] != 15) { + $goodslist_lock = GoodsList::field('sum(`surplus_stock`) as surplus_stock') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) + ->find(); + #判断库存 + if ($goodslist_lock['surplus_stock'] <= 0) { + Db::rollback(); + $redis->del($redis_key); + return $this->renderError("已售空,请刷新重试"); + } } #===================================================*********** @@ -1317,6 +1163,8 @@ class Goods extends Base $attach = 'order_zzs'; } elseif ($goods['type'] == 10) { $attach = 'order_scs'; + } elseif ($goods['type'] == 15) { + $attach = 'order_flw'; } else { $attach = 'order_qts'; } @@ -1630,36 +1478,5 @@ class Goods extends Base } } - /** - * 获取用户在指定时间范围内的消费情况 - * - * @param int $userId 用户ID - * @param int $startTime 开始时间戳 - * @param int $endTime 结束时间戳 - * @return array 包含余额消费和微信支付消费的数组 - */ - public 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; - } } diff --git a/app/api/controller/Notify.php b/app/api/controller/Notify.php index 44dbc48..6816aea 100755 --- a/app/api/controller/Notify.php +++ b/app/api/controller/Notify.php @@ -480,34 +480,10 @@ class Notify extends Base ->where(['goods_id' => $goods_id]) ->where(['num' => $num]) ->where(['status' => 0]) - ->where('order_type', 'in', [1, 3, 5, 6, 11]) + ->where('order_type', 'in', [1, 3, 5, 6, 11, 15]) ->find(); if ($order) { - #套库存============================= -// $all_stock = GoodsList::field('stock')->where('goods_id', '=', $goods_id)->where('num', '=', $num) -// ->where('shang_id', 'between', self::$shang_prize_id)->sum('stock'); -// -// $redis = (new \app\common\server\RedisHelper())->getRedis(); -// $redis_key = 'kpw_' . $goods_id . '_' . $num; -// for ($r = 1; $r <= $order['prize_num']; $r++) { -// $redis->lPush($redis_key, 1); -// } -// $redis_len = $redis->lLen($redis_key); -// if ($redis_len >= $all_stock) { -// for ($l = 1; $l <= $order['prize_num']; $l++) { -// $redis->rPop($redis_key); -// } -// #卡单了 -// $kd_is = Order::field('kd_is') -// ->where(['id' => $order['id']]) -// ->update([ -// 'kd_is' => 1 -// ]); -// return $kd_is; -// } - #套库存============================= - #改变状态 $res[] = Order::field('status,pay_time') @@ -536,63 +512,64 @@ class Notify extends Base if (!empty($order['coupon_id'])) { $coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]); } - - #判断是否发积分 - #分销奖励 $res[] = User::distribution($order); #分销奖励 - #普通奖品========================================================== - $res[] = $this->ordinary_prize_notice($order); - #普通奖品========================================================== - - #判断是否发积分 发券 - $res[] = User::is_integral_coupon($order); - //升级欧气值 - if ($order['price'] > 0) { - User::ou_qi_level_up($order['user_id'], $order['price']); - } - - #特殊奖品********************************************************** - #普通奖品余量信息 - $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('shang_id', 'between', self::$shang_prize_id) - ->find(); - // var_dump($goodslist['surplus_stock']); - #普通赏销量数量 - $sale_count = $goodslist['stock'] - $goodslist['surplus_stock']; - #普通奖品一半 - $first_count = floor($goodslist['stock'] / 2); - #FIRST - if ($order['order_type'] != 3 && $sale_count >= $first_count) { - $res[] = $this->special_first_notice($order, $first_count); - } - #LAST赏 全局赏 拳王赏 - if ($goodslist['surplus_stock'] <= 0) { - // var_dump($goodslist['surplus_stock']); - - $res[] = $this->special_prize_notice($order, $first_count); - // var_dump($res); - #擂台赏 - if ($order['order_type'] == 3) { - $res[] = $this->quan_prize_notice($order); + if ($order['order_type'] == 15) { + $res[] = $this->ordinary_prize_notice_flw($order); + } else { + #普通奖品========================================================== + $res[] = $this->ordinary_prize_notice($order); + #普通奖品========================================================== + #判断是否发积分 发券 + $res[] = User::is_integral_coupon($order); + //升级欧气值 + if ($order['price'] > 0) { + User::ou_qi_level_up($order['user_id'], $order['price']); } - #增加盒子库存 - $goods = Goods::field('id,stock,sale_stock') - ->where(['id' => $goods_id]) + + + #特殊奖品********************************************************** + #普通奖品余量信息 + $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) ->find(); - $save_update = []; - $goods_sale_stock = $goods['sale_stock'] + 1; - if ($goods_sale_stock >= $goods['stock']) { - $save_update['status'] = 3; + // var_dump($goodslist['surplus_stock']); + #普通赏销量数量 + $sale_count = $goodslist['stock'] - $goodslist['surplus_stock']; + #普通奖品一半 + $first_count = floor($goodslist['stock'] / 2); + #FIRST + if ($order['order_type'] != 3 && $sale_count >= $first_count) { + $res[] = $this->special_first_notice($order, $first_count); + } + #LAST赏 全局赏 拳王赏 + if ($goodslist['surplus_stock'] <= 0) { + // var_dump($goodslist['surplus_stock']); + + $res[] = $this->special_prize_notice($order, $first_count); + // var_dump($res); + #擂台赏 + if ($order['order_type'] == 3) { + $res[] = $this->quan_prize_notice($order); + } + #增加盒子库存 + $goods = Goods::field('id,stock,sale_stock') + ->where(['id' => $goods_id]) + ->find(); + $save_update = []; + $goods_sale_stock = $goods['sale_stock'] + 1; + if ($goods_sale_stock >= $goods['stock']) { + $save_update['status'] = 3; + } + $save_update['sale_stock'] = $goods_sale_stock; + $res[] = Goods::field('id,status,sale_stock') + ->where(['id' => $goods_id]) + ->update($save_update); } - $save_update['sale_stock'] = $goods_sale_stock; - $res[] = Goods::field('id,status,sale_stock') - ->where(['id' => $goods_id]) - ->update($save_update); } #特殊奖品********************************************************** } else { @@ -796,6 +773,47 @@ class Notify extends Base return $res; } + + /** + * 福利屋购买 + * @param $order 订单信息 + */ + protected function ordinary_prize_notice_flw($order) + { + $user_id = $order['user_id']; + $order_id = $order['id']; + $goods_id = $order['goods_id']; + $order_type = $order['order_type']; + $goods_price = $order['goods_price']; + #入库奖品 + $save_order_goods = []; + #开普通奖品 + $save_order_goods[] = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => 0, + 'source' => 3, + 'shang_id' => 0, + 'goodslist_id' => 0, + 'goodslist_title' => '', + 'goodslist_imgurl' => '', + 'goodslist_price' => $goods_price, + 'goodslist_money' => 0, + 'goodslist_type' => 3, + 'goodslist_sale_time' => '', + 'addtime' => time(), + 'prize_code' => '', + 'order_type' => $order_type, + ]; + if ($save_order_goods) { + #新增奖品列表 + $res[] = OrderList::insertAll($save_order_goods); + } + return $res; + } + /** * 特殊奖品开奖 FIRST * @param $order 订单信息 @@ -1272,7 +1290,7 @@ class Notify extends Base #入库=== $res[] = OrderList::insert($save_prize_info); } - + } else { $res[] = 0; } @@ -1896,7 +1914,7 @@ class Notify extends Base $wxServer->post_order($open_id, $access_token, $order_num); } - /** + /** * 无限赏开始抽赏====================================================== * @param int $user_id 会员id * @param int $order_id 订单ID @@ -1904,7 +1922,7 @@ class Notify extends Base */ public function infinite_shangchengshang_notice($user_id = 0, $order_id = 0, $goods_id = 0, $num = 0) { - + $res = []; $order = Order::where(['id' => $order_id]) ->where(['user_id' => $user_id]) diff --git a/app/api/controller/Sign.php b/app/api/controller/Sign.php index 7c7398f..b45bf99 100644 --- a/app/api/controller/Sign.php +++ b/app/api/controller/Sign.php @@ -12,7 +12,7 @@ use app\common\service\RewardService; use think\Request; use app\common\model\Order; use app\common\server\RedisHelper; - +use app\common\service\CommonService; class Sign extends Base { /** @@ -37,24 +37,20 @@ class Sign extends Base try { $app_setting = getConfig('app_setting'); + if ($app_setting && $app_setting['sign_in_spend_limit'] && $app_setting['sign_in_spend_limit'] > 0) { //消费门槛 + $is_sign = false; //获取当天开始和结束时间 $today_start = strtotime(date('Y-m-d 00:00:00')); $today_end = strtotime(date('Y-m-d 23:59:59')); - - //查询用户今日是否达到消费门槛 - $order_count = Order::where('user_id', '=', $user_id) - ->where('status', '=', 1) // 已支付订单 - ->where(function ($query) { - $query->where('price', '>', 0) - ->whereOr('use_money', '>', 0); - }) - ->where('pay_time', '>=', $today_start) - ->where('pay_time', '<=', $today_end) - ->count(); - - if ($order_count == 0) { + $user_consumption = CommonService::getUserConsumptionByTimeRange($user_id, $today_start, $today_end); + if ($user_consumption) { + if ($user_consumption['total_consumed'] >= $app_setting['sign_in_spend_limit']) { + $is_sign = true; + } + } + if (!$is_sign) { // 删除锁后返回错误 $redis->delete($lock_key); return $this->renderError('您今日尚未达到签到消费门槛,请消费后再来签到'); diff --git a/app/api/route/app.php b/app/api/route/app.php index 02d6c18..43921a9 100755 --- a/app/api/route/app.php +++ b/app/api/route/app.php @@ -80,8 +80,7 @@ Route::any('quan_yi_ling', 'QuanYi/quan_yi_ling'); #Goods.php盒子管理 #============================ Route::any('goods', 'Goods/goods'); -Route::any('goods_fuliwu', 'Goods/goods_fuliwu'); -Route::any('goods_fuliwu_detail', 'Goods/goods_fuliwu_detail'); + Route::any('goodsdetail', 'Goods/goodsdetail'); Route::any('goodslist_count', 'Goods/goodslist_count'); @@ -96,9 +95,9 @@ Route::any('prizeorderlog', 'Goods/prizeorderlog'); Route::any('luck_draw_log', 'Goods/luck_draw_log'); //首页弹出优惠券 -Route::any('coupon','Coupon/index'); -Route::any('receive','Coupon/receive'); -Route::any('used','Coupon/used'); +Route::any('coupon', 'Coupon/index'); +Route::any('receive', 'Coupon/receive'); +Route::any('used', 'Coupon/used'); #============================ #Infinite.php 无限赏管理 @@ -138,27 +137,27 @@ Route::any('warehouse_order_logistics', 'Warehouse/warehouse_order_logistics'); #============================ Route::any('notify/order_notify', 'Notify/order_notify'); Route::any('notify/ceshi', 'Notify/ceshi'); -Route::any('notify/order_notify7','Notify/order_notify7'); -Route::any('ad_notify','Notify/ad_notify'); -Route::any('user_sign_list','User/user_sign_list'); -Route::any('user_sign','User/user_sign'); -Route::any('shop_seckill_index','Seckill/shop_seckill_index'); -Route::any('shop_seckill_product','Seckill/shop_seckill_product'); -Route::any('shop_goods_detail','Seckill/shop_goods_detail'); -Route::any('shop_goods_share','Seckill/shop_goods_share'); -Route::any('shop_goods_detail_spec','Seckill/shop_goods_detail_spec'); -Route::any('shop_goods_browse','Seckill/shop_goods_browse'); -Route::any('shop_order_buy','Seckill/shop_order_buy'); -Route::any('seckill_order_list','Seckill/seckill_order_list'); -Route::any('seckill_order_detail','Seckill/seckill_order_detail'); -Route::any('seckill_order_confirm','Seckill/seckill_order_confirm'); -Route::any('seckill_order_logistics','Seckill/seckill_order_logistics'); +Route::any('notify/order_notify7', 'Notify/order_notify7'); +Route::any('ad_notify', 'Notify/ad_notify'); +Route::any('user_sign_list', 'User/user_sign_list'); +Route::any('user_sign', 'User/user_sign'); +Route::any('shop_seckill_index', 'Seckill/shop_seckill_index'); +Route::any('shop_seckill_product', 'Seckill/shop_seckill_product'); +Route::any('shop_goods_detail', 'Seckill/shop_goods_detail'); +Route::any('shop_goods_share', 'Seckill/shop_goods_share'); +Route::any('shop_goods_detail_spec', 'Seckill/shop_goods_detail_spec'); +Route::any('shop_goods_browse', 'Seckill/shop_goods_browse'); +Route::any('shop_order_buy', 'Seckill/shop_order_buy'); +Route::any('seckill_order_list', 'Seckill/seckill_order_list'); +Route::any('seckill_order_detail', 'Seckill/seckill_order_detail'); +Route::any('seckill_order_confirm', 'Seckill/seckill_order_confirm'); +Route::any('seckill_order_logistics', 'Seckill/seckill_order_logistics'); + - //generate_urllink -Route::any('generate_urllink','Index/generate_urllink'); +Route::any('generate_urllink', 'Index/generate_urllink'); -Route::any('generate_urllinks','Index/generate_urllinks'); +Route::any('generate_urllinks', 'Index/generate_urllinks'); // @@ -181,4 +180,14 @@ Route::any('welfare_house_list', 'WelfareHouse/getList'); # 签到管理 #============================ Route::rule('sign', 'Sign/signAdd', 'POST'); -Route::rule('sign_info', 'Sign/info', 'POST'); \ No newline at end of file +Route::rule('sign_info', 'Sign/info', 'POST'); + +#============================ +# 福利屋 +#============================ +Route::any('fuliwu', 'FuLiWu/index'); +Route::any('fuliwu_detail', 'FuLiWu/fuliwu_detail'); +Route::any('fuliwu_participants', 'FuLiWu/fuliwu_participants'); +Route::any('fuliwu_records', 'FuLiWu/fuliwu_records'); +Route::any('fuliwu_user_records', 'FuLiWu/fuliwu_user_records'); +Route::any('fuliwu_user_winning_records', 'FuLiWu/fuliwu_user_winning_records'); \ No newline at end of file diff --git a/app/command/FlwOpen.php b/app/command/FlwOpen.php index d2ebe68..4946070 100755 --- a/app/command/FlwOpen.php +++ b/app/command/FlwOpen.php @@ -29,142 +29,172 @@ use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use think\facade\Db; - +use app\common\service\RewardService; class FlwOpen extends Command { - static $shang_prize_id = [10, 33];#抽奖赏品id - static $shang_give_first_id = 1;#赠送(FIRST赏)赏品id - static $shang_give_arr = [2, 3, 4];#赠送(LAST赏 最终赏 全局赏)赏品id - static $shang_give_w_id = 4;#赠送(全局赏)赏品id - static $shang_give_quan_id = 5;#赠送(拳王赏)赏品id - static $secretKey = null; - static $secretKeyAccount = null; - + + protected function configure() { - $this->setName('FlwOpen')->setDescription('福利屋开奖'); + $this->setName('FlwOpen')->setDescription('福利屋开奖'); } - + //cd /www/wwwroot/192.168.195.11 && php think FlwOpen protected function execute(Input $input, Output $output) { //php /www/wwwroot/xinglanmh.shequtuangou.vip think FlwOpen - $time = date('Y-m-d H:i:s', time()); - Log::info('福利屋开奖' . $time); - Log::info('福利屋开奖' . $time); - Log::info('福利屋开奖' . $time); - print('福利屋开奖' . $time); - print('福利屋开奖' . $time); - print_r(time()); - // $notify=new Notify(); - //cd /www/wwwroot/xinglanmh.shequtuangou.vip && php think FlwOpen - $goodslist = Goods::where('status', 1)->where('is_flw', 1)->where('is_open', 0)->select()->toArray(); - foreach ($goodslist as $k => $v) { - Log::info('福利屋开奖$goods_id' . $v['id']); - Log::info('福利屋开奖open_time' . date('Y-m-d H:i:s', $v['open_time'])); - print_r('福利屋开奖open_time' . date('Y-m-d H:i:s', $v['open_time'])); - if($v['open_time']special_prize_notice_time($v['id']); - - } - - } - - $time = date('Y-m-d H:i:s', time()); - $output->writeln('success' . $time); + Log::info('福利屋开奖' . date('Y-m-d H:i:s', time())); + echo ('福利屋开奖' . date('Y-m-d H:i:s', time())) . PHP_EOL; + + $start_time = time(); + // 当前时间-1分钟 + $end_time = time() - 60; + $goodslist = Goods::where('status', 1)->where('is_flw', 1) + ->where('is_open', 0) + ->where('open_time', '<=', $start_time) + // ->where('open_time', '>=', $end_time) + ->select() + ->toArray(); + foreach ($goodslist as $k => $v) { + Log::info('福利屋开奖$goods_id' . $v['id'] . '福利屋开奖$goods_title' . $v['title']); + echo ('福利屋开奖$goods_id' . $v['id'] . '福利屋开奖$goods_title' . $v['title']) . PHP_EOL; + + $this->special_prize_notice_time($v['id'], $v['title']); + + } + $start_time = date('Y-m-d H:i:s', time()); + $output->writeln('开奖结束' . $start_time); } - + /** * 特殊奖品开奖 LAST 最终赏 全局赏 到时间开奖修改此处 * @param $order 订单信息 * @param $first_count */ - protected function special_prize_notice_time($goods_id) + protected function special_prize_notice_time($goods_id, $goods_title) { - - $res = []; - // $goods_id = $order['goods_id']; - // $num = $order['num']; - // $order_type = $order['order_type']; - - // $goods_id=request()->param('goods_id/d', 0); - // $num = request()->param('goods_num/d', 0); #第几箱 - $num = 1; #第几箱 - $order_type =6; - - print_r('$goods_id'.$goods_id); - #特殊奖品存在 - $special_prize = GoodsList::where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('surplus_stock', '>', 0) - ->where('shang_id', 'in', self::$shang_give_arr) - ->select()->toArray(); - $surplus_give_stock = $special_prize ? array_sum(array_column($special_prize, 'surplus_stock')) : 0; - if ($special_prize && $surplus_give_stock > 0) { - #所有奖品信息 - $all_order_list = OrderList::field('id,user_id') - ->where('goods_id', '=', $goods_id) - ->where('num', '=', $num) - ->where('order_type', '=', $order_type) - ->where('shang_id', 'between', self::$shang_prize_id) - ->order('id asc') - ->select()->toArray(); - $order_goods = []; - foreach ($special_prize as $k => $v) { - if ($v['shang_id'] == 4) {#全局赏 - for ($surplus_stock_i = 0; $surplus_stock_i < $v['surplus_stock']; $surplus_stock_i++) { - $overall_order_list = $all_order_list; - shuffle($overall_order_list); - shuffle($overall_order_list); - $prize_info = $overall_order_list[0]; - $user_id = $prize_info['user_id']; - #特殊赏中奖订单id - $order_list_id = $prize_info['id']; - #中奖奖项 - $ordinary_prize_info = $v; - $order_goods[] = [ - 'order_id' => 0, - 'user_id' => $user_id, - 'status' => 0,#0未操作 1选择兑换 2选择发货 - 'goods_id' => $goods_id, - 'num' => $num, - 'shang_id' => $ordinary_prize_info['shang_id'], - 'goodslist_id' => $ordinary_prize_info['id'], - 'goodslist_title' => $ordinary_prize_info['title'], - 'goodslist_imgurl' => $ordinary_prize_info['imgurl'], - 'goodslist_price' => $ordinary_prize_info['price'], - 'goodslist_money' => $ordinary_prize_info['money'], - 'goodslist_type' => $ordinary_prize_info['goods_type'], - 'goodslist_sale_time' => $ordinary_prize_info['sale_time'], - 'addtime' => time(), - 'prize_code' => $ordinary_prize_info['prize_code'], - 'order_type' => $order_type, - 'order_list_id' => $order_list_id, - ]; - - - print_r($user_id.'中奖'.$goods_id.'商品'.$v['title']); - - #减少库存 - $res[] = GoodsList::field('surplus_stock') - ->where(['id' => $ordinary_prize_info['id']]) - ->dec('surplus_stock') - ->update(); - } + $res = []; + + $num = 0; #第几箱 + + + print_r('$goods_id' . $goods_id); + //获取所有的奖品 + $goods_list = GoodsList::where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->select() + ->toArray(); + + $goods_list_all = []; + // + foreach ($goods_list as $k => $v) { + $stock = $v['stock']; + if ($stock > 0) { + for ($i = 0; $i < $stock; $i++) { + $goods_list_all[] = $v; } } - #新增奖品列表 - $res[] = OrderList::insertAll($order_goods); - Goods::where(['id' => $goods_id])->update([ - 'is_open' => 1 - ]); - Log::info('福利屋开奖$goods_idupdate_is_open' . $goods_id); - } else { - $res[] = 1; } + //打散全部奖品 + shuffle($goods_list_all); + shuffle($goods_list_all); + //抽奖人数 + $all_order_list = OrderList::where('goods_id', '=', $goods_id) + ->select() + ->toArray(); + // 如果没有参与用户,直接返回 + if (empty($all_order_list)) { + Goods::where(['id' => $goods_id])->update(['is_open' => 1]); + return $res; + } + + // 打散参与用户 + shuffle($all_order_list); + shuffle($all_order_list); + $prize_index = 0; + $prize_count = count($goods_list_all); + + // 遍历所有参与用户 + foreach ($all_order_list as $user) { + $order_info = OrderList::find($user['id']); + // 如果还有奖品可抽 + try { + // 开启事务 + Db::startTrans(); + + if ($prize_index < $prize_count) { + $prize = $goods_list_all[$prize_index]; + + // 检查库存 + $current_stock = GoodsList::where(['id' => $prize['id']])->value('surplus_stock'); + if ($current_stock <= 0) { + throw new \Exception('奖品库存不足'); + } + + // 更新订单信息 + $order_info['goodslist_id'] = $prize['id']; + $order_info['goodslist_title'] = $prize['title']; + $order_info['goodslist_imgurl'] = $prize['imgurl']; + $order_info['goodslist_money'] = $prize['money']; + $order_info['goodslist_type'] = $prize['goods_type']; + $order_info['shang_id'] = $prize['shang_id']; + $order_info['prize_code'] = $prize['reward_id']; + + // 减少库存 + $update_stock = GoodsList::where(['id' => $prize['id']]) + ->where('surplus_stock', '>', 0) + ->dec('surplus_stock') + ->update(); + + if (!$update_stock) { + throw new \Exception('更新库存失败'); + } + + // 发放奖品 + RewardService::sendReward($user['user_id'], $prize['reward_id'], $goods_title . '开奖'); + echo ('发放奖品成功 goods_id: ' . $goods_id . ', user_id: ' . $user['user_id'] . ', reward_id: ' . $prize['reward_id']) . PHP_EOL; + $prize_index++; + } else { + // 轮空处理 + $order_info['goodslist_id'] = 0; + $order_info['goodslist_title'] = '轮空'; + $order_info['goodslist_money'] = 0; + $order_info['shang_id'] = 0; + $order_info['prize_code'] = ''; + } + + // 保存订单信息 + if (!$order_info->save()) { + throw new \Exception('保存订单信息失败'); + } + + // 提交事务 + Db::commit(); + + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + echo ('福利屋开奖失败 goods_id: ' . $goods_id . ', user_id: ' . $user['user_id'] . ', error: ' . $e->getMessage()) . PHP_EOL; + // 继续处理下一个用户 + continue; + } + } + + // 检查是否所有用户都已完成开奖 + $unprocessed_count = OrderList::where('goods_id', $goods_id) + ->where('goodslist_id', 0) + ->where('goodslist_title', '') + ->count(); + Goods::where(['id' => $goods_id])->update(['is_open' => 1, 'status' => 3]); + if ($unprocessed_count == 0) { + // 更新福利屋状态为已开奖 + + echo ('福利屋开奖完成 goods_id: ' . $goods_id) . PHP_EOL; + } else { + echo ('福利屋开奖未完成 goods_id: ' . $goods_id . ', 未处理用户数: ' . $unprocessed_count) . PHP_EOL; + } + return $res; } diff --git a/app/common/service/CommonService.php b/app/common/service/CommonService.php new file mode 100644 index 0000000..532fee3 --- /dev/null +++ b/app/common/service/CommonService.php @@ -0,0 +1,42 @@ +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; + } +} \ No newline at end of file diff --git a/config/console.php b/config/console.php index cb7d6f1..51178f7 100755 --- a/config/console.php +++ b/config/console.php @@ -7,5 +7,6 @@ return [ 'UserStatisticsDay' => 'app\command\UserStatisticsDay',#自动发货 'AutoGoodsOffshelf' => 'app\command\AutoGoodsOffshelf',#自动下架利润率过低的盒子 'CreateOffshelfLogTable' => 'app\command\CreateOffshelfLogTable',#创建盒子自动下架日志表 + 'FlwOpen' => 'app\command\FlwOpen',#福利屋开奖 ], ];