Match.php 6.2 KB
<?php

namespace app\index\controller\match;

use app\common\controller\Frontend;
use app\common\model\Member;
use app\school\model\match\MatchItems;
use app\index\model\MatchTagLog;
use app\school\model\review\Review;
use app\school\model\review\ReviewScore;
use app\common\model\match\MatchItemPrize;
use think\Collection;
use think\Db;
use think\Request;

class Match extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->assign('index','index');
    }

    public function index()
    {
        $type = request()->param('type');
        $uid = $this->auth->id;
        $memberModel = new Member();
        $query =  $memberModel->alias('m')
            ->field('i.*,m.is_captain,mt.match_title,mt.match_starttime,mt.match_endtime,mp.name')
            ->join('match_items i','m.item_id = i.id','inner')
            ->join('match mt','i.match_id = mt.id','left')
            ->join('match_type mp','i.match_type = mp.id','left');
        if($type == 'ing') {
            $query->where('mt.match_starttime','<=',time())->where('mt.match_endtime','>=',time());
        }else if($type == 'ed'){
            $query->where('mt.match_endtime','<',time());
        }
        $items = $query->where(['m.member_id' => $uid,'m.member_type' => Member::STUDENT,'m.member_source' => Member::MATCH])
            ->where(['i.school_id' => $this->school_id])
            ->order('i.createtime','desc')
            ->paginate(2, false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);

        $countQuery = $memberModel->alias('m')
            ->field('i.*,m.is_captain,mt.match_title,mt.match_starttime,mt.match_endtime,mp.name')
            ->join('match_items i','m.item_id = i.id','inner')
            ->join('match mt','i.match_id = mt.id','left')
            ->join('match_type mp','i.match_type = mp.id','left');
        if($type == 'ing') {
            $countQuery->where('mt.match_starttime','<=',time())->where('mt.match_endtime','>=',time());
        }else if($type == 'ed'){
            $countQuery->where('mt.match_endtime','<',time());
        }
        $countQuery->where(['m.member_id' => $uid,'m.member_type' => Member::STUDENT,'m.member_source' => Member::MATCH])
            ->where(['i.school_id' => $this->school_id]);
        $itemsCount = $countQuery->count();
  /*      $itemsModel = new MatchItems();
        $query = $itemsModel->alias('i')
            ->field('i.*,m.match_title,mt.name,m.match_starttime,m.match_endtime')
            ->join('match m','m.id=i.match_id','left')
            ->join('match_type mt','mt.id=i.match_type','left');
            if($type == 'ing') {
                $query->where('m.match_starttime','<=',time())->where('m.match_endtime','>=',time());
            }else if($type == 'ed'){
                $query->where('m.match_endtime','<',time());
            }
        $items= $query ->where('i.user_id',$uid)
            ->where("i.school_id",$this->school_id)
            ->order('i.createtime','desc')
            ->paginate(2, false, [
                'query' => Request::instance()->param(),//不丢失已存在的url参数
            ]);
        $countQuery = $itemsModel->alias('i')
            ->field('i.id')
            ->join('match m','m.id=i.match_id','left')
            ->where(['i.user_id' => $uid,'i.school_id' => $this->school_id]);
        if($type == 'ing') {
            $countQuery->where('m.match_starttime','<=',time())->where('m.match_endtime','>=',time());
        }else if($type == 'ed'){
            $countQuery->where('m.match_endtime','<',time());
        }
        $itemsCount = $countQuery->count();*/
        if($items) {
            foreach ($items as &$val) {
                $top = [];
                $medium = [];
                $down = [];
                $info =  Db::name('match_tag_log')->alias('l')
                    ->field('l.createtime,m.tag_name,l.id')
                    ->join('match_tags m','l.match_tag_id = m.id','left')
                    ->where(['l.match_items_id' => $val['id']])
                    ->order('l.createtime','desc')
                    ->limit(9)
                    ->select();
                if($info) {
                    $info = sortArrByManyField($info,'createtime',SORT_ASC);
                }
                if($info) {
                    foreach ($info as $key => $tag) {
                        if($key <= 3) {
                            $top[] = $tag;
                        }else if ($key > 4) {
                            $down[] = $tag;
                        }else {
                            $medium[] = $tag;
                        }
                    }
                }
                $val['top'] = $top;
                $val['medium'] = $medium;
                $val['down'] = $down;
            }
        }
        $this->assign('_list',$items);
        $this->assignconfig('type',$type);
        $this->assign('_count',$itemsCount);
        return $this->view->fetch();
    }

    public function getRemark()
    {
        $id = request()->param('id');
        $list = [];
        $reviewModel = new Review();
        $info = Db::name('match_tag_log')->where(['id' => $id])->find();
        $review = $reviewModel->where('find_in_set(:id,match_tags_id)',['id'=>$info['match_tag_id']])->find();
        if($review) {
            $model = new ReviewScore;
            $list = $model
                ->alias('s')
                ->field('s.remark,s.score_total,e.nickname')
                ->join('project_experts e','s.judge_id = e.id','left')
                ->where(['s.match_items_id' => $info['match_items_id'],'review_id' => $review['id']])
                ->select();
        }
        $this->assign('list',$list);
	    $this->assign('login','');
        return $this->view->fetch();
    }

    public function prize_view($item_id)
    {
        $model = new MatchItemPrize();
        $info = $model->where(['match_items_id' => $item_id])->find();
        $this->assign('prize_type',$model->getPrizeType());
        $this->assign('login','');
        $this->assign('row',$info);
        return $this->view->fetch();
    }

}