Active.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_count_config = [
  68. 1 => 1,
  69. 2 => 1,
  70. 3 => 2,
  71. 4 => 2,
  72. 5 => 3,
  73. 6 => 3,
  74. 7 => 4,
  75. ];
  76. $box_count = $box_count_config[date('N')];//今天开N个
  77. //所有已存在的不重复号码,打乱之后,取前N个
  78. $box = Db::name('active_user_number')->whereTime('createtime','today')->group('number')->column('number');
  79. shuffle($box);
  80. $box_count = $box_count > count($box) ? count($box) : $box_count;
  81. $gift_number = array_chunk($box,$box_count)[0]; //中奖数组,不重复
  82. $gift_number = implode(',',$gift_number);
  83. //中奖列表
  84. $list = Db::name('active_user_number')->whereTime('createtime','today')->where('number','IN',$gift_number)->select();
  85. $log_ids = array_column($list,'id');//id更新快一点
  86. //每股中奖额度
  87. $jiangchi = $this->jiangchi();
  88. $price = bcdiv($jiangchi,count($list),0);
  89. //发奖结果集
  90. $fajiang = [];
  91. foreach($list as $key => $val){
  92. if(isset($fajiang[$val['user_id']])){
  93. $fajiang[$val['user_id']] += 1;
  94. }else{
  95. $fajiang[$val['user_id']] = 1;
  96. }
  97. }
  98. Db::startTrans();
  99. //发奖
  100. foreach($fajiang as $key => $val){
  101. $gold = bcmul($val,$price,0);
  102. $rs = model('wallet')->lockChangeAccountRemain($key,0,'gold',$gold,91,'第'.date('Ymd').'期');
  103. if($rs['status'] === false){
  104. Db::rollback();
  105. $this->error('开奖失败');
  106. }
  107. }
  108. //修改状态
  109. $rs = Db::name('active_user_number')->where('id','IN',$log_ids)->update(['gold'=>$price,'status'=>1,'updatetime'=>time()]);
  110. if($rs === false){
  111. Db::rollback();
  112. $this->error('开奖失败');
  113. }
  114. //开奖记录
  115. $log = [
  116. 'gift_number' => $gift_number,
  117. 'jiangchi' => $jiangchi,
  118. 'giftcount' => count($list),
  119. 'price' => $price,
  120. 'usercount' => count($fajiang),
  121. 'opendate' => date('Ymd'),
  122. 'createtime' => time(),
  123. ];
  124. $log_id = Db::name('active_log')->insertGetId($log);
  125. if(!$log_id){
  126. Db::rollback();
  127. $this->error('开奖失败');
  128. }
  129. Db::commit();
  130. $this->success('开奖完成');
  131. }
  132. //开奖结果
  133. public function active_result(){}
  134. //参与统计
  135. //参与日志
  136. //历史中奖
  137. //我今天已经领取的气泡,数字
  138. private function get_my_number(){
  139. $list = Db::name('active_user_number')->whereTime('createtime','today')->where('user_id',$this->auth->id)->order('id asc')->select();
  140. return $list;
  141. }
  142. //我今天的应得气泡数量
  143. private function get_qipao_count(){
  144. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  145. $xiaofei_total = Db::name('user_gold_log')->whereTime('createtime','today')->where('user_id',$this->auth->id)->where('log_type','IN',$xiaofei_log_type)
  146. ->sum('change_value');
  147. $xiaofei_total = abs($xiaofei_total);
  148. $qipao_value = config('site.active_qipao_value'); //气泡价格
  149. if($qipao_value <= 0){
  150. return 0; //0不能做除数
  151. }
  152. $qipao = bcdiv($xiaofei_total,$qipao_value); //金币没有小数点
  153. return intval($qipao); //退一法
  154. }
  155. //随机一个中奖号码
  156. private function get_rand_number(){
  157. $min = config('site.active_number_min');
  158. $max = config('site.active_number_max');
  159. return rand($min,$max);
  160. }
  161. //奖池金额
  162. private function jiangchi(){
  163. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  164. //奖池为昨日平台消费总金额百分比
  165. $starttime = strtotime(date('Y-m-d')) - 86400;
  166. $endtime = $starttime + 86399;
  167. $xiaofei_total = Db::name('user_gold_log')->where('createtime','BETWEEN',[$starttime,$endtime])->where('log_type','IN',$xiaofei_log_type)->sum('change_value');
  168. $xiaofei_total = abs($xiaofei_total);
  169. $jiangchi_bili = config('site.active_jiangchi_bili');
  170. $jiangchi = bcdiv(bcmul($xiaofei_total,$jiangchi_bili,0),100,0); //金币没有小数点
  171. return $jiangchi;
  172. }
  173. }