VisitingController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Customer;
  8. use app\model\WellnessRecord;
  9. use support\exception\BusinessException;
  10. use support\Request;
  11. use support\Response;
  12. class VisitingController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new WellnessRecord();
  17. $this->validate = true;
  18. $this->validateClass = new WellnessRecordValidate();
  19. }
  20. /**
  21. * @Desc 查询列表
  22. * @Author Gorden
  23. * @Date 2024/3/21 9:58
  24. *
  25. * @param Request $request
  26. * @return Response
  27. * @throws \support\exception\BusinessException
  28. */
  29. public function select(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $field = $field ?? 'wellness_record_addtimes';
  33. $order = $request->get('order', 'desc');
  34. $where['wellness_record_category'] = 'Visiting';
  35. $m = $request->get('m', 'MEMBER');
  36. if ($m == 'MEMBER') {
  37. $isOwner = 'N';
  38. $query = $this->doSelect($where, $field, $order, $isOwner);
  39. } elseif ($m == 'OWNER') {
  40. $isOwner = 'Y';
  41. $query = $this->doSelect($where, $field, $order, $isOwner);
  42. } elseif ($m == 'CUSTOMER') {
  43. $field = 'created_at';
  44. $query = $this->doSelectCustomer($where, $field, $order);
  45. }
  46. return $this->doFormat($query, $format, $limit);
  47. }
  48. protected function doSelect(array $where, string $field = null, string $order = 'desc', $isOwner = 'N')
  49. {
  50. $model = $this->model->leftJoin('member', 'member.member_id', '=', 'wellness_record.join_wellness_record_member_id')
  51. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'wellness_record.join_wellness_record_member_id')
  52. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'wellness_record.*')
  53. ->where('member.member_is_owner', $isOwner);
  54. // ->with([
  55. // 'member' => function ($query) {
  56. // $query->where('member_is_owner', 'N')->select('member_id', 'member_mobile');
  57. // },
  58. // 'cert' => function ($query) {
  59. // $query->select('join_cert_member_id', 'member_cert_name');
  60. // }
  61. // ]);
  62. foreach ($where as $column => $value) {
  63. if (is_array($value)) {
  64. if ($value[0] === 'like' || $value[0] === 'not like') {
  65. $model = $model->where($column, $value[0], "%$value[1]%");
  66. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  67. $model = $model->where($column, $value[0], $value[1]);
  68. } elseif ($value[0] == 'in' && !empty($value[1])) {
  69. $valArr = $value[1];
  70. if (is_string($value[1])) {
  71. $valArr = explode(",", trim($value[1]));
  72. }
  73. $model = $model->whereIn($column, $valArr);
  74. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  75. $valArr = $value[1];
  76. if (is_string($value[1])) {
  77. $valArr = explode(",", trim($value[1]));
  78. }
  79. $model = $model->whereNotIn($column, $valArr);
  80. } elseif ($value[0] == 'null') {
  81. $model = $model->whereNull($column);
  82. } elseif ($value[0] == 'not null') {
  83. $model = $model->whereNotNull($column);
  84. } elseif ($value[0] !== '' || $value[1] !== '') {
  85. $model = $model->whereBetween($column, $value);
  86. }
  87. } else {
  88. $model = $model->where($column, $value);
  89. }
  90. }
  91. if ($field) {
  92. $model = $model->orderBy($field, $order);
  93. }
  94. return $model;
  95. }
  96. protected function doSelectCustomer(array $where, string $field = null, string $order = 'desc')
  97. {
  98. $model = $this->model->leftJoin('customer', 'customer.id', '=', 'wellness_record.join_wellness_record_member_id')
  99. ->select('customer.name', 'customer.mobile', 'wellness_record.*')
  100. ->where('customer.name','<>', "");
  101. // ->with([
  102. // 'member' => function ($query) {
  103. // $query->where('member_is_owner', 'N')->select('member_id', 'member_mobile');
  104. // },
  105. // 'cert' => function ($query) {
  106. // $query->select('join_cert_member_id', 'member_cert_name');
  107. // }
  108. // ]);
  109. foreach ($where as $column => $value) {
  110. if (is_array($value)) {
  111. if ($value[0] === 'like' || $value[0] === 'not like') {
  112. $model = $model->where($column, $value[0], "%$value[1]%");
  113. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  114. $model = $model->where($column, $value[0], $value[1]);
  115. } elseif ($value[0] == 'in' && !empty($value[1])) {
  116. $valArr = $value[1];
  117. if (is_string($value[1])) {
  118. $valArr = explode(",", trim($value[1]));
  119. }
  120. $model = $model->whereIn($column, $valArr);
  121. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  122. $valArr = $value[1];
  123. if (is_string($value[1])) {
  124. $valArr = explode(",", trim($value[1]));
  125. }
  126. $model = $model->whereNotIn($column, $valArr);
  127. } elseif ($value[0] == 'null') {
  128. $model = $model->whereNull($column);
  129. } elseif ($value[0] == 'not null') {
  130. $model = $model->whereNotNull($column);
  131. } elseif ($value[0] !== '' || $value[1] !== '') {
  132. $model = $model->whereBetween($column, $value);
  133. }
  134. } else {
  135. $model = $model->where($column, $value);
  136. }
  137. }
  138. if ($field) {
  139. $model = $model->orderBy($field, $order);
  140. }
  141. return $model;
  142. }
  143. protected function afterQuery($items)
  144. {
  145. foreach ($items as &$item) {
  146. if (!empty($item->wellness_record_file_json)) {
  147. $item->wellness_record_file_json = json_decode($item->wellness_record_file_json, true);
  148. $wellnessRecordFileJson = [];
  149. foreach ($item->wellness_record_file_json as $value) {
  150. $wellnessRecordFileJson[] = [
  151. 'url' => getenv('STORAGE_DOMAIN') . $value['url'],
  152. 'name' => $value['name']
  153. ];
  154. }
  155. $item->wellness_record_file_json = $wellnessRecordFileJson;
  156. }
  157. if (empty($item->member)) {
  158. unset($item);
  159. }
  160. }
  161. return $items;
  162. }
  163. protected function insertInput(Request $request): array
  164. {
  165. $data = $this->inputFilter($request->post());
  166. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  167. if (!empty($data['wellness_record_file_json'])) {
  168. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  169. $wellnessRecordFileJson = [];
  170. foreach ($data['wellness_record_file_json'] as $value) {
  171. $wellnessRecordFileJson[] = [
  172. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  173. 'name' => $value['name']
  174. ];
  175. }
  176. $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
  177. } else {
  178. $data['wellness_record_file_json'] = '[]';
  179. }
  180. return $data;
  181. }
  182. protected function updateInput(Request $request): array
  183. {
  184. $primary_key = $this->model->getKeyName();
  185. $id = $request->post($primary_key);
  186. $data = $this->inputFilter($request->post());
  187. $data['wellness_record_datetime'] = date('Y-m-d H:i:s', strtotime($data['wellness_record_datetime']));
  188. if (!empty($data['wellness_record_file_json'])) {
  189. $data['wellness_record_file_json'] = json_decode($data['wellness_record_file_json'], true);
  190. $wellnessRecordFileJson = [];
  191. foreach ($data['wellness_record_file_json'] as $value) {
  192. $wellnessRecordFileJson[] = [
  193. 'url' => str_replace(getenv('STORAGE_DOMAIN'), '', $value['url']),
  194. 'name' => $value['name']
  195. ];
  196. }
  197. $data['wellness_record_file_json'] = json_encode($wellnessRecordFileJson);
  198. } else {
  199. $data['wellness_record_file_json'] = '[]';
  200. }
  201. $model = $this->model->find($id);
  202. if (!$model) {
  203. throw new BusinessException('记录不存在', 2);
  204. }
  205. unset($data[$primary_key]);
  206. return [$id, $data];
  207. }
  208. }