model = new BodyProfileModel; } /** * 默认生成的控制器所继承的父类中有index方法,在这里重写下 */ 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(['user']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible(['id','profile_name','user_id','gender','is_own','relation','age','height','weight','createtime','updatetime']); $row->visible(['user']); $row->getRelation('user')->visible(['username','nickname']); // 计算BMI $row['bmi'] = $row->calculateBMI(); $row['bmi_level'] = $row->getBMILevel(); } $result = array("total" => $list->total(), "rows" => $list->items()); 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 (isset($params['body_photos']) && is_array($params['body_photos'])) { $params['body_photos'] = json_encode($params['body_photos']); } $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', '')); } $this->view->assign('genderList', ['1' => __('Male'), '2' => __('Female')]); $this->view->assign('isOwnList', ['1' => __('Own profile'), '0' => __('Others profile')]); 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')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); // 处理身体照片 if (isset($params['body_photos']) && is_array($params['body_photos'])) { $params['body_photos'] = json_encode($params['body_photos']); } $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', '')); } $this->view->assign('row', $row); $this->view->assign('genderList', ['1' => __('Male'), '2' => __('Female')]); $this->view->assign('isOwnList', ['1' => __('Own profile'), '0' => __('Others profile')]); return $this->view->fetch(); } /** * 删除 */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ?: $this->request->post("ids"); if (empty($ids)) { $this->error(__("Parameter %s can not be empty", "ids")); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $item) { // 删除相关的测量数据 Db::name('body_measurements')->where('profile_id', $item[$pk])->delete(); // 删除相关的体型选择数据 Db::name('body_type_selections')->where('profile_id', $item[$pk])->delete(); // 删除相关的AI报告 Db::name('ai_reports')->where('profile_id', $item[$pk])->delete(); $count += $item->delete(); } Db::commit(); } catch (PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(); } $this->error(__("No rows were deleted")); } /** * 查看详情 */ public function detail($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 测量数据管理 */ public function measurements($profile_id = null) { $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } $this->view->assign("profile", $profile); return $this->view->fetch(); } /** * 添加测量数据 */ public function addMeasurement($profile_id = null) { $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } if ($this->request->isPost()) { $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__("Parameter %s can not be empty", "")); } // TODO: 实现添加测量数据逻辑 $this->success(); } $this->view->assign("profile", $profile); return $this->view->fetch(); } /** * 体型选择管理 */ public function bodyTypes($profile_id = null) { $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } $this->view->assign("profile", $profile); return $this->view->fetch(); } /** * 生成AI报告 */ public function generateReport($profile_id = null) { $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) { $this->error(__("You have no permission")); } // TODO: 实现AI报告生成逻辑 $this->success("AI报告生成功能开发中..."); } /** * 查看AI报告 */ public function viewReport($report_id = null) { if (!$report_id) { $this->error(__("Parameter %s can not be empty", "report_id")); } // TODO: 实现AI报告查看逻辑 $this->view->assign("report_id", $report_id); return $this->view->fetch(); } /** * AI测量 */ public function aiMeasurement($profile_id = null) { if ($this->request->isPost()) { $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } // TODO: 实现AI测量逻辑 $this->success("AI测量功能开发中..."); } $profile = $this->model->get($profile_id); if (!$profile) { $this->error(__("No Results were found")); } $this->view->assign("profile", $profile); return $this->view->fetch(); } /** * 统计分析 */ public function statistics() { $adminIds = $this->getDataLimitAdminIds(); $where = []; if (is_array($adminIds)) { $where[$this->dataLimitField] = ['in', $adminIds]; } // 基础统计 $totalProfiles = $this->model->where($where)->count(); $maleCount = $this->model->where($where)->where('gender', 1)->count(); $femaleCount = $this->model->where($where)->where('gender', 2)->count(); $ownProfiles = $this->model->where($where)->where('is_own', 1)->count(); $otherProfiles = $this->model->where($where)->where('is_own', 0)->count(); $statistics = [ 'total_profiles' => $totalProfiles, 'male_count' => $maleCount, 'female_count' => $femaleCount, 'own_profiles' => $ownProfiles, 'other_profiles' => $otherProfiles, ]; $this->view->assign("statistics", $statistics); return $this->view->fetch(); } }