私域操盘手 - 删除设备返工

This commit is contained in:
柳清爽
2025-05-09 11:46:34 +08:00
parent 45e133e8a4
commit 689b6ec82d
7 changed files with 144 additions and 575 deletions

View File

@@ -3,6 +3,7 @@
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 设备用户关联模型
@@ -10,6 +11,8 @@ use think\Model;
*/
class DeviceUser extends Model
{
use SoftDelete;
/**
* 数据表名
* @var string
@@ -20,31 +23,6 @@ class DeviceUser extends Model
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
/**
* 关联用户模型
* @return \think\model\relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('User', 'userId', 'id');
}
/**
* 关联设备模型
* @return \think\model\relation\BelongsTo
*/
public function device()
{
return $this->belongsTo('app\common\model\Device', 'deviceId', 'id');
}
/**
* 关联公司模型
* @return \think\model\relation\BelongsTo
*/
public function company()
{
return $this->belongsTo('app\common\model\Company', 'companyId', 'id');
}
protected $deleteTime = 'deleteTime';
protected $defaultSoftDelete = 0;
}

View File

@@ -17,8 +17,8 @@ Route::group('v1/', function () {
Route::get(':id', 'app\cunkebao\controller\device\GetDeviceDetailV1Controller@index'); // 获取设备详情
Route::post('', 'app\cunkebao\controller\device\PostAddDeviceV1Controller@index'); // 添加设备
Route::put('refresh', 'app\cunkebao\controller\device\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态
Route::delete(':id', 'app\cunkebao\controller\Device@delete'); // 删除设备
Route::post('task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index');
Route::delete(':id', 'app\cunkebao\controller\device\DeleteDeviceV1Controller@index'); // 删除设备
Route::post('task-config', 'app\cunkebao\controller\device\UpdateDeviceTaskConfigV1Controller@index'); // 设备任务配置
});
// 设备微信相关

View File

@@ -1,175 +0,0 @@
<?php
namespace app\cunkebao\controller;
use app\cunkebao\model\Device as DeviceModel;
use app\cunkebao\model\DeviceHandleLog;
use think\Controller;
use think\Db;
use think\facade\Request;
/**
* 设备管理控制器
*/
class Device extends Controller
{
/**
* 用户信息
* @var object
*/
protected $user;
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
// 设置时区
date_default_timezone_set('Asia/Shanghai');
}
/**
* 添加设备
* @return \think\response\Json
*/
public function save()
{
try {
// 获取登录用户信息
$userInfo = request()->userInfo;
// 检查用户权限,只有管理员可以添加设备
if ($userInfo['isAdmin'] != 1) {
return json([
'code' => 403,
'msg' => '您没有权限添加设备'
]);
}
// 获取设备数据
$data = Request::post();
// 验证IMEI是否为空
if (empty($data['imei'])) {
return json([
'code' => 400,
'msg' => '设备IMEI不能为空'
]);
}
// 验证IMEI是否已存在
$exists = DeviceModel::where('imei', $data['imei'])->where('isDeleted', 0)->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '设备IMEI已存在'
]);
}
// 设置设备公司ID
$data['companyId'] = $userInfo['companyId'];
$data['id'] = time();
try {
Db::startTrans();
// 添加设备
$id = DeviceModel::addDevice($data);
// 添加设备操作记录
DeviceHandleLog::addLog(
[
'imei' => $data['imei'],
'userId' => $userInfo['id'],
'content' => '添加设备',
'companyId' => $userInfo['companyId'],
]
);
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage()
]);
}
// 此处调用底层API
return json([
'code' => 200,
'msg' => '添加成功',
'data' => [
'id' => $id
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage()
]);
}
}
/**
* 删除设备
* @return \think\response\Json
*/
public function delete()
{
try {
// 获取登录用户信息
$userInfo = request()->userInfo;
// 检查用户权限,只有管理员可以删除设备
if ($userInfo['isAdmin'] != 1) {
return json([
'code' => 403,
'msg' => '您没有权限删除设备'
]);
}
// 获取设备ID
$id = Request::param('id/d');
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误'
]);
}
// 验证设备是否存在
$exists = DeviceModel::where('id', $id)
->where('isDeleted', 0)
->where('companyId', $userInfo['companyId'])
->find();
if (!$exists) {
return json([
'code' => 404,
'msg' => '设备不存在或无权限操作'
]);
}
// 删除设备
$result = DeviceModel::deleteDevice($id);
return json([
'code' => 200,
'msg' => '删除成功',
'data' => [
'result' => $result
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace app\cunkebao\controller\device;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceTaskconf as DeviceTaskconfModel;
use app\common\model\DeviceUser as DeviceUserModel;
use app\common\model\User as UserModel;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\Db;
/**
* 设备管理控制器
*/
class DeleteDeviceV1Controller extends BaseController
{
/**
* 删除设备关联用户信息
*
* @param int $deviceId
* @return void
*/
protected function deleteDeviceUser(int $deviceId): void
{
$companyId = $this->getUserInfo('companyId');
$deviceUser = DeviceUserModel::where(compact('companyId', 'deviceId'))->find();
// 有关联数据则删除
if ($deviceUser) {
if (!$deviceUser->delete()) {
throw new \Exception('设备用户关联数据删除失败', 402);
}
}
}
/**
* 删除设备任务配置记录
*
* @param int $deviceId
* @return void
* @throws \Exception
*/
protected function deleteDeviceConf(int $deviceId): void
{
$companyId = $this->getUserInfo('companyId');
$deviceConf = DeviceTaskconfModel::where(compact('companyId', 'deviceId'))->find();
// 有配置信息则删除
if ($deviceConf) {
if (!$deviceConf->delete()) {
throw new \Exception('设备设置信息删除失败', 402);
}
}
}
/**
* 删除主设备信息
*
* @param int $id
* @return DeviceModel
* @throws \Exception
*/
protected function deleteDevice(int $id): void
{
$device = DeviceModel::where('companyId', $this->getUserInfo('companyId'))->find($id);
if (!$device) {
throw new \Exception('设备不存在或无权限操作', 404);
}
if (!$device->delete()) {
throw new \Exception('设备删除失败', 402);
}
}
/**
* 删除存客宝设备数据
*
* @param int $id
* @return $this
* @throws \Exception
*/
protected function deleteCkbAbout(int $id): self
{
$this->deleteDevice($id);
$this->deleteDeviceConf($id);
$this->deleteDeviceUser($id);
return $this;
}
/**
* TODO 删除存客宝设备数据
*
* @return self
*/
protected function deleteS2About(): self
{
}
/**
* 检查用户权限,只有操盘手可以删除设备
*
* @return $this
*/
protected function checkPermission(): self
{
if ($this->getUserInfo('typeId') != UserModel::MASTER_USER) {
throw new \Exception('您没有权限删除设备', 403);
}
return $this;
}
/**
* 删除设备
*
* @return \think\response\Json
*/
public function index()
{
try {
$id = $this->request->param('id/d');
Db::startTrans();
$this->checkPermission();
$this->deleteCkbAbout($id)->deleteS2About($id);
Db::commit();
return ResponseHelper::success();
} catch (\Exception $e) {
Db::rollback();
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
}

View File

@@ -1,106 +0,0 @@
<?php
namespace app\cunkebao\model;
use think\Model;
use think\Db;
/**
* 设备模型类
*/
class Device extends Model
{
// 设置表名
protected $name = 'device';
/**
* 获取设备总数
* @param array $where 查询条件
* @return int 设备总数
*/
public static function getDeviceCount($where = [])
{
// 默认只统计未删除的设备
if (!isset($where['deleteTime']) && !isset($where['d.deleteTime'])) {
$where['deleteTime'] = 0;
}
// 确定是否使用了表别名
$hasAlias = false;
foreach ($where as $key => $value) {
if (strpos($key, '.') !== false) {
$hasAlias = true;
break;
}
}
// 如果使用了表别名,则需要使用查询构造器
if ($hasAlias) {
return self::alias('d')->where($where)->count();
} else {
return self::where($where)->count();
}
}
/**
* 添加设备
* @param array $data 设备数据
* @return int 新增设备ID
*/
public static function addDevice($data)
{
$device = new self();
$device->allowField(true)->save($data);
return $device->id;
}
/**
* 更新设备
* @param int $id 设备ID
* @param array $data 设备数据
* @return bool 更新结果
*/
public static function updateDevice($id, $data)
{
return self::where('id', $id)
->where('isDeleted', 0)
->update($data);
}
/**
* 删除设备(软删除)
* @param int $id 设备ID
* @return bool 删除结果
*/
public static function deleteDevice($id)
{
return self::where('id', $id)
->update([
'isDeleted' => 1,
'deleteTime' => date('Y-m-d H:i:s', time())
]);
}
/**
* 按设备品牌统计数量
* @return array 统计结果
*/
public static function countByBrand()
{
return self::where('isDeleted', 0)
->group('brand')
->field('brand, count(*) as count')
->select();
}
/**
* 按设备在线状态统计数量
* @return array 统计结果
*/
public static function countByStatus()
{
return self::where('isDeleted', 0)
->group('alive')
->field('alive, count(*) as count')
->select();
}
}

View File

@@ -1,90 +0,0 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 设备操作日志模型类
*/
class DeviceHandleLog extends Model
{
// 设置表名
protected $name = 'device_handle_log';
/**
* 获取设备操作日志列表
* @param array $where 查询条件
* @param string $order 排序方式
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getLogList($where = [], $order = 'createTime desc', $page = 1, $limit = 10)
{
return self::where($where)
->order($order)
->paginate($limit, false, ['page' => $page]);
}
/**
* 根据IMEI获取设备操作日志
* @param string $imei 设备IMEI
* @param int $companyId 租户ID
* @param int $limit 获取条数
* @return array 日志记录
*/
public static function getLogsByImei($imei, $companyId = null, $limit = 10)
{
$query = self::where('imei', $imei);
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
return $query->order('createTime', 'desc')
->limit($limit)
->select();
}
/**
* 根据用户ID获取操作日志
* @param int $userId 用户ID
* @param int $companyId 租户ID
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
public static function getLogsByUser($userId, $companyId = null, $page = 1, $limit = 10)
{
$query = self::where('userId', $userId);
if ($companyId !== null) {
$query->where('companyId', $companyId);
}
return $query->order('createTime', 'desc')
->paginate($limit, false, ['page' => $page]);
}
/**
* 记录设备操作日志的便捷方法
* @param string $imei 设备IMEI
* @param int $userId 操作用户ID
* @param string $content 操作内容
* @param int $companyId 租户ID
* @return int 日志ID
*/
public static function recordLog($imei, $userId, $content, $companyId = null)
{
$data = [
'imei' => $imei,
'userId' => $userId,
'content' => $content,
'companyId' => $companyId
];
return self::addLog($data);
}
}

View File

@@ -1,175 +0,0 @@
<?php
namespace app\cunkebao\model;
use think\Model;
/**
* 设备用户关联模型
* 用于管理设备与操盘手(用户)的关联关系
*/
class DeviceUser extends Model
{
/**
* 数据表名
* @var string
*/
protected $name = 'device_user';
/**
* 获取指定用户的所有设备ID
* @param int $userId 用户ID
* @param int $companyId 公司ID
* @return array 设备ID数组
*/
public static function getUserDeviceIds($userId, $companyId = null)
{
$where = ['userId' => $userId];
if (!is_null($companyId)) {
$where['companyId'] = $companyId;
}
return self::where($where)->column('deviceId');
}
/**
* 获取指定设备的所有用户ID
* @param int $deviceId 设备ID
* @param int $companyId 公司ID
* @return array 用户ID数组
*/
public static function getDeviceUserIds($deviceId, $companyId = null)
{
$where = ['deviceId' => $deviceId];
if (!is_null($companyId)) {
$where['companyId'] = $companyId;
}
return self::where($where)
->column('userId');
}
/**
* 添加设备与用户的关联
* @param int $companyId 公司ID
* @param int $userId 用户ID
* @param int $deviceId 设备ID
* @return bool 是否添加成功
*/
public static function addRelation($companyId, $userId, $deviceId)
{
// 检查关联是否已存在
$exists = self::where([
'companyId' => $companyId,
'userId' => $userId,
'deviceId' => $deviceId
])->find();
if ($exists) {
return true; // 已存在,视为添加成功
}
// 添加新关联
return self::create([
'companyId' => $companyId,
'userId' => $userId,
'deviceId' => $deviceId
]) ? true : false;
}
/**
* 批量添加设备与用户的关联
* @param int $companyId 公司ID
* @param int $userId 用户ID
* @param array $deviceIds 设备ID数组
* @return int 成功添加的记录数
*/
public static function batchAddRelations($companyId, $userId, array $deviceIds)
{
if (empty($deviceIds)) {
return 0;
}
$data = [];
foreach ($deviceIds as $deviceId) {
$data[] = [
'companyId' => $companyId,
'userId' => $userId,
'deviceId' => $deviceId
];
}
// 批量添加前先删除已存在的关联,避免主键冲突
self::where('userId', $userId)
->where('companyId', $companyId)
->whereIn('deviceId', $deviceIds)
->delete();
return self::insertAll($data);
}
/**
* 删除设备与用户的关联
* @param int $companyId 公司ID
* @param int $userId 用户ID
* @param int $deviceId 设备ID
* @return bool 是否删除成功
*/
public static function removeDeviceUserRelation($companyId, $userId, $deviceId)
{
return self::where([
'companyId' => $companyId,
'userId' => $userId,
'deviceId' => $deviceId
])->delete() !== false;
}
/**
* 检查用户是否有权限操作指定设备
* @param int $userId 用户ID
* @param int $deviceId 设备ID
* @param int $companyId 公司ID
* @return bool 是否有权限
*/
public static function checkUserDevicePermission($userId, $deviceId, $companyId = null)
{
$where = [
'userId' => $userId,
'deviceId' => $deviceId
];
if (!is_null($companyId)) {
$where['companyId'] = $companyId;
}
return self::where($where)->count() > 0;
}
/**
* 关联用户模型
* @return \think\model\relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('User', 'userId', 'id');
}
/**
* 关联设备模型
* @return \think\model\relation\BelongsTo
*/
public function device()
{
return $this->belongsTo('app\devices\model\Device', 'deviceId', 'id');
}
/**
* 关联公司模型
* @return \think\model\relation\BelongsTo
*/
public function company()
{
return $this->belongsTo('Company', 'companyId', 'id');
}
}