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.

138 lines
3.7 KiB

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