Matchgroup.php 6.3 KB
<?php

namespace app\school\controller\match;

use app\common\controller\ManystoreBase;
use fast\Export;
use app\school\model\basic\College;
use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 赛事管理
 *
 * @icon fa fa-circle-o
 */
class Matchgroup extends ManystoreBase
{

    protected $noNeedRight = ['*'];
    /**
     * Matchgroup模型对象
     * @var \app\school\model\match\Matchgroup
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\school\model\match\Matchgroup;
        $this->view->assign("matchLevelList", $this->model->getMatchLevelList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("college_list", (new College())->getCollegeList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/store/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if(empty($params['college'])) {
                    $this->error(__('Parameter %s can not be empty', ''));
                }
                $college = Db::name('college')->where(['id' => $params['college']])->find();
                if(!$college) {
                    $this->error(__('该院校不存在', ''));
                }
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    $params['school_id'] = $college['school_id'];
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if(empty($params['college'])) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $college = Db::name('college')->where(['id' => $params['college']])->find();
            if(!$college) {
                $this->error(__('该院校不存在', ''));
            }
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    $params['school_id'] = $college['school_id'];
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }

                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    public function getSchool(){
        $param= $this->request->param();
        $data = Db::name('manystore_shop')->where(['id'=>$param['school_id']])->find();
        return json($data);
    }

    public function getCollege(){
        $param= $this->request->param();
        $data = Db::name('college')->where(['id'=>$param['college_id']])->find();
        return json($data);
    }

    /**
     * 赛事数据导出功能
     */
    public function export()
    {
//        dump($this->request->param());die;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,true);
        $total = $this->model
            ->where($where)
            ->count();
        if($total == 0){
            $this->error('暂无可导出数据');
        }
        $sql = $this->model
            ->alias('m')
            ->field('m.id,sc.name,m.match_name,m.organizer,m.undertake,m.website,m.logo,CASE m.match_level WHEN 0 THEN \'校级\' WHEN 1 THEN \'市级\' WHEN 2 THEN \'省级\' ELSE \'国家级\' END AS match_level,CASE m.status WHEN 1 THEN \'正常\' ELSE \'隐藏\' END AS status')
            ->join('manystore_shop sc','sc.id = m.school_id','left')
            ->where($where)
            ->fetchSql(true)
            ->select();
        //开始导出
        $export = new Export();
        $export->setCount($total);
        $export->setSql($sql);
        $export->setFileName(time().'match_export');
        $export->setDirName(date('Y-m-d'));
        $export->setHeader(['编号','所属学校','竞赛标题','主办单位','承办单位','赛事官网地址','赛事LOGO','所属级别
','状态']);
        $export->setFields('id,name,match_name,organizer,undertake,website,logo,match_level_text,status_text');

        $exportRes = $export->out();
        if($exportRes) {
            $url = $export->getUrl();
            header( "Content-Type: application/zip" );
            header( "Content-Transfer-Encoding: Binary" );
            header( "Content-Length: " . filesize ($url));
            header( "Content-Disposition: attachment; filename=\"".basename ($url)."\"" );
            readfile( $url);
            exit ;
        }else{
            $this->error('导出失败,稍后请重新尝试');
        }
    }
}