'asc' ]; $model = new Vote(); $count = $model->alias('a')->where($where)->count(); $list = $model->alias('b') ->where($where) ->order($order) ->page($get['page_no'], $get['page_size']) ->select() ->toArray(); foreach ($list as &$item) { $item['status_show'] = $item['start_time']>time()?"未开始":($item['end_time'] $list, 'page_no' => $get['page_no'], 'page_size' => $get['page_size'], 'count' => $count, 'more' => $more ]; return $data; } catch (\Exception $e) { return ['error'=>$e->getMessage()]; } } /** * @Notes: 文章列表 * @Author: 张无忌 * @param $get * @return array */ public static function rank($get) { $where = [ ['status', '=', 1], ['vote_id',"=",$get['id']] ]; $order = [ 'vote_num' => 'desc', 'views' => 'desc' ]; $model = new VotePlayer(); $list = $model->alias('b') ->where($where) ->order($order) ->limit(50) ->select() ->toArray(); $data = [ 'list' => $list ]; return $data; } /** * @Notes: 文章详细 * @Author: 张无忌 * @param $id * @return array */ public static function detail($id,$uid) { $article = Vote::field('*')->where('id', $id)->findOrEmpty(); $palers = new VotePlayer(); if($article->isEmpty()) { $article = []; }else{ $article->views = $article->views + 1; $article->save(); $article = $article->toArray(); $article['status_show'] = $article['start_time']]*>(.*?)<\/a>/i'; $replacement = ''; $article['content'] = preg_replace($pattern, $replacement, $article['content']); $article['views'] = $article['views'] +$article['base_visit'] ; // $article['images'] = explode(",",$article['images']); if($article['images'] ){ $article['images'] = explode(",",$article['images']); }else{ $article['images'] = []; } $article['player_count'] = $palers->where("vote_id","=",$id)->where("status",'=',1)->where("del","=",0)->count(); } return $article; } public static function players($get,$uid) { $article = Vote::field('*')->where('id', $get['id'])->findOrEmpty(); $where = [ ['status', '=', 1], ['vote_id',"=",$get['id']] ]; if(!empty($get['keyword']) && trim($get['keyword'])){ $where[] = ['name',"like","%{$get['keyword']}%"]; } $order = [ 'add_time' => 'asc' ]; $model = new VotePlayer(); $count = $model->alias('a')->where($where)->count(); $list = $model->alias('b') ->where($where) ->order($order) ->page($get['page_no'], $get['page_size']) ->select() ->toArray(); foreach ($list as &$item) { $item['can_vote'] = VoteLogic::getPlayerVoteStatus($article['id'],$item['id'],$uid,$article['is_redo']); } $more = is_more($count, $get['page_no'], $get['page_size']); $data = [ 'list' => $list, 'page_no' => $get['page_no'], 'page_size' => $get['page_size'], 'count' => $count, 'more' => $more ]; return $data; } public static function playersDetail($id,$uid) { $article = VotePlayer::field('*')->where('id', $id)->findOrEmpty(); $vote = Vote::field('*')->where('id', $article['vote_id'])->findOrEmpty(); $mode = new VotePlayer(); if($article->isEmpty()) { $article = []; }else{ $article->views = $article->views + 1; $article->save(); $article = $article->toArray(); $article['can_vote'] = VoteLogic::getPlayerVoteStatus($vote['id'],$article['id'],$uid,$vote['is_redo']); $article['user_vote_count'] = VoteLogic::getUserPlayerVoteNums($vote['id'],$uid,$vote['vote_type']); $article['title'] = $vote['title']; $article['start_time'] = $vote['start_time']; $article['end_time'] = $vote['end_time']; if($article['images'] ){ $article['images'] = explode(",",$article['images']); }else{ $article['images'] = []; } $article['rank'] = $mode->where('vote_id', $article['vote_id'])->where("vote_num",">",$article['vote_num'])->count() + 1; if($article['rank']>1){ $last = $mode->field("*")->where('vote_id', $article['vote_id'])->where("vote_num",">",$article['vote_num'])->order("vote_num","asc")->limit(1)->select(); $article['rank_last'] = $last[0]['vote_num'] - $article['vote_num']; }else{ $article['rank_last'] = 0; } } return $article; } public static function vote($id,$uid) { $article = VotePlayer::field('*')->where('id', $id)->findOrEmpty(); $vote = Vote::field('*')->where('id', $article['vote_id'])->findOrEmpty(); if($article->isEmpty() || $vote->isEmpty()) { return [ 'msg' => '投票成功', 'data' => 1 ]; } $can_vote = VoteLogic::getPlayerVoteStatus($vote['id'],$article['id'],$uid,$vote['is_redo']); $user_count = VoteLogic::getUserPlayerVoteNums($vote['id'],$uid,$vote['vote_type']); if( !$can_vote ) { return [ 'msg' => '您已经给TA投过票啦!!', 'data' => 0 ]; } if($vote['vote_num']<=$user_count){ return [ 'msg' => '您'.($vote['vote_type']==0?'今日':'所有').'的投票数量已达上限!!', 'data' => 0 ]; } $data = [ "user_id" => $uid, "vid" => $vote['id'], "pid" => $article['id'], "create_time" => time(), "update_time" => time(), ]; $voteLog = new VoteLog(); $res = $voteLog->insert($data); if($res){ //总投票数和选手投票数+1 $article->vote_num = $article->vote_num + 1; $article->save(); $vote->vote_total = $vote->vote_total + 1; $vote->save(); return [ 'msg' => '投票成功', 'data' => 1 ]; }else{ return [ 'msg' => '投票失败', 'data' => 0 ]; } } public static function getPlayerVoteStatus($id=0,$player_id=0,$uid=0,$is_redo=0){ $voteLog = new VoteLog(); $can_vote = 1; //当天不重复 统计当天是否给该选手投过票即可 if($is_redo == 0 ){ $where = [ ["vid","=",$id], ["pid","=",$player_id], ["user_id","=",$uid], ["create_time",">",strtotime(date("Y-m-d"))] ]; $count = $voteLog->where($where)->count(); if($count){ $can_vote = 0; } }else if($is_redo = 1){ // 全部不重复 则统计活动该用户是否给过选手投票 $where = [ ["vid","=",$id], ["pid","=",$player_id], ["user_id","=",$uid], ]; $count = $voteLog->where($where)->count(); if($count){ $can_vote = 0; } } return $can_vote; } public static function getUserPlayerVoteNums($id=0,$uid=0,$vote_type=0){ $voteLog = new VoteLog(); $count = 0; //当天不重复 统计当天是否给该选手投过票即可 if($vote_type == 0 ){ $where = [ ["vid","=",$id], ["user_id","=",$uid], ["create_time",">",strtotime(date("Y-m-d"))] ]; $count = $voteLog->where($where)->count(); }else if($vote_type = 1){ // 全部不重复 则统计活动该用户是否给过选手投票 $where = [ ["vid","=",$id], ["user_id","=",$uid], ]; $count = $voteLog->where($where)->count(); } return $count; } }