DeviceController.php 5.6 KB

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