model = new BodyProfileModel; } /** * 查看列表 */ public function index() { if ($this->request->isAjax()) { // 分页参数 $page = $this->request->get('page/d', 1); $limit = $this->request->get('limit/d', 10); $search = $this->request->get('search', ''); $gender = $this->request->get('gender', ''); $is_own = $this->request->get('is_own', ''); $where = []; // 搜索条件 if ($search) { $where[] = ['profile_name|relation', 'like', '%' . $search . '%']; } if ($gender !== '') { $where[] = ['gender', '=', $gender]; } if ($is_own !== '') { $where[] = ['is_own', '=', $is_own]; } // 查询数据 $list = $this->model ->with(['user', 'latestMeasurement']) ->where($where) ->order('id DESC') ->paginate($limit) ->each(function($item) { // 添加额外信息 $item['bmi'] = $item->calculateBMI(); $item['bmi_level'] = $item->getBMILevel(); return $item; }); return json(['code' => 0, 'msg' => '', 'count' => $list->total(), 'data' => $list->items()]); } return $this->view->fetch(); } /** * 添加档案 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } // 验证必填字段 if (empty($params['profile_name']) || empty($params['user_id'])) { $this->error('档案名称和用户ID不能为空'); } // 处理身体照片 if (isset($params['body_photos']) && is_array($params['body_photos'])) { $params['body_photos'] = json_encode($params['body_photos']); } Db::startTrans(); try { $result = $this->model->save($params); if ($result === false) { throw new \Exception($this->model->getError()); } Db::commit(); } catch (\Throwable $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } // 获取用户列表 $userList = \app\common\model\User::field('id,username,nickname')->select(); $this->assign('userList', $userList); 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)) { $this->error(__('Parameter %s can not be empty', '')); } // 处理身体照片 if (isset($params['body_photos']) && is_array($params['body_photos'])) { $params['body_photos'] = json_encode($params['body_photos']); } Db::startTrans(); try { $result = $row->save($params); if ($result === false) { throw new \Exception($row->getError()); } Db::commit(); } catch (\Throwable $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } // 获取用户列表 $userList = \app\common\model\User::field('id,username,nickname')->select(); $this->assign('userList', $userList); $this->assign('row', $row); return $this->view->fetch(); } /** * 删除档案 */ public function del($ids = null) { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $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) { // 删除相关数据 BodyMeasurements::where('profile_id', $item->id)->delete(); BodyTypeSelection::where('profile_id', $item->id)->delete(); BodyAiReport::where('profile_id', $item->id)->delete(); $count += $item->delete(); } Db::commit(); } catch (\Throwable $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } /** * 查看档案详情 */ public function detail($ids = null) { $profile = $this->model->with(['user', 'latestMeasurement', 'bodyTypeSelections.typeConfig', 'latestAiReport'])->find($ids); if (!$profile) { $this->error('档案不存在'); } // 获取完整档案数据 $profileData = $profile->getFullProfileData(); $this->assign('profile', $profileData); return $this->view->fetch(); } /** * 测量数据管理 */ public function measurements($profile_id = null) { if (!$profile_id) { $this->error('档案ID不能为空'); } $profile = $this->model->find($profile_id); if (!$profile) { $this->error('档案不存在'); } if ($this->request->isAjax()) { // 获取测量记录 $list = BodyMeasurements::where('profile_id', $profile_id) ->order('measurement_date DESC') ->paginate(10); return json(['code' => 0, 'msg' => '', 'count' => $list->total(), 'data' => $list->items()]); } $this->assign('profile', $profile); return $this->view->fetch(); } /** * 添加测量数据 */ public function addMeasurement($profile_id = null) { if (!$profile_id) { $this->error('档案ID不能为空'); } $profile = $this->model->find($profile_id); if (!$profile) { $this->error('档案不存在'); } if ($this->request->isPost()) { $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params['profile_id'] = $profile_id; // 处理测量日期 if (isset($params['measurement_date']) && $params['measurement_date']) { $params['measurement_date'] = strtotime($params['measurement_date']); } else { $params['measurement_date'] = time(); } $measurement = new BodyMeasurements(); $result = $measurement->save($params); if ($result === false) { $this->error($measurement->getError()); } $this->success(); } // 获取测量字段 $measurementFields = BodyMeasurements::getMeasurementFields($profile->gender); $this->assign('profile', $profile); $this->assign('measurementFields', $measurementFields); return $this->view->fetch(); } /** * 体型选择管理 */ public function bodyTypes($profile_id = null) { if (!$profile_id) { $this->error('档案ID不能为空'); } $profile = $this->model->find($profile_id); if (!$profile) { $this->error('档案不存在'); } if ($this->request->isPost()) { $selections = $this->request->post('selections/a'); if (empty($selections)) { $this->error('请选择体型'); } $result = BodyTypeSelection::saveUserSelections($profile_id, $selections); if (!$result) { $this->error('保存失败'); } $this->success(); } // 获取所有体型分类和选项 $bodyTypeCategories = BodyTypeConfig::getAllCategories($profile->gender); // 获取用户已选择的体型 $userSelections = BodyTypeSelection::getUserSelections($profile_id); $this->assign('profile', $profile); $this->assign('bodyTypeCategories', $bodyTypeCategories); $this->assign('userSelections', $userSelections); return $this->view->fetch(); } /** * 生成AI报告 */ public function generateReport($profile_id = null) { if (!$profile_id) { $this->error('档案ID不能为空'); } $profile = $this->model->find($profile_id); if (!$profile) { $this->error('档案不存在'); } $report = BodyAiReport::generateReport($profile_id); if (!$report) { $this->error('生成报告失败'); } $this->success('报告生成成功', null, ['report_id' => $report->id]); } /** * 查看AI报告 */ public function viewReport($report_id = null) { if (!$report_id) { $this->error('报告ID不能为空'); } $report = BodyAiReport::with(['profile'])->find($report_id); if (!$report) { $this->error('报告不存在'); } $this->assign('report', $report); return $this->view->fetch(); } /** * AI测量页面 */ public function aiMeasurement($profile_id = null) { if (!$profile_id) { $this->error('档案ID不能为空'); } $profile = $this->model->with(['user'])->find($profile_id); if (!$profile) { $this->error('档案不存在'); } $this->assign('profile', $profile); return $this->view->fetch(); } /** * 统计数据 */ public function statistics() { // 档案统计 $profileStats = [ 'total' => $this->model->count(), 'male' => $this->model->where('gender', 1)->count(), 'female' => $this->model->where('gender', 2)->count(), 'own' => $this->model->where('is_own', 1)->count(), 'others' => $this->model->where('is_own', 0)->count(), ]; // BMI分布统计 $bmiStats = []; $profiles = $this->model->where('height', '>', 0)->where('weight', '>', 0)->select(); foreach ($profiles as $profile) { $bmi = $profile->calculateBMI(); $level = $profile->getBMILevel(); $bmiStats[$level] = isset($bmiStats[$level]) ? $bmiStats[$level] + 1 : 1; } // 体型选择统计 $bodyTypeStats = BodyTypeSelection::getSelectionStatistics(); $this->assign('profileStats', $profileStats); $this->assign('bmiStats', $bmiStats); $this->assign('bodyTypeStats', $bodyTypeStats); return $this->view->fetch(); } }