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(); } }