123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace app\admin\controller\client;
- use app\controller\Curd;
- use app\model\ClientMessage;
- use support\exception\BusinessException;
- use support\Request;
- class MessageController extends Curd
- {
- public function __construct()
- {
- $this->model = new ClientMessage();
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with('member');
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- if (!empty($data['client_message_datetimes'])) {
- $data['client_message_datetimes'] = date('Y-m-d H:i:s', strtotime($data['client_message_datetimes']));
- }
- if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])){
- $data['client_message_header_json'] = json_encode(['header'=>$data['client_message_header_json']]);
- }
- if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])){
- $data['client_message_body_json'] = json_encode(['header'=>$data['client_message_body_json']]);
- }
- return $data;
- }
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- if (!empty($data['client_message_datetimes'])) {
- $data['client_message_datetimes'] = date('Y-m-d H:i:s', strtotime($data['client_message_datetimes']));
- }
- if (!empty($data['client_message_header_json']) && !is_json($data['client_message_header_json'])){
- $data['client_message_header_json'] = json_encode(['header'=>$data['client_message_header_json']]);
- }
- if (!empty($data['client_message_body_json']) && !is_json($data['client_message_body_json'])){
- $data['client_message_body_json'] = json_encode(['header'=>$data['client_message_body_json']]);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|