GetAdGroups.php 8.2 KB
<?php

namespace App\Service\Requests\Direct;

use App\Jobs\ProcessCallLimitedAPI;
use App\Models\AdGroup;
use App\Models\Campaigns;
use App\Models\NegativeKeywordSharedSet;
use App\Models\Pivots\DictionaryCampaign;
use App\Service\Contract\APIRequest;
use App\Service\Requests\DirectRequest;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class GetAdGroups extends DirectRequest
{
    protected $max_count = -1;
    protected $max_count_CampaignIds = 10;
    protected $max_count_Ids = 10000;

    function call($params = null)
    {
        $this->requestPrepare($params);
        $process = new ProcessCallLimitedAPI($this);
        dispatch($process)->onQueue('limits');
    }

    public function getObjectsCount()
    {
        $params = $this->getParams();
        if (isset($params['SelectionCriteria']['Ids'])) {
            return count($params['SelectionCriteria']['Ids']);
        }
        return parent::getObjectsCount();
    }

    public function slice($maxObjects): ?APIRequest
    {
        $params = $this->getParams();

        if (isset($params['SelectionCriteria']['Ids'])) {
            return $this->sliceByKey($maxObjects, ['SelectionCriteria', 'Ids']);
        }

        return null;
    }

    function handle($response)
    {
        $external_ids = [];

        if (!isset($response['result']['AdGroups'])) {
            return;
        }

        foreach ($response['result']['AdGroups'] as $ad_group) {
            $external_id = (string)$ad_group['CampaignId'];

            if (isset($external_ids[$external_id])) {
                continue;
            }
            $external_ids[$external_id] = true;
        }

        if (!count($external_ids)) {
            return;
        }

        if ($this->getToken()->isMain()) {
            $campaigns = Campaigns::whereIn('external_id', array_keys($external_ids))
                ->get()
                ->keyBy('external_id');
        } else {
            $campaigns = DictionaryCampaign::whereIn('external_id', array_keys($external_ids))
                ->get()
                ->keyBy('external_id');
        }

        $campaign_ids_synced_need = [];

        foreach ($response['result']['AdGroups'] as $ad_group) {

            $external_id = (string)$ad_group['CampaignId'];

            if (!$campaign = $campaigns[$external_id] ?? false) {
                continue;
            }


            $external_id = (string)$ad_group['Id'];

            if ($this->getToken()->isMain()) {

                $negative_keyword_shared_sets = collect();

                $data = [
                    'campaign_id' => $campaign->getKey(),
                    'external_id' => $external_id,
                    'campaign_external_id' => $campaign->external_id,
                    'name' => $ad_group['Name'],
                    'region_ids' => $ad_group['RegionIds'],
                    'negative_keywords' => $ad_group['NegativeKeywords'],
                    'tracking_params' => $ad_group['TrackingParams'],
                    'mobile_app_ad_group' => $ad_group['MobileAppAdGroup'] ?? null,
                    'dynamic_text_ad_group' => $ad_group['DynamicTextAdGroup'] ?? null,
                    'dynamic_text_feed_ad_group' => $ad_group['DynamicTextFeedAdGroup'] ?? null,
                    'cpm_banner_user_profile_ad_group' => $ad_group['CpmBannerUserProfileAdGroup'] ?? null,
                    'cpm_video_ad_group' => $ad_group['CpmVideoAdGroup'] ?? null,
                    'smart_ad_group' => $ad_group['SmartAdGroup'] ?? null,

                    'status' => $ad_group['Status'],
                    'type' => $ad_group['Type'],
                    'sub_type' => $ad_group['Subtype'],
                    'serving_status' => $ad_group['ServingStatus'],
                    'restricted_region_ids' => $ad_group['RestrictedRegionIds'],
                    'updated_self' => null,
                    'updated_at' => Carbon::now(),
                    'deleted_at' => null,
                ];

                $negative_keyword_shared_set_external_ids_array = [];

                foreach ($ad_group['NegativeKeywordSharedSetIds']['Items'] ?? [] as $negative_keyword_shared_set_external_id) {

                    $negative_keyword_shared_set_external_id = (string)$negative_keyword_shared_set_external_id;

                    $negativeKeywordSharedSet = NegativeKeywordSharedSet::firstWhere('external_id', $negative_keyword_shared_set_external_id);

                    if (!$negativeKeywordSharedSet) {
                        $negative_keyword_shared_set_external_ids_array[] = $negative_keyword_shared_set_external_id;
                        continue;
                    }

                    $negative_keyword_shared_sets->push($negativeKeywordSharedSet);

                }

                $data['negative_keyword_shared_set_external_ids'] = $negative_keyword_shared_set_external_ids_array;

                $ad_group = AdGroup::where('external_id', $external_id)->withTrashed()->first();

                if ($ad_group) {
                    $ad_group->update($data);
                    $campaign_ids_synced_need[$ad_group->campaign_id] = true;
                } else {
                    $ad_group = AdGroup::create($data);

                    if ($ad_group->wasChanged(['campaign_id'])) {
                        $campaign_ids_synced_need[$ad_group->campaign_id] = true;
                    } elseif (
                        $ad_group->wasChanged($ad_group::getPropertiesWatch()->toArray())
                        ||
                        !empty($negativeKeywordSharedSets_sync['attached'])
                        ||
                        !empty($negativeKeywordSharedSets_sync['detached'])
                        ||
                        !empty($negativeKeywordSharedSets_sync['updated'])
                    ) {
                        $ad_group->goalGroups()->has('dictionaryCampaign')->forExternal()->update([
                            'updated_need' => Carbon::now(),
                        ]);
                    }

                }

                $negativeKeywordSharedSets_sync = $ad_group->negativeKeywordSharedSets()->sync($negative_keyword_shared_sets->pluck('id'));

            }

        }

        if ($this->getToken()->isMain()) {

            if (
                !empty($this->getParams()['SelectionCriteria']['CampaignIds'])
                &&
                !isset($response['result']['LimitedBy'])
            ) {
                $campaign_ids = $this->getParams()['SelectionCriteria']['CampaignIds'];

                DB::update("UPDATE ad_groups ag
                            INNER JOIN campaigns ca ON ag.campaign_id = ca.id
                    SET ag.deleted_at = now()
                    WHERE ca.groups_loaded_at IS NOT NULL
                        AND ag.updated_at <= ca.groups_loaded_at
                        AND ag.deleted_at is null
                        AND ca.external_id in (" . implode(", ", $campaign_ids) . ")");

                Campaigns::whereIn('external_id', $campaign_ids)
                    ->update([
                        'groups_loaded_at' => Carbon::now(),
                    ]);
            }

            if (count($campaign_ids_synced_need)) {
                Campaigns::findMany(array_keys($campaign_ids_synced_need))->each(function (Campaigns $campaign) {
                    $campaign->dictionaryCampaigns()->update([
                        'synced_need' => Carbon::now(),
                    ]);
                });
            }

        }
    }

    private function requestPrepare($filter)
    {
        $this->setService('AdGroups');
        $this->setMethod('get');
        $params = [
            "FieldNames" => [
                "Id", "CampaignId", "Name", "NegativeKeywords",
                "NegativeKeywordSharedSetIds", "RegionIds", "RestrictedRegionIds",
                "ServingStatus", "Status", "Subtype", "TrackingParams", "Type",
            ],
        ];
        if (isset($filter['CampaignIds'])) {
            $params['SelectionCriteria'] = [
                'CampaignIds' => $filter['CampaignIds'],
            ];
        }
        if (isset($filter['Ids'])) {
            $this->max_count = $this->max_count_Ids;
            $params['SelectionCriteria'] = [
                'Ids' => $filter['Ids'],
            ];
        }
        $this->setParams($params);
    }
}