You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.5 KiB

5 years ago
5 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Commons\Log;
  4. use App\Constants\LogLabel;
  5. use App\Model\Order;
  6. use App\Model\SpeakerDevic;
  7. use App\Model\Store;
  8. use Hyperf\Di\Annotation\Inject;
  9. use Hyperf\Utils\ApplicationContext;
  10. use App\TaskWorker\AliIotTask;
  11. class DeviceServiceImp implements DeviceServiceInterface
  12. {
  13. /**
  14. * @Inject
  15. * @var Log
  16. */
  17. protected $log;
  18. /**
  19. * @Inject
  20. * @var IOTServiceInterface
  21. */
  22. protected $IOTService;
  23. /**
  24. * 获取绑定列表
  25. * @param $store_id
  26. * @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection
  27. */
  28. public function getListByStoreId($store_id)
  29. {
  30. return SpeakerDevic::query()->where(['store_id' => $store_id, 'is_bind' => SpeakerDevic::IS_BIND_YES])->get()->toArray();
  31. }
  32. /**
  33. * 绑定
  34. * @param $dev_name
  35. * @param $store_id
  36. * @return SpeakerDevic|null
  37. * @throws \Throwable
  38. */
  39. public function bindByStoreId($dev_name, $store_id)
  40. {
  41. $sd = null;
  42. if ($this->checkDeviceEnable($dev_name)) {
  43. return $sd;
  44. }
  45. try {
  46. // 获取市场ID
  47. $market_id = Store::query()->where(['id' => $store_id])->value('market_id');
  48. $sd = SpeakerDevic::query()->updateOrCreate(
  49. ['store_id' => $store_id, 'device_name' => $dev_name],
  50. ['market_id' => $market_id, 'bind_time' => time(), 'is_bind' => SpeakerDevic::IS_BIND_YES]
  51. );
  52. } catch (Exception $e) {
  53. $this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]);
  54. }
  55. return $sd;
  56. }
  57. /**
  58. * 解绑
  59. * @param $bind_id
  60. * @return int
  61. */
  62. public function unbindById($bind_id)
  63. {
  64. return SpeakerDevic::query()->where(['id' => $bind_id])->update(['is_bind' => SpeakerDevic::IS_BIND_NO]);
  65. }
  66. /**
  67. * 发布语音消息
  68. * @param $dev_names
  69. * @param $msg
  70. * @return bool
  71. */
  72. public function pubMsgToStoreByDevName($dev_names, $msg)
  73. {
  74. foreach ($dev_names as $key => $dev_name) {
  75. $this->IOTService->pub($dev_name['device_name'], $msg);
  76. }
  77. return true;
  78. }
  79. public function pubMsgToStoreByOrderMainId($order_id, $is_main = true)
  80. {
  81. // 获取订单
  82. $orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']);
  83. if ($is_main) {
  84. $orders = $orders->where(['order_main_id' => $order_id])->get()->toArray();
  85. } else {
  86. $orders = $orders->where(['id' => $order_id])->get()->toArray();
  87. }
  88. if(empty($orders)) return;
  89. // 循环发送
  90. foreach ($orders as $k => &$order) {
  91. $device_names = SpeakerDevic::query()
  92. ->select(['device_name'])
  93. ->where(['store_id' => $order['store_id'], 'is_bind' => SpeakerDevic::IS_BIND_YES])
  94. ->get()
  95. ->toArray();
  96. $msg = $order['type']==1 ? "{\"msg\":\"您有新的外卖订单\"}" : "{\"msg\":\"到账".$order['money']."\"}";
  97. foreach ($device_names as $key => $dev_name) {
  98. $this->IOTService->pub($dev_name['device_name'], $msg);
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * 当前设备是否已经被绑定
  105. * @param $dev_name
  106. * @return bool
  107. */
  108. protected function checkDeviceEnable($dev_name)
  109. {
  110. return SpeakerDevic::query()->where(['device_name' => $dev_name, 'is_bind' => SpeakerDevic::IS_BIND_YES])->exists();
  111. }
  112. }