123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Distributor\Repositories;
- use App\Models\SmmUserAccount as Model;
- use Carbon\Carbon;
- use Dcat\Admin\Repositories\EloquentRepository;
- class SmmUserAccount extends EloquentRepository
- {
-
- protected $eloquentClass = Model::class;
-
- public static function createAccountIfMediaExists($mediaName, $accountId,$accountName, $accessToken,$expiresAt,$refreshToken = '',$backupField1 = '')
- {
- $model = new Model();
-
- $mediaRecord = $model->where('name', $mediaName)->first();
- if (!$mediaRecord) {
- return null;
- }
-
- $userRow = $model->where('account_id', $accountId)->first();
- if ($userRow) {
- $userRow->access_token = $accessToken;
- $userRow->expires_at = $expiresAt;
- $userRow->name = $accountName;
- $userRow->refresh_token = $refreshToken;
- $userRow->backup_field1 = $backupField1;
- $userRow->save();
- } else {
-
- $data = [
- 'account_id' => $accountId,
- 'name' => $accountName,
- 'access_token' => $accessToken,
- 'expires_at' => $expiresAt,
- 'parent_id' => $mediaRecord->id,
- 'dist_id' => getDistributorId(),
- 'created_at' => Carbon::now(),
- 'updated_at' => Carbon::now(),
- 'refresh_token' => $refreshToken,
- 'backup_field1' => $backupField1,
- ];
- $model->insert($data);
- }
- return true;
- }
-
- public static function getRootAccounts()
- {
- $model = new Model();
- $accounts = $model->where('dist_id', 0)->where('parent_id', 0)->get();
- return $accounts;
- }
-
- public static function getUserAccounts()
- {
- $model = new Model();
- $accounts = $model->where('dist_id','>', 0)->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
- return $accounts;
- }
-
- public static function getAllYouTubeUserAccounts()
- {
- $model = new Model();
- $youTube = $model->where('name', 'YouTube')->first();
- $accounts = $model->where('parent_id','=',$youTube->id)->where('expires_at','>', Carbon::now())->orderBy('id', 'asc')->get();
-
- return $accounts;
- }
-
- public static function getAccountsByIds($ids)
- {
- $model = new Model();
- $accounts = $model->whereIn('id', $ids)->get();
- return $accounts;
- }
- public static function getAccountById($id)
- {
- $model = new Model();
- $account = $model->find($id);
- return $account;
- }
-
- public static function returnChildCount($id)
- {
- $model = new Model();
- $count = $model->where('parent_id', $id)->where('expires_at','>', Carbon::now())->count();
- return $count;
- }
- public static function getYoutubeCategory()
- {
- $model = new Model();
- $categories = $model->where('name', 'YouTube')->first();
- $categories = json_decode($categories->backup_field1,true);
- return $categories;
- }
- public static function deleteAccount($id)
- {
- $model = new Model();
- $account = $model->find($id);
- if ($account && $account->dist_id == getDistributorId() && $account->parent_id > 0) {
- $account->delete();
- return true;
- }
- return false;
- }
- }
|