Study.php 6.2 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\controller\Resource;

/**
 * 学生管理
 *
 * @icon fa fa-circle-o
 */
class Study extends Backend
{

    /**
     * Study模型对象
     * @var \app\admin\model\Study
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Study;
        $this->view->assign("genderList", $this->model->getGenderList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['school', 'team'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {

                $row->getRelation('school')->visible(['title']);
                $row->getRelation('team')->visible(['title']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 生成现场二维码
     */
    public function barcode($ids)
    {
        $barcode = $this->model->where('id', $ids)->value('barcode');
        if (!empty($barcode)) {
            $this->error('已生成二维码,无需重复生成');
        }
        $brpath = Resource::StudyBar($ids);
        $this->model->save(['qrcode' => $brpath], ['id' => $ids]);
        $this->success('操作成功');
    }


    //导出数据
    public function export()
    {
        if ($this->request->isPost()) {
            set_time_limit(0);
            $ids = $this->request->post('ids');

            $excel = new \PHPExcel();
            try {
                $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
                $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
                $excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
                $excel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
                $excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
                $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
                $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
                $excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);

                // 列名表头文字加粗
                $excel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true);
                // 列表头文字居中
                $excel->getActiveSheet()->getStyle('A1:J1')->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                // 开始操作excel表
                $worksheet = $excel->setActiveSheetIndex(0)    //操作第一个工作表
                ->setCellValue('A1', '序号')   //设置表格标题名称
                ->setCellValue('B1', '姓名')   //设置表格标题名称
                ->setCellValue('C1', '性别')
                    ->setCellValue('D1', '班级')
                    ->setCellValue('E1', '学校')
                    ->setCellValue('F1', '手环ID')
                    ->setCellValue('G1', '学号')
                    ->setCellValue('H1', '总积分');
                if ($ids == 'all' || empty($ids)) {    //判断当前数据为全部或者选中的
                    $list = $this->model->with('school')->select();
                } else {
                    $list = $this->model->with('school')->select($ids);
                }

                foreach ($list as $k => $val) {
                    $kk = $k + 1;
                    $k = $k + 2;//表格是从2开始的
                    $excel->getActiveSheet()->setTitle($val['school']['title'] . '积分表');
                    $worksheet
                        ->setCellValue('A' . $k, $val['id'])
                        ->setCellValue('B' . $k, $val['name'])
                        ->setCellValue('C' . $k, $val['gender'] ? '男' : '女')
                        ->setCellValue('D' . $k, $val['grade'])
                        ->setCellValue('E' . $k, $val['school']['title'])
                        ->setCellValue('F' . $k, $val['unique'])
                        ->setCellValue('G' . $k, $val['sno'])
                        ->setCellValue('H' . $k, $val['earn_score']);
                }
                $excel->createSheet();
                $title = "学生成绩" . date("YmdHis");
                header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
                header('Cache-Control: max-age=0');
                header('Cache-Control: max-age=1');

                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
                header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
                header('Pragma: public'); // HTTP/1.0
                $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
                $objWriter->save('php://output');
            } catch (\PHPExcel_Exception $e) {
                $this->error('参数错误', $e->getMessage());
            }
            exit;
        }
    }
}