| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
- /**
- * 分类.
- *
- * @author jim <jim458734573@gmail.com>
- * @date 2020-01-31 10:39:38
- */
- declare(strict_types=1);
- namespace App\Repositories;
- use App\Constants\CategoryConstant;
- use App\Model\CategoryModel;
- use App\Model\TypeModel;
- use App\Model\VideoModel;
- use App\Model\VideoPerfectModel;
- use App\Repositories\Utils\M3u8CacheRepository;
- use KZ2\Repositories\Model\Repository;
- use Psr\SimpleCache\CacheInterface;
- class CategoryRepository extends Repository
- {
- public function model()
- {
- return CategoryModel::class;
- }
- /**
- * 创建一个
- * @param array $setarr
- * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model
- */
- public function createOne(array $setarr)
- {
- $item = [
- 'name' => data_get($setarr, 'name', ''),
- 'alias_name' => strtolower(data_get($setarr, 'alias_name', '')),
- 'position' => strtolower(data_get($setarr, 'position', '')),
- 'squad' => data_get($setarr, 'squad', 1000),
- 't_color' => data_get($setarr, 't_color', ''),
- 'parent_id' => data_get($setarr, 'parent_id', 0),
- 'cover_pic' => data_get($setarr, 'cover_pic', ''),
- 'show_num' => intval(data_get($setarr, 'show_num', 10)),
- ];
- $obj = CategoryModel::query()->create($item);
- return $obj;
- }
- /**
- * 更新一个.
- * @param mixed $id
- * @param array $setarr
- * @return mixed|array
- */
- public function updateOne($id, array $setarr)
- {
- $resultInfo = [
- 'is_ok' => true,
- 'old' => null,
- 'new' => null,
- ];
- $item = CategoryModel::query()->where(['id' => $id])->firstOrFail();
- $resultInfo['old'] = clone $item;
- if (isset($setarr['name'])) {
- $item->name = $setarr['name'];
- }
- if (isset($setarr['alias_name'])) {
- $item->alias_name = strtolower($setarr['alias_name']);
- }
- if (isset($setarr['position'])) {
- $item->position = strtolower($setarr['position']);
- }
- if (isset($setarr['squad'])) {
- $item->squad = $setarr['squad'];
- }
- if (isset($setarr['t_color'])) {
- $item->t_color = $setarr['t_color'];
- }
- if (isset($setarr['parent_id'])) {
- $item->parent_id = $setarr['parent_id'];
- }
- if (isset($setarr['cover_pic'])) {
- $item->cover_pic = $setarr['cover_pic'];
- }
- if (isset($setarr['show_num'])) {
- $item->show_num = $setarr['show_num'];
- }
- $result = $item->save();
- if ($result) {
- $resultInfo['new'] = $item;
- if ($resultInfo['old']->alias_name != $item->alias_name) {
- $this->clearCategoryCacheByAlias($resultInfo['old']->alias_name);
- $this->clearCategoryCacheByAlias($item->alias_name);
- } else {
- $this->clearCategoryCacheByAlias($resultInfo['old']->alias_name);
- }
- if ($resultInfo['old']->postion != $item->position) {
- $this->clearCategoryCacheByPosition($resultInfo['old']->position);
- $this->clearCategoryCacheByPosition($item->position);
- } else {
- $this->clearCategoryCacheByPosition($resultInfo['old']->position);
- }
- $this->clearVideoCacheByAlias($resultInfo['old']->alias_name);
- } else {
- $resultInfo['is_ok'] = false;
- }
- return $resultInfo;
- }
- /**
- * 删除一个.
- * @param mixed $id
- * @return bool|int|mixed|null
- * @throws mixed|array
- */
- public function deleteOne($id)
- {
- $resultInfo = [
- 'is_ok' => true,
- 'old' => null,
- 'new' => null,
- ];
- $item = CategoryModel::query()->where(['id' => $id])->firstOrFail();
- $resultInfo['old'] = clone $item;
- $result = $item->delete();
- if ($result) {
- $this->clearCategoryCacheByAlias($resultInfo['old']->alias_name);
- $this->clearCategoryCacheByPosition($resultInfo['old']->position);
- $this->clearVideoCacheByAlias($resultInfo['old']->alias_name);
- } else {
- $resultInfo['is_ok'] = false;
- }
- return $resultInfo;
- }
- /**
- * 删除多个.
- * @param mixed $ids
- * @return array
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function deleteMulti($ids)
- {
- $result = [
- 'total' => 0,
- 'success_ids' => [],
- 'fail_ids' => [],
- ];
- $arrIds = $ids;
- if (is_string($ids)) {
- $arrIds = [$ids];
- }
- $result['total'] = count($arrIds);
- if (!empty($arrIds)) {
- foreach ($arrIds as $id) {
- $item = CategoryModel::query()->where(['id' => $id])->first();
- if ($item) {
- $oldItem = clone $item;
- if ($item->delete()) {
- $result['success_ids'][] = $id;
- $this->clearCategoryCacheByAlias($oldItem->alias_name);
- $this->clearCategoryCacheByPosition($oldItem->position);
- $this->clearVideoCacheByAlias($oldItem->alias_name);
- }
- unset($oldItem);
- } else {
- $result['fail_ids'][] = $id;
- }
- }
- }
- return $result;
- }
- /**
- * 获取视频.
- * @param $categoryAliasName
- * @param int $num
- * @param null $position
- * @return array|mixed
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function getVideos($categoryAliasName, $num = 10, $position = null)
- {
- if ($num > 200) {
- $num = 200;
- }
- $flagPosition = $position;
- if (is_null($position)) {
- $flagPosition = 'nullnull';
- }
- $cacheRepo = make(CacheInterface::class);
- $cacheTime = 24 * 60 * 60;
- $cacheKey = 'category:video:'. $categoryAliasName;
- $arrData = $cacheRepo->get($cacheKey, []);
- if (empty($arrData)) {
- $where = [
- 'category_alias_name' => $categoryAliasName,
- ];
- if (!is_null($position)) {
- $where['position'] = $position;
- }
- $arrData = [];
- $columns = ['id', 'title', 'cover', 'like', 'uid', 'coins', 'pay_type', 'mu', 'comment', 'watch', 'resolution', 'duration'];
- $strColumns = implode(',', $columns);
- $tempItems = VideoModel::query()->with(['category:type.id as type_id,type.title','user', 'perfect'])->whereHas('perfect', function ($q) use ($categoryAliasName) {
- $q->where(['category_alias_name' => $categoryAliasName]);
- })->orderByDesc('created_at')->orderByDesc('id')->take($num)->get($columns);
- if ($tempItems->count()) {
- $tempItems = $tempItems->toArray();
- foreach ($tempItems as $v) {
- $v['cover'] = M3u8CacheRepository::getImageUrl($v['cover']);
- if (!empty($v['user'])) {
- $v['user']['avatar'] = !empty($v['user']['avatar']) ? M3u8CacheRepository::getImageUrl($v['user']['avatar']) : '';
- }
- if (!empty($v['category'])) {
- foreach ($v['category'] as $ck => $t) {
- unset($v['category'][$ck]['pivot']);
- }
- }
- $v['mu'] = ''; // 列表干掉m3u8地址.
- unset($v['perfect']);
- $arrData[] = $v;
- }
- }
- if (!empty($arrData)) {
- $cacheRepo->set($cacheKey, $arrData, $cacheTime);
- }
- }
- return $arrData;
- }
- /**
- * 查1个
- * @param $aliasName
- * @param false $isForce
- * @return array|mixed
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function findOneByAlias($aliasName, $isForce = false)
- {
- $cacheRepo = make(CacheInterface::class);
- $cacheTime = 24 * 60 * 60;
- $cacheKey = 'category:detail:alias_name:'. $aliasName;
- $data = $cacheRepo->get($cacheKey, []);
- if (empty($data) || $isForce == true) {
- $obj = CategoryModel::query()->where(['alias_name' => $aliasName])->first();
- if (!empty($obj->id)) {
- $data = $obj->toArray();
- $cacheRepo->set($cacheKey, $data, $cacheTime);
- }
- }
- return $data;
- }
- /**
- * 查1个
- * @param $position
- * @param false $isForce
- * @return array|mixed
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function findAllByPosition($position, $isForce = false)
- {
- $cacheRepo = make(CacheInterface::class);
- $cacheTime = 24 * 60 * 60;
- $cacheKey = 'category:lists:position:'. $position;
- $data = $cacheRepo->get($cacheKey, []);
- if (empty($data) || $isForce == true) {
- $lists = CategoryModel::query()->where(['position' => $position])
- ->orderByDesc('squad')
- ->orderByDesc('id')
- ->get();
- if ($lists->isNotEmpty()) {
- $data = $lists->toArray();
- $cacheRepo->set($cacheKey, $data, $cacheTime);
- }
- }
- return $data;
- }
- /**
- * 清除分类缓存.
- * @param $aliasName
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function clearCategoryCacheByAlias($aliasName)
- {
- $cacheRepo = make(CacheInterface::class);
- $cacheKey = 'category:detail:alias_name:'. $aliasName;
- $cacheRepo->delete($cacheKey);
- return true;
- }
- /**
- * 清除视频缓存.
- * @param $aliasName
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function clearVideoCacheByAlias($aliasName)
- {
- $cacheRepo = make(CacheInterface::class);
- $cacheKey = 'category:video:'. $aliasName;
- $cacheRepo->delete($cacheKey);
- return true;
- }
- /**
- * @param $position
- * @return bool
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function clearCategoryCacheByPosition($position)
- {
- $cacheRepo = make(CacheInterface::class);
- $cacheKey = 'category:lists:position:'. $position;
- $cacheRepo->delete($cacheKey);
- return true;
- }
- /**
- * 推送到视频.
- * @param $aliasName
- * @param $ids
- * @return array
- */
- public function pushPerfectVideo($aliasName, $ids)
- {
- $result = [
- 'total' => 0,
- 'success_ids' => [],
- 'fail_ids' => [],
- ];
- $result['total'] = count($ids);
- foreach ($ids as $videoId) {
- $obj = VideoPerfectModel::query()->where(['category_alias_name' => $aliasName, 'video_id' => $videoId])->first();
- if (empty($obj)) {
- $setarr = [
- 'video_id' => $videoId,
- 'category_alias_name' => $aliasName,
- ];
- $saveStatus = VideoPerfectModel::query()->create($setarr);
- if ($saveStatus) {
- $result['success_ids'][] = $videoId;
- } else {
- $result['fail_ids'][] = $videoId;
- }
- } else {
- $result['fail_ids'][] = $videoId;
- }
- }
- $info = $this->findOneByAlias($aliasName);
- if (!empty($info)) {
- $this->clearCategoryCacheByPosition($info['position']);
- }
- $this->clearVideoCacheByAlias($aliasName);
- // make(VideoRepository::class)->clearAllLastestPagesCache();
- return $result;
- }
- /**
- * 剔除桶.
- * @param $ids
- * @return array
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function popPerfectVideo($ids)
- {
- $result = [
- 'total' => 0,
- 'success_ids' => [],
- 'fail_ids' => [],
- ];
- $result['total'] = count($ids);
- $arrAlias = [];
- $arrPosition = [];
- foreach ($ids as $id) {
- $obj = VideoPerfectModel::query()->where(['id' => $id])->first();
- if (!empty($obj)) {
- if (!in_array($obj->category_alias_name, $arrAlias)) {
- $arrAlias[] = $obj->category_alias_name;
- }
- if (!in_array($obj->position, $arrPosition)) {
- $arrPosition[] = $obj->position;
- }
- $delStatus = $obj->delete();
- if ($delStatus) {
- $result['success_ids'][] = $id;
- } else {
- $result['fail_ids'][] = $id;
- }
- } else {
- $result['fail_ids'][] = $id;
- }
- }
- if ($arrAlias) {
- foreach ($arrAlias as $aliasName) {
- $this->clearVideoCacheByAlias($aliasName);
- }
- }
- if ($arrPosition) {
- foreach ($arrPosition as $position) {
- $this->clearCategoryCacheByPosition($position);
- }
- }
- // make(VideoRepository::class)->clearAllLastestPagesCache();
- return $result;
- }
- /**
- * 获取所有导航列表
- *
- * @return void
- */
- public function allCategory($type)
- {
-
- $cache = make(CacheInterface::class);
- $res = $cache->rememberForever(CategoryConstant::ALL_CATEGORY . $type, function() use ($type) {
- $columns = ['id','name', 'alias_name', 'parent_id'];
- $categoryParentData = CategoryModel::query()->where('parent_id', 0)->where('type', $type)->orderByDesc('squad')->get($columns);
- foreach ($categoryParentData as &$v) {
- $v['sub'] = CategoryModel::query()->where('parent_id', $v->id)->orderByDesc('squad')->get($columns);
- }
-
- return $categoryParentData;
- });
- return $res;
- }
- /**
- * 获取标签
- *
- * @param [type] $isHot
- * @param [type] $type
- * @return void
- */
- public function getTag($type, $isHot)
- {
- $cache = make(CacheInterface::class);
- $res = $cache->rememberForever(CategoryConstant::TAG_TYPE . $type . $isHot,function() use ($isHot, $type) {
- $query = TypeModel::query();
- if (!empty($isHot)) {
- $query = $query->where('is_hot', $isHot)->take(10);
- }
- $columns = ['id','title', 'cover', 'is_hot', 'description'];
- $data = $query->where(['type' => $type, 'status' => 1])->orderByDesc('sort')->orderByDesc('is_hot')->get($columns);
- foreach ($data as &$v) {
- $v['cover'] = M3u8CacheRepository::getImageUrl($v['cover']);
- }
- return $data;
- });
- return $res;
- }
- }
|