123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- namespace App\Services\Sys;
- use App\Imports\StarInformationImport;
- use App\Imports\StarInstitutionImport;
- use App\Models\Sys\SysGroup;
- use App\Models\Sys\SysRolePermission;
- use App\Models\Sys\SysStarInformation;
- use App\Models\Sys\SysStarInstitution;
- use App\Models\Sys\SysStarLibraryClassify;
- use App\Models\Sys\SysStarLibraryUpload;
- use App\Models\Sys\SysUserRole;
- use App\Models\Sys\SysUsers;
- use App\Support\Log;
- use Illuminate\Support\Facades\Auth;
- use Maatwebsite\Excel\Facades\Excel;
- use DB;
- class StarLibraryService
- {
- public static function infoUpload($ptype, $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' => $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];
- }
- }
- }
|