From 419d5b42a0d8cfdef9a8dcd9ce69e655bd2d6c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Wed, 23 Apr 2025 16:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=AE=A1=E5=90=8E=E5=8F=B0=20-=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=85=AC=E5=8F=B8=E5=85=B3=E8=81=94=E7=9A=84?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/model/DeviceWechatLogin.php | 12 ++- .../application/superadmin/config/route.php | 1 + .../GetCompanyDevicesForProfileController.php | 95 +++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Server/application/superadmin/controller/company/GetCompanyDevicesForProfileController.php diff --git a/Server/application/common/model/DeviceWechatLogin.php b/Server/application/common/model/DeviceWechatLogin.php index 88efba35..290ed32a 100644 --- a/Server/application/common/model/DeviceWechatLogin.php +++ b/Server/application/common/model/DeviceWechatLogin.php @@ -17,4 +17,14 @@ class DeviceWechatLogin extends Model protected $createTime = 'createTime'; protected $updateTime = 'updateTime'; -} \ No newline at end of file + /** + * 获取设备最新登录记录的id + * + * @param array $deviceIds + * @return array + */ + public static function getDevicesLatestLogin(array $deviceIds): array + { + return self::fieldRaw('max(id) as lastedId,deviceId')->whereIn('deviceId', $deviceIds)->group('deviceId')->select()->toArray(); + } +} \ No newline at end of file diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php index e4514efd..d1bada0a 100644 --- a/Server/application/superadmin/config/route.php +++ b/Server/application/superadmin/config/route.php @@ -41,5 +41,6 @@ Route::group('', function () { Route::get('list', 'app\superadmin\controller\company\GetCompanyListController@index'); Route::get('detail/:id', 'app\superadmin\controller\company\GetCompanyDetailForUpdateController@index'); Route::get('profile/:id', 'app\superadmin\controller\company\GetCompanyDetailForProfileController@index'); + Route::get('devices', 'app\superadmin\controller\company\GetCompanyDevicesForProfileController@index'); }); })->middleware(['app\superadmin\middleware\AdminAuth']); \ No newline at end of file diff --git a/Server/application/superadmin/controller/company/GetCompanyDevicesForProfileController.php b/Server/application/superadmin/controller/company/GetCompanyDevicesForProfileController.php new file mode 100644 index 00000000..1855ec81 --- /dev/null +++ b/Server/application/superadmin/controller/company/GetCompanyDevicesForProfileController.php @@ -0,0 +1,95 @@ +request->param('companyId/d', 0); + + $devices = DeviceModel::where(compact('companyId'))->field('id,memo,phone,model,brand,alive,id deviceId') + ->select() + ->toArray(); + + if (empty($devices)) { + throw new \Exception('暂无设备', 200); + } + + return $devices; + } + + /** + * 查询设备与微信的关联关系. + * + * @return array + */ + protected function getDeviceWechatRelationsByDeviceIds(array $deviceIds): array + { + // 获取设备最新登录记录的id + $latestLogs = DeviceWechatLoginModel::getDevicesLatestLogin($deviceIds); + + // 获取最新登录记录id + $latestIds = array_column($latestLogs, 'lastedId'); + + return DeviceWechatLoginModel::field('deviceId,wechatId,alive wAlive') + ->whereIn('id', $latestIds) + ->select() + ->toArray(); + } + + /** + * 获取设备的微信好友统计 + * + * @param array $ownerWechatId + * @return void + */ + protected function getWechatFriendsCount(array $deviceIds): array + { + // 查询设备与微信的关联关系 + $relations = $this->getDeviceWechatRelationsByDeviceIds($deviceIds); + + // 统计微信好友数量 + $friendCounts = WechatFriendModel::alias('f')->field('ownerWechatId wechatId,count(*) friendCount') + ->whereIn('ownerWechatId', array_column($relations, 'wechatId')) + ->group('ownerWechatId') + ->select() + ->toArray(); + + return ArrHelper::join($relations, $friendCounts, 'wechatId'); + } + + /** + * 获取公司关联的设备列表 + * + * @return \think\response\Json + */ + public function index() + { + try { + $devices = $this->getDevicesWithCompanyId(); + $friendCount = $this->getWechatFriendsCount(array_column($devices, 'id')); + + $result = ArrHelper::leftJoin($devices, $friendCount, 'deviceId'); + + return ResponseHelper::success($result); + } catch (\Exception $e) { + return ResponseHelper::error($e->getMessage(), $e->getCode()); + } + } +} \ No newline at end of file