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.
 
 

139 lines
3.7 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Commons\Log;
use App\Constants\v3\LogLabel;
use App\Model\v3\Order;
use App\Model\v3\SpeakerDevic;
use App\Model\v3\Store;
use App\Service\v3\Interfaces\DeviceServiceInterface;
use App\Service\v3\Interfaces\IOTServiceInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\ApplicationContext;
use Throwable;
class DeviceService implements DeviceServiceInterface
{
/**
* @Inject
* @var Log
*/
protected $log;
/**
* @Inject
* @var IOTServiceInterface
*/
protected $IOTService;
/**
* 获取绑定列表
* @param $store_id
* @return Builder[]|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(
['device_name' => $dev_name],
['store_id' => $store_id, 'market_id' => $market_id, 'bind_time' => time(), 'is_bind' => SpeakerDevic::IS_BIND_YES]
);
} catch (\Exception $e) {
$this->log->event(LogLabel::DEVICE_BIND_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($global_order_id, $is_main = true)
{
// 获取订单
$orders = Order::query()->with('orderMain');
if ($is_main) {
$orders = $orders->where(['order_main_id' => $global_order_id])->get()->toArray();
} else {
$orders = $orders->where(['global_order_id' => $global_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['order_main']['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();
}
}