CheckupController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\WellnessRecord;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. class CheckupController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new WellnessRecord();
  16. $this->validate = true;
  17. $this->validateClass = new WellnessRecordValidate();
  18. }
  19. /**
  20. * @Desc 查询列表
  21. * @Author Gorden
  22. * @Date 2024/3/21 9:58
  23. *
  24. * @param Request $request
  25. * @return Response
  26. * @throws \support\exception\BusinessException
  27. */
  28. public function select(Request $request): Response
  29. {
  30. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  31. $field = $field ?? 'wellness_record_addtimes';
  32. $order = $request->get('order', 'desc');
  33. $where['wellness_record_category'] = 'Examine';
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  38. {
  39. $model = $this->model->with([
  40. 'member' => function ($query) {
  41. $query->select('member_id', 'member_mobile');
  42. },
  43. 'cert' => function ($query) {
  44. $query->select('join_cert_member_id', 'member_cert_name');
  45. }
  46. ]);
  47. foreach ($where as $column => $value) {
  48. if (is_array($value)) {
  49. if ($value[0] === 'like' || $value[0] === 'not like') {
  50. $model = $model->where($column, $value[0], "%$value[1]%");
  51. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  52. $model = $model->where($column, $value[0], $value[1]);
  53. } elseif ($value[0] == 'in' && !empty($value[1])) {
  54. $valArr = $value[1];
  55. if (is_string($value[1])) {
  56. $valArr = explode(",", trim($value[1]));
  57. }
  58. $model = $model->whereIn($column, $valArr);
  59. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  60. $valArr = $value[1];
  61. if (is_string($value[1])) {
  62. $valArr = explode(",", trim($value[1]));
  63. }
  64. $model = $model->whereNotIn($column, $valArr);
  65. } elseif ($value[0] == 'null') {
  66. $model = $model->whereNull($column);
  67. } elseif ($value[0] == 'not null') {
  68. $model = $model->whereNotNull($column);
  69. } elseif ($value[0] !== '' || $value[1] !== '') {
  70. $model = $model->whereBetween($column, $value);
  71. }
  72. } else {
  73. $model = $model->where($column, $value);
  74. }
  75. }
  76. if ($field) {
  77. $model = $model->orderBy($field, $order);
  78. }
  79. return $model;
  80. }
  81. protected function afterQuery($items)
  82. {
  83. foreach ($items as &$item) {
  84. if (!empty($item->wellness_record_file_json)) {
  85. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  86. $wellnessRecordFileJson = [];
  87. foreach ($item->wellness_record_file_json as $value) {
  88. if (empty($value['url']) || empty($value['name'])){
  89. continue;
  90. }
  91. $wellnessRecordFileJson[] = [
  92. 'url' => getenv('STORAGE_DOMAIN') . $value['url'],
  93. 'name' => $value['name']
  94. ];
  95. }
  96. $item->wellness_record_file_json = $wellnessRecordFileJson;
  97. }
  98. }
  99. return $items;
  100. }
  101. protected function insertInput(Request $request): array
  102. {
  103. $data = $this->inputFilter($request->post());
  104. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  105. if (!empty($data['wellness_record_file_json'])) {
  106. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  107. $wellnessRecordFileJson = [];
  108. foreach ($data['wellness_record_file_json'] as $value) {
  109. $wellnessRecordFileJson[] = [
  110. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  111. 'name' => $value['name']
  112. ];
  113. }
  114. $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
  115. } else {
  116. $data['wellness_record_file_json'] = '[]';
  117. }
  118. return $data;
  119. }
  120. protected function updateInput(Request $request): array
  121. {
  122. $primary_key = $this->model->getKeyName();
  123. $id = $request->post($primary_key);
  124. $data = $this->inputFilter($request->post());
  125. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  126. if (!empty($data['wellness_record_file_json'])) {
  127. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  128. $wellnessRecordFileJson = [];
  129. foreach ($data['wellness_record_file_json'] as $value) {
  130. $wellnessRecordFileJson[] = [
  131. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  132. 'name' => $value['name']
  133. ];
  134. }
  135. $data['wellness_record_file_json'] = $wellnessRecordFileJson;
  136. }
  137. $model = $this->model->find($id);
  138. if (!$model) {
  139. throw new BusinessException('记录不存在', 2);
  140. }
  141. unset($data[$primary_key]);
  142. return [$id, $data];
  143. }
  144. }