123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Service;
- use App\Error;
- use App\Log;
- use App\Models\Material;
- use App\Models\Media;
- use App\Models\MediaGroup;
- use Illuminate\Support\Facades\DB;
- class MediaService
- {
- /**
- * 创建/编辑 素材组
- * */
- public static function editGroup($groupId, $groupName, $sysGroupId, $adminId)
- {
- # 创建/编辑 素材组
- $errno = MediaGroup::editGroup($groupId, $groupName, $sysGroupId, $adminId);
- return [$groupId, $errno];
- }
- /**
- * 删除素材组
- * */
- public static function delGroup($groupId, $sysGroupId, $adminId)
- {
- try {
- DB::beginTransaction();
- # 判断素材组是否存在
- $groupModel = MediaGroup::where('id', $groupId)->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];
- }
- }
|