isValid()) return [false, 1061]; $fileExt = $file->getClientOriginalExtension(); if ($fileExt != 'xlsx') return [false, 1062]; $fileName = $file->getClientOriginalName(); try { $sysUserId = Auth::id(); // 插入上传记录 $uploadInfo = SysStarLibraryUpload::query() ->create([ 'file' => $fileName, 'ptype' => $ptype, 'sys_user_id' => $sysUserId ]); // 获取 groupId $groupId = self::getGroupId($sysUserId); if (empty($ptype) || empty($groupId) || empty($sysUserId)) { return [false, 400]; } // 导入数据 $import = new StarInformationImport($ptype, $groupId, $sysUserId); Excel::import($import, $file); $uploadInfo->update(['row_num' => $import->getRowCount(), 'status' => 1]); return [true, 0]; } catch (\Throwable $e) { Log::error('达人信息导入失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [false, 400]; } } public static function instUpload($file) { if (!$file->isValid()) return [false, 1061]; $fileExt = $file->getClientOriginalExtension(); if ($fileExt != 'xlsx') return [false, 1062]; $fileName = $file->getClientOriginalName(); try { $sysUserId = Auth::id(); // 插入上传记录 $uploadInfo = SysStarLibraryUpload::query() ->create([ 'file' => $fileName, 'ptype' => 0, 'sys_user_id' => $sysUserId ]); // 获取 groupId $groupId = self::getGroupId($sysUserId); if (empty($groupId) || empty($sysUserId)) { return [false, 400]; } $import = new StarInstitutionImport($groupId, $sysUserId); Excel::import($import, $file); $uploadInfo->update(['row_num' => $import->getRowCount(), 'status' => 1]); return [true, 0]; } catch (\Throwable $e) { Log::error('达人机构导入失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [false, 400]; } } public static function category($ptype) { try { $category = SysStarLibraryClassify::query() ->where('ptype', $ptype) ->where('enable', 1) ->value('categorys'); $category = json_decode($category, true); return [$category, 0]; } catch (\Throwable $e) { Log::error('获取类别失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [[], 400]; } } public static function infoList( $ptype, $catId, $insId, $minFans, $maxFans, $minQprice, $maxQprice, $keyword, $sortField, $sortOrder, $page, $pageSize ) { try { $sysUserId = Auth::id(); $groupId = self::getGroupId($sysUserId); $isViewContact = self::isViewContact(Auth::id()); // 获取该所属字段 $field = SysStarLibraryClassify::query() ->where('ptype', $ptype) ->where('enable', 1) ->value('fields'); // 获取对象 $query = SysStarInformation::query() ->where('ptype', $ptype) ->where('enable', 1); if (!empty($groupId)) $query->where('group_id', $groupId); // 当前平台总数 $extra = self::getAll($groupId); $extra['ptype_num'] = $query->count(); // 指定字段 $query->select(json_decode($field, true)); // 筛选条件 $isFilter = false; if (!empty($catId)) { $isFilter = true; $query->where('cat_id', $catId); } if (!empty($insId)) { $isFilter = true; $query->where('ins_id', $insId); } if (is_numeric($minFans)) { $isFilter = true; $query->where('fans', '>=', ($minFans * 10000)); } if (is_numeric($maxFans)) { $isFilter = true; $query->where('fans', '<=', ($maxFans * 10000)); } if (is_numeric($minQprice)) { $isFilter = true; $query->where('quoted_price_1', '>=', $minQprice); } if (is_numeric($maxQprice)) { $isFilter = true; $query->where('quoted_price_1', '<=', $maxQprice); } if (!empty($keyword)) { $isFilter = true; if (is_numeric($keyword)) { $query->where('accid', 'like', '%' . $keyword . '%'); } else { $query->where('nickname', 'like', '%' . $keyword . '%'); } } // 获取总数 $total = $query->count(); $extra['filt_num'] = $isFilter ? $total : 0; if ($total == 0) return [[], 0, $extra]; // 获取列表 $list = $query ->orderBy($sortField, $sortOrder) ->offset(($page - 1) * $pageSize) ->limit($pageSize) ->get() ->map(function ($item) use ($sysUserId, $isViewContact) { $item->fans = round($item->fans, 2); if (!$isViewContact && ($item->sys_user_id != $sysUserId)) { $item->contact = '***********'; } return $item; }); return [$list, $total, $extra]; } catch (\Throwable $e) { Log::error('获取达人信息列表失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [[], 0, []]; } } public static function instList($catId, $keyword, $page, $pageSize) { try { $sysUserId = Auth::id(); $groupId = self::getGroupId($sysUserId); $isViewContact = self::isViewContact(Auth::id()); // 获取该所属字段 $field = SysStarLibraryClassify::query() ->where('ptype', 0) ->where('enable', 1) ->value('fields'); // 获取对象 $query = SysStarInstitution::query() // ->where(function ($query) use ($catId, $keyword) { // }) ->where('enable', 1); if (!empty($groupId)) $query->where('group_id', $groupId); // 当前平台总数 $extra = self::getAll($groupId); $extra['ptype_num'] = $query->count(); // 指定字段 $query->select(json_decode($field, true)); // 筛选条件 $isFilter = false; if (!empty($catId)) { $isFilter = true; $query->where('cat_id', $catId); } if (!empty($keyword)) { $isFilter = true; $query->where('name', 'like', '%' . $keyword . '%'); } // 获取总数 $total = $query->count(); $extra['filt_num'] = $isFilter ? $total : 0; if ($total == 0) return [[], 0, $extra]; $list = $query ->orderByDesc('created_at') ->offset(($page - 1) * $pageSize) ->limit($pageSize) ->get() ->map(function($item) use ($sysUserId, $isViewContact) { if (!$isViewContact && ($item->sys_user_id != $sysUserId)) { $item->contact = '***********'; } return $item; }); return [$list, $total, $extra]; } catch (\Throwable $e) { Log::error('获取达人机构列表失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [[], 0, []]; } } public static function getAll($groupId=null) { $infoQuery = SysStarInformation::query()->where('enable', 1); $instQuery = SysStarInstitution::query()->where('enable', 1); if (!empty($groupId)) { $infoQuery->where('group_id', $groupId); $instQuery->where('group_id', $groupId); } return [ 'info_num' => $infoQuery->count(), 'inst_num' => $instQuery->count() ]; } public static function upList($page, $pageSize) { try { $query = SysStarLibraryUpload::query()->where('enable', 1); $total = $query->count(); if ($total == 0) return [[], 0]; $list = $query->orderByDesc('created_at') ->offset(($page - 1) * $pageSize) ->limit($pageSize) ->get(); $ptypeListMap = [ 0 => '机构', 1 => '快手', 2 => '抖音', 3 => '小红书' ]; $sysUserListMap = SysUsers::query() ->whereIn('id', $list->pluck('sys_user_id')) ->pluck('name', 'id'); foreach ($list as $item) { $item->sys_user_name = $sysUserListMap->get($item->sys_user_id) ?? null; $item->ptype_name = $ptypeListMap[$item->ptype] ?? null; } return [$list, $total]; } catch (\Throwable $e) { Log::error('获取上传列表失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [[], 0]; } } public static function getGroupId($sysUserId) { try { $groupId = SysUsers::query() ->where('id', $sysUserId) ->where('enable', 1) ->value('group_id'); return $groupId; } catch (\Throwable $e) { Log::error('获取用户组失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return null; } } public static function isViewContact($sysUserId) { try { # 获取角色集合 $role_ids = SysUserRole::query()->where('sys_user_id', $sysUserId)->where('enable', 1)->pluck('role_id')->all(); if (in_array(1, $role_ids)) { return true; } # 获取权限集合(是否包含联系方式) $permissionIds = SysRolePermission::query()->whereIn('role_id', $role_ids)->where('enable', 1)->pluck('permission_id')->all(); if (in_array(19, $permissionIds)) { return true; } return false; } catch (\Throwable $e) { Log::error('获取联系方式是否可见', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return false; } } public static function groupList() { try { $list = SysGroup::query()->where('enable', 1)->orderByDesc('created_at')->get(); return $list; } catch (\Throwable $e) { Log::error('获取用户组列表失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return []; } } public static function groupAdd($name) { try { $isExist = SysGroup::query() ->where('name', $name) ->where('enable', 1) ->exists(); if ($isExist) return [false, 1070]; SysGroup::query() ->insert([ 'name' => $name ]); return [true, 0]; } catch (\Throwable $e) { Log::error('添加用户组失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [false, 400]; } } public static function groupEdit($groupId, $name) { try { $groupInfo = SysGroup::query() ->where('id', $groupId) ->where('enable', 1) ->first(); if (empty($groupInfo)) return [false, 1071]; $isExist = SysGroup::query() ->where('id', '!=', $groupId) ->where('name', $name) ->where('enable', 1) ->exists(); if ($isExist) return [false, 1070]; $groupInfo->update(['name' => $name]); return [true, 0]; } catch (\Throwable $e) { Log::error('编辑用户组失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); return [false, 400]; } } public static function groupDel($groupId) { DB::beginTransaction(); try { $groupInfo = SysGroup::query() ->where('id', $groupId) ->where('enable', 1) ->first(); if (empty($groupInfo)) return [false, 1071]; // 更新账号表 SysUsers::query() ->where('group_id', $groupId) ->update(['group_id' => null]); // 更新用户组表 $groupInfo->update(['enable' => 0]); DB::commit(); return [true, 0]; } catch (\Throwable $e) { Log::error('删除用户组失败', [ 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString() ], 'StarLibraryService'); DB::rollBack(); return [false, 400]; } } }