WellnessRecordController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\validate\life\CinemaIlkValidate;
  4. use app\admin\validate\member\WellnessRecordValidate;
  5. use app\controller\Curd;
  6. use app\model\CinemaIlk as CinemaIlkModel;
  7. use app\model\SysField;
  8. use app\model\SysUpload;
  9. use app\model\WellnessRecord;
  10. use support\exception\BusinessException;
  11. use support\Request;
  12. use support\Response;
  13. use Tinywan\Jwt\JwtToken;
  14. class WellnessRecordController extends Curd
  15. {
  16. public function __construct()
  17. {
  18. $this->model = new WellnessRecord();
  19. $this->validate = true;
  20. $this->validateClass = new WellnessRecordValidate();
  21. }
  22. public function info(Request $request): Response
  23. {
  24. $primaryKey = $this->model->getKeyName();
  25. $validateArr = [
  26. $primaryKey => $request->get('id')
  27. ];
  28. if ($this->validate && !$this->validateClass->scene('info')->check($validateArr)) {
  29. return json_fail($this->validateClass->getError());
  30. }
  31. $data = $this->model->where($primaryKey, $request->get('id'))->first();
  32. if ($data['wellness_record_category'] == 'Examine'){
  33. $data->wellness_record_status = 'ACTIVED';
  34. $data->save();
  35. }
  36. if (method_exists($this, "afterInfoQuery")) {
  37. $data = call_user_func([$this, "afterInfoQuery"], $data);
  38. }
  39. return json_success('', $data);
  40. }
  41. public function insert(Request $request): Response
  42. {
  43. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  44. return json_fail($this->validateClass->getError());
  45. }
  46. try {
  47. $data = $this->insertInput($request);
  48. if ($data['wellness_record_category'] == 'Examine' && !empty($data['wellness_record_file_json'])) {
  49. $recordJson = json_decode($data['wellness_record_file_json'], true);
  50. foreach ($recordJson as $file) {
  51. unset($data['wellness_record_file_json']);
  52. $data['wellness_record_file_json'] = json_encode($file);
  53. $this->doInsert($data);
  54. }
  55. }else{
  56. $this->doInsert($data);
  57. }
  58. } catch (BusinessException $customException) {
  59. return json_fail($customException->getMessage());
  60. } catch (\Exception $e) {
  61. dump($e->getMessage());
  62. return json_fail('数据写入失败11');
  63. }
  64. return json_success('success');
  65. }
  66. /**
  67. * @Desc 我的档案
  68. * @Author Gorden
  69. * @Date 2024/4/1 10:54
  70. *
  71. * @param $memberId
  72. * @return mixed
  73. */
  74. public function my($memberId)
  75. {
  76. $query = $this->model->where('wellness_record_category', 'Archives')
  77. ->where('join_wellness_record_member_id', $memberId);
  78. $paginator = $query->paginate(20);
  79. $total = $paginator->total();
  80. $items = $paginator->items();
  81. $itemsNew = [];
  82. foreach ($items as $item) {
  83. $itemNew = [
  84. 'wellness_record_id' => $item->wellness_record_id,
  85. 'join_wellness_record_member_id' => $item->join_wellness_record_member_id,
  86. 'wellness_record_status' => $item->wellness_record_status,
  87. 'wellness_record_name' => $item->wellness_record_name,
  88. 'wellness_record_datetime' => $item->wellness_record_datetime,
  89. 'wellness_record_json' => $item->wellness_record_json,
  90. 'wellness_record_content' => $item->wellness_record_content,
  91. 'wellness_record_remark' => $item->wellness_record_remark,
  92. 'wellness_record_extend_json' => $item->wellness_record_extend_json,
  93. 'wellness_record_addtimes' => date('Y-m-d H:i:s', $item->wellness_record_addtimes),
  94. ];
  95. if (!empty($item->wellness_record_file_json)) {
  96. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  97. $upload = SysUpload::whereIn('upload_file_path', $item->wellness_record_file_json)->get();
  98. foreach ($upload as $value) {
  99. $itemNew['wellness_record_file_json'][] = [
  100. 'url' => getenv('STORAGE_DOMAIN') . $value->upload_file_path,
  101. 'name' => $value->upload_name
  102. ];
  103. }
  104. }
  105. $itemsNew[] = $itemNew;
  106. }
  107. $data = [
  108. 'total' => $total,
  109. 'rows' => $itemsNew
  110. ];
  111. return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  112. }
  113. /**
  114. * @Desc 我的医嘱
  115. * @Author Gorden
  116. * @Date 2024/4/2 8:44
  117. *
  118. * @param $memberId
  119. * @return Response
  120. */
  121. public function advice($memberId)
  122. {
  123. $query = $this->model->where('wellness_record_category', 'Advice')
  124. ->where('join_wellness_record_member_id', $memberId)
  125. ->orderBy('wellness_record_addtimes', 'desc');
  126. $paginator = $query->paginate(20);
  127. $total = $paginator->total();
  128. $items = $paginator->items();
  129. $itemsNew = [];
  130. foreach ($items as $item) {
  131. $itemNew = [
  132. 'wellness_record_id' => $item->wellness_record_id,
  133. 'join_wellness_record_member_id' => $item->join_wellness_record_member_id,
  134. 'wellness_record_status' => $item->wellness_record_status,
  135. 'wellness_record_name' => $item->wellness_record_name,
  136. 'wellness_record_datetime' => $item->wellness_record_datetime,
  137. 'wellness_record_json' => $item->wellness_record_json,
  138. 'wellness_record_content' => $item->wellness_record_content,
  139. 'wellness_record_remark' => $item->wellness_record_remark,
  140. 'wellness_record_extend_json' => $item->wellness_record_extend_json,
  141. 'wellness_record_addtimes' => $item->wellness_record_addtimes,
  142. ];
  143. if (!empty($item->wellness_record_file_json)) {
  144. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  145. foreach ($item->wellness_record_file_json as $value) {
  146. $itemNew['wellness_record_file_json'][] = [
  147. 'url' => isset($value['url']) ? getenv('STORAGE_DOMAIN') . $value['url'] : '',
  148. 'name' => isset($value['name']) ? $value['name'] : ''
  149. ];
  150. }
  151. }
  152. if (!empty($item->wellness_record_json)) {
  153. $itemNew['wellness_record_json'] = json_decode($item->wellness_record_json);
  154. }
  155. if (!empty($item->wellness_record_extend_json)) {
  156. $itemNew['wellness_record_extend_json'] = json_decode($item->wellness_record_extend_json);
  157. }
  158. $itemsNew[] = $itemNew;
  159. }
  160. $data = [
  161. 'total' => $total,
  162. 'rows' => $itemsNew
  163. ];
  164. return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  165. }
  166. /**
  167. * @Desc 我的档案
  168. * @Author Gorden
  169. * @Date 2024/4/22 10:46
  170. *
  171. * @param $memberId
  172. * @return Response
  173. */
  174. public function archives($memberId)
  175. {
  176. $archives = $this->model->where('wellness_record_category', 'Archives')
  177. ->where('join_wellness_record_member_id', $memberId)
  178. ->first();
  179. if ($archives) {
  180. if (!empty($archives->wellness_record_json)) {
  181. $recordJson = json_decode($archives->wellness_record_json);
  182. $fields = SysField::pluck('field_name', 'field_form_key');
  183. $newJson = [];
  184. foreach ($recordJson as $key => $item) {
  185. if (isset($fields[$key])) {
  186. $newJson[$fields[$key]] = is_array($item) ? implode(',', $item) : $item;
  187. }
  188. }
  189. $archives->wellness_record_json = $newJson;
  190. }
  191. if (!empty($archives->wellness_record_file_json)) {
  192. $archives->wellness_record_file_json = json_decode($archives->wellness_record_file_json, true);
  193. $recordFileJson = [];
  194. foreach ($archives->wellness_record_file_json as $value) {
  195. $recordFileJson[] = [
  196. 'url' => isset($value['url']) ? getenv('STORAGE_DOMAIN') . $value['url'] : '',
  197. 'name' => isset($value['name']) ? $value['name'] : ''
  198. ];
  199. }
  200. $archives->wellness_record_file_json = $recordFileJson;
  201. }
  202. }
  203. // ->orderBy('wellness_record_addtimes', 'desc');
  204. // $paginator = $query->paginate(20);
  205. // $total = $paginator->total();
  206. // $items = $paginator->items();
  207. // $itemsNew = [];
  208. // foreach ($items as $item) {
  209. // $itemNew = [
  210. // 'wellness_record_id' => $item->wellness_record_id,
  211. // 'join_wellness_record_member_id' => $item->join_wellness_record_member_id,
  212. // 'wellness_record_status' => $item->wellness_record_status,
  213. // 'wellness_record_name' => $item->wellness_record_name,
  214. // 'wellness_record_datetime' => $item->wellness_record_datetime,
  215. // 'wellness_record_json' => $item->wellness_record_json,
  216. // 'wellness_record_content' => $item->wellness_record_content,
  217. // 'wellness_record_remark' => $item->wellness_record_remark,
  218. // 'wellness_record_extend_json' => $item->wellness_record_extend_json,
  219. // 'wellness_record_addtimes' => $item->wellness_record_addtimes,
  220. // ];
  221. // if (!empty($item->wellness_record_file_json)) {
  222. // $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  223. // foreach ($item->wellness_record_file_json as $value) {
  224. // $itemNew['wellness_record_file_json'][] = [
  225. // 'url' => isset($value['url']) ? getenv('STORAGE_DOMAIN') . $value['url'] : '',
  226. // 'name' => isset($value['name']) ? $value['name'] : ''
  227. // ];
  228. // }
  229. // }
  230. //
  231. // $itemsNew[] = $itemNew;
  232. // }
  233. //
  234. // $data = [
  235. // 'total' => $total,
  236. // 'rows' => $itemsNew
  237. // ];
  238. // return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  239. return json_success('', $archives);
  240. }
  241. public function checkup($memberId)
  242. {
  243. $query = $this->model->where('wellness_record_category', 'Examine')
  244. ->where('join_wellness_record_member_id', $memberId)
  245. ->orderBy('wellness_record_addtimes', 'desc');
  246. $paginator = $query->paginate(20);
  247. $total = $paginator->total();
  248. $items = $paginator->items();
  249. $itemsNew = [];
  250. foreach ($items as $item) {
  251. $itemNew = [
  252. 'wellness_record_id' => $item->wellness_record_id,
  253. 'join_wellness_record_member_id' => $item->join_wellness_record_member_id,
  254. 'wellness_record_status' => $item->wellness_record_status,
  255. 'wellness_record_name' => $item->wellness_record_name,
  256. 'wellness_record_datetime' => $item->wellness_record_datetime,
  257. 'wellness_record_json' => $item->wellness_record_json,
  258. 'wellness_record_content' => $item->wellness_record_content,
  259. 'wellness_record_remark' => $item->wellness_record_remark,
  260. 'wellness_record_extend_json' => $item->wellness_record_extend_json,
  261. 'wellness_record_addtimes' => $item->wellness_record_addtimes,
  262. ];
  263. if (!empty($item->wellness_record_file_json)) {
  264. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  265. if (isset($item->wellness_record_file_json['file_filename']) && isset($item->wellness_record_file_json['file_savepath'])) {
  266. $itemNew['wellness_record_file_json'][] = [
  267. 'url' => getenv('APP_IMAGE_DOMAIN') .$item->wellness_record_file_json['file_savepath'].$item->wellness_record_file_json['file_savename'],
  268. 'name' => $item->wellness_record_file_json['file_filename']
  269. ];
  270. } else if (isset($item->wellness_record_file_json['url']) && isset($item->wellness_record_file_json['name'])) {
  271. $itemNew['wellness_record_file_json'][] = [
  272. 'url' => getenv('STORAGE_DOMAIN') . $item->wellness_record_file_json['url'],
  273. 'name' => $item->wellness_record_file_json['name']
  274. ];
  275. } else {
  276. foreach ($item->wellness_record_file_json as $value) {
  277. $itemNew['wellness_record_file_json'][] = [
  278. 'url' => isset($value['url']) ? getenv('STORAGE_DOMAIN') . $value['url'] : '',
  279. 'name' => isset($value['name']) ? $value['name'] : ''
  280. ];
  281. }
  282. }
  283. }
  284. $itemsNew[] = $itemNew;
  285. }
  286. $data = [
  287. 'total' => $total,
  288. 'rows' => $itemsNew
  289. ];
  290. return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  291. }
  292. public function visit($memberId)
  293. {
  294. $query = $this->model->where('wellness_record_category', 'Visiting')
  295. ->where('join_wellness_record_member_id', $memberId)
  296. ->orderBy('wellness_record_addtimes', 'desc');
  297. $paginator = $query->paginate(20);
  298. $total = $paginator->total();
  299. $items = $paginator->items();
  300. $itemsNew = [];
  301. foreach ($items as $item) {
  302. $itemNew = [
  303. 'wellness_record_id' => $item->wellness_record_id,
  304. 'join_wellness_record_member_id' => $item->join_wellness_record_member_id,
  305. 'wellness_record_status' => $item->wellness_record_status,
  306. 'wellness_record_name' => $item->wellness_record_name,
  307. 'wellness_record_datetime' => $item->wellness_record_datetime,
  308. 'wellness_record_json' => $item->wellness_record_json,
  309. 'wellness_record_content' => $item->wellness_record_content,
  310. 'wellness_record_remark' => $item->wellness_record_remark,
  311. 'wellness_record_extend_json' => $item->wellness_record_extend_json,
  312. 'wellness_record_addtimes' => $item->wellness_record_addtimes,
  313. ];
  314. if (!empty($item->wellness_record_file_json)) {
  315. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  316. foreach ($item->wellness_record_file_json as $value) {
  317. $itemNew['wellness_record_file_json'][] = [
  318. 'url' => isset($value['url']) ? getenv('STORAGE_DOMAIN') . $value['url'] : '',
  319. 'name' => isset($value['name']) ? $value['name'] : ''
  320. ];
  321. }
  322. }
  323. $itemsNew[] = $itemNew;
  324. }
  325. $data = [
  326. 'total' => $total,
  327. 'rows' => $itemsNew
  328. ];
  329. return json(['code' => 200, 'msg' => 'success', 'data' => $data]);
  330. }
  331. /**
  332. * @Desc 查询列表
  333. * @Author Gorden
  334. * @Date 2024/3/21 9:58
  335. *
  336. * @param Request $request
  337. * @return Response
  338. * @throws \support\exception\BusinessException
  339. */
  340. public function select(Request $request): Response
  341. {
  342. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  343. $field = $field ?? 'wellness_record_addtimes';
  344. $order = $request->get('order', 'desc');
  345. $where['wellness_record_category'] = 'Archives';
  346. $query = $this->doSelect($where, $field, $order);
  347. return $this->doFormat($query, $format, $limit);
  348. }
  349. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  350. {
  351. $model = $this->model->with([
  352. 'member' => function ($query) {
  353. $query->select('member_id', 'member_mobile');
  354. },
  355. 'cert' => function ($query) {
  356. $query->select('join_cert_member_id', 'member_cert_name');
  357. }
  358. ]);
  359. foreach ($where as $column => $value) {
  360. if (is_array($value)) {
  361. if ($value[0] === 'like' || $value[0] === 'not like') {
  362. $model = $model->where($column, $value[0], "%$value[1]%");
  363. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  364. $model = $model->where($column, $value[0], $value[1]);
  365. } elseif ($value[0] == 'in' && !empty($value[1])) {
  366. $valArr = $value[1];
  367. if (is_string($value[1])) {
  368. $valArr = explode(",", trim($value[1]));
  369. }
  370. $model = $model->whereIn($column, $valArr);
  371. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  372. $valArr = $value[1];
  373. if (is_string($value[1])) {
  374. $valArr = explode(",", trim($value[1]));
  375. }
  376. $model = $model->whereNotIn($column, $valArr);
  377. } elseif ($value[0] == 'null') {
  378. $model = $model->whereNull($column);
  379. } elseif ($value[0] == 'not null') {
  380. $model = $model->whereNotNull($column);
  381. } elseif ($value[0] !== '' || $value[1] !== '') {
  382. $model = $model->whereBetween($column, $value);
  383. }
  384. } else {
  385. $model = $model->where($column, $value);
  386. }
  387. }
  388. if ($field) {
  389. $model = $model->orderBy($field, $order);
  390. }
  391. return $model;
  392. }
  393. protected function afterQuery($items)
  394. {
  395. foreach ($items as &$item) {
  396. if (!empty($item->wellness_record_file_json)) {
  397. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  398. $wellnessRecordFileJson = [];
  399. if (isset($item->wellness_record_file_json['file_filename']) && isset($item->wellness_record_file_json['file_savepath'])) {
  400. $wellnessRecordFileJson[] = [
  401. 'url' => getenv('STORAGE_DOMAIN') . $item->wellness_record_file_json['file_savepath'],
  402. 'name' => $item->wellness_record_file_json['file_filename']
  403. ];
  404. } else if (isset($item->wellness_record_file_json['url']) && isset($item->wellness_record_file_json['name'])) {
  405. $wellnessRecordFileJson[] = [
  406. 'url' => getenv('STORAGE_DOMAIN') . $item->wellness_record_file_json['url'],
  407. 'name' => $item->wellness_record_file_json['name']
  408. ];
  409. } else {
  410. foreach ($item->wellness_record_file_json as $value) {
  411. if (!empty($value['url']) && !empty($value['name'])) {
  412. $wellnessRecordFileJson[] = [
  413. 'url' => getenv('STORAGE_DOMAIN') . $value['url'],
  414. 'name' => $value['name']
  415. ];
  416. }
  417. }
  418. }
  419. $item->wellness_record_file_json = $wellnessRecordFileJson;
  420. }
  421. }
  422. return $items;
  423. }
  424. protected function insertInput(Request $request): array
  425. {
  426. $data = $this->inputFilter($request->post());
  427. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  428. if (!empty($data['wellness_record_file_json'])) {
  429. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  430. $wellnessRecordFileJson = [];
  431. foreach ($data['wellness_record_file_json'] as $value) {
  432. $wellnessRecordFileJson[] = [
  433. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  434. 'name' => $value['name']
  435. ];
  436. }
  437. $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
  438. } else {
  439. $data['wellness_record_file_json'] = '[]';
  440. }
  441. $data['wellness_record_json'] = '[]';
  442. if (!empty($request->post('records'))) {
  443. $data['wellness_record_json'] = json_encode($request->post('records'));
  444. }
  445. // 记录医生
  446. $extend = JwtToken::getExtend();
  447. $data['wellness_record_extend_json'] = json_encode([
  448. 'doctor' => [
  449. 'name' => $extend['name'],
  450. 'header' => '',
  451. 'userid' => $extend['id']
  452. ]]);
  453. return $data;
  454. }
  455. protected function updateInput(Request $request): array
  456. {
  457. $primary_key = $this->model->getKeyName();
  458. $id = $request->post($primary_key);
  459. $data = $this->inputFilter($request->post());
  460. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  461. if (!empty($data['wellness_record_file_json'])) {
  462. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  463. $wellnessRecordFileJson = [];
  464. foreach ($data['wellness_record_file_json'] as $value) {
  465. $wellnessRecordFileJson[] = [
  466. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  467. 'name' => $value['name']
  468. ];
  469. }
  470. $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
  471. } else {
  472. $data['wellness_record_file_json'] = '[]';
  473. }
  474. $data['wellness_record_json'] = '[]';
  475. if (!empty($request->post('records'))) {
  476. $data['wellness_record_json'] = json_encode($request->post('records'));
  477. }
  478. $model = $this->model->find($id);
  479. if (!$model) {
  480. throw new BusinessException('记录不存在', 2);
  481. }
  482. unset($data[$primary_key]);
  483. return [$id, $data];
  484. }
  485. }