Active.php 8.0 KB

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