DeviceController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\SysSerial;
  7. use support\exception\BusinessException;
  8. use support\Request;
  9. use support\Response;
  10. class DeviceController extends Curd
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new Device();
  15. $this->validate = true;
  16. $this->validateClass = new DeviceValidate();
  17. }
  18. public function select(Request $request): Response
  19. {
  20. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  21. $order = $request->get('order', 'desc');
  22. $type = $request->get('type','');
  23. $field = $field ?? 'device_addtimes';
  24. if ($type == 'bind'){
  25. $where['device_status'] = 'ACTIVED';
  26. }
  27. $query = $this->doSelect($where, $field, $order);
  28. return $this->doFormat($query, $format, $limit);
  29. }
  30. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  31. {
  32. $model = $this->model->with([
  33. 'ledger' => function ($query) {
  34. $query->select('device_ledger_id', 'device_ledger_name');
  35. }
  36. ]);
  37. foreach ($where as $column => $value) {
  38. if (is_array($value)) {
  39. if ($value[0] === 'like' || $value[0] === 'not like') {
  40. $model = $model->where($column, $value[0], "%$value[1]%");
  41. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  42. $model = $model->where($column, $value[0], $value[1]);
  43. } elseif ($value[0] == 'in' && !empty($value[1])) {
  44. $valArr = $value[1];
  45. if (is_string($value[1])) {
  46. $valArr = explode(",", trim($value[1]));
  47. }
  48. $model = $model->whereIn($column, $valArr);
  49. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  50. $valArr = $value[1];
  51. if (is_string($value[1])) {
  52. $valArr = explode(",", trim($value[1]));
  53. }
  54. $model = $model->whereNotIn($column, $valArr);
  55. } elseif ($value[0] == 'null') {
  56. $model = $model->whereNull($column);
  57. } elseif ($value[0] == 'not null') {
  58. $model = $model->whereNotNull($column);
  59. } elseif ($value[0] !== '' || $value[1] !== '') {
  60. $model = $model->whereBetween($column, $value);
  61. }
  62. } else {
  63. $model = $model->where($column, $value);
  64. }
  65. }
  66. if ($field) {
  67. $model = $model->orderBy($field, $order);
  68. }
  69. return $model;
  70. }
  71. /* 设备列表(下拉选项)*/
  72. public function selectList()
  73. {
  74. $class = get_class($this->model);
  75. $data = $class::whereIn('device_status', ['PROCESSING', 'PENDING'])
  76. ->select('device_id', 'device_name', 'device_src_key')
  77. ->get()
  78. ->toArray();
  79. return json_success('', $data);
  80. }
  81. public function insert(Request $request): Response
  82. {
  83. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  84. return json_fail($this->validateClass->getError());
  85. }
  86. try {
  87. $data = $this->insertInput($request);
  88. dump($data);
  89. $this->doInsert($data);
  90. } catch (BusinessException $customException) {
  91. return json_fail($customException->getMessage());
  92. } catch (\Exception $e) {
  93. dump($e->getMessage());
  94. return json_fail('数据写入失败');
  95. }
  96. _syslog("添加设备", "设备名称【" . $request->post('device_name') . '】');
  97. return json_success('success');
  98. }
  99. public function update(Request $request): Response
  100. {
  101. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  102. return json_fail($this->validateClass->getError());
  103. }
  104. try {
  105. [$id, $data] = $this->updateInput($request);
  106. $this->doUpdate($id, $data);
  107. } catch (BusinessException $e) {
  108. return json_fail($e->getMessage());
  109. } catch (\Exception $e) {
  110. dump($e->getTrace());
  111. return json_fail('数据更新失败');
  112. }
  113. _syslog("编辑设备", "设备名称【" . $request->post('device_name') . '】');
  114. return json_success('success');
  115. }
  116. protected function insertInput(Request $request): array
  117. {
  118. $data = $this->inputFilter($request->post());
  119. $data['device_id'] = $this->generateDeviceId();
  120. return $data;
  121. }
  122. /**
  123. * @Desc 生成设备ID
  124. * @Author Gorden
  125. * @Date 2024/3/26 13:32
  126. *
  127. * @return string
  128. * @throws \support\exception\BusinessException
  129. */
  130. private function generateDeviceId()
  131. {
  132. $id = SysSerial::getSerial();
  133. return "DE" . str_pad($id, 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  134. }
  135. public function delete(Request $request): Response
  136. {
  137. $ids = $this->deleteInput($request);
  138. if (Device::whereIn('device_id', $ids)->where('device_status', '<>', 'WAITING')->exists()) {
  139. return json_fail('只有库存状态的设备可以删除');
  140. }
  141. $devices = Device::whereIn('device_id', $ids)->get()->toArray();
  142. $this->doDelete($ids);
  143. _syslog("删除设备", "删除的设备ID【" . implode(',', $ids) . '】', $devices);
  144. return json_success('success');
  145. }
  146. }