DeviceController.php 5.5 KB

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