Active.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 活动
  7. */
  8. class Active extends Api
  9. {
  10. // 无需登录的接口,*表示全部
  11. protected $noNeedLogin = ['active_finish'];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. //首页
  15. public function index(){
  16. $result = [];
  17. //开奖时间
  18. $result['kaijiang_time'] = '每天'.date('H:i',strtotime(config('site.active_kaijiang_time'))).'开奖';
  19. //奖池金额
  20. $result['jiangchi'] = $this->jiangchi();
  21. //我今天已经领取的气泡,数字
  22. $result['my_number'] = $this->get_my_number();
  23. //我今天的应得气泡数量,扣除已经领掉的,最大4个
  24. $qipao_count = $this->get_qipao_count() - count($result['my_number']);
  25. $qipao_count = $qipao_count > 4 ? 4 : $qipao_count ;
  26. $result['qipao_count'] = $qipao_count;
  27. $this->success('success',$result);
  28. }
  29. //收集所有气泡
  30. public function open_qipao(){
  31. //我今天的应得气泡数量
  32. $qipao_count = $this->get_qipao_count();
  33. //我今天打开的气泡数量
  34. $my_count = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('user_id',$this->auth->id)->count();
  35. if($my_count >= $qipao_count){
  36. $this->error('没有气泡可以收集了');
  37. }
  38. $week = [
  39. 1 => 4,
  40. 2 => 4,
  41. 3 => 4,
  42. 4 => 4,
  43. 5 => 4,
  44. 6 => 4,
  45. 7 => 10,
  46. ];
  47. $today_max = $week[date('N')];
  48. if($my_count >= $today_max){
  49. $this->error('今天最多能收集'.$today_max.'个气泡,已到上限');
  50. }
  51. //一次收集完
  52. $count = $qipao_count - $my_count;
  53. if($count > $today_max - $my_count){
  54. $count = $today_max - $my_count;
  55. }
  56. for($i = 1;$i <= $count;$i++){
  57. $data[] = [
  58. 'user_id' => $this->auth->id,
  59. 'number' => $this->get_rand_number(),
  60. 'createtime' => time(),
  61. 'createdate' => date('Ymd'),
  62. ];
  63. }
  64. Db::name('active_user_number')->insertAll($data);
  65. $this->success('已收集',$this->get_my_number());
  66. }
  67. //开奖,计划任务
  68. public function active_finish(){
  69. $check = Db::name('active_log')->where('opendate',date('Ymd'))->find();
  70. if(!empty($check)){
  71. $this->error('今天已开奖');
  72. }
  73. //中奖号码组。
  74. $box_count_config = [
  75. 1 => 1,
  76. 2 => 1,
  77. 3 => 2,
  78. 4 => 2,
  79. 5 => 3,
  80. 6 => 3,
  81. 7 => 4,
  82. ];
  83. $box_count = $box_count_config[date('N')];//今天开N个
  84. //所有已存在的不重复号码,打乱之后,取前N个
  85. $box = Db::name('active_user_number')->where('createdate',date('Ymd'))->group('number')->column('number');
  86. if(empty($box)){
  87. $this->error('今天没有人参与');
  88. }
  89. shuffle($box);
  90. $box_count = $box_count > count($box) ? count($box) : $box_count;
  91. $gift_number = array_chunk($box,$box_count)[0]; //中奖数组,不重复
  92. $gift_number = implode(',',$gift_number);
  93. //中奖列表
  94. $list = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('number','IN',$gift_number)->select();
  95. $log_ids = array_column($list,'id');//id更新快一点
  96. //每股中奖额度
  97. $jiangchi = $this->jiangchi();
  98. $price = bcdiv($jiangchi,count($list),0);
  99. //发奖结果集
  100. $fajiang = [];
  101. foreach($list as $key => $val){
  102. if(isset($fajiang[$val['user_id']])){
  103. $fajiang[$val['user_id']] += 1;
  104. }else{
  105. $fajiang[$val['user_id']] = 1;
  106. }
  107. }
  108. Db::startTrans();
  109. //发奖
  110. foreach($fajiang as $key => $val){
  111. $gold = bcmul($val,$price,0);
  112. $rs = model('wallet')->lockChangeAccountRemain($key,0,'gold',$gold,91,'第'.date('Ymd').'期');
  113. if($rs['status'] === false){
  114. Db::rollback();
  115. $this->error('开奖失败');
  116. }
  117. }
  118. //修改状态
  119. $rs = Db::name('active_user_number')->where('id','IN',$log_ids)->update(['gold'=>$price,'status'=>1,'updatetime'=>time()]);
  120. if($rs === false){
  121. Db::rollback();
  122. $this->error('开奖失败');
  123. }
  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' => $this->get_week(),
  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. $createdate = Db::name('active_user_number')->where('user_id',$this->auth->id)->group('createdate')->column('createdate');
  170. $limit = input('listrow',10);
  171. $createdate = array_chunk($createdate,$limit);
  172. $page = input('page',1);
  173. if($page > count($createdate)){
  174. $this->success(1,[]);
  175. }
  176. $page_qi = $createdate[$page-1]; //这一页的期
  177. //开始
  178. $result = [];
  179. foreach($page_qi as $key => $qi){
  180. $qi_text = '第'.$qi.'期 '. $this->get_week(date('N',strtotime($qi)));
  181. $list = Db::name('active_user_number')->where('user_id',$this->auth->id)->where('createdate',$qi)->select();
  182. $result[] = [
  183. 'qi' => $qi_text,
  184. 'list' => $list,
  185. ];
  186. }
  187. $this->success(1,$result);
  188. }
  189. //历史中奖
  190. public function my_gold_log(){
  191. $list = Db::name('user_gold_log')->where('user_id',$this->auth->id)->where('log_type',91)->autopage()->order('id desc')->select();
  192. $this->success('success',$list);
  193. }
  194. //中奖榜
  195. public function active_finish_result(){
  196. $qi = date('Ymd',time()-86400);
  197. // $qi = '20241225';
  198. $sql = Db::name('active_user_number')->field('user_id,sum(gold) as sum_gold')->where('createdate',$qi)->where('status',1)->group('user_id')->buildSql();
  199. $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();
  200. $qi_text = '第'.$qi.'期 '. $this->get_week(date('N',strtotime($qi)));
  201. $result = [
  202. 'qi' => $qi_text,
  203. 'list' => $list,
  204. ];
  205. $this->success(1,$result);
  206. }
  207. private function get_week($week = 8){
  208. $arr = [
  209. 0 => '周日',
  210. 1 => '周一',
  211. 2 => '周二',
  212. 3 => '周三',
  213. 4 => '周四',
  214. 5 => '周五',
  215. 6 => '周六',
  216. 7 => '周日',
  217. ];
  218. if(!isset($arr[$week])){
  219. $week = date('N');
  220. }
  221. return $arr[$week];
  222. }
  223. //我今天已经领取的气泡,数字
  224. private function get_my_number(){
  225. $list = Db::name('active_user_number')->where('createdate',date('Ymd'))->where('user_id',$this->auth->id)->order('id asc')->select();
  226. return $list;
  227. }
  228. //我今天的应得气泡数量
  229. private function get_qipao_count(){
  230. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  231. $xiaofei_total = Db::name('user_gold_log')->whereTime('createtime','today')->where('user_id',$this->auth->id)->where('log_type','IN',$xiaofei_log_type)
  232. ->sum('change_value');
  233. $xiaofei_total = abs($xiaofei_total);
  234. $qipao_value = config('site.active_qipao_value'); //气泡价格
  235. if($qipao_value <= 0){
  236. return 0; //0不能做除数
  237. }
  238. $qipao = bcdiv($xiaofei_total,$qipao_value); //金币没有小数点
  239. return intval($qipao); //退一法
  240. }
  241. //随机一个中奖号码
  242. private function get_rand_number(){
  243. $min = config('site.active_number_min');
  244. $max = config('site.active_number_max');
  245. return rand($min,$max);
  246. }
  247. //奖池金额
  248. private function jiangchi(){
  249. $xiaofei_log_type = '11,12,13,53,59,71,81';//消费金币的log
  250. //奖池为昨日平台消费总金额百分比
  251. $starttime = strtotime(date('Y-m-d')) - 86400;
  252. $endtime = $starttime + 86399;
  253. $xiaofei_total = Db::name('user_gold_log')->where('createtime','BETWEEN',[$starttime,$endtime])->where('log_type','IN',$xiaofei_log_type)->sum('change_value');
  254. $xiaofei_total = abs($xiaofei_total);
  255. $jiangchi_bili = config('site.active_jiangchi_bili');
  256. $jiangchi = bcdiv(bcmul($xiaofei_total,$jiangchi_bili,0),100,0); //金币没有小数点
  257. return $jiangchi;
  258. }
  259. }