存客宝应用接口初始化
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller\device;
|
||||
|
||||
use app\api\controller\DeviceController as ApiDeviceController;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\cunkebao\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 设备控制器
|
||||
*/
|
||||
class GetAddResultedV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 通过账号id 获取项目id。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return int
|
||||
*/
|
||||
protected function getCompanyIdByAccountId(int $accountId): int
|
||||
{
|
||||
return UserModel::where('s2_accountId', $accountId)->value('companyId');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目下的所有设备。
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllDevicesIdWithInCompany(int $companyId): array
|
||||
{
|
||||
return DeviceModel::where('companyId', $companyId)->column('id') ?: [0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行数据迁移。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return void
|
||||
*/
|
||||
protected function migrateData(int $accountId): void
|
||||
{
|
||||
$companyId = $this->getCompanyIdByAccountId($accountId);
|
||||
$deviceIds = $this->getAllDevicesIdWithInCompany($companyId) ?: [0];
|
||||
|
||||
// 从 s2_device 导入数据。
|
||||
$this->getNewDeviceFromS2_device($deviceIds, $companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 s2_device 导入数据。
|
||||
*
|
||||
* @param array $ids
|
||||
* @param int $companyId
|
||||
* @return void
|
||||
*/
|
||||
protected function getNewDeviceFromS2_device(array $ids, int $companyId): void
|
||||
{
|
||||
$ids = implode(',', $ids);
|
||||
|
||||
$sql = "INSERT INTO ck_device(`id`, `imei`, `model`, phone, operatingSystem, memo, alive, brand, rooted, xPosed, softwareVersion, extra, createTime, updateTime, deleteTime, companyId)
|
||||
SELECT
|
||||
d.id, d.imei, d.model, d.phone, d.operatingSystem, d.memo, d.alive, d.brand, d.rooted, d.xPosed, d.softwareVersion, d.extra, d.createTime, d.lastUpdateTime, d.deleteTime, a.departmentId AS companyId
|
||||
FROM s2_device d
|
||||
JOIN s2_company_account a ON d.currentAccountId = a.id
|
||||
WHERE isDeleted = 0 AND deletedAndStop = 0 AND d.id NOT IN ({$ids}) AND a.departmentId = {$companyId}
|
||||
ON DUPLICATE KEY UPDATE
|
||||
imei = VALUES(imei),
|
||||
model = VALUES(model),
|
||||
phone = VALUES(phone),
|
||||
operatingSystem = VALUES(operatingSystem),
|
||||
memo = VALUES(memo),
|
||||
alive = VALUES(alive),
|
||||
brand = VALUES(brand),
|
||||
rooted = VALUES(rooted),
|
||||
xPosed = VALUES(xPosed),
|
||||
softwareVersion = VALUES(softwareVersion),
|
||||
extra = VALUES(extra),
|
||||
updateTime = VALUES(updateTime),
|
||||
deleteTime = VALUES(deleteTime),
|
||||
companyId = VALUES(companyId)";
|
||||
|
||||
Db::query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前设备数量
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getCkbDeviceCount(): int
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$cacheKey = 'deviceNum_'.$companyId;
|
||||
$deviceNum = Cache::get($cacheKey);
|
||||
if (empty($deviceNum)) {
|
||||
$deviceNum = DeviceModel::where(['companyId' => $companyId])->count('*');
|
||||
Cache::set($cacheKey,$deviceNum,120);
|
||||
}
|
||||
return $deviceNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取添加的关联设备结果。
|
||||
*
|
||||
* @param int $accountId
|
||||
* @return bool
|
||||
*/
|
||||
protected function getAddResulted(int $accountId): bool
|
||||
{
|
||||
$deviceNum = $this->getCkbDeviceCount();
|
||||
$result = (new ApiDeviceController())->getlist(
|
||||
[
|
||||
'accountId' => $accountId,
|
||||
'pageIndex' => 0,
|
||||
'pageSize' => 100
|
||||
],
|
||||
true
|
||||
);
|
||||
$result = json_decode($result, true);
|
||||
$result = $result['data']['results'] ?? false;
|
||||
|
||||
if (empty($result)){
|
||||
return false;
|
||||
}else{
|
||||
if (count($result) > $deviceNum){
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$cacheKey = 'deviceNum_'.$companyId;
|
||||
Cache::rm($cacheKey);
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取基础统计信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$accountId = $this->request->param('accountId/d');
|
||||
|
||||
if (empty($accountId)){
|
||||
return ResponseHelper::error('参数缺失');
|
||||
}
|
||||
|
||||
$isAdded = $this->getAddResulted($accountId);
|
||||
$isAdded && $this->migrateData($accountId);
|
||||
|
||||
return ResponseHelper::success(
|
||||
[
|
||||
'added' => $isAdded
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user