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.
|
|
<?php
namespace App\Service;
use App\Commons\Log;use App\Constants\LogLabel;use App\Model\Order;use App\Model\SpeakerDevic;use App\Model\Store;use Hyperf\Di\Annotation\Inject;use Hyperf\Utils\ApplicationContext;use App\TaskWorker\AliIotTask;
class DeviceServiceImp implements DeviceServiceInterface{ /** * @Inject * @var Log */ protected $log;
/** * @Inject * @var IOTServiceInterface */ protected $IOTService;
/** * 获取绑定列表 * @param $store_id * @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection */ public function getListByStoreId($store_id) { return SpeakerDevic::query()->where(['store_id' => $store_id, 'is_bind' => SpeakerDevic::IS_BIND_YES])->get()->toArray(); }
/** * 绑定 * @param $dev_name * @param $store_id * @return SpeakerDevic|null * @throws \Throwable */ public function bindByStoreId($dev_name, $store_id) { $sd = null;
if ($this->checkDeviceEnable($dev_name)) { return $sd; }
try {
// 获取市场ID
$market_id = Store::query()->where(['id' => $store_id])->value('market_id');
$sd = SpeakerDevic::query()->updateOrCreate( ['store_id' => $store_id, 'device_name' => $dev_name], ['market_id' => $market_id, 'bind_time' => time(), 'is_bind' => SpeakerDevic::IS_BIND_YES] );
} catch (Exception $e) { $this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]); } return $sd; }
/** * 解绑 * @param $bind_id * @return int */ public function unbindById($bind_id) { return SpeakerDevic::query()->where(['id' => $bind_id])->update(['is_bind' => SpeakerDevic::IS_BIND_NO]); }
/** * 发布语音消息 * @param $dev_names * @param $msg * @return bool */ public function pubMsgToStoreByDevName($dev_names, $msg) { foreach ($dev_names as $key => $dev_name) { $this->IOTService->pub($dev_name['device_name'], $msg); }
return true; }
public function pubMsgToStoreByOrderMainId($order_id, $is_main = true) {
// 获取订单
$orders = Order::query()->select(['id','order_num','money', 'pay_type', 'store_id', 'type']); if ($is_main) { $orders = $orders->where(['order_main_id' => $order_id])->get()->toArray(); } else { $orders = $orders->where(['id' => $order_id])->get()->toArray(); }
if(empty($orders)) return;
// 循环发送
foreach ($orders as $k => &$order) {
$device_names = SpeakerDevic::query() ->select(['device_name']) ->where(['store_id' => $order['store_id'], 'is_bind' => SpeakerDevic::IS_BIND_YES]) ->get() ->toArray(); $msg = $order['type']==1 ? "{\"msg\":\"您有新的懒族外卖订单\"}" : "{\"msg\":\"微信到账".floatval($order['money'])."元\"}"; foreach ($device_names as $key => $dev_name) { $this->IOTService->pub($dev_name['device_name'], $msg); } }
return true;
}
/** * 当前设备是否已经被绑定 * @param $dev_name * @return bool */ protected function checkDeviceEnable($dev_name) { return SpeakerDevic::query()->where(['device_name' => $dev_name, 'is_bind' => SpeakerDevic::IS_BIND_YES])->exists(); }
}
|