BodyProfile.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\BodyProfile as BodyProfileModel;
  5. use app\common\model\BodyMeasurements;
  6. use app\common\model\BodyTypeConfig;
  7. use app\common\model\BodyTypeSelection;
  8. use app\common\model\BodyAiReport;
  9. use think\Db;
  10. use think\exception\ValidateException;
  11. use think\exception\PDOException;
  12. use think\exception\DbException;
  13. use Exception;
  14. /**
  15. * 身体档案管理
  16. *
  17. * @icon fa fa-user
  18. * @remark 管理用户身体档案信息,包括基础数据、测量记录、体型选择等
  19. */
  20. class BodyProfile extends Backend
  21. {
  22. /**
  23. * BodyProfile模型对象
  24. * @var \app\common\model\BodyProfile
  25. */
  26. protected $model = null;
  27. /**
  28. * 无需登录的方法,同时也就无需鉴权了
  29. * @var array
  30. */
  31. protected $noNeedLogin = [];
  32. /**
  33. * 无需鉴权的方法,但需要登录
  34. * @var array
  35. */
  36. protected $noNeedRight = [];
  37. /**
  38. * 快速搜索时执行查找的字段
  39. * @var string
  40. */
  41. protected $searchFields = 'profile_name,relation';
  42. /**
  43. * 关联查询
  44. * @var array
  45. */
  46. protected $relationSearch = true;
  47. public function _initialize()
  48. {
  49. parent::_initialize();
  50. $this->model = new BodyProfileModel;
  51. }
  52. /**
  53. * 默认生成的控制器所继承的父类中有index方法,在这里重写下
  54. */
  55. public function index()
  56. {
  57. //当前是否为关联查询
  58. $this->relationSearch = true;
  59. //设置过滤方法
  60. $this->request->filter(['strip_tags', 'trim']);
  61. if ($this->request->isAjax()) {
  62. //如果发送的来源是Selectpage,则转发到Selectpage
  63. if ($this->request->request('keyField')) {
  64. return $this->selectpage();
  65. }
  66. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  67. $list = $this->model
  68. ->with(['user'])
  69. ->where($where)
  70. ->order($sort, $order)
  71. ->paginate($limit);
  72. foreach ($list as $row) {
  73. $row->visible(['id','profile_name','user_id','gender','is_own','relation','age','height','weight','createtime','updatetime']);
  74. $row->visible(['user']);
  75. $row->getRelation('user')->visible(['username','nickname']);
  76. // 计算BMI
  77. $row['bmi'] = $row->calculateBMI();
  78. $row['bmi_level'] = $row->getBMILevel();
  79. }
  80. $result = array("total" => $list->total(), "rows" => $list->items());
  81. return json($result);
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. if ($params) {
  93. $params = $this->preExcludeFields($params);
  94. // 处理身体照片
  95. if (isset($params['body_photos']) && is_array($params['body_photos'])) {
  96. $params['body_photos'] = json_encode($params['body_photos']);
  97. }
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. //是否采用模型验证
  102. if ($this->modelValidate) {
  103. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  105. $this->model->validateFailException(true)->validate($validate);
  106. }
  107. $result = $this->model->allowField(true)->save($params);
  108. Db::commit();
  109. } catch (ValidateException $e) {
  110. Db::rollback();
  111. $this->error($e->getMessage());
  112. } catch (PDOException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (Exception $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. }
  119. if ($result !== false) {
  120. $this->success();
  121. } else {
  122. $this->error(__('No rows were inserted'));
  123. }
  124. }
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $this->view->assign('genderList', ['1' => __('Male'), '2' => __('Female')]);
  128. $this->view->assign('isOwnList', ['1' => __('Own profile'), '0' => __('Others profile')]);
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 编辑
  133. */
  134. public function edit($ids = null)
  135. {
  136. $row = $this->model->get($ids);
  137. if (!$row) {
  138. $this->error(__('No Results were found'));
  139. }
  140. $adminIds = $this->getDataLimitAdminIds();
  141. if (is_array($adminIds)) {
  142. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  143. $this->error(__('You have no permission'));
  144. }
  145. }
  146. if ($this->request->isPost()) {
  147. $params = $this->request->post("row/a");
  148. if ($params) {
  149. $params = $this->preExcludeFields($params);
  150. // 处理身体照片
  151. if (isset($params['body_photos']) && is_array($params['body_photos'])) {
  152. $params['body_photos'] = json_encode($params['body_photos']);
  153. }
  154. $result = false;
  155. Db::startTrans();
  156. try {
  157. //是否采用模型验证
  158. if ($this->modelValidate) {
  159. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  160. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  161. $row->validateFailException(true)->validate($validate);
  162. }
  163. $result = $row->allowField(true)->save($params);
  164. Db::commit();
  165. } catch (ValidateException $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. } catch (PDOException $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. } catch (Exception $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. }
  175. if ($result !== false) {
  176. $this->success();
  177. } else {
  178. $this->error(__('No rows were updated'));
  179. }
  180. }
  181. $this->error(__('Parameter %s can not be empty', ''));
  182. }
  183. $this->view->assign('row', $row);
  184. $this->view->assign('genderList', ['1' => __('Male'), '2' => __('Female')]);
  185. $this->view->assign('isOwnList', ['1' => __('Own profile'), '0' => __('Others profile')]);
  186. return $this->view->fetch();
  187. }
  188. /**
  189. * 删除
  190. */
  191. public function del($ids = null)
  192. {
  193. if (false === $this->request->isPost()) {
  194. $this->error(__("Invalid parameters"));
  195. }
  196. $ids = $ids ?: $this->request->post("ids");
  197. if (empty($ids)) {
  198. $this->error(__("Parameter %s can not be empty", "ids"));
  199. }
  200. $pk = $this->model->getPk();
  201. $adminIds = $this->getDataLimitAdminIds();
  202. if (is_array($adminIds)) {
  203. $this->model->where($this->dataLimitField, 'in', $adminIds);
  204. }
  205. $list = $this->model->where($pk, 'in', $ids)->select();
  206. $count = 0;
  207. Db::startTrans();
  208. try {
  209. foreach ($list as $item) {
  210. // 删除相关的测量数据
  211. Db::name('body_measurements')->where('profile_id', $item[$pk])->delete();
  212. // 删除相关的体型选择数据
  213. Db::name('body_type_selections')->where('profile_id', $item[$pk])->delete();
  214. // 删除相关的AI报告
  215. Db::name('ai_reports')->where('profile_id', $item[$pk])->delete();
  216. $count += $item->delete();
  217. }
  218. Db::commit();
  219. } catch (PDOException | Exception $e) {
  220. Db::rollback();
  221. $this->error($e->getMessage());
  222. }
  223. if ($count) {
  224. $this->success();
  225. }
  226. $this->error(__("No rows were deleted"));
  227. }
  228. /**
  229. * 查看详情
  230. */
  231. public function detail($ids = null)
  232. {
  233. $row = $this->model->get($ids);
  234. if (!$row) {
  235. $this->error(__("No Results were found"));
  236. }
  237. $adminIds = $this->getDataLimitAdminIds();
  238. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  239. $this->error(__("You have no permission"));
  240. }
  241. $this->view->assign("row", $row);
  242. return $this->view->fetch();
  243. }
  244. /**
  245. * 测量数据管理
  246. */
  247. public function measurements($profile_id = null)
  248. {
  249. $profile = $this->model->get($profile_id);
  250. if (!$profile) {
  251. $this->error(__("No Results were found"));
  252. }
  253. $adminIds = $this->getDataLimitAdminIds();
  254. if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) {
  255. $this->error(__("You have no permission"));
  256. }
  257. $this->view->assign("profile", $profile);
  258. return $this->view->fetch();
  259. }
  260. /**
  261. * 添加测量数据
  262. */
  263. public function addMeasurement($profile_id = null)
  264. {
  265. $profile = $this->model->get($profile_id);
  266. if (!$profile) {
  267. $this->error(__("No Results were found"));
  268. }
  269. $adminIds = $this->getDataLimitAdminIds();
  270. if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) {
  271. $this->error(__("You have no permission"));
  272. }
  273. if ($this->request->isPost()) {
  274. $params = $this->request->post('row/a');
  275. if (empty($params)) {
  276. $this->error(__("Parameter %s can not be empty", ""));
  277. }
  278. // TODO: 实现添加测量数据逻辑
  279. $this->success();
  280. }
  281. $this->view->assign("profile", $profile);
  282. return $this->view->fetch();
  283. }
  284. /**
  285. * 体型选择管理
  286. */
  287. public function bodyTypes($profile_id = null)
  288. {
  289. $profile = $this->model->get($profile_id);
  290. if (!$profile) {
  291. $this->error(__("No Results were found"));
  292. }
  293. $adminIds = $this->getDataLimitAdminIds();
  294. if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) {
  295. $this->error(__("You have no permission"));
  296. }
  297. $this->view->assign("profile", $profile);
  298. return $this->view->fetch();
  299. }
  300. /**
  301. * 生成AI报告
  302. */
  303. public function generateReport($profile_id = null)
  304. {
  305. $profile = $this->model->get($profile_id);
  306. if (!$profile) {
  307. $this->error(__("No Results were found"));
  308. }
  309. $adminIds = $this->getDataLimitAdminIds();
  310. if (is_array($adminIds) && !in_array($profile[$this->dataLimitField], $adminIds)) {
  311. $this->error(__("You have no permission"));
  312. }
  313. // TODO: 实现AI报告生成逻辑
  314. $this->success("AI报告生成功能开发中...");
  315. }
  316. /**
  317. * 查看AI报告
  318. */
  319. public function viewReport($report_id = null)
  320. {
  321. if (!$report_id) {
  322. $this->error(__("Parameter %s can not be empty", "report_id"));
  323. }
  324. // TODO: 实现AI报告查看逻辑
  325. $this->view->assign("report_id", $report_id);
  326. return $this->view->fetch();
  327. }
  328. /**
  329. * AI测量
  330. */
  331. public function aiMeasurement($profile_id = null)
  332. {
  333. if ($this->request->isPost()) {
  334. $profile = $this->model->get($profile_id);
  335. if (!$profile) {
  336. $this->error(__("No Results were found"));
  337. }
  338. // TODO: 实现AI测量逻辑
  339. $this->success("AI测量功能开发中...");
  340. }
  341. $profile = $this->model->get($profile_id);
  342. if (!$profile) {
  343. $this->error(__("No Results were found"));
  344. }
  345. $this->view->assign("profile", $profile);
  346. return $this->view->fetch();
  347. }
  348. /**
  349. * 统计分析
  350. */
  351. public function statistics()
  352. {
  353. $adminIds = $this->getDataLimitAdminIds();
  354. $where = [];
  355. if (is_array($adminIds)) {
  356. $where[$this->dataLimitField] = ['in', $adminIds];
  357. }
  358. // 基础统计
  359. $totalProfiles = $this->model->where($where)->count();
  360. $maleCount = $this->model->where($where)->where('gender', 1)->count();
  361. $femaleCount = $this->model->where($where)->where('gender', 2)->count();
  362. $ownProfiles = $this->model->where($where)->where('is_own', 1)->count();
  363. $otherProfiles = $this->model->where($where)->where('is_own', 0)->count();
  364. $statistics = [
  365. 'total_profiles' => $totalProfiles,
  366. 'male_count' => $maleCount,
  367. 'female_count' => $femaleCount,
  368. 'own_profiles' => $ownProfiles,
  369. 'other_profiles' => $otherProfiles,
  370. ];
  371. $this->view->assign("statistics", $statistics);
  372. return $this->view->fetch();
  373. }
  374. }