MetadataController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\device;
  3. use app\admin\validate\device\DeviceValidate;
  4. use app\controller\Curd;
  5. use app\model\Device;
  6. use app\model\Metadata;
  7. use app\model\SysSerial;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. class MetadataController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new Metadata();
  16. // $this->validate = true;
  17. // $this->validateClass = new DeviceValidate();
  18. }
  19. public function select(Request $request): Response
  20. {
  21. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  22. $order = $request->get('order', 'desc');
  23. $field = $field ?? 'metadata_addtimes';
  24. $query = $this->doSelect($where, $field, $order);
  25. return $this->doFormat($query, $format, $limit);
  26. }
  27. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  28. {
  29. $model = $this->model;
  30. foreach ($where as $column => $value) {
  31. if (is_array($value)) {
  32. if ($value[0] === 'like' || $value[0] === 'not like') {
  33. $model = $model->where($column, $value[0], "%$value[1]%");
  34. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  35. $model = $model->where($column, $value[0], $value[1]);
  36. } elseif ($value[0] == 'in' && !empty($value[1])) {
  37. $valArr = $value[1];
  38. if (is_string($value[1])) {
  39. $valArr = explode(",", trim($value[1]));
  40. }
  41. $model = $model->whereIn($column, $valArr);
  42. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  43. $valArr = $value[1];
  44. if (is_string($value[1])) {
  45. $valArr = explode(",", trim($value[1]));
  46. }
  47. $model = $model->whereNotIn($column, $valArr);
  48. } elseif ($value[0] == 'null') {
  49. $model = $model->whereNull($column);
  50. } elseif ($value[0] == 'not null') {
  51. $model = $model->whereNotNull($column);
  52. } elseif ($value[0] !== '' || $value[1] !== '') {
  53. $model = $model->whereBetween($column, $value);
  54. }
  55. } else {
  56. $model = $model->where($column, $value);
  57. }
  58. }
  59. if ($field) {
  60. $model = $model->orderBy($field, $order);
  61. }
  62. return $model;
  63. }
  64. /* 设备列表(下拉选项)*/
  65. public function selectList()
  66. {
  67. $class = get_class($this->model);
  68. $data = $class::select('metadata_id', 'metadata_name', 'metadata_key')
  69. ->get()
  70. ->toArray();
  71. return json_success('', $data);
  72. }
  73. public function insert(Request $request): Response
  74. {
  75. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  76. return json_fail($this->validateClass->getError());
  77. }
  78. try {
  79. $data = $this->insertInput($request);
  80. $this->doInsert($data);
  81. } catch (BusinessException $customException) {
  82. return json_fail($customException->getMessage());
  83. } catch (\Exception $e) {
  84. dump($e->getMessage());
  85. return json_fail('数据写入失败');
  86. }
  87. _syslog("添加元数据", "元数据名称【" . $request->post('metadata_name') . '】');
  88. return json_success('success');
  89. }
  90. public function update(Request $request): Response
  91. {
  92. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  93. return json_fail($this->validateClass->getError());
  94. }
  95. try {
  96. [$id, $data] = $this->updateInput($request);
  97. $this->doUpdate($id, $data);
  98. } catch (BusinessException $e) {
  99. return json_fail($e->getMessage());
  100. } catch (\Exception $e) {
  101. dump($e->getTrace());
  102. return json_fail('数据更新失败');
  103. }
  104. _syslog("编辑元数据", "元数据名称【" . $request->post('metadata_name') . '】');
  105. return json_success('success');
  106. }
  107. public function delete(Request $request): Response
  108. {
  109. $ids = $this->deleteInput($request);
  110. $metas = Metadata::whereIn('metadata_id', $ids)->get()->toArray();
  111. $this->doDelete($ids);
  112. _syslog("删除元数据", "删除的元数据ID【" . implode(',', $ids) . '】', $metas);
  113. return json_success('success');
  114. }
  115. }