'上半场随机获得', // 2 => '下半场随机获得', // 3 => '最后一发获得', // 4 => '全场随机获得', // 5 => '获取指定数量全局赏获得', // ];#赠送(FIRST赏 LAST赏 最终赏 全局赏 拳王赏)赏品id static $shang_give_arr = [ 1 => '只赠不售', 2 => '只赠不售', 3 => '只赠不售', 4 => '只赠不售', 5 => '只赠不售', ];#赠送(FIRST赏 LAST赏 最终赏 全局赏 拳王赏)赏品id /** * 首页盒子列表 * @param Request $request * @param [type] 类型 (14)推荐 (1)一番赏 (2)积分赏 (3)擂台赏 * @return \think\response\Json * @throws \think\db\exception\DbException * created by Admin at 2022/12/7 15:49 */ public function goods(Request $request) { $whe = []; $whe[] = ['status', '=', 1]; $whe[] = ['show_is', '=', 0]; $paginate = 15; $type_str = request()->param('type', -1); // 1一番赏 2无限赏 3擂台赏 4抽卡机 5积分赏 6全局赏 7福利盲盒 8领主赏 9连击赏 10 商品赏 if ($type_str == 1) { $whe[] = ['type', '=', 1]; } elseif ($type_str == 2) { $whe[] = ['type', '=', 2]; } elseif ($type_str == 3) { $whe[] = ['type', '=', 3]; } elseif ($type_str == 5) { $whe[] = ['type', '=', 5]; } elseif ($type_str == 6) { $whe[] = ['type', '=', 6]; } elseif ($type_str == 7) { $whe[] = ['type', '=', 7]; } elseif ($type_str == 8) { $whe[] = ['type', '=', 8]; } elseif ($type_str == 9) { $whe[] = ['type', 'in', [9]]; } elseif ($type_str == 10) { $paginate = 999; $whe[] = ['type', '=', 10]; } elseif ($type_str == 11) { $whe[] = ['type', '=', 11]; } elseif ($type_str == 12) { $whe[] = ['type', '=', 12]; } elseif ($type_str == 15) { $whe[] = ['type', '=', 15]; } else { $whe[] = ['type', 'not in', [4, 10, 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,stock,sale_stock,status,lock_is,is_shou_zhe,new_is") ->order("sort desc,id desc")->paginate($paginate)->each(function ($itme) { $itme['imgurl'] = imageUrl($itme['imgurl']); #剩余 $itme['sale_stock'] = $itme['stock'] - $itme['sale_stock']; if ($itme['type'] == 10) { $goods_id = $itme['id']; #本箱子余量 $goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') ->where('goods_id', '=', $goods_id) ->where('num', '=', 1) ->where('shang_id', 'between', self::$shang_prize_id) ->find(); $stock1 = intval($goodslist['stock']); $surplus_stock1 = intval($goodslist['surplus_stock']); //库存-剩余库存 // $surplus_stock =$stock1 - $surplus_stock1 ; $itme['sale_stock'] = $surplus_stock1; $itme['stock'] = $stock1; } #参与次数 $join_count = OrderList::field('id') ->where('shang_id', 'between', self::$shang_count_id) ->where('goods_id', '=', $itme['id']) ->where('order_type', '=', $itme['type']) ->count(); $itme['join_count'] = $join_count; $itme['need_draw_num'] = 0; if ($itme['type'] == 7) { $itme['need_draw_num'] = 1; } $type_text = ''; if ($itme['type'] == 1) { $type_text = '一番赏'; } elseif ($itme['type'] == 2) { $type_text = '无限赏'; } elseif ($itme['type'] == 3) { $type_text = '擂台赏'; } elseif ($itme['type'] == 5) { $type_text = '积分赏'; } elseif ($itme['type'] == 6) { $type_text = '全局赏'; } elseif ($itme['type'] == 8) { $type_text = '领主赏'; } elseif ($itme['type'] == 9) { $type_text = '连击赏'; } elseif ($itme['type'] == 10) { $type_text = '商品赏'; } elseif ($itme['type'] == 11) { $type_text = '自制赏'; } // elseif ($itme['type'] == 9) { // $type_text = '冲冲赏'; // } $itme['type_text'] = $type_text; }); $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(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 盒子箱号 */ public function goodsdetail() { $user_info = $this->getUser(); $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') ->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } if ($goods['status'] != 1 && $goods['status'] != 3) { return $this->renderError("盒子已下架"); } if (($goods_num > $goods['stock']) || ($goods_num < 0)) { return $this->renderError("箱号错误"); } if ($goods['sale_time']) { $goods['addtime'] = date('m-d', $goods['sale_time']); } else { $goods['addtime'] = ''; } $goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']); #自动找箱号 if ($goods_num == 0) { #奖品信息 $goodslist_info = GoodsList::field('num,sum(`surplus_stock`) as all_surplus_stock') ->where(['goods_id' => $goods_id]) ->where('shang_id', 'not in', [1, 2, 3, 4, 5]) ->having('all_surplus_stock', '>', 0) ->group('num') ->order('num asc') ->find(); if ($goodslist_info) { $goods_num = $goodslist_info['num']; } else { $goods_num = 1; } } $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; #本箱子余量 $goods_surplus = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') ->where('goods_id', '=', $goods_id) ->where('num', '=', $goods_num) ->where('shang_id', 'between', self::$shang_prize_id) ->find(); $all_surplus_stock = $goods_surplus['surplus_stock']; $goods['surplus_stock'] = $goods['stock'] - $goods['sale_stock']; $goods['goodslist_stock'] = $goods_surplus['stock']; $goods['goodslist_surplus_stock'] = $goods_surplus['surplus_stock']; #概率不足时 $pro_all = 0; $pro_max = 0; $pro_key = 0; #所有奖品信息 $goodslist = GoodsList::field('id,shang_id,title,stock,surplus_stock,imgurl,goods_type,sale_time,price,sc_money') ->append(['shang_info']) ->where(['goods_id' => $goods_id]) ->where(['num' => $goods_num]) ->order('sort desc,shang_id asc,id asc') ->select()->toArray(); foreach ($goodslist as $key => &$value) { #价格 $value['price'] = $value['price'] * 1; #预售时间 if ($value['sale_time']) { $value['sale_time'] = date('Y-m-d', $value['sale_time']); } #剩余 $surplus_stock = $value['surplus_stock']; if (array_key_exists($value['shang_id'], self::$shang_give_arr)) { #概率 $pro = self::$shang_give_arr[$value['shang_id']]; $pro_num = 0; } else { #概率 if ($surplus_stock > 0) { $pro_basics = bcdiv("$surplus_stock", "$all_surplus_stock", 4); $pro = '概率:' . ($pro_basics * 100) . '%'; $pro_num = ($pro_basics * 100); } else { $pro = '概率:' . 0 . '%'; $pro_num = 0; } } $value['surplus_stock'] = $surplus_stock; $value['pro'] = $pro; $value['imgurl'] = imageUrl($value['imgurl']); #计算剩余概率 $pro_all += $pro_num; if ($pro_num >= $pro_max) { $pro_max = $pro_num; $pro_key = $key; } } #概率不足时 if ($pro_max > 0 && $pro_all < 100) { $surplus_pro = bcsub("100", "$pro_all", 2); $goodslist[$pro_key]['pro'] = bcadd("$pro_max", "$surplus_pro", 2) . '%'; } #锁箱信息=============== $goods_lock_user_nickname = 0; $goods_lock_user_headimg = 0; $goods_lock_surplus_time = 0; $goods_id_num = $goods_id . '_' . $goods_num; $goods_lock_info = GoodsLock::where(['goods_id_num' => $goods_id_num]) ->where('endtime', '>', time()) ->order('id desc') ->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']); } $lock_info = [ 'lock_is' => $goods['lock_is'], 'goods_lock_user_nickname' => $goods_lock_user_nickname, 'goods_lock_user_headimg' => $goods_lock_user_headimg, 'goods_lock_surplus_time' => $goods_lock_surplus_time, ]; #锁箱信息=============== #参与人数 $join_user = OrderList::field('user_id') ->append(['userinfo']) ->where('goods_id', '=', $goods_id) ->where('num', '=', $goods_num) ->where('shang_id', 'between', self::$shang_count_id) ->where('order_type', '=', $goods['type']) ->order('id desc') ->group('user_id') ->limit(5) ->select(); $new_join_user = []; foreach ($join_user as $join_user_value) { $new_join_user[] = $join_user_value['userinfo']['headimg']; } #参与次数 $join_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']) ->count(); #时间配置 $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, ]; } $new_data = [ 'goods' => $goods, 'lock_info' => $lock_info, 'join_user' => $new_join_user, 'join_count' => $join_count, 'goodslist' => $goodslist, 'xuangou' => $xiangou ]; return $this->renderSuccess("请求成功", $new_data); } /** * 换箱箱号 */ public function goodslist_count() { $goods_id = request()->param('goods_id/d', 0); #盒子信息 $goods = Goodsmodel::field('id,stock,status') ->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } if ($goods['status'] != 1 && $goods['status'] != 3) { return $this->renderError("盒子已下架"); } $page_size = self::$page_size; $length = ceil($goods['stock'] / $page_size); $data = []; for ($i = 0; $i < $length; $i++) { $start_length = ($i * $page_size) + 1; $end_length = ($i * $page_size) + $page_size; if ($end_length >= $goods['stock']) { $end_length = $goods['stock']; } $data[] = [ 'title' => $start_length . '-' . $end_length, 'page_no' => $i, ]; } return $this->renderSuccess("请求成功", $data); } /** * 统计多少箱 */ public function goodslist_content(Request $request) { $sort = request()->param('sort/d', 0);#排序1箱号高 2余量高 $goods_id = request()->param('goods_id/d', 0); $page_no = request()->param('page_no/d', 0);#页码 if ($page_no <= 0) { $page_no = 0; } #盒子信息 $goods = Goodsmodel::field('id,stock,status') ->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } if ($goods['status'] != 1 && $goods['status'] != 3) { return $this->renderError("盒子已下架"); } $page_size = self::$page_size; $start_length = ($page_no * $page_size) + 1; $end_length = ($page_no * $page_size) + $page_size; if ($end_length >= $goods['stock']) { $end_length = $goods['stock']; } $data = []; for ($goods_num = $start_length; $goods_num <= $end_length; $goods_num++) { #奖品信息 $goodlist = GoodsList::field('id,stock,surplus_stock,shang_id') ->append(['shang_info']) ->where(['goods_id' => $goods_id]) ->where(['num' => $goods_num]) ->order('sort desc,shang_id asc,id asc') ->select(); #剩余 $surplus_all_stock = 0; foreach ($goodlist as $key => $value) { if (!array_key_exists($value['shang_id'], self::$shang_give_arr)) { $surplus_all_stock += $value['surplus_stock']; } unset($value['id']); } if ($surplus_all_stock <= 0) { $surplus_all_stock = 0; } $data[] = [ 'num' => $goods_num, 'surplus_all_stock' => $surplus_all_stock, 'goodslist' => $goodlist, ]; } if ($sort == 2) { $data = arraySequence($data, 'surplus_all_stock'); } return $this->renderSuccess("请求成功", $data); } /** * 中赏记录 */ public function shang_log(Request $request) { $shang_id = request()->param('shang_id/d', 0); $goods_id = request()->param('goods_id/d', 0); $goods_num = request()->param('goods_num/d', 0); #盒子信息 $goods = Goodsmodel::field('id,stock,status,type') ->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } if ($goods['status'] != 1 && $goods['status'] != 3) { return $this->renderError("盒子已下架"); } if (RegInt($goods_num)) { return $this->renderError("箱号选择错误"); } #中奖记录分类 $category = GoodsList::field('shang_id') ->append(['shang_title']) ->where('goods_id', '=', $goods_id) ->where('num', '=', $goods_num) ->group('shang_id') ->select()->toArray(); array_unshift($category, ['shang_id' => 0, 'shang_title' => '全部']); $where = []; if ($shang_id) { $where[] = ['shang_id', '=', $shang_id]; } $data = OrderList::field('user_id,goodslist_title,goodslist_imgurl,shang_id,addtime,count(`id`) as prize_num') ->append(['shang_title', 'user_info', 'shang_color']) ->where('goods_id', '=', $goods_id) ->where('num', '=', $goods_num) ->where('order_type', '=', $goods['type']) ->where('source', '=', 1) ->where($where) ->order('id desc') ->group("order_id,goodslist_id,user_id") ->paginate(100)->each(function ($item) { $item['user_info']['headimg'] = imageUrl($item['user_info']['headimg']); $item['addtime'] = date('Y-m-d H:i:s', $item['addtime']); $item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']); return $item; }); // dd($data); $new_data = [ 'category' => $category, 'data' => $data->items(), 'last_page' => $data->lastPage(), ]; return $this->renderSuccess("请求成功", $new_data); } /** * 下单计算金额 */ public function ordermoney() { $user = $this->getUser(); $prize_num = request()->param('prize_num/d', 0); #抽几发 $goods_id = request()->param('goods_id/d', 0); #盒子ID $num = request()->param('goods_num/d', 0); #第几箱 $use_money_is = request()->param('use_money_is/d', 0); #0不抵扣 1抵扣 $use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣 $use_money2_is = request()->param('use_money2_is/d', 0); #0不抵扣 1抵扣 货币2抵扣 $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]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } # 获取盒子类型配置 $goodsType = \app\common\model\GoodsType::where('value', $goods['type'])->find(); if (!$goodsType) { return $this->renderError("盒子类型配置不存在"); } if ($goods['status'] != 1) { return $this->renderError("盒子已下架"); } $goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']); if (RegInt($num)) { return $this->renderError("箱号选择错误"); } if ($goods['type'] == 6 && $goods['quanju_xiangou'] > 0) { //限购 $user_xiangou_count = OrderList::field('id')->where('goods_id', '=', $goods_id) ->where('num', '=', $num) ->where('shang_id', 'between', self::$shang_count_id) ->where('order_type', '=', $goods['type']) ->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("剩余数量不足,请刷新重试"); } #盒子单价 $box_price = $goods['price']; //是否首抽五折 $shou_zhe_price = 0; $is_shou_zhe = 0; if ($goods['type'] != 5 && $goods['type'] != 10) { $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) { $shou_zhe_price = bcmul("$box_price", "0.5", 2); } } $goods['shou_zhe_price'] = $shou_zhe_price; $price = bcmul("$box_price", "$prize_num", 2); $price = bcsub("$price", "$shou_zhe_price", 2); #订单金额 微信支付金额 $order_total = $order_zhe_total = $price; #使用优惠券 $coupon_price = 0; # 判断是否可使用优惠券 if ($shou_zhe_price <= 0 && !empty($coupon_id) && $goodsType['pay_coupon'] == 1) { # 获取优惠券信息 $coupon = CouponReceiveModel::where([ 'id' => $coupon_id, 'status' => 0, 'user_id' => $user['id'] ])->where('man_price', '<=', $price) ->where('end_time', '>', time()) # 确保优惠券未过期 ->find(); if ($coupon) { $coupon_price = $coupon['price']; } else { $coupon_id = 0; } } else { $coupon_id = 0; } $price = bcsub("$price", "$coupon_price", 2); if ($price <= 0) { $price = 0; } $order_zhe_total = $price; # 初始化变量 $use_money = 0; # 余额抵扣 $use_integral = 0; # 货币1抵扣 $use_money2 = 0; # 货币2抵扣 $zhe = 0; # 会员折扣 if ($shou_zhe_price <= 0) { $iszhifu = 0; # 余额抵扣 if ($use_money_is == 1 && $goodsType['pay_balance'] == 1) { if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if ($user['money'] >= $price) { $use_money = $price; $price = 0; } else { $use_money = $user['money']; $price = bcsub("$price", "$use_money", 2); } } else { # 支付模式 if ($user['money'] >= $price) { $use_money = $price; $price = 0; $iszhifu++; } else { # 支付模式下余额不足无法抵扣 $use_money = 0; return $this->renderError('金额不足'); } } } # 货币1抵扣 if ($use_integral_is == 1 && $goodsType['pay_currency'] == 1) { $price_in_currency = $price * 100; # 1:100比例 if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if ($user['integral'] >= $price_in_currency) { $use_integral = $price_in_currency; $price = 0; } else { $use_integral = $user['integral']; $price = bcsub("$price", bcdiv("$use_integral", "100", 2), 2); } } else { # 支付模式 if ($user['integral'] >= $price_in_currency) { $use_integral = $price_in_currency; $price = 0; $iszhifu++; } else { # 支付模式下货币不足无法抵扣 $use_integral = 0; return $this->renderError('金额不足'); } } } # 货币2抵扣 if ($use_money2_is == 1 && $goodsType['pay_currency2'] == 1) { $price_in_currency2 = $price * 100; # 1:100比例 if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if (isset($user['money2']) && $user['money2'] >= $price_in_currency2) { $use_money2 = $price_in_currency2; $price = 0; } else if (isset($user['money2'])) { $use_money2 = $user['money2']; $price = bcsub("$price", bcdiv("$use_money2", "100", 2), 2); } } else { # 支付模式 if (isset($user['money2']) && $user['money2'] >= $price_in_currency2) { $use_money2 = $price_in_currency2; $price = 0; $iszhifu++; } else { # 支付模式下货币2不足无法抵扣 $use_money2 = 0; return $this->renderError('金额不足'); } } } if ($goodsType['is_deduction'] == 0 && $iszhifu == 0 && $goodsType['pay_wechat'] == 0) { return $this->renderError('请选择支付方式'); } } #抽奖数量 $goods['prize_num'] = $prize_num; $data = [ 'goods' => $goods, 'order_total' => round(floatval($order_total), 2), 'order_zhe_total' => round($order_zhe_total, 2), 'zhe' => round($zhe, 2), 'price' => round($price, 2), 'integral' => round($user['integral'], 2), 'use_integral' => round($use_integral, 2), 'use_integral_money' => round(round($use_integral, 2) / 100, 2), 'money' => round($user['money'], 2), 'use_money' => round($use_money, 2), 'score' => $user['money2'], 'use_score' => $use_money2, 'coupon_price' => round($coupon_price, 2), 'coupon_id' => $coupon_id ]; return $this->renderSuccess("请求成功", $data); } /** * 创建订单 */ public function orderbuy() { $ad_id = request()->header('adid'); $user = $this->getUser(); if (empty($user['mobile'])) { return $this->renderError('请先绑定手机号', [], -9); } $prize_num = request()->param('prize_num/d', 0); #抽几发 $goods_id = request()->param('goods_id/d', 0); #盒子ID $num = request()->param('goods_num/d', 0); #第几箱 $use_money_is = request()->param('use_money_is/d', 0); #0不抵扣 1抵扣 余额抵扣 $use_integral_is = request()->param('use_integral_is/d', 0); #0不抵扣 1抵扣 货币1抵扣 $use_money2_is = request()->param('use_money2_is/d', 0); #0不抵扣 1抵扣 货币2抵扣 $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]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } # 获取盒子类型配置 $goodsType = \app\common\model\GoodsType::where('value', $goods['type'])->find(); if (!$goodsType) { return $this->renderError("盒子类型配置不存在"); } if ($goods['status'] != 1) { return $this->renderError("盒子已下架"); } $goods['imgurl_detail'] = imageUrl($goods['imgurl_detail']); if (RegInt($num)) { 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) . "元"); } } #奖品信息 $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'] == 1 || $goods['type'] == 6 || $goods['type'] == 11) { $goods_id_num = $goods_id . '_' . $num; $lock_info = GoodsLock::where(['goods_id_num' => $goods_id_num]) ->where('endtime', '>', time()) ->order('id desc') ->find(); if ($lock_info && $lock_info['endtime'] > time() && $lock_info['user_id'] !== $user['id']) { $surplus_time = $lock_info['endtime'] - time(); return $this->renderError('有会员正在锁箱,请等待' . $surplus_time . '秒'); } } #锁箱信息=============== #擂台赏限购=============== if ($goods['type'] == 3) { $user_order_list = OrderList::field('id') ->where('goods_id', '=', $goods_id) ->where('user_id', $user['id']) ->where('num', '=', $num) ->where('order_type', '=', $goods['type']) ->where('shang_id', 'between', self::$shang_prize_id) ->find(); if ($user_order_list) { return $this->renderError('限购一发'); } } #擂台赏限购=============== #奖品信息 $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) { $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) { $shou_zhe_price = bcmul("$box_price", "0.5", 2); $is_shou_zhe = 1; } } $goods['shou_zhe_price'] = $shou_zhe_price; $price = bcmul("$box_price", "$prize_num", 2); $price = bcsub("$price", "$shou_zhe_price", 2); #订单金额 微信支付金额 $order_total = $order_zhe_total = $price; #使用优惠券 $coupon_price = 0; # 判断是否可使用优惠券 if ($shou_zhe_price <= 0 && !empty($coupon_id) && $goodsType['pay_coupon'] == 1) { # 获取优惠券信息 $coupon = CouponReceiveModel::where([ 'id' => $coupon_id, 'status' => 0, 'user_id' => $user['id'] ])->where('man_price', '<=', $price) ->where('end_time', '>', time()) # 确保优惠券未过期 ->find(); if ($coupon) { $coupon_price = $coupon['price']; } else { $coupon_id = 0; } } else { $coupon_id = 0; } $price = bcsub("$price", "$coupon_price", 2); if ($price <= 0) { $price = 0; } $order_zhe_total = $price; # 初始化变量 $use_integral = 0; # 货币1抵扣 $use_money = 0; # 余额抵扣 $use_money2 = 0; # 货币2抵扣 $use_score = 0; # 积分抵扣 $zhe = 0; # 会员折扣 # 对于非积分赏类型,应用会员折扣 if ($shou_zhe_price <= 0) { $iszhifu = 0; # 余额抵扣 if ($use_money_is == 1 && $goodsType['pay_balance'] == 1) { if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if ($user['money'] >= $price) { $use_money = $price; $price = 0; } else { $use_money = $user['money']; $price = bcsub("$price", "$use_money", 2); } } else { # 支付模式 if ($user['money'] >= $price) { $use_money = $price; $price = 0; $iszhifu++; } else { # 支付模式下余额不足无法抵扣 $use_money = 0; return $this->renderError('金额不足'); } } } # 货币1抵扣 if ($use_integral_is == 1 && $goodsType['pay_currency'] == 1) { $price_in_currency = $price * 100; # 1:100比例 if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if ($user['integral'] >= $price_in_currency) { $use_integral = $price_in_currency; $price = 0; } else { $use_integral = $user['integral']; $price = bcsub("$price", bcdiv("$use_integral", "100", 2), 2); } } else { # 支付模式 if ($user['integral'] >= $price_in_currency) { $use_integral = $price_in_currency; $price = 0; $iszhifu++; } else { # 支付模式下货币不足无法抵扣 $use_integral = 0; return $this->renderError('金额不足'); } } } # 货币2抵扣 if ($use_money2_is == 1 && $goodsType['pay_currency2'] == 1) { $price_in_currency2 = $price * 100; # 1:100比例 if ($goodsType['is_deduction'] == 1) { # 抵扣模式 if (isset($user['money2']) && $user['money2'] >= $price_in_currency2) { $use_money2 = $price_in_currency2; $price = 0; } else if (isset($user['money2'])) { $use_money2 = $user['money2']; $price = bcsub("$price", bcdiv("$use_money2", "100", 2), 2); } } else { # 支付模式 if (isset($user['money2']) && $user['money2'] >= $price_in_currency2) { $use_money2 = $price_in_currency2; $price = 0; $iszhifu++; } else { # 支付模式下货币2不足无法抵扣 $use_money2 = 0; return $this->renderError('金额不足'); } } } if ($goodsType['is_deduction'] == 0 && $iszhifu == 0 && $goodsType['pay_wechat'] == 0) { return $this->renderError('请选择支付方式'); } } #一番赏锁箱 if ($goods['type'] == 1 || $goods['type'] == 6 || $goods['type'] == 11) { #盒子id_箱号 $goods_id_num = $goods_id . '_' . $num; #盒子是否配置锁箱 if ($goods['lock_is'] == 1 && $goods['lock_time'] > 0) { $redis = (new \app\common\server\RedisHelper())->getRedis(); $redis_key = "kpw_lock" . '_' . $goods_id_num; $redis_key_info = $redis->get($redis_key); if ($redis_key_info) { return $this->renderError("有会员正在锁箱,请等待"); } else { $redis->set($redis_key, 1, 1); } #默认锁箱时间 $defaulttime = (time() + $goods['lock_time']); #锁箱信息 $lock_info = GoodsLock::where('goods_id_num', '=', $goods_id_num) ->where('endtime', '>', time()) ->order('id desc') ->find(); if ($lock_info) {#存在锁箱 if ($prize_num == 3 || $prize_num == 5) { $config_time = getConfig('base'); if ($prize_num == 3) { $endtime = $defaulttime + $config_time['three_time']; } elseif ($prize_num == 5) { $endtime = $defaulttime + $config_time['five_time']; } GoodsLock::where('id', '=', $lock_info['id']) ->update([ 'endtime' => $endtime, 'update_time' => time(), ]); } else { GoodsLock::where('id', '=', $lock_info['id']) ->update([ 'endtime' => $defaulttime, 'update_time' => time(), ]); } } else { if ($prize_num == 3 || $prize_num == 5) { $config_time = getConfig('base'); if ($prize_num == 3) { $endtime = $defaulttime + $config_time['three_time']; } elseif ($prize_num == 5) { $endtime = $defaulttime + $config_time['five_time']; } #新增锁箱信息 GoodsLock::insert([ 'user_id' => $user['id'], 'goods_id_num' => $goods_id_num, 'endtime' => $endtime, 'update_time' => time(), ]); } else { #新增锁箱信息 GoodsLock::insert([ 'user_id' => $user['id'], 'goods_id_num' => $goods_id_num, 'endtime' => $defaulttime, 'update_time' => time(), ]); } } } } #一番赏锁箱 $redis = (new \app\common\server\RedisHelper())->getRedis(); $redis_key = "kpw_orderbuy" . '_' . $user['id']; $redis_key_info = $redis->get($redis_key); if ($redis_key_info) { return $this->renderError("当前操作太快了,请等待"); } else { $redis->set($redis_key, 1, 2); } Db::startTrans(); #===================================================*********** #奖品信息加锁 GoodsList::field('id,stock,surplus_stock') ->where('goods_id', '=', $goods_id) ->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("已售空,请刷新重试"); } #===================================================*********** $res = []; $order_num = create_order_no('MH_', 'order', 'order_num'); #创建订单 $res[] = $order_id = Order::insertGetId([ 'user_id' => $user['id'], 'order_num' => $order_num, 'order_total' => $order_total,#订单金额 'order_zhe_total' => $order_zhe_total,#订单折扣金额 'price' => $price,#微信支付 'use_money' => $use_money,#余额抵扣 'use_integral' => $use_integral,#货币1抵扣 'use_money2' => $use_money2,#货币2抵扣 'use_score' => $use_score,#积分抵扣 'zhe' => $zhe,#会员折扣 'goods_id' => $goods_id, 'num' => $num, 'goods_price' => $goods['price'], 'goods_title' => $goods['title'], 'goods_imgurl' => $goods['imgurl_detail'], 'prize_num' => $prize_num, 'status' => 0, 'pay_type' => 1,#1微信 2支付宝 'order_type' => $goods['type'], 'addtime' => time(), 'coupon_id' => $coupon_id ? $coupon_id : 0, 'use_coupon' => $coupon_price, #优惠券抵扣 'is_shou_zhe' => $is_shou_zhe, 'ad_id' => $ad_id, 'click_id' => $user['click_id'] ]); #微信支付金额大于0 if ($price > 0) { $body = '购买盒子' . $goods['title']; if ($goods['type'] == 1) { $attach = 'order_yfs'; } elseif ($goods['type'] == 3) { $attach = 'order_lts'; } elseif ($goods['type'] == 5) { $attach = 'order_jfs'; } elseif ($goods['type'] == 6) { $attach = 'order_lts'; } elseif ($goods['type'] == 11) { $attach = 'order_zzs'; } elseif ($goods['type'] == 10) { $attach = 'order_scs'; } else { $attach = 'order_qts'; } $payRes = (new Pay())->wxCreateOrder($order_num, $price, $user['openid'], $body, $attach); if ($payRes['status'] == 1) { #结果集 $new_data = [ 'status' => 1, 'order_num' => $order_num, 'res' => $payRes['data'], ]; } else { Db::rollback(); #删除redis $redis->del($redis_key); return $this->renderError("下单失败"); } } else { try { #开盒子 infinite_drawprize_notice $res[] = (new Notify($this->app))->drawprize_notice($user['id'], $order_id, $goods_id, $num); } catch (\Throwable $e) { Db::rollback(); #删除redis $redis->del($redis_key); return $this->renderError("火爆中...请刷新重新购买"); } #结果集 $new_data = [ 'status' => 0, 'order_num' => $order_num, ]; } if (resCheck($res)) { Db::commit(); #删除redis $redis->del($redis_key); return $this->renderSuccess("下单成功", $new_data); } else { Db::rollback(); #删除redis $redis->del($redis_key); return $this->renderError("购买失败,请刷新重试"); } } /** * 抽中的奖品 */ public function prizeorderlog() { $user = $this->getUser(); $order_num = request()->param('order_num', ''); $order_info = Order::field('id,goods_id,num,order_type') ->where('order_num', '=', $order_num) ->where('user_id', '=', $user['id']) ->find(); if (!$order_info) { return $this->renderError("支付异常,请刷新重试"); } $userCoupon = UserCoupon::field('id,level,title,num') ->where('user_id', '=', $user['id']) ->where('from_id', '=', $order_info['id']) ->order('level desc') ->find(); if ($userCoupon != null) { //1特级赏券 2终极赏券 3高级赏券 4普通赏券 if ($userCoupon['level'] == 1) { $userCoupon['level_text'] = '特级赏券'; $userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_a.png'); } elseif ($userCoupon['level'] == 2) { $userCoupon['level_text'] = '终极赏券'; $userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_b.png'); } elseif ($userCoupon['level'] == 3) { $userCoupon['level_text'] = '高级赏券'; $userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_c.png'); } elseif ($userCoupon['level'] == 4) { $userCoupon['level_text'] = '普通赏券'; $userCoupon['level_img'] = imageUrl('/storage/coupon/coupon_d.png'); } } #普通赏 $data = OrderList::field('id,user_id,shang_id,goodslist_id,goodslist_title,goodslist_imgurl,goodslist_money,count(id) as prize_num') ->append(['shang_title']) ->where('user_id', '=', $user['id']) ->where('order_id', '=', $order_info['id']) ->where('order_type', '=', $order_info['order_type']) ->order('shang_id asc, id asc') ->group('prize_code') ->paginate(100)->each(function ($item) { $item['goodslist_imgurl'] = imageUrl($item['goodslist_imgurl']); return $item; }); //重抽卡数量 $item_card_count = Db::name('user_item_card')->where(['user_id' => $user['id'], 'status' => 1])->count(); $new_data = [ 'data' => $data->items(), 'item_card_count' => $item_card_count, 'user_coupon' => $userCoupon ]; return $this->renderSuccess("请求成功", $new_data); } /** * 收藏列表 */ public function listCollect(Request $request) { //1一番赏 2无限赏 3擂台赏 4抽卡机 5积分赏 6全局赏 7福利盲盒 8领主赏 9连击赏 $user = $this->getUser(); $type = \request()->param('type', 0); $data = Collect::field('id,goods_id,type,num') ->where(['user_id' => $user['id']]) ->where('type', '=', $type) ->append(['goods_info']) ->paginate(100)->each(function ($item) { $item['goods_title'] = $item['goods_info']['title']; $item['goods_price'] = $item['goods_info']['price']; $item['imgurl'] = imageUrl($item['goods_info']['imgurl']); if (in_array($item['type'], [1, 3, 5, 6, 10, 11])) { #库存 $surplus = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock') ->where('goods_id', '=', $item['goods_id']) ->where('num', '=', $item['num']) ->where('shang_id', 'between', self::$shang_prize_id) ->find(); $item['stock'] = $surplus['stock']; $item['surplus_stock'] = $surplus['surplus_stock']; } else { $item['stock'] = 0; $item['surplus_stock'] = 0; } return $item; }); $new_data = [ 'data' => $data->items(), 'last_page' => $data->lastPage(), ]; return $this->renderSuccess("操作成功", $new_data); } /** * 添加收藏 */ public function addCollect(Request $request) { $user = $this->getUser(); $goods_id = request()->param('goods_id/d', 0); $goods_num = request()->param('goods_num/d', 0); #盒子信息 $goods = Goodsmodel::field('id,stock,status,type') ->where(['id' => $goods_id]) ->find(); if (!$goods) { return $this->renderError("盒子不存在"); } if ($goods['status'] != 1 && $goods['status'] != 3) { return $this->renderError("盒子已下架"); } $info = Collect::field('id') ->where(['user_id' => $user['id']]) ->where(['goods_id' => $goods_id]) ->where(['num' => $goods_num]) ->find(); if ($info) { $res = Collect::where(['id' => $info['id']])->delete(); } else { $res = Collect::insert([ 'user_id' => $user['id'], 'goods_id' => $goods_id, 'num' => $goods_num, 'type' => $goods['type'], 'addtime' => time(), ]); } if ($res) { return $this->renderSuccess("操作成功"); } else { return $this->renderError("操作失败"); } } /** * 删除收藏 */ public function delCollect(Request $request) { $user = $this->getUser(); $id = request()->param('id/d', 0); $info = Collect::field('id') ->where(['user_id' => $user['id']]) ->where(['id' => $id]) ->find(); if (!$info) { return $this->renderError("请求重复操作"); } $res = Collect::where(['id' => $info['id']])->delete(); if ($res) { return $this->renderSuccess("删除成功"); } else { return $this->renderError("删除失败"); } } /** * 抽奖统计 * @param Request $request *created by Admin at 2022/12/21 11:14 */ public function luck_draw_log(Request $request) { $id = $request->param('id'); $goods = Goodsmodel::where('id', $id)->find(); if (!$goods) { return $this->renderError("盒子不存在"); } $user_ids = OrderList::field('user_id') ->append(['userinfo']) ->where('goods_id', $goods['id']) ->where('source', '=', 1) ->group('user_id') ->paginate(20); foreach ($user_ids as $value) { $count = OrderList::field('user_id') ->where('goods_id', $goods['id']) ->where('user_id', $value['user_id']) ->count(); $value['count'] = $count; } return $this->renderSuccess("操作成功", $user_ids); } /** * 接收盒子同步数据 */ public function receive_sync() { // 获取POST的JSON数据 $input = file_get_contents('php://input'); $data = json_decode($input, true); if (!$data || !isset($data['goods']) || !isset($data['goodsList']) || !isset($data['async_code'])) { return $this->renderError('无效的同步数据'); } // 开始事务 Db::startTrans(); try { $goodsData = $data['goods']; $goodsListData = $data['goodsList']; $async_code = $data['async_code']; // 检查async_code是否存在 $existingGoods = Goodsmodel::where('async_code', $async_code)->find(); // 准备商品数据 $goods = $goodsData; if (isset($goods['id'])) { unset($goods['id']); } if ($existingGoods) { // 更新现有商品 $goodsId = $existingGoods->id; // 更新商品数据 $goods['async_date'] = date('Y-m-d H:i:s'); $existingGoods->save($goods); // 删除现有的商品列表数据 GoodsList::where('goods_id', $goodsId)->delete(); } else { // 创建新商品 $goods['async_date'] = date('Y-m-d H:i:s'); $goods['addtime'] = time(); $goods['update_time'] = time(); $goodsModel = new Goodsmodel(); $goodsModel->save($goods); $goodsId = $goodsModel->id; } // 处理商品列表数据 foreach ($goodsListData as $listItem) { // 处理商品列表项 if (isset($listItem['id'])) { unset($listItem['id']); } $listItem['goods_id'] = $goodsId; // 创建新的商品列表项 $goodsListModel = new GoodsList(); $goodsListModel->save($listItem); } // 提交事务 Db::commit(); return $this->renderSuccess('同步成功'); } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $this->renderError('同步失败: ' . $e->getMessage()); } } /** * 获取用户在指定时间范围内的消费情况 * * @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; } }