MessageController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\admin\controller\client;
  3. use app\controller\Curd;
  4. use app\model\ClientMessage;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. use support\Response;
  8. class MessageController extends Curd
  9. {
  10. public function __construct()
  11. {
  12. $this->model = new ClientMessage();
  13. }
  14. public function select(Request $request): Response
  15. {
  16. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  17. $order = $request->get('order', 'desc');
  18. $field = $field ?? 'client_message_addtimes';
  19. $query = $this->doSelect($where, $field, $order);
  20. return $this->doFormat($query, $format, $limit);
  21. }
  22. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  23. {
  24. $model = $this->model->with(['member', 'cert']);
  25. foreach ($where as $column => $value) {
  26. if (is_array($value)) {
  27. if ($value[0] === 'like' || $value[0] === 'not like') {
  28. $model = $model->where($column, $value[0], "%$value[1]%");
  29. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  30. $model = $model->where($column, $value[0], $value[1]);
  31. } elseif ($value[0] == 'in' && !empty($value[1])) {
  32. $valArr = $value[1];
  33. if (is_string($value[1])) {
  34. $valArr = explode(",", trim($value[1]));
  35. }
  36. $model = $model->whereIn($column, $valArr);
  37. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  38. $valArr = $value[1];
  39. if (is_string($value[1])) {
  40. $valArr = explode(",", trim($value[1]));
  41. }
  42. $model = $model->whereNotIn($column, $valArr);
  43. } elseif ($value[0] == 'null') {
  44. $model = $model->whereNull($column);
  45. } elseif ($value[0] == 'not null') {
  46. $model = $model->whereNotNull($column);
  47. } elseif ($value[0] !== '' || $value[1] !== '') {
  48. $model = $model->whereBetween($column, $value);
  49. }
  50. } else {
  51. $model = $model->where($column, $value);
  52. }
  53. }
  54. if ($field) {
  55. $model = $model->orderBy($field, $order);
  56. }
  57. return $model;
  58. }
  59. protected function insertInput(Request $request): array
  60. {
  61. $data = $this->inputFilter($request->post());
  62. if (!empty($data['client_message_datetimes'])) {
  63. $data['client_message_datetimes'] = date('Y-m-d H:i:s', strtotime($data['client_message_datetimes']));
  64. }
  65. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  66. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  67. }
  68. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  69. $data['client_message_body_json'] = json_encode(['header' => $data['client_message_body_json']]);
  70. }
  71. return $data;
  72. }
  73. protected function updateInput(Request $request): array
  74. {
  75. $primary_key = $this->model->getKeyName();
  76. $id = $request->post($primary_key);
  77. $data = $this->inputFilter($request->post());
  78. $model = $this->model->find($id);
  79. if (!$model) {
  80. throw new BusinessException('记录不存在', 2);
  81. }
  82. if (!empty($data['client_message_datetimes'])) {
  83. $data['client_message_datetimes'] = date('Y-m-d H:i:s', strtotime($data['client_message_datetimes']));
  84. }
  85. if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])) {
  86. $data['client_message_header_json'] = json_encode(['header' => $data['client_message_header_json']]);
  87. }
  88. if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])) {
  89. $data['client_message_body_json'] = json_encode(['header' => $data['client_message_body_json']]);
  90. }
  91. unset($data[$primary_key]);
  92. return [$id, $data];
  93. }
  94. }