From 689b6ec82dacfb73c887a628187f6123526d8629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Fri, 9 May 2025 11:46:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=81=E5=9F=9F=E6=93=8D=E7=9B=98=E6=89=8B?= =?UTF-8?q?=20-=20=E5=88=A0=E9=99=A4=E8=AE=BE=E5=A4=87=E8=BF=94=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/common/model/DeviceUser.php | 32 +--- Server/application/cunkebao/config/route.php | 4 +- .../cunkebao/controller/Device.php | 175 ------------------ .../device/DeleteDeviceV1Controller.php | 137 ++++++++++++++ Server/application/cunkebao/model/Device.php | 106 ----------- .../cunkebao/model/DeviceHandleLog.php | 90 --------- .../application/cunkebao/model/DeviceUser.php | 175 ------------------ 7 files changed, 144 insertions(+), 575 deletions(-) delete mode 100644 Server/application/cunkebao/controller/Device.php create mode 100644 Server/application/cunkebao/controller/device/DeleteDeviceV1Controller.php delete mode 100644 Server/application/cunkebao/model/Device.php delete mode 100644 Server/application/cunkebao/model/DeviceHandleLog.php delete mode 100644 Server/application/cunkebao/model/DeviceUser.php diff --git a/Server/application/common/model/DeviceUser.php b/Server/application/common/model/DeviceUser.php index fcbebaba..738ca4ef 100644 --- a/Server/application/common/model/DeviceUser.php +++ b/Server/application/common/model/DeviceUser.php @@ -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; } \ No newline at end of file diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 49d7af83..0ce2a9dc 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -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'); // 设备任务配置 }); // 设备微信相关 diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php deleted file mode 100644 index 749151ba..00000000 --- a/Server/application/cunkebao/controller/Device.php +++ /dev/null @@ -1,175 +0,0 @@ -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() - ]); - } - } - - -} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/DeleteDeviceV1Controller.php b/Server/application/cunkebao/controller/device/DeleteDeviceV1Controller.php new file mode 100644 index 00000000..9d6642bf --- /dev/null +++ b/Server/application/cunkebao/controller/device/DeleteDeviceV1Controller.php @@ -0,0 +1,137 @@ +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()); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/Device.php b/Server/application/cunkebao/model/Device.php deleted file mode 100644 index 19b1da19..00000000 --- a/Server/application/cunkebao/model/Device.php +++ /dev/null @@ -1,106 +0,0 @@ - $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(); - } -} \ No newline at end of file diff --git a/Server/application/cunkebao/model/DeviceHandleLog.php b/Server/application/cunkebao/model/DeviceHandleLog.php deleted file mode 100644 index 4afe46cf..00000000 --- a/Server/application/cunkebao/model/DeviceHandleLog.php +++ /dev/null @@ -1,90 +0,0 @@ -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); - } -} \ No newline at end of file diff --git a/Server/application/cunkebao/model/DeviceUser.php b/Server/application/cunkebao/model/DeviceUser.php deleted file mode 100644 index 411f9759..00000000 --- a/Server/application/cunkebao/model/DeviceUser.php +++ /dev/null @@ -1,175 +0,0 @@ - $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'); - } -} \ No newline at end of file