MessageController.php 3.8 KB

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