Active.php 9.3 KB

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