jiangchi(); //我今天已经领取的气泡,数字 $result['my_number'] = $this->get_my_number(); //我今天的应得气泡数量,扣除已经领掉的,最大4个 $qipao_count = $this->get_qipao_count() - count($result['my_number']); $qipao_count = $qipao_count > 4 ? 4 : $qipao_count ; $result['qipao_count'] = $qipao_count; $this->success('success',$result); } //收集所有气泡 public function open_qipao(){ //我今天的应得气泡数量 $qipao_count = $this->get_qipao_count(); //我今天打开的气泡数量 $my_count = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('user_id',$this->auth->id)->count(); if($my_count >= $qipao_count){ $this->error('没有气泡可以收集了'); } $week = [ 1 => 4, 2 => 4, 3 => 4, 4 => 4, 5 => 4, 6 => 4, 7 => 10, ]; $today_max = $week[date('N')]; if($my_count >= $today_max){ $this->error('今天最多能收集'.$today_max.'个气泡,已到上限'); } //一次收集完 $count = $qipao_count - $my_count; if($count > $today_max - $my_count){ $count = $today_max - $my_count; } for($i = 1;$i <= $count;$i++){ $data[] = [ 'user_id' => $this->auth->id, 'number' => $this->get_rand_number(), 'createtime' => time(), 'createdate' => date('Ymd'), ]; } Db::name('active_user_number')->insertAll($data); $this->success('已收集',$this->get_my_number()); } //开奖,计划任务 public function active_finish(){ $check = Db::name('active_log')->where('opendate',date('Ymd'))->find(); if(!empty($check)){ $this->error('今天已开奖'); } //中奖号码组。 $box_count_config = [ 1 => 1, 2 => 1, 3 => 2, 4 => 2, 5 => 3, 6 => 3, 7 => 4, ]; $box_count = $box_count_config[date('N')];//今天开N个 //所有已存在的不重复号码,打乱之后,取前N个 $box = Db::name('active_user_number')->where('createdate',date('Ymd'))->group('number')->column('number'); if(empty($box)){ $this->error('今天没有人参与'); } shuffle($box); $box_count = $box_count > count($box) ? count($box) : $box_count; $gift_number = array_chunk($box,$box_count)[0]; //中奖数组,不重复 $gift_number = implode(',',$gift_number); //中奖列表 $list = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('number','IN',$gift_number)->select(); $log_ids = array_column($list,'id');//id更新快一点 //每股中奖额度 $jiangchi = $this->jiangchi(); $price = bcdiv($jiangchi,count($list),0); //发奖结果集 $fajiang = []; foreach($list as $key => $val){ if(isset($fajiang[$val['user_id']])){ $fajiang[$val['user_id']] += 1; }else{ $fajiang[$val['user_id']] = 1; } } Db::startTrans(); //发奖 foreach($fajiang as $key => $val){ $gold = bcmul($val,$price,0); $rs = model('wallet')->lockChangeAccountRemain($key,0,'gold',$gold,91,'第'.date('Ymd').'期'); if($rs['status'] === false){ Db::rollback(); $this->error('开奖失败'); } } //修改状态 $rs = Db::name('active_user_number')->where('id','IN',$log_ids)->update(['gold'=>$price,'status'=>1,'updatetime'=>time()]); if($rs === false){ Db::rollback(); $this->error('开奖失败'); } //开奖记录 $log = [ 'gift_number' => $gift_number, 'jiangchi' => $jiangchi, 'giftcount' => count($list), 'price' => $price, 'usercount' => count($fajiang), 'opendate' => date('Ymd'), 'openweek' => $this->get_week(), 'createtime' => time(), ]; $log_id = Db::name('active_log')->insertGetId($log); if(!$log_id){ Db::rollback(); $this->error('开奖失败'); } Db::commit(); $this->success('开奖完成'); } //开奖结果,往期开奖记录 public function active_log(){ $list = Db::name('active_log')->order('id desc')->autopage()->select(); foreach($list as $key => &$val){ $val['opendate'] = '第'.$val['opendate'].'期'; } $this->success('success',$list); } //参与统计,三个数字 public function my_active_tongji(){ //中奖总额 $gold_sum = Db::name('user_gold_log')->where('user_id',$this->auth->id)->where('log_type',91)->sum('change_value'); //中奖次数 $gift_times = Db::name('user_gold_log')->where('user_id',$this->auth->id)->where('log_type',91)->count('id'); //参与次数 $join_times = Db::name('active_user_number')->where('user_id',$this->auth->id)->group('createdate')->count('id'); $rs = [ 'gold_sum' => $gold_sum, 'gift_times' => $gift_times, 'join_times' => $join_times, ]; $this->success('success',$rs); } //活动参与 public function my_active_log(){ //所有的期 $createdate = Db::name('active_user_number')->where('user_id',$this->auth->id)->group('createdate')->column('createdate'); $limit = input('listrow',10); $createdate = array_chunk($createdate,$limit); $page = input('page',1); if($page > count($createdate)){ $this->success(1,[]); } $page_qi = $createdate[$page-1]; //这一页的期 //开始 $result = []; foreach($page_qi as $key => $qi){ $qi_text = '第'.$qi.'期 '. $this->get_week(date('N',strtotime($qi))); $list = Db::name('active_user_number')->where('user_id',$this->auth->id)->where('createdate',$qi)->select(); $result[] = [ 'qi' => $qi_text, 'list' => $list, ]; } $this->success(1,$result); } //历史中奖 public function my_gold_log(){ $list = Db::name('user_gold_log')->where('user_id',$this->auth->id)->where('log_type',91)->autopage()->order('id desc')->select(); $this->success('success',$list); } //中奖榜 public function active_finish_result(){ $qi = date('Ymd',time()-86400); // $qi = '20241225'; $sql = Db::name('active_user_number')->field('user_id,sum(gold) as sum_gold')->where('createdate',$qi)->where('status',1)->group('user_id')->buildSql(); $list = Db::Table($sql)->alias('a')->field('user.avatar,user.nickname,a.user_id,a.sum_gold')->join('user','a.user_id = user.id','LEFT')->order('a.sum_gold desc')->select(); $qi_text = '第'.$qi.'期 '. $this->get_week(date('N',strtotime($qi))); $result = [ 'qi' => $qi_text, 'list' => $list, ]; $this->success(1,$result); } private function get_week($week = 8){ $arr = [ 0 => '周日', 1 => '周一', 2 => '周二', 3 => '周三', 4 => '周四', 5 => '周五', 6 => '周六', 7 => '周日', ]; if(!isset($arr[$week])){ $week = date('N'); } return $arr[$week]; } //我今天已经领取的气泡,数字 private function get_my_number(){ $list = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('user_id',$this->auth->id)->order('id asc')->select(); return $list; } //我今天的应得气泡数量 private function get_qipao_count(){ $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log $xiaofei_total = Db::name('user_gold_log')->whereTime('createtime','today')->where('user_id',$this->auth->id)->where('log_type','IN',$xiaofei_log_type) ->sum('change_value'); $xiaofei_total = abs($xiaofei_total); $qipao_value = config('site.active_qipao_value'); //气泡价格 if($qipao_value <= 0){ return 0; //0不能做除数 } $qipao = bcdiv($xiaofei_total,$qipao_value); //金币没有小数点 return intval($qipao); //退一法 } //随机一个中奖号码 private function get_rand_number(){ $min = config('site.active_number_min'); $max = config('site.active_number_max'); return rand($min,$max); } //奖池金额 private function jiangchi(){ $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log //奖池为昨日平台消费总金额百分比 $starttime = strtotime(date('Y-m-d')) - 86400; $endtime = $starttime + 86399; $xiaofei_total = Db::name('user_gold_log')->where('createtime','BETWEEN',[$starttime,$endtime])->where('log_type','IN',$xiaofei_log_type)->sum('change_value'); $xiaofei_total = abs($xiaofei_total); $jiangchi_bili = config('site.active_jiangchi_bili'); $jiangchi = bcdiv(bcmul($xiaofei_total,$jiangchi_bili,0),100,0); //金币没有小数点 return $jiangchi; } }