diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php index 3d3994f..d3fe641 100644 --- a/app/admin/controller/Statistics.php +++ b/app/admin/controller/Statistics.php @@ -3,6 +3,8 @@ namespace app\admin\controller; use app\common\model\Order as OrderModel; use app\common\model\OrderList; +use app\common\model\OrderListRecovery; +use app\common\model\User; use app\admin\controller\Base; use app\common\model\Category; use app\common\model\Yushou; @@ -44,7 +46,7 @@ class Statistics extends Base } $field = "*"; $order = "id desc"; - $data = GoodsModel::getList($whe, $field, $order, $this->page); + $data = GoodsModel::getList($whe, $field, $order, 20); //订单收入:实际收入:出货价值:实际利润率:订单利润率: $sum_dingdan = 0; $sum_shiji = 0; @@ -124,7 +126,7 @@ class Statistics extends Base $value['count_use_coupon'] = $use_coupon; $value['count_heji'] = $count_price + $count_yue + $use_integral + $use_score + $use_coupon; $value['count_heji_yanzheng'] = $count_OrderList * $value['price']; - + $value['count_OrderList'] = $count_OrderList; $goodslist_price1 = round($goodslist_price, 2); $value['goodslist_price'] = $goodslist_price1; @@ -142,7 +144,7 @@ class Statistics extends Base $sum_dingdan += $count_OrderList * $value['price']; $sum_shiji += $order_zhe_total; - $sum_chuhuo +=$goodslist_price1; + $sum_chuhuo += $goodslist_price1; $sum_shijilirun += $value['lirun']; $sum_dingdanlirun += $value['liruns']; } @@ -159,5 +161,179 @@ class Statistics extends Base } + /** + * 数据统计 + * @param \think\Request $request + * @return string + */ + public function DataStand(Request $request) + { + $userCount = User::count("id"); + + // 今日时间 + $ranges['today_start'] = strtotime('today'); + $ranges['today_end'] = strtotime('tomorrow') - 1; + + // 昨日时间 + $ranges['yesterday_start'] = strtotime('yesterday'); + $ranges['yesterday_end'] = strtotime('today') - 1; + + // 本周时间(周一到周日) + $ranges['this_week_start'] = strtotime('monday this week'); + $ranges['this_week_end'] = strtotime('sunday this week') + 86399; + + // 上周时间(上周一到上周日) + $ranges['last_week_start'] = strtotime('monday last week'); + $ranges['last_week_end'] = strtotime('sunday last week') + 86399; + + // 本月时间(1号到月末) + $ranges['this_month_start'] = strtotime(date('Y-m-01')); + $ranges['this_month_end'] = strtotime(date('Y-m-t')) + 86399; + + // 上月时间(上个月1号到月末) + $ranges['last_month_start'] = strtotime(date('Y-m-01', strtotime('-1 month'))); + $ranges['last_month_end'] = strtotime(date('Y-m-t', strtotime('-1 month'))) + 86399; + + + // 今日注册人数 + $user_today = User::whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->count('id'); + + // 昨日注册人数 + $user_yesterday = User::whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->count('id'); + + // 本周注册人数 + $user_this_week = User::whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->count('id'); + + // 上周注册人数 + $user_last_week = User::whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->count('id'); + + // 本月注册人数 + $user_this_month = User::whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->count('id'); + // 上月注册人数 + $user_last_month = User::whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->count('id'); + + // 本日充值金额 + $order_today = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('price'); + + // 昨日充值金额 + $order_yesterday = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('price'); + + // 本周充值金额 + $order_this_week = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->sum('price'); + + // 上周充值金额 + $order_last_week = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->sum('price'); + + // 本月充值金额 + $order_this_month = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->sum('price'); + + // 上月充值金额 + $order_last_month = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->sum('price'); + // 消费人数统计 + $consume_today = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->field('user_id')->group('user_id')->count('user_id'); + $consume_yesterday = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->field('user_id')->group('user_id')->count('user_id'); + $consume_this_week = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->field('user_id')->group('user_id')->count('user_id'); + $consume_last_week = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->field('user_id')->group('user_id')->count('user_id'); + $consume_this_month = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->field('user_id')->group('user_id')->count('user_id'); + $consume_last_month = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->field('user_id')->group('user_id')->count('user_id'); + + // RMB消费人数统计 + $rmb_consume_today = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->field('user_id')->group('user_id')->count('user_id'); + $rmb_consume_yesterday = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->field('user_id')->group('user_id')->count('user_id'); + $rmb_consume_this_week = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->field('user_id')->group('user_id')->count('user_id'); + $rmb_consume_last_week = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->field('user_id')->group('user_id')->count('user_id'); + $rmb_consume_this_month = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->field('user_id')->group('user_id')->count('user_id'); + $rmb_consume_last_month = OrderModel::where('status', '=', 1)->where('price', '>', 0)->whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->field('user_id')->group('user_id')->count('user_id'); + + //今日余额消费 + $money_today = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('use_money'); + $money_yesterday = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('use_money'); + //今日积分抵扣 + $score_yesterday = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('use_score'); + $score_today = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('use_score'); + //今日余额发放 + $money_recovery_today = OrderListRecovery::whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('money'); + $money_recovery_yesterday = OrderListRecovery::whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('money'); + //今日优惠卷消费 + $coupon_today = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('use_coupon'); + + //今日收入 + $order_zhe_total = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('order_zhe_total'); + $order_goodslist_money = OrderList::whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('goodslist_money'); + $order_goodslist_count = OrderList::whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->count(); + $order_count = OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->count(); + + $order_zhe_total_yester = round(OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('order_zhe_total'), 2); + $order_goodslist_money_yester = round( OrderList::whereBetweenTime('addtime', $ranges['yesterday_start'], $ranges['yesterday_end'])->sum('goodslist_money'), 2); + + $order_zhe_total_week = round(OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->sum('order_zhe_total'), 2); + $order_goodslist_money_week = round(OrderList::whereBetweenTime('addtime', $ranges['this_week_start'], $ranges['this_week_end'])->sum('goodslist_money'), 2); + + $order_zhe_total_last_week = round(OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->sum('order_zhe_total'), 2); + $order_goodslist_money__last_week = round(OrderList::whereBetweenTime('addtime', $ranges['last_week_start'], $ranges['last_week_end'])->sum('goodslist_money'), 2); + + + $order_zhe_total_month = round(OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->sum('order_zhe_total'), 2); + $order_goodslist_money_month = round(OrderList::whereBetweenTime('addtime', $ranges['this_month_start'], $ranges['this_month_end'])->sum('goodslist_money'), 2); + + $order_zhe_total_last_month = round( OrderModel::where('status', '=', 1)->whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->sum('order_zhe_total'), 2); + $order_goodslist_money__last_month = round(OrderList::whereBetweenTime('addtime', $ranges['last_month_start'], $ranges['last_month_end'])->sum('goodslist_money'), 2); + + + View::assign('money_today', $money_today); + View::assign('money_yesterday', $money_yesterday); + View::assign('coupon_today', $coupon_today); + + View::assign('score_today', $score_today); + View::assign('score_yesterday', $score_yesterday); + View::assign('money_recovery_today', $money_recovery_today); + View::assign('money_recovery_yesterday', $money_recovery_yesterday); + View::assign('order_zhe_total', $order_zhe_total); + View::assign('order_goodslist_money', $order_goodslist_money); + View::assign('order_goodslist_count', $order_goodslist_count); + View::assign('order_count', $order_count); + View::assign('order_lirun', round($order_zhe_total - $order_goodslist_money, 2)); + View::assign('order_lirun_yester', round($order_zhe_total_yester - $order_goodslist_money_yester, 2) . " ($order_zhe_total_yester - $order_goodslist_money_yester)"); + View::assign('order_lirun_week', round($order_zhe_total_week - $order_goodslist_money_week, 2) . " ({$order_zhe_total_week} - $order_goodslist_money_week)"); + View::assign('order_lirun_last_week', round($order_zhe_total_last_week - $order_goodslist_money__last_week, 2) . " ($order_zhe_total_last_week - $order_goodslist_money__last_week)"); + View::assign('order_zhe_total_month', round($order_zhe_total_month - $order_goodslist_money_month, 2). " ($order_zhe_total_month - $order_goodslist_money_month)"); + View::assign('order_lirun_last_month', round($order_zhe_total_last_month - $order_goodslist_money__last_month, 2). " ($order_zhe_total_last_month - $order_goodslist_money__last_month)"); + + // 赋值给模板 + View::assign('userCount', $userCount); + View::assign('user_today', $user_today); + View::assign('user_yesterday', $user_yesterday); + View::assign('user_this_week', $user_this_week); + View::assign('user_last_week', $user_last_week); + View::assign('user_this_month', $user_this_month); + View::assign('user_last_month', $user_last_month); + // 充值订单数据 + View::assign('order_today', $order_today); + View::assign('order_yesterday', $order_yesterday); + View::assign('order_this_week', $order_this_week); + View::assign('order_last_week', $order_last_week); + View::assign('order_this_month', $order_this_month); + View::assign('order_last_month', $order_last_month); + + View::assign('consume_today', $consume_today); + View::assign('consume_yesterday', $consume_yesterday); + View::assign('consume_this_week', $consume_this_week); + View::assign('consume_last_week', $consume_last_week); + View::assign('consume_this_month', $consume_this_month); + View::assign('consume_last_month', $consume_last_month); + + View::assign('rmb_consume_today', $rmb_consume_today); + View::assign('rmb_consume_yesterday', $rmb_consume_yesterday); + View::assign('rmb_consume_this_week', $rmb_consume_this_week); + View::assign('rmb_consume_last_week', $rmb_consume_last_week); + View::assign('rmb_consume_this_month', $rmb_consume_this_month); + View::assign('rmb_consume_last_month', $rmb_consume_last_month); + + View::assign('ranges', $ranges); + + + return View::fetch("Statistics/dataStand"); + } + } diff --git a/app/admin/route/app.php b/app/admin/route/app.php index 259a59a..9a7c130 100755 --- a/app/admin/route/app.php +++ b/app/admin/route/app.php @@ -264,4 +264,7 @@ Route::rule('productcate_del', 'ProductCate/del', 'GET|POST'); //利润统计 -Route::rule('statistics_profit', 'Statistics/profit', 'GET'); \ No newline at end of file +Route::rule('statistics_profit', 'Statistics/profit', 'GET'); + +//数据看台 +Route::rule('statistics_dataStand', 'Statistics/DataStand', 'GET'); \ No newline at end of file diff --git a/app/admin/view/Statistics/dataStand.html b/app/admin/view/Statistics/dataStand.html new file mode 100644 index 0000000..61f3d4b --- /dev/null +++ b/app/admin/view/Statistics/dataStand.html @@ -0,0 +1,303 @@ +{include file="Public:header2"/} + + + +
+
+
+
+
+
+ 总注册人数: + + {$userCount} + +
+
+
+
+
+
用户注册数据
+
+
+
+ 今日新增用户: {$user_today} +
+
+ 昨日新增用户: {$user_yesterday} +
+
+
+
+ 本周新增用户: {$user_this_week} +
+
+ 上周新增用户: {$user_last_week} +
+
+
+
+ 本月新增用户: {$user_this_month} +
+
+ 上月新增用户: {$user_last_month} +
+
+
+
+
+
+
+
用户充值数据
+
+
+
+ 今日充值金额: {$order_today} +
+
+ 昨日充值金额: {$order_yesterday} +
+
+
+
+ 本周充值金额: {$order_this_week} +
+
+ 上周充值金额: {$order_last_week} +
+
+
+
+ 本月充值金额: {$order_this_month} +
+
+ 上月充值金额: {$order_last_month} +
+
+
+
+
+
+ +
+
+
+
+
+
+ 消费与虚拟货币 + + + +
+
+
+
+
+
消费数据(RMB/全部)
+
+
+
+ 今日消费人数: {$rmb_consume_today}/{$consume_today} +
+
+ 昨日消费人数: {$rmb_consume_yesterday}/{$consume_yesterday} +
+
+
+
+ 本周消费人数: {$rmb_consume_this_week}/{$consume_this_week} +
+
+ 上周消费人数: {$rmb_consume_last_week}/{$consume_last_week} +
+
+
+
+ 本月消费人数: {$rmb_consume_this_month}/{$consume_this_month} +
+
+ 上月消费人数: {$rmb_consume_last_month}/{$consume_last_month} +
+
+
+
+
+
+
+
虚拟货币统计
+
+
+
+ 今日余额消费: {$money_today} +
+
+ 今日余额发放: {$money_recovery_today} +
+ +
+
+
+ 今日积分消费: {$score_today} +
+
+ 今日消费卷抵扣: {$coupon_today} +
+ +
+
+
+ 昨日积分消费: {$score_yesterday} +
+ +
+ 昨日余额发放: {$money_recovery_yesterday} +
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+ 今日收入汇总 +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
订单收入:{$order_zhe_total}(收入不包含折扣,不包含优惠卷)
RMB收入:{$order_today}
订单出货:{$order_goodslist_money}
订单笔数:{$order_count}
发数:{$order_goodslist_count}
利润:{$order_lirun}
利润计算方式:收入-支出=当日利润
+
+
+
+
+
+
+ 今日毛利统计 +
+
+ + + + + + + + + + + + + + + + + + + + + +
今日毛利:{$order_lirun}昨日毛利:{$order_lirun_yester}
本周毛利:{$order_lirun_week}上周毛利:{$order_lirun_last_week}
本月毛利:{$order_zhe_total_month}上月毛利:{$order_lirun_last_month}
+
+
+
+
+
+
+ {include file="Public:footer"/} + + + + \ No newline at end of file diff --git a/app/api/controller/AliNotify.php b/app/api/controller/AliNotify.php new file mode 100644 index 0000000..a5b7698 --- /dev/null +++ b/app/api/controller/AliNotify.php @@ -0,0 +1,2472 @@ +quan_prize_notice($order); + } + + /** + * 特殊奖品开奖 拳王赏 + * @param $order 订单信息 + */ + protected function quan_prize_notice($order) + { + $res = []; + $goods_id = $order['goods_id']; + $num = $order['num']; + $order_type = $order['order_type']; + #盒子 + $goods_info = Goods::field('prize_num,stock')->where(['id' => $goods_id])->find(); + #特殊奖品存在where('goods_id', '=', $goods_id) + $special_prize = GoodsList::where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('surplus_stock', '>', 0) + ->where('shang_id', '=', self::$shang_give_quan_id) + ->order('prize_num desc') + ->select(); + // dd($special_prize); + $user_ids = []; + foreach ($special_prize as $value) { + $surplus_give_stock = $value ? $value['surplus_stock'] : 0; + if ($value && $surplus_give_stock > 0) { + + #符合条件的订单 + $quan_prize_num = $value['prize_num']; + // dd($quan_prize_num); + // var_dump($quan_prize_num); + $prize_num_data = OrderList::field('id,user_id,count(`id`) as all_num') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('order_type', '=', $order_type) + ->where('shang_id', '=', self::$shang_give_w_id) + ->having('all_num', '>=', $quan_prize_num) + ->group('user_id') + ->select()->toArray(); + + $prize_num_data_arr = []; + foreach ($prize_num_data as $val) { + if ($val['all_num'] >= $quan_prize_num && !in_array($val['user_id'], $user_ids)) { + $prize_num_data_arr[] = $val; + } + } + // dd($prize_num_data_arr); + if ($prize_num_data_arr) { + // dd($prize_num_data); + #多个随机 + shuffle($prize_num_data_arr); + + $quan_prize_info = $prize_num_data_arr[0]; + + $user_id = $quan_prize_info['user_id']; + $user_ids[] = $quan_prize_info['user_id']; + + $order_goods = [ + 'order_id' => 0, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => $num, + 'shang_id' => $value['shang_id'], + 'goodslist_id' => $value['id'], + 'goodslist_title' => $value['title'], + 'goodslist_imgurl' => $value['imgurl'], + 'goodslist_price' => $value['price'], + 'goodslist_money' => $value['money'], + 'goodslist_type' => $value['goods_type'], + 'goodslist_sale_time' => $value['sale_time'], + 'addtime' => time(), + 'prize_code' => $value['prize_code'], + 'order_type' => $order_type, + 'order_list_id' => 0, + ]; + // var_dump($order_goods); + #新增奖品列表 + $res[] = OrderList::insert($order_goods); + + #减少库存 + $res[] = GoodsList::field('surplus_stock') + ->where(['id' => $value['id']]) + ->dec('surplus_stock') + ->update(); + // var_dump($res); + } + } + + } + #赏品 + $goods_list_zd = GoodsList::where(['goods_id' => $goods_id]) + ->where(['num' => 1]) + ->select()->toArray(); + if ($goods_list_zd) { + #循环数据 + $save_sports_data = []; + $start_num = $goods_info['stock'] + 1; + for ($i = $start_num; $i <= $start_num; $i++) { + foreach ($goods_list_zd as $k => $v) { + unset($v['id']); + unset($v['num']); + $v['num'] = $i; + $v['surplus_stock'] = $v['stock']; + $save_sports_data[] = $v; + } + } + #添加赏品 + $res[] = GoodsList::insertAll($save_sports_data); + #增加库存 + $res[] = Goods::where(['id' => $goods_id])->inc('stock', 1)->update(); + } + return $res; + } + + /* + * 支付宝支付回调修改订单状态 + */ + public function alipay_notify() + { + //原始订单号 + $out_trade_no = input('out_trade_no'); + //支付宝交易号 + $trade_no = input('trade_no'); + //交易状态 + $trade_status = input('trade_status'); + $ext_infos=input('ext_infos'); + $table = input('subject'); + + // Log::info('alipay_notify=================$out_trade_no' . $out_trade_no); + // Log::info('alipay_notify=================$trade_no' . $out_trade_no); + // Log::info('$table=================$table'.$table ); + Db::name('pay_log')->insert([ + 'user_id' => '11', + 'out_trade_no' => $out_trade_no, + 'addtime' => time(), + 'trade_no' => $trade_no, + 'content'=>$trade_status + ]); + + if ( $trade_status == 'TRADE_SUCCESS') { + + + + // try { + // $orderInfo111 = Order::where('order_num', '=', $out_trade_no) + // ->where('status', '=', 0) + // ->find(); + // if($orderInfo111){ + // $user111 = User::where('id', $orderInfo111['user_id'])->find(); + // if($user111){ + // Db::name('wxpay_log')->insert([ + // 'order_no' => $data['out_trade_no'], + // 'content' => json_encode($data), + // 'type' => 2, + // 'user_id' => $user111['id'], + // 'addtime' => time(), + // ]); + // } + // } + + // } catch (\Throwable $e) { + // $this->CallbackSuccess(); + // } + $user = null; + if ($table == 'user_recharge') {#余额充值 + $orderInfo = UserRecharge::where('order_num', '=', $out_trade_no) + ->where(['status' => 1]) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 1) { + $user = User::where('id', $orderInfo['user_id'])->find(); + + $user_id = $orderInfo['user_id']; + $money = $orderInfo['money']; + Db::startTrans(); + #修改订单状态 + $res[] = UserRecharge::field('status,pay_time') + ->where(['id' => $orderInfo['id']])->update([ + 'status' => 2, + 'pay_time' => time(), + ]); + $res[] = User::changeMoney($user_id, $money, 2, '在线充值'); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $money, + 'content' => '支付宝', + 'pay_type' => 2,#1微信 2支付宝 + 'addtime' => time(), + ]); + if (resCheck($res)) { + Db::commit(); + echo 'success'; + } else { + Db::rollback(); + echo 'fail'; + } + }else{ + echo 'success'; + Log::info('$orderInfo=================重复'); + return; + } + } elseif ($table == 'order_yfs' || $table == 'order_lts' || $table == 'order_zzs') {#抽赏一番赏 擂台赏 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + $user = User::where('id', $orderInfo['user_id'])->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + + Db::startTrans(); + try { + #开盒子 + $res[] = $this->drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id'], $orderInfo['num']); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + #广告收益回调 + if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) { + $order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count(); + if ($order_ad_num <= 1) { + + $this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']); + + } + + } + } catch (\Throwable $e) { + Db::rollback(); + #卡单了 + Order::field('kd_is')->where(['id' => $orderInfo['id']]) + ->update(['kd_is' => 1]); + #通知微信 + $this->CallbackSuccess(); + } + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 2,#1微信 2支付宝 + 'addtime' => time(), + ]); + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_wxs') {#抽赏无限赏 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + $user = User::where('id', $orderInfo['user_id'])->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + Db::startTrans(); + $res[] = $this->infinite_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 2,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + #广告收益回调 + if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) { + $order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count(); + if ($order_ad_num <= 1) { + $this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']); + } + + + } + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_ckj') {#抽赏抽卡机 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->where('order_type', '=', 4) + ->find(); + $user = User::where('id', $orderInfo['user_id'])->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + Db::startTrans(); + $res[] = $this->cardextractor_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 2,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_list_send') {#背包发货 + $orderInfo = OrderListSend::where('send_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + $user = User::where('id', $orderInfo['user_id'])->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $freight = $orderInfo['freight']; + Db::startTrans(); + $res[] = $this->reward_order_handle($user_id, $orderInfo['id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $freight, + 'content' => '背包发货', + 'pay_type' => 2,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_js') { + + } + + + + + // try { + // Db::name('wxpay_log')->insert([ + // 'order_no' => $data['out_trade_no'], + // 'content' => json_encode($data), + // 'type' => 1, + // 'channel'=>2, + // 'user_id' => $user['id'], + // 'addtime' => time(), + // ]); + // } catch (\Throwable $e) { + // $this->CallbackSuccess(); + // } + + + + + + + + + }else{ + echo "fail"; + } + + + + } + + /** + * + * 获取支付结果通知数据 + * return array + */ + public function order_notify() + { + #获取通知的数据 + $xml = file_get_contents("php://input"); + if (empty($xml)) { + return false; + } + #转成数组 + $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); + $sign = $data['sign']; + // Log::info('order_notify=================out_trade_no' . $data['out_trade_no']); + // Log::error('错误信息' . $e->getMessage()); + unset($data['sign']); + $newSign = $this->MakeSign($data); + $newSignAccount = $this->MakeSignAccount($data); + #验证签名 + if ($sign == $newSign || $sign == $newSignAccount) { + //如果成功返回了 + if ($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') { + $user = User::where('openid', $data['openid'])->find(); + try { + Db::name('wxpay_log')->insert([ + 'order_no' => $data['out_trade_no'], + 'content' => json_encode($data), + 'type' => 1, + 'user_id' => $user['id'], + 'addtime' => time(), + ]); + } catch (\Throwable $e) { + $this->CallbackSuccess(); + } + $out_trade_no = $data['out_trade_no'];//订单号 + $table = $data['attach'];#表名 + if ($table == 'user_recharge') {#余额充值 + $orderInfo = UserRecharge::where('order_num', '=', $out_trade_no) + ->where(['status' => 1]) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 1) { + $user_id = $orderInfo['user_id']; + $money = $orderInfo['money']; + Db::startTrans(); + #修改订单状态 + $res[] = UserRecharge::field('status,pay_time') + ->where(['id' => $orderInfo['id']])->update([ + 'status' => 2, + 'pay_time' => time(), + ]); + $res[] = User::changeMoney($user_id, $money, 2, '在线充值'); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $money, + 'content' => '微信支付', + 'pay_type' => 1,#1微信 2支付宝 + 'addtime' => time(), + ]); + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_yfs' || $table == 'order_lts' || $table == 'order_zzs') {#抽赏一番赏 擂台赏 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + Db::startTrans(); + try { + #开盒子 + $res[] = $this->drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id'], $orderInfo['num']); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + #广告收益回调 + if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) { + $order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count(); + if ($order_ad_num <= 1) { + + $this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']); + + } + + } + } catch (\Throwable $e) { + Db::rollback(); + #卡单了 + Order::field('kd_is')->where(['id' => $orderInfo['id']]) + ->update(['kd_is' => 1]); + #通知微信 + $this->CallbackSuccess(); + } + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 1,#1微信 2支付宝 + 'addtime' => time(), + ]); + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_wxs') {#抽赏无限赏 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + Db::startTrans(); + $res[] = $this->infinite_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 1,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + #广告收益回调 + if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) { + $order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count(); + if ($order_ad_num <= 1) { + $this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']); + } + + + } + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_ckj') {#抽赏抽卡机 + $orderInfo = Order::where('order_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->where('order_type', '=', 4) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $price = $orderInfo['price']; + Db::startTrans(); + $res[] = $this->cardextractor_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $price, + 'content' => '购买盒子' . $orderInfo['goods_title'], + 'pay_type' => 1,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_list_send') {#背包发货 + $orderInfo = OrderListSend::where('send_num', '=', $out_trade_no) + ->where('status', '=', 0) + ->find(); + #这个订单存在状态对 + if ($orderInfo && $orderInfo['status'] == 0) { + $user_id = $orderInfo['user_id']; + $freight = $orderInfo['freight']; + Db::startTrans(); + $res[] = $this->reward_order_handle($user_id, $orderInfo['id']); + #记录微信支付 + $res[] = ProfitPay::insert([ + 'user_id' => $user_id, + 'order_num' => $out_trade_no, + 'change_money' => $freight, + 'content' => '背包发货', + 'pay_type' => 1,#1微信 2支付宝 + 'addtime' => time(), + ]); + + $this->wx_gf_fahuo($user_id, $orderInfo['order_num']); + + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } elseif ($table == 'order_js') { + + } + } + } + $this->CallbackSuccess(); + } + + /* + 广告推广 + */ + public function ad_notify($ads_id, $click_id) + { + $tencent_ad = Ads::where('id', $ads_id)->find(); + if (!empty($tencent_ad)) { + $result = tencent_ad_attribution($click_id, 1, $tencent_ad['account_id'], $tencent_ad['access_token'], $tencent_ad['user_action_set_id']); + dump($result); + } + } + + /** + * 生成签名 + * @return 签名 + */ + public function MakeSign($params) + { + //签名步骤一:按字典序排序数组参数 + ksort($params); + $string = $this->ToUrlParams($params); + //签名步骤二:在string后加入KEY + $string = $string . "&key=" . static::$secretKey; + //签名步骤三:MD5加密 + $string = md5($string); + //签名步骤四:所有字符转为大写 + $result = strtoupper($string); + return $result; + } + + + /** + * 生成签名 + * @return 签名 + */ + public function MakeSignAccount($params) + { + //签名步骤一:按字典序排序数组参数 + ksort($params); + $string = $this->ToUrlParams($params); + //签名步骤二:在string后加入KEY + $string = $string . "&key=" . static::$secretKeyAccount; + //签名步骤三:MD5加密 + $string = md5($string); + //签名步骤四:所有字符转为大写 + $result = strtoupper($string); + return $result; + } + + /** + * 将参数拼接为url: key=value&key=value + * @param $params + * @return string + */ + public function ToUrlParams($params) + { + $string = ''; + if (!empty($params)) { + $array = array(); + foreach ($params as $key => $value) { + $array[] = $key . '=' . $value; + } + $string = implode("&", $array); + } + return $string; + } + + /** + * 接收通知成功后应答输出XML数据 + * @param string $xml + */ + public function CallbackSuccess() + { + $html = ""; + die($html); + } + + /** + * 抽赏开始抽赏====================================================== + * @param int $user_id 会员id + * @param int $order_id 订单ID + * @param int $goods_id 盒子id + * @param int $num 盒子箱号 + */ + public function drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0, $num = 0) + { + + $res = []; + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => $num]) + ->where(['status' => 0]) + ->where('order_type', 'in', [1, 3, 5, 6, 11]) + ->find(); + + if ($order) { + //判断是否限制购买一次 + $sbuser=User::where(['id' => $user_id])->find(); + $sbgoods=User::where(['id' => $goods_id])->find(); + $user_xiangou_count = OrderList::field('id')->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', [10, 38]) + ->where('order_type', 'in', [1, 3, 5, 6, 11]) + ->where('user_id', '=', $user_id) + ->count(); + + if (($sbgoods['type'] == 6 ||$sbgoods['type'] == 1) && $sbgoods['quanju_xiangou'] > 0) { + if($user_xiangou_count>= $sbgoods['quanju_xiangou']){ + Log::info('====用户id'.$user_id.'试图作弊,已停发本次赏品'); + Log::info('====用户id'.$user_id.'试图作弊,已停发本次赏品'); + Log::info('====用户id'.$user_id.'试图作弊,已停发本次赏品'); + $res1[] = 0; + return $res1; + } + } + + + + #改变状态 + $res[] = Order::field('status,pay_time') + ->where(['id' => $order['id']]) + ->update([ + 'status' => 1, + 'pay_time' => time(), + ]); + #扣余额 + if ($order['use_money'] > 0) { + $res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']); + } + #扣星岚币 + if ($order['use_integral'] > 0) { + $res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']); + } + #扣积分 + if ($order['use_score'] > 0) { + $res[] = User::changeScore($order['user_id'], -$order['use_score'], 2, '购买盒子' . $order['goods_title']); + } + #判断一下优惠券 + if (!empty($order['coupon_id'])) { + $coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]); + } + + #判断是否发积分 + + #分销奖励 + $res[] = User::distribution($order); + #分销奖励 + //对对碰 + $ddp_goods=Goods::where('id',$goods_id)->find(); + if($ddp_goods&&($ddp_goods['is_ddp']==1||$ddp_goods['is_zp']==1)){ + + }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']); + } + + + if($ddp_goods&&$ddp_goods['is_zp']==1){ + + $goodslist_xz = GoodsList::where(['goods_id' => $ddp_goods['id']]) + ->order('pro asc') + ->select()->toArray(); + $randomValue = mt_rand(1, 100); // 生成0到100之间的随机数 + $nowpro=0; + $read_goods=[]; + foreach ($goodslist_xz as $key => &$value1) { + $nowpro+=$value1['pro']; + if($randomValue<$nowpro){ + $read_goods=$value1; + Db::name('error_log')->insert([ + 'user_id' => $user_id+1260, + 'goods_id' => $ddp_goods['id'], + 'addtime' => time(), + 'content'=>'支付宝付费抽奖开启概率'.$randomValue.',累计概率'.$nowpro.',本商品概率'.$value1['pro'].',本商品名称'.$value1['title'].',抽取id'.$user_id, + 'trade_no' => '55' + + ]); + + $randomValue=99999; + } + } + + $order_goods = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' =>$read_goods['goods_id'], + 'num' => 1, + 'shang_id' => $read_goods['shang_id'], + 'goodslist_id' => $read_goods['id'], + 'goodslist_title' => $read_goods['title'], + 'goodslist_imgurl' => $read_goods['imgurl'], + 'goodslist_price' => $read_goods['price'], + 'goodslist_money' => $read_goods['money'], + 'goodslist_type' => $read_goods['goods_type'], + 'goodslist_sale_time' => $read_goods['sale_time'], + 'addtime' => time(), + 'prize_code' => $read_goods['prize_code'], + 'order_type' => 11, + 'order_list_id' => 0, + 'xz_id'=>0 + ]; + + + + $res[] =OrderList::insert($order_goods); + + }else if($ddp_goods&&$ddp_goods['is_ddp']==1){ + $touch = Db::name('touch_user_list')->where('goods_id',$goods_id)->where('user_id',$user_id) + ->find(); + $mystatus=3; + if($ddp_goods['ddp_type']==2){ + $mystatus=4; + } + $updata=[ + 'cards'=>'', + 'user_xy'=>0, + 'is_xy'=>0, + 'xy'=>'', + 'status'=>$mystatus, + 'user_id'=>$user_id, + 'goods_id'=>$goods_id, + 'order_id'=>$order['id'], + 'update_time' => time(), + 'num'=>0 + ]; + if ($touch) { + $res[]=Db::name('touch_user_list')->where('goods_id',$goods_id)->where('user_id',$user_id) + ->update($updata); + }else{ + $res[]=Db::name('touch_user_list')->insert($updata); + } + + }else{ + + #特殊奖品********************************************************** + #普通奖品余量信息 + $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赏 全局赏 拳王赏 + // Log::info('=================================================打印日志surplus_stock'.$goodslist['surplus_stock']); + if ($goodslist['surplus_stock'] <= 0) { + // var_dump($goodslist['surplus_stock']); + $res[] = $this->openFd($goods_id, $num); + + $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); + } + #特殊奖品********************************************************** + + + } + } else { + $res[] = 0; + } + return $res; + } + + /** + * 普通奖品开奖 + * @param $order 订单信息 + */ + protected function ordinary_prize_notice($order) + { + $user_id = $order['user_id']; + $order_id = $order['id']; + $goods_id = $order['goods_id']; + $num = $order['num']; + $order_type = $order['order_type']; + #抽奖数量 + $prize_num = $order['prize_num']; + #普通奖品信息 + $ordinary_prize = GoodsList::where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('shang_id', 'between', self::$shang_prize_id) + ->order('sort desc,shang_id asc') + ->select()->toArray(); + if (!$ordinary_prize) { + $res[] = 0; + return $res; + } + //获取最低抽奖次数 + $goods_extend_model_list = GoodsExtendListModel::getGoodsExtendList($goods_id); + #抽了多少抽 + $stock = array_sum(array_column($ordinary_prize, 'stock')); + $surplus_stock = array_sum(array_column($ordinary_prize, 'surplus_stock')); + $reward_num_count = $stock - $surplus_stock; + #普通奖暂存信息 + $ordinary_prize_all = []; + #普通奖暂存信息 + $ordinary_prize_special_all = []; + #其他 + $ordinary_prize_other = []; + foreach ($ordinary_prize as $k => $v) { + #剩余 + $surplus_prize = $v['surplus_stock']; + //奖品id + $prize_code = $v['prize_code']; + $goods_list_id = $v['id']; + //扩展配置 + $goods_extend_model = $goods_extend_model_list->where('goods_list_id', '=', $goods_list_id)->first(); + //最低抽奖次数 + $reward_num = 0; + if ($goods_extend_model != null) { + if ($goods_extend_model['rawrd_type'] == 1) { + $reward_num = $goods_extend_model->reward_num; + } else if ($goods_extend_model['rawrd_type'] == 2) { + //当前抽数 + $goods_extend_model_reward_num = $goods_extend_model['reward_num']; + if ($reward_num_count < $goods_extend_model_reward_num) { + //未过期 + $v['goods_extend'] = $goods_extend_model; + $ordinary_prize_special_all[] = $v; + $ordinary_prize_all[] = $v; + continue; + } + } else if ($goods_extend_model['rawrd_type'] == 3) { + //当前抽数 + // if ($reward_num_count >= $goods_extend_model['reward_num'] && $reward_num_count <= $goods_extend_model['reward_num_1']) { + //未过期 + $v['goods_extend'] = $goods_extend_model; + $ordinary_prize_special_all[] = $v; + $ordinary_prize_all[] = $v; + continue; + // } + } + } + $prize_code = ''; + //库存数量 + for ($i = 1; $i <= $surplus_prize; $i++) { + if ($reward_num_count > 0) { + if ($reward_num_count > $reward_num) { + $ordinary_prize_other[] = $v; + + } + } else if ($v['reward_num'] > 0) { + if ($reward_num_count >= $v['reward_num']) { + #全部延迟出赏普通奖品 + $ordinary_prize_other[] = $v; + + } + } else { + #全部未延迟出赏普通奖品 + $ordinary_prize_other[] = $v; + + } + $ordinary_prize_all[] = $v; + + } + } + $is_special = true; + if ($prize_num > count($ordinary_prize_other)) { + #全部商品 + $end_ordinary_prize = $ordinary_prize_all; + $is_special = false; + } else { + #延迟出赏 + $end_ordinary_prize = $ordinary_prize_other; + } + shuffle($end_ordinary_prize); + shuffle($end_ordinary_prize); + if ($is_special) { + foreach ($ordinary_prize_special_all as $kk => $vv) { + // $surplus_prize_1 = 1; + $surplus_prize_1 = $vv['surplus_stock']; + //奖品id + $goods_extend_model_1 = $vv['goods_extend']; + for ($i = 1; $i <= $surplus_prize_1; $i++) { + //指定抽数 + if ($goods_extend_model_1['rawrd_type'] == 2) { + //指定抽奖数- + $randomNumber = $goods_extend_model_1['reward_num'] - $reward_num_count - 1; + //如果小于0,则表示 + if ($randomNumber < 0) { + $randomNumber = random_int(0, count($end_ordinary_prize)); + } + if ($randomNumber >= 0) { + //表示第二个对象 goods_list_id + if (isset($end_ordinary_prize[$randomNumber]['goods_extend'])) { + $goods_extend_model_2 = $end_ordinary_prize[$randomNumber]['goods_extend']; + if ($goods_extend_model_2 != null) { + //表示相同抽数 + if ($goods_extend_model_1['reward_num'] == $goods_extend_model_2['reward_num']) { + $randomNumber = random_int($randomNumber, count($end_ordinary_prize)); + } + } + } + if (isset($end_ordinary_prize[$randomNumber]['prize_code']) && isset($vv['prize_code'])) { + if ($end_ordinary_prize[$randomNumber]['prize_code'] == $vv['prize_code']) { + if (count($end_ordinary_prize) > $randomNumber) { + $randomNumber = random_int($randomNumber, count($end_ordinary_prize)); + } else { + $randomNumber = random_int(0, count($end_ordinary_prize)); + } + } + } + array_splice($end_ordinary_prize, $randomNumber, 0, [$vv]); + } + } + //抽奖范围 + if ($goods_extend_model_1['rawrd_type'] == 3) { + $randomNumber = 0; + if ($reward_num_count >= $goods_extend_model_1['reward_num'] && $reward_num_count <= $goods_extend_model_1['reward_num_1']) { + //在范围里面 + $max_randomNumber = $goods_extend_model_1['reward_num_1'] - $reward_num_count - 1; + $randomNumber = random_int(0, intval($max_randomNumber)); + } else if ($reward_num_count <= $goods_extend_model_1['reward_num']) { + //$goods_extend_model_1['reward_num']-$reward_num_count + $randomNumber = count($end_ordinary_prize) - 1; + } + array_splice($end_ordinary_prize, $randomNumber, 0, [$vv]); + } + } + } + } + #入库奖品 + $save_order_goods = []; + #开普通奖品 + for ($i = 0; $i < $prize_num; $i++) { + $ordinary_prize_info = $end_ordinary_prize[$i]; + $save_order_goods[ + + ] = [ + 'order_id' => $order_id, + '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, + 'is_fd'=>$ordinary_prize_info['is_fd'] + ]; + #减少库存 + $res[] = GoodsList::field('surplus_stock') + ->where(['id' => $ordinary_prize_info['id']]) + ->dec('surplus_stock') + ->update(); + } + if ($save_order_goods) { + #新增奖品列表 + $res[] = OrderList::insertAll($save_order_goods); + } + return $res; + } + /** + * openFd 福袋开启 + */ + protected function openFd($goods_id, $num){ + $res = []; + $all_order_list = OrderList::field('id,num,user_id,goods_id,is_fd,status,goodslist_id')->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('is_fd', '=', 1) + ->where('status', '=', 0) + ->order('id asc') + ->select()->toArray(); + + if (count($all_order_list)>0) { + $orders=[]; + $goodss=[]; + $goodslist_read=[]; + $readindex=0; + $v_goods=null; + foreach ($all_order_list as $order) { + if(!$v_goods){ + $v_goods=GoodsList::field('id,prize_code,is_fd')->where(['id'=>$order['goodslist_id']])->find(); + } + + if($v_goods['is_fd']==1){ + $orders[]=[ + 'id'=>$order['id'], + 'recovery_num'=>$order['user_id'].'FD'."|".$order['id'], + 'status'=>4, + 'choice_time'=>time() + ]; + if(count($goodslist_read)==0){ + + $goodslist_xz = GoodsList::where(['xz_pid' => $v_goods['prize_code']]) + ->order('pro asc') + ->select()->toArray(); + + foreach ($goodslist_xz as $key => &$value1) { + for ($i = 0; $i < $value1['pro']; $i++) { + $goodslist_read[]=$value1; + } + } + shuffle($goodslist_read); + shuffle($goodslist_read); + } + $read_goods=$goodslist_read[$readindex]; + $readindex++; + $goodss[] = [ + 'order_id' => 0, + 'user_id' => $order['user_id'], + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' =>$read_goods['goods_id'], + 'num' => 0, + 'shang_id' => $read_goods['shang_id'], + 'goodslist_id' => $read_goods['id'], + 'goodslist_title' => $read_goods['title'], + 'goodslist_imgurl' => $read_goods['imgurl'], + 'goodslist_price' => $read_goods['price'], + 'goodslist_money' => $read_goods['money'], + 'goodslist_type' => $read_goods['goods_type'], + 'goodslist_sale_time' => $read_goods['sale_time'], + 'addtime' => time(), + 'prize_code' => $read_goods['prize_code'], + 'order_type' => 10, + 'order_list_id' => 0, + 'xz_id'=>$order['id'] + ]; + } + + } + + $orderList=new OrderList; + $res[]=$orderList->saveAll($orders); + + $res[] =OrderList::insertAll($goodss); + + }else{ + $res[] = 1; + } + return $res; + } + /** + * 特殊奖品开奖 FIRST + * @param $order 订单信息 + * @param $first_count + */ + protected function special_first_notice($order, $first_count) + { + $res = []; + $goods_id = $order['goods_id']; + $num = $order['num']; + $order_type = $order['order_type']; + #特殊奖品存在 + $special_prize = GoodsList::where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('surplus_stock', '>', 0) + ->where('shang_id', '=', self::$shang_give_first_id) + ->find(); + #剩余数量 + $surplus_give_stock = $special_prize ? $special_prize['surplus_stock'] : 0; + if ($special_prize && $surplus_give_stock > 0) { + #所有奖品信息 + $all_order_list = OrderList::where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('order_type', '=', $order_type) + ->where('shang_id', 'between', self::$shang_prize_id) + ->order('id asc') + ->limit((int) $first_count) + ->select()->toArray(); + shuffle($all_order_list); + shuffle($all_order_list); + $prize_info = $all_order_list[0]; + $user_id = $prize_info['user_id']; + #特殊赏中奖订单id + $order_list_id = $prize_info['id']; + #中奖奖项 + $order_goods = [ + 'order_id' => 0, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => $num, + 'shang_id' => $special_prize['shang_id'], + 'goodslist_id' => $special_prize['id'], + 'goodslist_title' => $special_prize['title'], + 'goodslist_imgurl' => $special_prize['imgurl'], + 'goodslist_price' => $special_prize['price'], + 'goodslist_money' => $special_prize['money'], + 'goodslist_type' => $special_prize['goods_type'], + 'goodslist_sale_time' => $special_prize['sale_time'], + 'addtime' => time(), + 'prize_code' => $special_prize['prize_code'], + 'order_type' => $order_type, + 'order_list_id' => $order_list_id, + 'is_fd'=>$special_prize['is_fd'] + ]; + #增加销量 + $res[] = GoodsList::field('surplus_stock') + ->where(['id' => $special_prize['id']]) + ->dec('surplus_stock') + ->update(); + #新增奖品列表 + $res[] = OrderList::insert($order_goods); + } else { + $res[] = 1; + } + return $res; + } + + /** + * 特殊奖品开奖 LAST 最终赏 全局赏 //备注这个位置全局赏开奖啦 + * @param $order 订单信息 + * @param $first_count + */ + protected function special_prize_notice($order, $first_count) + { + // $res = []; +// $goods_id = $order['goods_id']; +// $num = $order['num']; +// $order_type = $order['order_type']; +// #盒子 +// $goods_info = Goods::field('prize_num,stock')->where(['id' => $goods_id])->find(); +// #特殊奖品存在where('goods_id', '=', $goods_id) +// $special_prize = GoodsList::where('goods_id', '=', $goods_id) +// ->where('num', '=', $num) +// ->where('surplus_stock', '>', 0) +// ->where('shang_id', '=', self::$shang_give_quan_id) +// ->order('prize_num desc') +// ->select(); +// // dd($special_prize); + + // foreach ($special_prize as $value){ +// $surplus_give_stock = $value ? $value['surplus_stock'] : 0; +// // dd($value); +// if ($value && $surplus_give_stock > 0) { +// #符合条件的订单 +// $quan_prize_num = $value['prize_num']; +// $prize_num_data = OrderList::field('id,user_id,count(`id`) as all_num') +// ->where('goods_id', '=', $goods_id) +// ->where('num', '=', $num) +// ->where('order_type', '=', $order_type) +// ->where('shang_id', '=', self::$shang_give_w_id) +// ->having('all_num', '>=', $quan_prize_num) +// ->group('user_id') +// ->select()->toArray(); +// // dd($quan_prize_num); +// if ($prize_num_data) { +// #多个随机 +// shuffle($prize_num_data); +// $quan_prize_info = $prize_num_data[0]; +// $user_id = $quan_prize_info['user_id']; +// $order_goods = [ +// 'order_id' => 0, +// 'user_id' => $user_id, +// 'status' => 0,#0未操作 1选择兑换 2选择发货 +// 'goods_id' => $goods_id, +// 'num' => $num, +// 'shang_id' => $value['shang_id'], +// 'goodslist_id' => $value['id'], +// 'goodslist_title' => $value['title'], +// 'goodslist_imgurl' => $value['imgurl'], +// 'goodslist_price' => $value['price'], +// 'goodslist_money' => $value['money'], +// 'goodslist_type' => $value['goods_type'], +// 'goodslist_sale_time' => $value['sale_time'], +// 'addtime' => time(), +// 'prize_code' => $value['prize_code'], +// 'order_type' => $order_type, +// 'order_list_id' => 0, +// ]; +// #新增奖品列表 +// $res[] = OrderList::insert($order_goods); +// #减少库存 +// $res[] = GoodsList::field('surplus_stock') +// ->where(['id' => $special_prize['id']]) +// ->dec('surplus_stock') +// ->update(); +// } +// } +// #赏品 +// $goods_list_zd = GoodsList::where(['goods_id' => $goods_id]) +// ->where(['num' => 1]) +// ->select()->toArray(); +// if ($goods_list_zd) { +// #循环数据 +// $save_sports_data = []; +// $start_num = $goods_info['stock'] + 1; +// for ($i = $start_num; $i <= $start_num; $i++) { +// foreach ($goods_list_zd as $k => $v) { +// unset($v['id']); +// unset($v['num']); +// $v['num'] = $i; +// $v['surplus_stock'] = $v['stock']; +// $save_sports_data[] = $v; +// } +// } +// #添加赏品 +// $res[] = GoodsList::insertAll($save_sports_data); +// #增加库存 +// $res[] = Goods::where(['id' => $goods_id])->inc('stock', 1)->update(); +// } +// } +// return $res; + $res = []; + $goods_id = $order['goods_id']; + + $flwgoods= Goods::where(['id' => $goods_id])->find(); + if($flwgoods['is_flw']==1){ + Log::info('拦截福利屋直接开箱' . $goods_id); + $res[] = 1; + return $res; + } + + $num = $order['num']; + $order_type = $order['order_type']; + #特殊奖品存在 + $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; + + if($v['is_xz']==1&&($v['is_hgxz']==0&&$v['is_fd']==0)){ + + + $goodslist_xz = GoodsList::where(['xz_pid' => $v['prize_code']]) + ->order('pro asc') + ->select()->toArray(); + $randomValue = mt_rand(1, 100); // 生成0到100之间的随机数 + $nowpro=0; + $read_goods=[]; + foreach ($goodslist_xz as $key => &$value1) { + $nowpro+=$value1['pro']; + if($randomValue<$nowpro){ + $read_goods=$value1; + Db::name('error_log')->insert([ + 'user_id' => '33', + 'goods_id' => '44', + 'addtime' => time(), + 'content'=>'支付宝回调触发抽取概率'.$randomValue.',累计概率'.$nowpro.',本商品概率'.$value1['pro'].',本商品名称'.$value1['title'].',抽取id'.$user_id, + 'trade_no' => '55' + + ]); + + $randomValue=99999; + } + } + + $order_goods[] = [ + 'order_id' => 0, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => $num, + 'shang_id' => $read_goods['shang_id'], + 'goodslist_id' => $read_goods['id'], + 'goodslist_title' => $read_goods['title'], + 'goodslist_imgurl' => $read_goods['imgurl'], + 'goodslist_price' => $read_goods['price'], + 'goodslist_money' => $read_goods['money'], + 'goodslist_type' => $read_goods['goods_type'], + 'goodslist_sale_time' => $read_goods['sale_time'], + 'addtime' => time(), + 'prize_code' => $read_goods['prize_code'], + 'order_type' => $order_type, + 'order_list_id' => $order_list_id, + 'is_fd'=>$read_goods['is_fd'] + ]; + + }else{ + $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, + 'is_fd'=>$ordinary_prize_info['is_fd'] + ]; + } + #减少库存 + $res[] = GoodsList::field('surplus_stock') + ->where(['id' => $ordinary_prize_info['id']]) + ->dec('surplus_stock') + ->update(); + } + } else { + if ($v['shang_id'] == 2) {#LAST赏 + $last_order_list = array_slice($all_order_list, (int) $first_count); + shuffle($last_order_list); + shuffle($last_order_list); + $prize_info = $last_order_list[0]; + #中奖用户id + $user_id = $prize_info['user_id']; + #特殊赏中奖订单id + $order_list_id = $prize_info['id']; + #中奖奖项 + $ordinary_prize_info = $v; + } elseif ($v['shang_id'] == 3) {#最终赏 + $end_order_list = $all_order_list; + $prize_info = end($end_order_list); + #中奖用户id + $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, + 'is_fd'=>$ordinary_prize_info['is_fd'] + ]; + #减少库存 + $res[] = GoodsList::field('surplus_stock') + ->where(['id' => $ordinary_prize_info['id']]) + ->dec('surplus_stock') + ->update(); + } + } + #新增奖品列表 + $res[] = OrderList::insertAll($order_goods); + } else { + $res[] = 1; + } + return $res; + } + + /** + * 无限赏开始抽赏====================================================== + * @param int $user_id 会员id + * @param int $order_id 订单ID + * @param int $box_id 盲盒id + */ + public function infinite_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0, $num = 0) + { + // if() + $res = []; + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => $num]) + ->where(['status' => 0]) + ->find(); + if ($order == null && $num == 0) { + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => 1]) + ->where(['status' => 0]) + ->find(); + if ($order != null) { + $order_type1 = $order['order_type']; + if ($order_type1 == 10) { + $num = 1; + } + } + } + if ($order) { + #改变状态 + $res[] = Order::field('status,pay_time') + ->where(['id' => $order['id']]) + ->update([ + 'status' => 1, + 'pay_time' => time(), + ]); + #扣余额 + if ($order['use_money'] > 0) { + $res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']); + } + #扣星岚币 + if ($order['use_integral'] > 0) { + $res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']); + } + + #判断一下优惠券 + if (!empty($order['coupon_id'])) { + $coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]); + } + + #分销奖励 + $res[] = User::distribution($order); + + #分销奖励 + #开奖================================================== + if ($num == 1) { + $res[] = $this->infinite_shangchengshang($order); + } else { + $res[] = $this->infinite_drawprize($order); + } + + #开奖================================================== + #判断是否发积分 发券 + $res[] = User::is_integral_coupon($order); + #判断是否发积分 + + //升级欧气值 + if ($order['price'] > 0) { + User::ou_qi_level_up($order['user_id'], $order['price']); + } + + } else { + $res[] = 0; + } + + return $res; + } + + /** + * 无限赏开奖逻辑 + * @param $order 订单信息 + */ + protected function infinite_drawprize($order = []) + { + $user_id = $order['user_id'];#用户ID + $order_id = $order['id'];#订单ID + $goods_id = $order['goods_id'];#盒子ID + $prize_num = $order['prize_num'];#抽奖数量 + $order_type = $order['order_type'];#订单类型 + $whe = []; + $whe[] = ['id', '=', $order['goods_id']]; + $infinite_goods = Goods::getInfo($whe, 'type'); + + $where = []; + $where[] = ['goods_id', '=', $goods_id]; + $where[] = ['num', '=', 0]; + $where[] = ['real_pro', '>', 0]; + if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) { + //秘宝池 + $where[] = ['lian_ji_type', '=', 1]; + } else { + //普通 + $where[] = ['lian_ji_type', '=', 0]; + } + + #查找奖品 + $goodslist = GoodsList::field('id,shang_id,real_pro') + ->where($where) + ->select()->toArray(); + if ($goodslist) { + #组合中奖商品 + $all_goods_id = []; + foreach ($goodslist as $value) { + $real_pro = $value['real_pro'] * 10000; + for ($i = 1; $i <= $real_pro; $i++) { + $all_goods_id[] = $value['id']; + } + } + + for ($i = 0; $i < $prize_num; $i++) { + #随机打乱 + shuffle($all_goods_id); + shuffle($all_goods_id); + $prize_id = $all_goods_id[0]; + $prize_info = GoodsList::where(['id' => $prize_id])->find(); + #编号 + $luck_no = OrderList::field('id') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', 0) + ->where('order_type', '=', $order_type) + ->order('id desc') + ->value('luck_no'); + $luck_no++; + #新增记录 + $save_prize_info = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => 0, + 'shang_id' => $prize_info['shang_id'], + 'goodslist_id' => $prize_info['id'], + 'goodslist_title' => $prize_info['title'], + 'goodslist_imgurl' => $prize_info['imgurl'], + 'goodslist_price' => $prize_info['price'], + 'goodslist_money' => $prize_info['money'], + 'goodslist_type' => $prize_info['goods_type'], + 'goodslist_sale_time' => $prize_info['sale_time'], + 'addtime' => time(), + 'prize_code' => $prize_info['prize_code'], + 'order_type' => $order_type, + 'luck_no' => $luck_no, + 'is_fd'=>$prize_info['is_fd'] + ]; + #入库=== + $res[] = OrderList::insert($save_prize_info); + } + + //去除秘宝池次数 + if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) { + User::where('id', $user_id)->dec('mb_number', $prize_num)->update(); + } + + //计算怒气值 + $this->rage($goods_id, $order_id, $user_id); + + //领主 + $this->ling_zhu($order, $order_id); + + //连击赏 + if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 0) { + $this->lian_ji($order, $order_id); + } + + } else { + $res[] = 0; + } + return $res; + + } + + /** + * 无限赏开奖逻辑 + * @param $order 订单信息 + */ + protected function infinite_shangchengshang($order = []) + { + $user_id = $order['user_id'];#用户ID + $order_id = $order['id'];#订单ID + $goods_id = $order['goods_id'];#盒子ID + $prize_num = $order['prize_num'];#抽奖数量 + $order_type = $order['order_type'];#订单类型 + $whe = []; + $whe[] = ['id', '=', $order['goods_id']]; + $infinite_goods = Goods::getInfo($whe, 'type'); + $num = 1; + $where = []; + $where[] = ['goods_id', '=', $goods_id]; + $where[] = ['num', '=', $num]; + #查找奖品 + $goodslist = GoodsList::field('id,shang_id,real_pro,surplus_stock') + ->where($where) + ->select()->toArray(); + if ($goodslist) { + #组合中奖商品 + $all_goods_id = []; + foreach ($goodslist as $value) { + $surplus_stock = $value['surplus_stock']; + for ($i = 1; $i <= $surplus_stock; $i++) { + $all_goods_id[] = $value['id']; + } + } + + for ($i = 0; $i < $prize_num; $i++) { + + $prize_id = $all_goods_id[0]; + $prize_info = GoodsList::where(['id' => $prize_id])->find(); + $prize_info_surplus_stock = $prize_info['surplus_stock']; + $prize_info['surplus_stock'] = $prize_info_surplus_stock - 1; + if ($prize_info_surplus_stock < 0) { + $res[] = 0; + return; + } + $prize_info->save(); + #编号 + $luck_no = OrderList::field('id') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', $num) + ->where('order_type', '=', $order_type) + ->order('id desc') + ->value('luck_no'); + $luck_no++; + #新增记录 + $save_prize_info = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => $num, + 'shang_id' => $prize_info['shang_id'], + 'goodslist_id' => $prize_info['id'], + 'goodslist_title' => $prize_info['title'], + 'goodslist_imgurl' => $prize_info['imgurl'], + 'goodslist_price' => $prize_info['price'], + 'goodslist_money' => $prize_info['money'], + 'goodslist_type' => $prize_info['goods_type'], + 'goodslist_sale_time' => $prize_info['sale_time'], + 'addtime' => time(), + 'prize_code' => $prize_info['prize_code'], + 'order_type' => $order_type, + 'luck_no' => $luck_no, + 'is_fd'=>$prize_info['is_fd'] + ]; + #入库=== + $res[] = OrderList::insert($save_prize_info); + } + + //去除秘宝池次数 + if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) { + User::where('id', $user_id)->dec('mb_number', $prize_num)->update(); + } + + //计算怒气值 + $this->rage($goods_id, $order_id, $user_id); + + //领主 + $this->ling_zhu($order, $order_id); + + //连击赏 + if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 0) { + $this->lian_ji($order, $order_id); + } + + } else { + $res[] = 0; + } + return $res; + + } + + //计算怒气值 + public function rage($goods_id, $order_id, $user_id) + { + $order_list = OrderList::field('id')->where(['order_id' => $order_id])->find(); + if (!$order_list) { + return false; + } + try { + $goods = Goods::field('id')->field('rage_is,rage,item_card_id')->where(['id' => $goods_id])->find(); + if ($goods['rage_is'] == 1 && $goods['rage'] > 0 && $goods['item_card_id'] > 0) { + $order = Order::field('order_total')->where(['id' => $order_id])->find(); + if ($order['order_total'] > 0) { + //获取奖品总价值 + $out_price_sum = OrderList::where(['order_id' => $order_id])->sum('goodslist_money'); + $rage_number = bcsub($order['order_total'], "$out_price_sum", 2); + $user_rage = Db::name('user_rage')->field('id,rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->find(); + + if ($user_rage) { + $rage_number2 = bcadd((string) ($user_rage['rage']), "$rage_number", 2); + if ($rage_number2 >= $goods['rage']) { + //赠送道具卡 + $item_card = ItemCard::field('id,title')->where(['id' => $goods['item_card_id']])->find(); + if ($item_card) { + Db::name('user_item_card')->insert(['user_id' => $user_id, 'item_card_id' => $item_card['id'], 'title' => $item_card['title'], 'status' => 1, 'addtime' => time(), 'updatetime' => time()]); + } + Db::name('user_rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->update(['rage' => 0, 'updatetime' => time()]); + } else { + Db::name('user_rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->update(['rage' => $rage_number2, 'updatetime' => time()]); + } + + } else { + Db::name('user_rage')->insert(['user_id' => $user_id, 'goods_id' => $goods_id, 'rage' => $rage_number, 'addtime' => time(), 'updatetime' => time()]); + } + } + } + } catch (\Exception $e) { + + } + + } + + //领主 + public function ling_zhu($order, $order_id) + { + //是否开启领主模式 + $whe = []; + $whe[] = ['id', '=', $order['goods_id']]; + $infinite_goods = Goods::getInfo($whe, 'type,lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id'); + if ($infinite_goods['type'] != 8) { + return false; + } + + if ($infinite_goods['lingzhu_is'] == 1) { + $whe2 = []; + $whe2[] = ['order_id', '=', $order_id]; + $order_list = OrderList::getAllList($whe2, 'id,shang_id,user_id,goods_id', 'id asc'); + foreach ($order_list as $k => $v) { + if ($v['shang_id'] == $infinite_goods['lingzhu_shang_id']) { + // 查找当前池子是否有领主 + if ($infinite_goods['king_user_id'] != 0) { + Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->update(['end_time' => time()]); + } + + //新的领主 + Goods::where(['id' => $v['goods_id']])->update(['king_user_id' => $v['user_id']]); + //多少发晋升领主 + $luck_no = OrderList::where([['goods_id', '=', $v['goods_id']], ['id', '<=', $v['id']]])->count(); + $rank = [ + 'user_id' => $v['user_id'], + 'goods_id' => $v['goods_id'], + 'count' => $luck_no, + 'order_list_id' => $v['id'], + 'addtime' => time(), + ]; + + $result = Db::name('goods_king_rank')->insert($rank); + } else { + // 查找当前池子是否有领主 + $infinite_goods = Goods::getInfo($whe, 'lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id'); + + if ($infinite_goods['king_user_id'] != 0) { + + Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->inc('z_nums', 1)->update(); + $king_money = $infinite_goods['lingzhu_fan']; + + if ($king_money && $king_money > 0) { + + Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->inc('money', floatval($king_money))->update(); + + User::changeIntegral($infinite_goods['king_user_id'], $king_money, 4, '领主收益'); + } + + } + } + } + } + } + + //连击赏 + public function lian_ji($order, $order_id) + { + //是否是连击赏 + $whe = []; + $whe[] = ['id', '=', $order['goods_id']]; + $infinite_goods = Goods::getInfo($whe, 'type,lian_ji_num,lian_ji_shang_id'); + if ($infinite_goods['type'] != 9) { + return false; + } + if ($order['is_mibao'] == 1) { + //减少秘宝池次数 + User::field('mb_number')->where(['id' => $order['user_id']])->dec('mb_number', $order['prize_num'])->update(); + } + $whe2 = []; + $whe2[] = ['order_id', '=', $order_id]; + $order_list = OrderList::getAllList($whe2, 'id,shang_id,goods_id,user_id', 'id asc'); + foreach ($order_list as $k => $v) { + //是否已有连击 + $user_goods_lian_ji = Db::name('user_goods_lian_ji')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->find(); + + if ($v['shang_id'] == $infinite_goods['lian_ji_shang_id']) { + + if ($infinite_goods['lian_ji_num'] != 0 && $infinite_goods['lian_ji_shang_id'] != 0) { + + if ($user_goods_lian_ji) { + Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->inc('number', 1)->update(['updatetime' => time()]); + //连击次数是否满足条件 + $lian_ji_number = Db::name('user_goods_lian_ji')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->limit(1)->value('number'); + if ($lian_ji_number && $lian_ji_number >= $infinite_goods['lian_ji_num']) { + //赠送秘宝池次数 + User::field('mb_number')->where(['id' => $v['user_id']])->inc('mb_number', 1)->update(); + //清空连击次数 + Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->update(['number' => 0, 'updatetime' => time()]); + } + } else { + Db::name('user_goods_lian_ji')->insert(['user_id' => $v['user_id'], 'goods_id' => $v['goods_id'], 'number' => 1, 'addtime' => time(), 'updatetime' => time()]); + } + } + } else { + if ($user_goods_lian_ji) { + Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->update(['number' => 0, 'updatetime' => time()]); + } else { + Db::name('user_goods_lian_ji')->insert(['user_id' => $v['user_id'], 'goods_id' => $v['goods_id'], 'number' => 0, 'addtime' => time(), 'updatetime' => time()]); + } + } + } + + } + + /** + * 抽卡机抽奖逻辑 + * @param int $user_id + * @param int $order_id + * @param int $goods_id + */ + public function cardextractor_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0) + { + $res = []; + // dd(11); + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => 0]) + ->where(['status' => 0]) + ->where(['order_type' => 4]) + ->find(); + // dd($order); + if ($order) { + #改变状态 + $res[] = Order::field('status,pay_time') + ->where(['id' => $order['id']]) + ->update([ + 'status' => 1, + 'pay_time' => time(), + ]); + #扣余额 + if ($order['use_money'] > 0) { + $res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']); + } + #扣星岚币 + if ($order['use_integral'] > 0) { + $res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']); + } + #判断一下优惠券 + if (!empty($order['coupon_id'])) { + $coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]); + } + #判断是否发积分 发券 + $res[] = User::is_integral_coupon($order); + #判断是否发积分 + + #分销奖励 + $res[] = User::distribution($order); + #分销奖励 + + #开奖================================================== + $res[] = $this->cardextractor_drawprize($order); + #开奖================================================== + + } else { + $res[] = 0; + } + return $res; + } + + /** + * 抽卡机开奖 + * @param array $order + */ + public function cardextractor_drawprize($order = []) + { + $user_id = $order['user_id'];#用户ID + $order_id = $order['id'];#订单ID + $goods_id = $order['goods_id'];#盒子ID + $prize_num = $order['prize_num'];#抽奖数量 + $order_type = $order['order_type'];#订单类型 + $prize_card_set = $order['prize_card_set'] ? json_decode($order['prize_card_set'], true) : '';#抽卡机必出设置 + // dd($order); + #排除必出等级卡条件 + $shang_where = []; + // dd(11111); + #必出卡等级 + $set_prize_shang = []; + if ($prize_card_set && isset($prize_card_set['shang_id']) && isset($prize_card_set['shang_count'])) { + + // dd($prize_card_set['shang_count']); + // foreach ($prize_card_set as $set_key => $set_value) { + + for ($seti = 1; $seti <= $prize_card_set['shang_count']; $seti++) { + $set_prize_shang[] = $prize_card_set['shang_id']; + } + // } + // dd($set_prize_shang); + + #排除必出等级卡条件 + $shang_where[] = ['id', 'not in', array_unique($set_prize_shang)]; + #减去必出数量 + $prize_num = $prize_num - count($set_prize_shang); + } + + // dd($shang_where); + #卡等级随机 + $shang = CardLevel::field('id,title') + // ->where('goods_id', '=', $goods_id) +// ->where('pro', '>', 0) + ->where($shang_where) + ->select()->toArray(); + // dd(1111) + // dd($shang); + if ($shang) { + + #卡等级随机 + $all_shang_id = []; + foreach ($shang as $shang_value) { + // $real_shang_pro = $shang_value['pro'] * 100; +// for ($is = 1; $is <= $real_shang_pro; $is++) { + $goods = GoodsList::where('shang_id', $shang_value['id'])->where('goods_id', '=', $goods_id)->find(); + if ($goods) { + $all_shang_id[] = $shang_value['id']; + } + + // } + } + // dd($all_shang_id); + #中奖卡等级 + $prize_shang = []; + for ($i = 0; $i < $prize_num; $i++) { + #随机打乱 + shuffle($all_shang_id); + shuffle($all_shang_id); + $prize_shang[] = $all_shang_id[0]; + } + // dd($prize_shang); + $prize_shang = array_merge($prize_shang, $set_prize_shang); + shuffle($prize_shang); + $save_prize_data = []; + // dd($prize_shang); + #根据卡等级中奖奖品 + foreach ($prize_shang as $prize_shang_id) { + #查找奖品 + $goodslist = GoodsList::field('id,shang_id,real_pro,special_stock') + ->where('goods_id', '=', $goods_id) + ->where('shang_id', '=', $prize_shang_id) + ->where('num', '=', 0) + ->where('real_pro', '>', 0) + ->where('special_stock = -100 OR special_stock > 0') + ->select()->toArray(); + if (empty($goodslist)) { + $res[] = 0; + return $res; + } + // dd($goodslist); + if ($goodslist) { + #组合中奖商品 + $all_goods_id = []; + foreach ($goodslist as $value) { + $real_pro = $value['real_pro'] * 100; + for ($i = 1; $i <= $real_pro; $i++) { + $all_goods_id[] = $value['id']; + } + } + #随机打乱 + shuffle($all_goods_id); + shuffle($all_goods_id); + $prize_id = $all_goods_id[0]; + $prize_info = GoodsList::where(['id' => $prize_id])->find(); + $save_prize_data[] = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => 0, + 'shang_id' => $prize_info['shang_id'], + 'goodslist_id' => $prize_info['id'], + 'goodslist_title' => $prize_info['title'], + 'goodslist_imgurl' => $prize_info['imgurl'], + 'goodslist_price' => $prize_info['price'], + 'goodslist_money' => $prize_info['money'], + 'goodslist_type' => $prize_info['goods_type'], + 'goodslist_sale_time' => $prize_info['sale_time'], + 'addtime' => time(), + 'prize_code' => $prize_info['prize_code'], + 'order_type' => $order_type, + 'is_fd'=>$prize_info['is_fd'] + ]; + if ($prize_info['special_stock'] >= 1) { + #减少库存 + $res[] = GoodsList::field('special_stock') + ->where(['id' => $prize_info['id']]) + ->dec('special_stock') + ->update(); + } + } else { + $res[] = $prize_shang_id; + } + } + #入库=== + $res[] = OrderList::insertAll($save_prize_data); + } else { + $res[] = 0; + } + return $res; + } + + /** + * 背包发货订单处理 + * @param int $user_id 用户id + * @param int $order_id 订单id + */ + public function reward_order_handle($user_id = 0, $order_id = 0) + { + $res = []; + $send_info = OrderListSend::where(['user_id' => $user_id]) + ->where('user_id', '=', $user_id) + ->where('id', '=', $order_id) + ->where('status', '=', 0)#0待支付 1待发货 2待收货 3已完成 + ->find(); + if ($send_info) { + #改变订单状态 + $res[] = OrderListSend::field('id,status,pay_time') + ->where('id', '=', $order_id) + ->update([ + 'status' => 1,#0待支付 1待发货 2待收货 3已完成 + 'pay_time' => time(),#支付时间 + ]); + #改变赏品信息 + $res[] = OrderList::field('id,status,send_num,choice_time') + ->where('user_id', '=', $user_id) + ->where('status', '=', 0) + ->where('goodslist_type', '=', 1) + ->where('send_num', '=', $send_info['send_num']) + ->update([ + 'status' => 2, + 'choice_time' => time(), + ]); + } else { + $res[] = 0; + } + return $res; + } + + + + + public function order_notify7() + { + $testxml = file_get_contents("php://input"); + $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA)); + $result = json_decode($jsonxml, true);//转成数组, +// wri($result['out_trade_no'], '秒杀商城回调--' . $result['out_trade_no']); + if ($result) { + //如果成功返回了 + $out_trade_no = $result['out_trade_no']; + if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') { + // writelog($out_trade_no, "秒杀商城支付成功--"); + Db::startTrans(); + $res[] = $this->order_update($out_trade_no, 7); + if (resCheck($res)) { + Db::commit(); + } else { + Db::rollback(); + } + } + } + $html = ""; + die($html); + } + + public static function order_update($order_num, $type) + { + $res = []; + $order_info = Db::name('kk_order') + ->where(['order_no' => $order_num]) + ->where(['status' => 0])#0待付款 1待发货 2待收货 3确认收货 + ->find(); + if ($order_info) { + #改变订单状态 + $res[] = Db::name('kk_order')->field('id,status,pay_time') + ->where(['id' => $order_info['id']]) + ->update([ + 'status' => 1,#0待付款 1待发货 2待收货 3确认收货 + 'pay_time' => time(),#支付时间 + ]); + if ($order_info['money'] > 0) { + // dd($order_info['user_id']); + User::changeMoney($order_info['user_id'], '-' . $order_info['money'], 7); + } + if ($order_info['integral'] > 0) { + User::changeIntegral($order_info['user_id'], '-' . $order_info['integral'], 4); + } + $goods_data = Db::name('kk_order_good') + ->field('id,goods_id,goods_num,goods_spec_id') + ->where(['order_id' => $order_info['id']]) + ->select()->toArray(); + foreach ($goods_data as $k => $v) { + #加销量 + Db::name('kk_product') + ->field('id,sale_num') + ->where('id', $v['goods_id']) + ->inc('sale_num', $v['goods_num']) + ->update(); + #减库存 + Db::name('kk_product_spec') + ->field('id,stock') + ->where('id', $v['goods_spec_id']) + ->dec('stock', $v['goods_num']) + ->update(); + } + } else { + $res[] = 0; + } + return $res; + + } + + /** + * 抽奖券====================================================== + * @param int $user_id 会员id + * @param int $order_id 订单ID + * @param int $box_id 盲盒id + */ + public function draw_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0) + { + $res = []; + + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => 0]) + ->where(['status' => 0]) + ->where(['order_type' => 7]) + ->find(); + if ($order) { + #改变状态 + $res[] = Order::field('status,pay_time') + ->where(['id' => $order['id']]) + ->update([ + 'status' => 1, + 'pay_time' => time(), + ]); + #扣抽奖券 + if ($order['use_draw'] > 0) { + $res[] = User::changeDraw($order['user_id'], -$order['use_draw'], 3, '抽奖券消费' . $order['goods_title']); + } + #开奖================================================== + $res[] = $this->draw_drawprize($order); + #开奖================================================== + } else { + $res[] = 0; + } + + return $res; + } + + /** + * 无限赏开奖逻辑 + * @param $order 订单信息 + */ + protected function draw_drawprize($order = []) + { + + $user_id = $order['user_id'];#用户ID + $order_id = $order['id'];#订单ID + $goods_id = $order['goods_id'];#盒子ID + $prize_num = $order['prize_num'];#抽奖数量 + $order_type = $order['order_type'];#订单类型 + + #查找奖品 + $goodslist = GoodsList::field('id,shang_id,real_pro') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', 0) + ->where('real_pro', '>', 0) + ->select()->toArray(); + if ($goodslist) { + #组合中奖商品 + $all_goods_id = []; + foreach ($goodslist as $value) { + $real_pro = $value['real_pro'] * 100; + for ($i = 1; $i <= $real_pro; $i++) { + $all_goods_id[] = $value['id']; + } + } + for ($i = 0; $i < $prize_num; $i++) { + #随机打乱 + shuffle($all_goods_id); + shuffle($all_goods_id); + $prize_id = $all_goods_id[0]; + $prize_info = GoodsList::where(['id' => $prize_id])->find(); + #编号 + $luck_no = OrderList::field('id') + ->where('goods_id', '=', $goods_id) + ->where('num', '=', 0) + ->where('order_type', '=', $order_type) + ->order('id desc') + ->value('luck_no'); + $luck_no++; + #新增记录 + $save_prize_info = [ + 'order_id' => $order_id, + 'user_id' => $user_id, + 'status' => 0,#0未操作 1选择兑换 2选择发货 + 'goods_id' => $goods_id, + 'num' => 0, + 'shang_id' => $prize_info['shang_id'], + 'goodslist_id' => $prize_info['id'], + 'goodslist_title' => $prize_info['title'], + 'goodslist_imgurl' => $prize_info['imgurl'], + 'goodslist_price' => $prize_info['price'], + 'goodslist_money' => $prize_info['money'], + 'goodslist_type' => $prize_info['goods_type'], + 'goodslist_sale_time' => $prize_info['sale_time'], + 'addtime' => time(), + 'prize_code' => $prize_info['prize_code'], + 'order_type' => $order_type, + 'luck_no' => $luck_no, + 'is_fd'=>$prize_info['is_fd'] + ]; + #入库=== + $res[] = OrderList::insert($save_prize_info); + } + } else { + $res[] = 0; + } + return $res; + + } + + /** + * 道具卡====================================================== + * @param int $user_id 会员id + * @param int $order_id 订单ID + * @param int $box_id 盲盒id + */ + public function item_card_notice($user_id = 0, $order_id = 0, $goods_id = 0) + { + $res = []; + + $order = Order::where(['id' => $order_id]) + ->where(['user_id' => $user_id]) + ->where(['goods_id' => $goods_id]) + ->where(['num' => 0]) + ->where(['status' => 0]) + ->find(); + if ($order) { + #改变状态 + $res[] = Order::field('status,pay_time') + ->where(['id' => $order['id']]) + ->update([ + 'status' => 1, + 'pay_time' => time(), + ]); + #扣道具卡 + if ($order['use_item_card'] > 0) { + $res[] = Db::name('user_item_card')->where(['user_id' => $user_id, 'status' => 1])->order('id asc')->limit($order['use_item_card'])->update(['status' => 2, 'order_id' => $order_id, 'updatetime' => time()]); + } + #开奖================================================== + $res[] = $this->draw_drawprize($order); + #开奖================================================== + } else { + $res[] = 0; + } + + return $res; + } + + public function wx_gf_fahuo($user_id, $order_num) + { + //微信官方发货 + $wxServer = new \app\common\server\Wx($this->app); + // //获取access_token + // $redis = new \Redis(); + // $redis->connect('127.0.0.1',6379); + // $weixinpay = getConfig('weixinpay'); + // $key = $weixinpay['appid'].'_access_token'; + // if(empty($redis->get($key))){ + // $access_token = $wxServer->get_access_token(); + // $redis->setEx($key,3600,$access_token); + // }else{ + // $access_token = $redis->get($key); + // } + $access_token = $wxServer->get_access_token(); + $open_id = Db::name('user')->where('id', $user_id)->value('openid'); + $wxServer->post_order($open_id, $access_token, $order_num); + } +} \ No newline at end of file diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index 2856525..c0ab2b2 100755 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -155,7 +155,7 @@ class Index extends Base $wxServer = new \app\common\server\Wx($this->app); $user_base = $wxServer->generateUrlLinks($userId); $autoload = new \app\common\server\autoload(); - $imageData = $autoload->generatePosterWithQR('/www/wwwroot/testbaji.onelight.vip/public/img_poster.jpg', $user_base); + $imageData = $autoload->generatePosterWithQR('/www/wwwroot/119.45.131.215_80/public/img_poster.jpg', $user_base); if ($imageData) { header('Content-Type: image/png'); header('Content-Length: ' . strlen($imageData)); // 设置图像长度,帮助浏览器处理流式内容 diff --git a/app/api/controller/Login.php b/app/api/controller/Login.php index 1408510..3bc4dc3 100755 --- a/app/api/controller/Login.php +++ b/app/api/controller/Login.php @@ -390,6 +390,7 @@ class Login extends Base $code = request()->param("code", ''); $wxServer = new \app\common\server\Wx($this->app); $mobile = $wxServer->getMobile($code); + // return $this->renderError($mobile,[$mobile,$code]); Db::startTrans(); $res = []; // $res[] = User::where(['id' => $user['id']])->update([ diff --git a/app/command/FlwOpen.php b/app/command/FlwOpen.php new file mode 100644 index 0000000..d2ebe68 --- /dev/null +++ b/app/command/FlwOpen.php @@ -0,0 +1,172 @@ +setName('FlwOpen')->setDescription('福利屋开奖'); + } + + 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); + } + + /** + * 特殊奖品开奖 LAST 最终赏 全局赏 到时间开奖修改此处 + * @param $order 订单信息 + * @param $first_count + */ + protected function special_prize_notice_time($goods_id) + { + + $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[] = OrderList::insertAll($order_goods); + Goods::where(['id' => $goods_id])->update([ + 'is_open' => 1 + ]); + Log::info('福利屋开奖$goods_idupdate_is_open' . $goods_id); + } else { + $res[] = 1; + } + return $res; + } + + +} diff --git a/app/command/Zd.php b/app/command/Zd.php index 05fbd77..67a9e9d 100755 --- a/app/command/Zd.php +++ b/app/command/Zd.php @@ -15,6 +15,7 @@ use think\facade\Db; class Zd extends Command { + //*/30 * * * * /www/server/cron/ZDFH protected function configure() { $this->setName('Zd')->setDescription('自动发货'); @@ -47,7 +48,7 @@ class Zd extends Command } // $data2 = Db::name('market_order')->field('id,user_id,order_num,zdfh_is,zdfh_time,addtime')->where('zdfh_is','in',[0,1])->select()->toArray(); - // if($data2){0. + // if($data2){ // foreach ($data2 as $value2){ // if($value2['zdfh_is'] == 0 && (time()-180) > $value2['addtime']){ // Db::name('market_order')->where('id',$value2['id'])->update(['zdfh_is'=>1,'zdfh_time'=>(time()+300)]); diff --git a/app/common/server/Wx.php b/app/common/server/Wx.php index c5606bf..1e80997 100755 --- a/app/common/server/Wx.php +++ b/app/common/server/Wx.php @@ -58,6 +58,7 @@ class Wx extends MyController $access_token = $this->get_access_token(); $mobile_url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $access_token; $mobile_info = $this->post_curl_data($mobile_url, ['code' => $code]); + // return $mobile_info; if ($mobile_info['errcode'] == 0) { return $mobile_info['phone_info']['phoneNumber']; } else { @@ -269,6 +270,11 @@ class Wx extends MyController public function post_order($openid, $access_token, $order_num, $title = '订单发货') { + $date = new \DateTime(); + //2023-08-07T17:16:31 + //2025-03-04T21:06:59 + // 格式化时间为 yyyy-MM-dd HH:mm:ss + $formattedDate = $date->format('Y-m-d\TH:i:s'); $request_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $access_token; $param = '{ "order_key": { @@ -276,14 +282,14 @@ class Wx extends MyController "mchid":"' . self::$mch . '", "out_trade_no":"' . $order_num . '" }, - "logistics_type": 4, + "logistics_type": 3, "delivery_mode": 1, "shipping_list": [ { "item_desc": "本单购买商品已发放至[小程序盒柜]" } ], - "upload_time": "2023-08-07T17:16:31+08:00", + "upload_time": "' . $formattedDate . '+08:00", "payer": { "openid":"' . $openid . '" } @@ -300,6 +306,12 @@ class Wx extends MyController public function post_order_one($openid, $access_token, $order_num, $title = '订单发货') { + // 创建一个 DateTime 对象,设置为当前时间 + $date = new \DateTime(); + //2023-08-07T17:16:31 + //2025-03-04T21:06:59 + // 格式化时间为 yyyy-MM-dd HH:mm:ss + $formattedDate = $date->format('Y-m-d\TH:i:s'); $request_url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $access_token; $param = '{ "order_key": { @@ -307,14 +319,14 @@ class Wx extends MyController "mchid":"' . self::$mch . '", "out_trade_no":"' . $order_num . '" }, - "logistics_type": 4, + "logistics_type": 3, "delivery_mode": 1, "shipping_list": [ { "item_desc": "本单购买商品已发放至[小程序盒柜]" } ], - "upload_time": "2023-08-07T17:16:31+08:00", + "upload_time": "' . $formattedDate . '+08:00", "payer": { "openid":"' . $openid . '" } diff --git a/app/common/server/autoload.php b/app/common/server/autoload.php index 0f7037c..05d6d5a 100644 --- a/app/common/server/autoload.php +++ b/app/common/server/autoload.php @@ -19,7 +19,7 @@ class autoload $backgroundHeight = imagesy($backgroundImage); // 生成二维码 - $qrSize = 500; + $qrSize = 850; $qrCode = new QrCode($qrText); $qrCode->setSize($qrSize); $writer = new PngWriter(); @@ -30,7 +30,7 @@ class autoload // 计算二维码的放置位置 $qrX = ($backgroundWidth - $qrSize) / 2; - $qrY = ($backgroundHeight - $qrSize) * 0.73; + $qrY = ($backgroundHeight - $qrSize) * 0.78; // 将二维码绘制到背景图上 imagecopy($backgroundImage, $qrImageResource, $qrX, $qrY, 0, 0, $qrSize, $qrSize); diff --git a/config/menu.php b/config/menu.php index 2faedfc..7872a63 100755 --- a/config/menu.php +++ b/config/menu.php @@ -64,10 +64,16 @@ return [ [ 'name' =>'数据统计', 'son' => [ + [ + 'url' => '/admin/statistics_dataStand', + 'name' => '数据看台', + ], [ 'url' => '/admin/statistics_profit', 'name' => '盒子利润统计', - ] + ], + + ], ], [ diff --git a/public/dow.png b/public/dow.png new file mode 100644 index 0000000..3831bbf Binary files /dev/null and b/public/dow.png differ diff --git a/public/img_poster.jpg b/public/img_poster.jpg new file mode 100644 index 0000000..f7ca56c Binary files /dev/null and b/public/img_poster.jpg differ