Browse Source

批量查询设备信息保存更新

master
wanghongjun 2 years ago
parent
commit
08269d6fa3
  1. 70
      application/api/controller/Base.php
  2. 89
      application/api/controller/Device.php
  3. 20
      application/api/controller/Product.php

70
application/api/controller/Base.php

@ -3,6 +3,12 @@
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
class Base extends Controller
{
@ -19,7 +25,7 @@ class Base extends Controller
return json_encode($arr);
}
public function jsonSuccess($msg = '成功',$data = [])
protected function jsonSuccess($msg = '成功',$data = [])
{
$arr = [
'code' => $this->SUCCESS_CODE,
@ -29,4 +35,66 @@ class Base extends Controller
return json_encode($arr);
}
/**
*
* @param $tableName
* @param $queryData
* @param $data
* @param $idArr
* @return array
* @throws Exception
* @throws PDOException
*/
protected function queryUpdate($tableName,$queryData,$data,$idArr)
{
$idArr = [];
if ($queryData) {
$id = $queryData['id'];
$update = [];
foreach ($queryData as $key => $row) {
if (isset($data[$key]) && $data[$key] != $row) {
$update[$key] = $row;
}
}
if ($update) {
Db::table($tableName)->where('id',$id)->update($update);
}
$idArr[] = $id;
} else {
Db::table($tableName)->insert($data);
$idArr[] = Db::table($tableName)->getLastInsID();
}
return $idArr;
}
/**
*
* @param $tableName
* @param $idArr
* @param $updateStatus
* @throws DataNotFoundException
* @throws DbException
* @throws Exception
* @throws ModelNotFoundException
* @throws PDOException
*/
protected function updateStatus($tableName,$idArr,$updateStatus = ['status' => 0])
{
if ($idArr) {
# 查询不存在的id
$notList = Db::table($tableName)->where('id', 'not in', $idArr)->field('id')->select();
$notIdArr = [];
foreach ($notList as $notListRow) $notList[] = $notListRow['id'];
if ($notIdArr) {
Db::table($tableName)->where('id','in',$notIdArr)->update($updateStatus);
}
} else {
Db::table($tableName)->update($updateStatus);
}
}
}

89
application/api/controller/Device.php

@ -0,0 +1,89 @@
<?php
namespace app\api\controller;
use app\common\AepSdk\Apis\Aep_device_management;
use think\Db;
use think\Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
class Device extends Base
{
/**
* 获取设备信息列表
* @return false|string
* @throws DataNotFoundException
* @throws DbException
* @throws Exception
* @throws ModelNotFoundException
* @throws PDOException
*/
public function queryDeviceList()
{
$app_key = config('iot.app_key');
$app_secret = config('iot.app_secret');
$tableName = 'bs_product_device';
$productList = Db::table('bs_product')->where('status', 1)->field('productId,apiKey')->select();
$updateStatus = ['status' => 0];
foreach ($productList as $productListRow) {
$apiKey = $productListRow['apiKey'];
$productId = $productListRow['productId'];
$idArr = [];
$result = Aep_device_management::QueryDeviceList($app_key, $app_secret, $apiKey, $productId);
$jsonDataDe = json_decode($result, true);
if ($jsonDataDe['code'] !== 0) continue;
$list = $jsonDataDe['result']['list'];
if (!$list) {
Db::table($tableName)->where('productId', $productId)->update($updateStatus);
continue;
}
foreach ($list as $item) {
$deviceId = $item['deviceId'];
$queryRes = Db::table($tableName)->where('deviceId', $deviceId)->find();
$data = [
'deviceId' => $deviceId, // 设备id
'deviceName' => $item['deviceName'], // 终端名称
'tenantId' => $item['tenantId'], // 租户id
'productId' => $item['productId'], // 产品id
'firmwareVersion' => $item['firmwareVersion'], // String,固件版本
'deviceStatus' => $item['deviceStatus'], //Integer 设备状态 0:已注册,1:已激活,2:已注销
'createTime' => $item['createTime'], // 创建时间
'updateTime' => $item['updateTime'], // 修改时间
'netStatus' => $item['netStatus'], // 设备在线状态
'onlineAt' => $item['onlineAt'], // 设备最后上线时间
'offlineAt' => $item['offlineAt'], // 设备最后下线时间
];
if (isset($item['imei'])) $data['imei'] = $item['imei'];// String,IMEI号,全局唯一,根据产品的Endpoint必填,创建时可相同,则删除原产品新建产品
if (isset($item['imsi'])) $data['imsi'] = $item['imsi'];// String,IMSI号,根据产品的Endpoint选填
if (isset($item['autoObserver'])) $data['autoObserver'] = $item['autoObserver'];// 是否订阅,0.订阅1.不订阅
if (isset($item['createBy'])) $data['createBy'] = $item['createBy'];// 创建者
if (isset($item['updateBy'])) $data['updateBy'] = $item['updateBy'];// 修改者
$idArr = $this->queryUpdate($tableName, $queryRes, $data, $idArr);
}
$this->updateStatus($tableName, $idArr);
}
return $this->jsonSuccess();
}
}

20
application/api/controller/Product.php

@ -35,6 +35,7 @@ class Product extends Base
$list = $jsonDataDe['result']['list'];
$idArr = [];
foreach ($list as $item) {
$productId = $item['productId'];
@ -78,24 +79,11 @@ class Product extends Base
// 'msgParserMode' => $item['msgParserMode'], // 脚本解析方式:1.js脚本解析
];
if ($query) {
$update = [];
foreach ($query as $key => $row) {
if (isset($data[$key]) && $data[$key] != $row) {
$update[$key] = $row;
}
}
if ($update) {
Db::table($tableName)->where('id',$query['id'])->update($update);
$idArr = $this->queryUpdate($tableName, $query, $data, $idArr);
}
} else {
Db::table($tableName)->insert($data);
}
}
# 修改状态为停用
$this->updateStatus($tableName, $idArr);
return $this->jsonError($jsonDataDe['msg']);
} else {

Loading…
Cancel
Save