123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- <?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;
- use think\exception\PDOException;
- use think\exception\DbException;
- use Exception;
- /**
- * 身体档案管理
- *
- * @icon fa fa-user
- * @remark 管理用户身体档案信息,包括基础数据、测量记录、体型选择等
- */
- class BodyProfile extends Backend
- {
- /**
- * BodyProfile模型对象
- * @var \app\common\model\BodyProfile
- */
- protected $model = null;
-
- /**
- * 无需登录的方法,同时也就无需鉴权了
- * @var array
- */
- protected $noNeedLogin = [];
-
- /**
- * 无需鉴权的方法,但需要登录
- * @var array
- */
- protected $noNeedRight = [];
-
- /**
- * 快速搜索时执行查找的字段
- * @var string
- */
- protected $searchFields = 'profile_name,relation';
-
- /**
- * 关联查询
- * @var array
- */
- protected $relationSearch = true;
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|