Active.php 10 KB

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