DeviceController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. public function afterQuery($items)
  73. {
  74. foreach ($items as &$item) {
  75. $deviceConfigJson = json_decode($item['device_config_json'], true);
  76. $item['dept_name'] = $deviceConfigJson['dept_name'] ?? '';
  77. }
  78. return $items;
  79. }
  80. /* 设备列表(下拉选项)*/
  81. public function selectList()
  82. {
  83. $class = get_class($this->model);
  84. $data = $class::whereIn('device_status', ['PROCESSING', 'PENDING'])
  85. ->select('device_id', 'device_name', 'device_src_key')
  86. ->get()
  87. ->toArray();
  88. return json_success('', $data);
  89. }
  90. public function insert(Request $request): Response
  91. {
  92. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  93. return json_fail($this->validateClass->getError());
  94. }
  95. try {
  96. $data = $this->insertInput($request);
  97. dump($data);
  98. $this->doInsert($data);
  99. } catch (BusinessException $customException) {
  100. return json_fail($customException->getMessage());
  101. } catch (\Exception $e) {
  102. dump($e->getMessage());
  103. return json_fail('数据写入失败');
  104. }
  105. _syslog("添加设备", "设备名称【" . $request->post('device_name') . '】');
  106. return json_success('success');
  107. }
  108. public function update(Request $request): Response
  109. {
  110. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  111. return json_fail($this->validateClass->getError());
  112. }
  113. try {
  114. [$id, $data] = $this->updateInput($request);
  115. $this->doUpdate($id, $data);
  116. } catch (BusinessException $e) {
  117. return json_fail($e->getMessage());
  118. } catch (\Exception $e) {
  119. dump($e->getTrace());
  120. return json_fail('数据更新失败');
  121. }
  122. _syslog("编辑设备", "设备名称【" . $request->post('device_name') . '】');
  123. return json_success('success');
  124. }
  125. protected function insertInput(Request $request): array
  126. {
  127. $data = $this->inputFilter($request->post());
  128. $data['device_id'] = $this->generateDeviceId();
  129. return $data;
  130. }
  131. /**
  132. * @Desc 生成设备ID
  133. * @Author Gorden
  134. * @Date 2024/3/26 13:32
  135. *
  136. * @return string
  137. * @throws \support\exception\BusinessException
  138. */
  139. private function generateDeviceId()
  140. {
  141. $id = SysSerial::getSerial();
  142. return "DE" . str_pad($id, 16, '0', STR_PAD_LEFT) . random_string(6, 'up');
  143. }
  144. public function delete(Request $request): Response
  145. {
  146. $ids = $this->deleteInput($request);
  147. if (Device::whereIn('device_id', $ids)->where('device_status', '<>', 'WAITING')->exists()) {
  148. return json_fail('只有库存状态的设备可以删除');
  149. }
  150. $devices = Device::whereIn('device_id', $ids)->get()->toArray();
  151. $this->doDelete($ids);
  152. _syslog("删除设备", "删除的设备ID【" . implode(',', $ids) . '】', $devices);
  153. return json_success('success');
  154. }
  155. /**
  156. * @Desc 获取所有的打印机
  157. * @Author Gorden
  158. * @Date 2024/12/18 15:19
  159. *
  160. * @return Response
  161. */
  162. public function getAllPrinter()
  163. {
  164. $printers = Device::where('device_type', 'PRINTER')
  165. ->where('device_status', 'ACTIVED')
  166. ->select('device_id', 'device_name', 'device_config_json')
  167. ->get()
  168. ->toArray();
  169. $data = [];
  170. foreach ($printers as $printer) {
  171. if (empty($printer['device_config_json'])) {
  172. continue;
  173. }
  174. $deviceConfigJson = json_decode($printer['device_config_json'], true);
  175. $data[] = [
  176. 'device_id' => $printer['device_id'],
  177. 'device_name' => (isset($deviceConfigJson['dept_name']) ? $deviceConfigJson['dept_name'] . '-' : '') . $printer['device_name']
  178. ];
  179. }
  180. return json_success('success', $data);
  181. }
  182. }