123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\BodyProfile as BodyProfileModel;
- use app\common\model\BodyMeasurements;
- use app\common\model\BodyTypeConfig;
- use app\common\model\BodyTypeSelection;
- use app\common\model\BodyAiReport;
- use think\Db;
- use think\exception\ValidateException;
- /**
- * 身体档案管理
- */
- class BodyProfile extends Backend
- {
- protected $model = null;
- protected $noNeedLogin = [];
- protected $noNeedRight = [];
- protected $searchFields = 'profile_name,relation';
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|