where('sys_group_id', $sysGroupId) ->where('enable', 1)->first(); if(empty($groupModel)) { DB::rollBack(); return 2410; } # 删除 $groupModel->enable = 0; $groupModel->admin_id = $adminId; $delResult = $groupModel->save(); if(!$delResult) { // 删除失败 DB::rollBack(); return 2412; } $mediaCount = Media::where('group_id', $groupId)->where('sys_group_id', $sysGroupId)->count(); if($mediaCount){ # 解除该组的素材分组信息 $updateRes = Media::where('group_id', $groupId)->where('sys_group_id', $sysGroupId)->update(['group_id' => null]); if(!$updateRes) { DB::rollBack(); return 2412; } } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('删除素材库流程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'params' => [ 'group_id' => $groupId, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId ] ], 'DelGroup'); } return 0; } /** * 获取素材组列表 * */ public static function getGroupList($sysGroupId, $keyword, $page, $pageSize) { # 获取列表数据 list($list, $count) = MediaGroup::groupList($sysGroupId, $keyword, $page, $pageSize); return [$list, $count]; } /** * 批量保存素材 * */ public static function editMedia($mediaData, $sysGroupId, $adminId, &$failList) { if(empty($mediaData)) return 2413; try { DB::beginTransaction(); foreach ($mediaData as $datum) { $mediaId = $datum['id'] ?? null; $groupId = $datum['group_id']; $platformId = $datum['platform_id']; $title = $datum['title']; $type = $datum['type']; $localPath = $datum['local_path']; $ossUrl = $datum['url']; if(!$localPath || !$ossUrl) continue; # 保存素材 $errorNo = Media::editMedia($title, $localPath, $ossUrl, $type, $mediaId, $groupId, $platformId, $sysGroupId, $adminId); if($errorNo) { $failList[] = array( 'id' => $mediaId, 'url' => $ossUrl, 'local_path' => $localPath, 'err_msg' => Error::getError($errorNo) ); } } DB::commit(); } catch (\Exception $e) { DB::rollBack(); Log::logError('批量保存素材流程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'params' => [ 'media_data' => $mediaData, 'sys_group_id' => $sysGroupId, 'admin_id' => $adminId ] ], 'EditMedia'); return 2415; } return 0; } /** * 获取素材列表 * */ public static function getMediaList($sysGroupId, $keyword, $platformId, $groupId, $page, $pageSize) { list($list, $count) = Media::mediaList($sysGroupId, $keyword, $platformId, $groupId, $page, $pageSize); # 获取列表中的组信息 $groupIdList = array_unique($list->pluck('group_id')->toArray()); $groupData = MediaGroup::whereIn('id', $groupIdList)->get(); # 获取平台列表 $platformList = IntelligentMassSendingService::platformIndex(); $platformList = collect($platformList); foreach ($list as $item) { # 处理平台信息 $platformInfo = $platformList->where('platform_id', $item->platform_id)->first(); $item->platform_name = $platformInfo['platform_name'] ?? null; # 处理分组信息 $groupInfo = $groupData->where('id', $item->group_id)->first(); $item->group_name = $groupInfo['name'] ?? null; } return [$list, $count]; } /** * 批量移动分组 * */ public static function changeGroup($mediaIds, $groupId, $sysGroupId, $adminId) { $mediaIds = explode(',', $mediaIds); if(empty($mediaIds)) return 2416; if($groupId){ # 判断分组id是否合法 $isExist = MediaGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->exists(); if(!$isExist) return 2417; } $result = Media::whereIn('id', $mediaIds) ->where('sys_group_id', $sysGroupId) ->where('enable', 1) ->update(['group_id' => $groupId, 'admin_id' => $adminId]); // return $result ? 0 : 2418; return 0; } /** * 批量删除素材 * */ public static function delMedia($mediaIds, $sysGroupId, $adminId) { $mediaIds = explode(',', $mediaIds); if(empty($mediaIds)) return 2419; $mediaList = Media::where('enable', 1)->whereIn('id', $mediaIds)->where('sys_group_id', $sysGroupId)->get(); foreach ($mediaList as $media) { $localPath = $media->local_path; $ossUrl = $media->oss_url; # 删除本地图片 @unlink($localPath); # 删除oss图片 $oss = new OssService('playlet'); $oss->delete($ossUrl); # 删除数据表中的数据 Media::where('id', $media->id)->update(['enable' => 0, 'admin_id' => $adminId]); } return 0; } /** * 保存素材 * */ public static function saveMaterial($mediaId, $corpid, &$errno) { # 查询素材信息 $mediaInfo = Media::where('enable', 1)->where('id', $mediaId)->first(); if(empty($mediaInfo)) { $errno = 2420; return []; } if(!$mediaInfo->oss_url || !$mediaInfo->local_path) { $errno = 2421; return []; } # 存储素材信息到material表 $materialId = Material::saveMaterial($corpid, $mediaInfo->type, $mediaInfo->local_path, $mediaInfo->oss_url); if($materialId === false) { $errno = 2422; return []; } return ['material_id' => $materialId, 'url' => $mediaInfo->oss_url]; } }