Active.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Tenim;
  5. use think\Db;
  6. /**
  7. * 活动
  8. */
  9. class Active extends Api
  10. {
  11. // 无需登录的接口,*表示全部
  12. protected $noNeedLogin = [''];
  13. // 无需鉴权的接口,*表示全部
  14. protected $noNeedRight = ['*'];
  15. //首页
  16. public function index(){
  17. $result = [];
  18. //开奖时间
  19. $result['kaijiang_time'] = date('H:i',strtotime(config('site.active_kaijiang_time')));
  20. //奖池金额
  21. $result['jiangchi'] = $this->jiangchi();
  22. //我今天已经领取的气泡,数字
  23. $result['my_number'] = $this->get_my_number();
  24. //我今天的应得气泡数量,扣除已经领掉的,最大4个
  25. $qipao_count = $this->get_qipao_count() - count($result['my_number']);
  26. $qipao_count = $qipao_count > 4 ? 4 : $qipao_count ;
  27. $result['qipao_count'] = $qipao_count;
  28. $this->success('success',$result);
  29. }
  30. //收集一个气泡
  31. public function open_qipao(){
  32. //我今天的应得气泡数量
  33. $qipao_count = $this->get_qipao_count();
  34. //我今天打开的气泡数量
  35. $my_count = Db::name('active_user_number')->whereTime('createtime','today')->where('user_id',$this->auth->id)->count();
  36. if($my_count + 1 > $qipao_count){
  37. $this->error('可收集气泡不足');
  38. }
  39. $week = [
  40. 1 => 4,
  41. 2 => 4,
  42. 3 => 4,
  43. 4 => 4,
  44. 5 => 4,
  45. 6 => 4,
  46. 7 => 10,
  47. ];
  48. $today_max = $week[date('N')];
  49. if($my_count + 1 > $today_max){
  50. $this->error('今天最多能收集'.$today_max.'个气泡,已到上限');
  51. }
  52. $data = [
  53. 'user_id' => $this->auth->id,
  54. 'number' => $this->get_rand_number(),
  55. 'createtime' => time(),
  56. ];
  57. Db::name('active_user_number')->insertGetId($data);
  58. $this->success('已收集',$this->get_my_number());
  59. }
  60. //开奖
  61. public function active_finish(){
  62. $check = Db::name('active_log')->where('opendate',date('Ymd'))->find();
  63. if(!empty($check)){
  64. $this->error('今天已开奖');
  65. }
  66. //中奖号码。所有号码中随机拿个一,就一定会中奖
  67. $box = Db::name('active_user_number')->whereTime('createtime','today')->column('number');
  68. $gift_number = $box[rand(0,count($box)-1)];
  69. //中奖列表
  70. $list = Db::name('active_user_number')->whereTime('createtime','today')->where('number',$gift_number)->select();
  71. $log_ids = array_column($list,'id');//id更新快一点
  72. //每股中奖额度
  73. $jiangchi = $this->jiangchi();
  74. $price = bcdiv($jiangchi,count($list),0);
  75. //发奖结果集
  76. $fajiang = [];
  77. foreach($list as $key => $val){
  78. if(isset($fajiang[$val['user_id']])){
  79. $fajiang[$val['user_id']] += 1;
  80. }else{
  81. $fajiang[$val['user_id']] = 1;
  82. }
  83. }
  84. Db::startTrans();
  85. //发奖
  86. foreach($fajiang as $key => $val){
  87. $gold = bcmul($val,$price,0);
  88. $rs = model('wallet')->lockChangeAccountRemain($key,0,'gold',$gold,91,'第'.date('Ymd').'期');
  89. if($rs['status'] === false){
  90. Db::rollback();
  91. $this->error('开奖失败');
  92. }
  93. }
  94. //修改状态
  95. $rs = Db::name('active_user_number')->where('id','IN',$log_ids)->update(['gold'=>$price,'status'=>1,'updatetime'=>time()]);
  96. if($rs === false){
  97. Db::rollback();
  98. $this->error('开奖失败');
  99. }
  100. //开奖记录
  101. $log = [
  102. 'gift_number' => $gift_number,
  103. 'jiangchi' => $jiangchi,
  104. 'giftcount' => count($list),
  105. 'price' => $price,
  106. 'usercount' => count($fajiang),
  107. 'opendate' => date('Ymd'),
  108. 'createtime' => time(),
  109. ];
  110. $log_id = Db::name('active_log')->insertGetId($log);
  111. if(!$log_id){
  112. Db::rollback();
  113. $this->error('开奖失败');
  114. }
  115. Db::commit();
  116. $this->success('开奖完成');
  117. }
  118. //开奖结果
  119. public function active_result(){}
  120. //参与统计
  121. //参与日志
  122. //历史中奖
  123. //我今天已经领取的气泡,数字
  124. private function get_my_number(){
  125. $list = Db::name('active_user_number')->whereTime('createtime','today')->where('user_id',$this->auth->id)->order('id asc')->select();
  126. return $list;
  127. }
  128. //我今天的应得气泡数量
  129. private function get_qipao_count(){
  130. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  131. $xiaofei_total = Db::name('user_gold_log')->whereTime('createtime','today')->where('user_id',$this->auth->id)->where('log_type','IN',$xiaofei_log_type)
  132. ->sum('change_value');
  133. $xiaofei_total = abs($xiaofei_total);
  134. $qipao_value = config('site.active_qipao_value'); //气泡价格
  135. if($qipao_value <= 0){
  136. return 0; //0不能做除数
  137. }
  138. $qipao = bcdiv($xiaofei_total,$qipao_value); //金币没有小数点
  139. return intval($qipao); //退一法
  140. }
  141. //随机一个中奖号码
  142. private function get_rand_number(){
  143. $min = config('site.active_number_min');
  144. $max = config('site.active_number_max');
  145. return rand($min,$max);
  146. }
  147. //奖池金额
  148. private function jiangchi(){
  149. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  150. //奖池为昨日平台消费总金额百分比
  151. $starttime = strtotime(date('Y-m-d')) - 86400;
  152. $endtime = $starttime + 86399;
  153. $xiaofei_total = Db::name('user_gold_log')->where('createtime','BETWEEN',[$starttime,$endtime])->where('log_type','IN',$xiaofei_log_type)->sum('change_value');
  154. $xiaofei_total = abs($xiaofei_total);
  155. $jiangchi_bili = config('site.active_jiangchi_bili');
  156. $jiangchi = bcdiv(bcmul($xiaofei_total,$jiangchi_bili,0),100,0); //金币没有小数点
  157. return $jiangchi;
  158. }
  159. }