| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | <?phpnamespace app\admin\controller\unishop;use app\common\controller\Backend;use app\admin\model\unishop\Area;use think\Db;/** * 学校管理 * * @icon fa fa-circle-o */class School extends Backend{        /**     * School模型对象     * @var \app\admin\model\unishop\School     */    protected $model = null;    public function _initialize()    {        parent::_initialize();        $this->model = new \app\admin\model\unishop\School;        $this->areamodel = new \app\admin\model\unishop\Area;        $this->grademodel = new \app\admin\model\unishop\Grade;        $this->classmodel = new \app\admin\model\unishop\Classes;        $this->ordermodel = new \app\admin\model\unishop\Order;        $this->studentmodel = new \app\admin\model\unishop\Student;    }    /**     * 查看     */    public function index()    {        //设置过滤方法        $this->request->filter(['strip_tags']);        if ($this->request->isAjax()) {            //如果发送的来源是Selectpage,则转发到Selectpage            if ($this->request->request('keyField')) {                return $this->selectpage();            }            list($where, $sort, $order, $offset, $limit) = $this->buildparams();            $total = $this->model                ->where($where)                ->with("Area")                ->order($sort, $order)                ->count();            $list = $this->model                ->where($where)                ->with("Area")                ->order($sort, $order)                ->limit($offset, $limit)                ->select();            $list = collection($list)->toArray();            $result = array("total" => $total, "rows" => $list);            return json($result);        }        return $this->view->fetch();    }    /**     * 添加     */    public function add()    {        if ($this->request->isPost()) {            $params = $this->request->post("row/a");            if ($params) {                $params = $this->preExcludeFields($params);                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {                    $params[$this->dataLimitField] = $this->auth->id;                }                $result = false;                Db::startTrans();                try {                    //是否采用模型验证                    if ($this->modelValidate) {                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;                        $this->model->validateFailException(true)->validate($validate);                    }                    $result = $this->model->allowField(true)->save($params);                    Db::commit();                } catch (ValidateException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (PDOException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } 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'));        }        $adminIds = $this->getDataLimitAdminIds();        if (is_array($adminIds)) {            if (!in_array($row[$this->dataLimitField], $adminIds)) {                $this->error(__('You have no permission'));            }        }        $areainfo = $this->areamodel->get($row["area_id"]);        if ($this->request->isPost()) {            $params = $this->request->post("row/a");            if ($params) {                $params = $this->preExcludeFields($params);                if(intval($params["area_id"]) == 0) {                    unset($params["area_id"]);                }                $result = false;                Db::startTrans();                try {                    //是否采用模型验证                    if ($this->modelValidate) {                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;                        $row->validateFailException(true)->validate($validate);                    }                    $result = $row->allowField(true)->save($params);                    Db::commit();                } catch (ValidateException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } catch (PDOException $e) {                    Db::rollback();                    $this->error($e->getMessage());                } 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', ''));        }        $row["area_name"] = "山东省/临沂市/".$areainfo->name;        $this->view->assign("row", $row);        return $this->view->fetch();    }    /**     * 获取订购数据     */    public function data($ids = null){        $school_id = $ids?$ids:0;        // 获取学校名称        $school_name = $this->model->where(["id"=>$school_id])->value("name");        // 根据学校ID获取年级班级数据        $gradeinfo = $this->grademodel->where(["school_id"=>$school_id])->select();//        $orderinfo = $this->ordermodel->where(["status"=>1,"have_paid"=>1])->select();//        print_r($orderinfo);exit;        // 获取所有成交订单信息        $orderinfo = $this->ordermodel->alias("o")            ->join("fa_student s","s.user_id = o.user_id")            ->where(["o.status"=>1,"o.have_paid"=>1,"s.is_main"=>1])->select();        $userids = [];        foreach($orderinfo as $k => $v) {            $num = isset($userids[$v["grade_id"]][$v["class_id"]])?$userids[$v["grade_id"]][$v["class_id"]]+1:1;            $userids[$v["grade_id"]][$v["class_id"]] = $num;        }        $data = [];        if($gradeinfo) {            foreach($gradeinfo as $k => $v) {                $data[$k]["grade"] = $v["name"];                $classinfo = $this->classmodel->where(["grade_id"=>$v["id"]])->select();                $data[$k]["class"] = [];                $data[$k]["num"] = 0;                if($classinfo) {                    foreach($classinfo as $ke => $va) {                        $data[$k]["class"][$ke]["name"] = $va["name"];                        $data[$k]["class"][$ke]["num"] = isset($userids[$v["id"]][$va["id"]])?$userids[$v["id"]][$va["id"]]:0;                        $data[$k]["num"] = $data[$k]["num"]+$data[$k]["class"][$ke]["num"];                    }                }            }        }//        print_r($ids);exit;        $this->view->assign("school_name", $school_name);        $this->view->assign("data", $data);//        print_r($data);exit;        return $this->view->fetch();    }}
 |