Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 5d10e8a3
authored
Jul 05, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#20364 Загрузка и синхронизация расширений
1 parent
886c1fd3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
187 additions
and
12 deletions
app/Console/Commands/AdExtensionsAdd.php
app/Console/Commands/DictionaryCampaignsSyncByCampaign.php
app/Models/Pivots/DictionaryCampaign.php
app/Service/Requests/Direct/AddAdExtensions.php
app/Console/Commands/AdExtensionsAdd.php
View file @
5d10e8a
...
...
@@ -2,11 +2,13 @@
namespace
App\Console\Commands
;
use
App\Models\
Campaigns
;
use
App\Models\
AdExtension
;
use
App\Models\Tokens
;
use
App\Service\API\API
;
use
App\Service\Requests\APIRequest
;
use
Carbon\Carbon
;
use
Illuminate\Console\Command
;
use
Illuminate\Database\Eloquent\Relations\HasMany
;
class
AdExtensionsAdd
extends
Command
{
...
...
@@ -42,19 +44,36 @@ class AdExtensionsAdd extends Command
*/
public
function
handle
()
{
$token
=
Tokens
::
where
(
'type'
,
Tokens
::
MAIN
)
->
first
();
$tokens
=
Tokens
::
whereHas
(
'dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate.adExtension'
)
->
where
(
'type'
,
'!='
,
Tokens
::
MAIN
)
->
get
();
if
(
!
$token
)
{
throw
new
\Exception
(
'Не найден токен блин'
);
}
foreach
(
$tokens
as
$token
)
{
$token
->
load
([
'dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate'
=>
function
(
HasMany
$query
)
{
return
$query
->
has
(
'adExtension'
);
},
'dictionaryCampaignsEnabledForExternalSynchronized.adExtensionsForNotExternalForNotReserveCreate.adExtension'
,
]);
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
->
getRequest
(
'AdExtensions'
,
'get'
)
->
call
([
'ModifiedSince'
=>
$token
->
check_changes_ad_extension
,
]);
$goalAdExtensions
=
$token
->
dictionaryCampaignsEnabledForExternalSynchronized
->
pluck
(
'adExtensionsForNotExternalForNotReserveCreate'
)
->
collapse
();
foreach
(
array_chunk
(
$goalAdExtensions
->
pluck
(
'id'
)
->
toArray
(),
1000
)
as
$items
)
{
AdExtension
::
whereIn
(
'id'
,
$items
)
->
update
([
'reserve_create_at'
=>
Carbon
::
now
(),
]);
}
$factory
->
getRequest
(
'AdExtensions'
,
'add'
)
->
call
([
'goalAdExtensions'
=>
$goalAdExtensions
,
]);
}
return
0
;
}
...
...
app/Console/Commands/DictionaryCampaignsSyncByCampaign.php
View file @
5d10e8a
...
...
@@ -132,7 +132,7 @@ class DictionaryCampaignsSyncByCampaign extends Command
LEFT JOIN goal_keywords gk on k.id = gk.keyword_id AND gk.goal_ad_group_id=gag.id
WHERE gk.keyword_id is null AND k.deleted_at is null
"
);
//грузим расширения которых по какой то причне нет в целевых.
DB
::
insert
(
"
INSERT INTO goal_ad_extensions(ad_extension_id, token_id, created_at, updated_at)
...
...
@@ -155,6 +155,19 @@ class DictionaryCampaignsSyncByCampaign extends Command
WHERE gad.advertisement_id is null and ad.campaign_id is not null
"
);
//грузим связь объевлений к расширения которых по какой то причне нет в целевых.
DB
::
insert
(
"
INSERT INTO goal_advertisement_goal_ad_extensions(goal_advertisement_id, goal_ad_extension_id, created_at, updated_at)
SELECT ae.id, gae.id, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM advertisement_ad_extensions aae
INNER JOIN ad_extensions ae on aae.ad_extension_id = ae.id
INNER JOIN goal_ad_extensions gae on ae.id = gae.ad_extension_id
INNER JOIN advertisements a on aae.advertisement_id = a.id
INNER JOIN goal_advertisements ga on a.id = ga.advertisement_id
LEFT JOIN goal_advertisement_goal_ad_extensions gagae on gae.id = gagae.goal_ad_extension_id and ga.id = gagae.goal_advertisement_id
WHERE gagae.ad_extension_id is null
"
);
//грузим объявления которых по какой то причне нет в целевых.
DB
::
insert
(
"
INSERT INTO goal_bid_modifiers(dictionary_campaign_external_id, goal_ad_group_external_id, dictionary_campaign_id,
...
...
app/Models/Pivots/DictionaryCampaign.php
View file @
5d10e8a
...
...
@@ -486,6 +486,16 @@ class DictionaryCampaign extends Pivot
return
$this
->
hasMany
(
GoalKeywordDelete
::
class
,
'dictionary_campaign_id'
);
}
public
function
adExtensions
()
{
return
$this
->
hasMany
(
GoalAdExtension
::
class
,
'dictionary_campaign_id'
);
}
public
function
adExtensionsForNotExternalForNotReserveCreate
()
{
return
$this
->
adExtensions
()
->
forNotExternal
()
->
forNotReserveCreate
();
}
public
function
goalAdvertisements
()
{
return
$this
->
hasMany
(
GoalAdvertisement
::
class
,
'dictionary_campaign_id'
);
...
...
app/Service/Requests/Direct/AddAdExtensions.php
0 → 100644
View file @
5d10e8a
<?php
namespace
App\Service\Requests\Direct
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Pivots\GoalAdExtension
;
use
App\Models\Pivots\GoalAdGroup
;
use
App\Models\Variable
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
App\Service\StrReplaceByVariables
;
use
Carbon\Carbon
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Support\Facades\Log
;
class
AddAdExtensions
extends
DirectRequest
{
protected
$max_count
=
1000
;
protected
$timestamp
;
/* @var Collection|GoalAdExtension[] */
protected
$goalAdExtensions
;
public
function
call
(
$params
=
null
)
{
$this
->
requestPrepare
(
$params
);
$process
=
new
ProcessCallLimitedAPI
(
$this
);
dispatch
(
$process
)
->
onQueue
(
'limits'
);
}
public
function
getObjectsCount
()
{
return
count
(
$this
->
getParams
()[
'AdExtensions'
]);
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
{
$splinter
=
$this
->
sliceByKey
(
$maxObjects
,
'AdExtensions'
);
$splinter
->
putParams
([
'goalAdExtensions'
=>
$this
->
goalAdExtensions
->
slice
(
$maxObjects
)
->
values
(),
]);
$this
->
putParams
([
'goalAdExtensions'
=>
$this
->
goalAdExtensions
->
slice
(
0
,
$maxObjects
),
]);
return
$splinter
;
}
public
function
handle
(
$response
)
{
try
{
if
(
!
isset
(
$response
[
'result'
][
'AddResults'
]))
{
return
;
}
foreach
(
$response
[
'result'
][
'AddResults'
]
as
$key
=>
$add_result
)
{
$goalAdExtension
=
$this
->
goalAdExtensions
->
get
(
$key
);
if
(
!
isset
(
$add_result
[
'Id'
]))
{
Log
::
debug
(
"AddAdExtension, empty Id"
);
Log
::
debug
(
$add_result
);
Log
::
debug
(
$this
->
getParams
()[
'AdExtensions'
][
$key
]);
$goalAdExtension
->
update
([
'reserve_create_at'
=>
null
,
]);
continue
;
}
$external_id
=
(
string
)
$add_result
[
'Id'
];
$goalAdExtension
->
update
([
'external_id'
=>
$external_id
,
'external_upload_at'
=>
Carbon
::
now
(),
'reserve_create_at'
=>
null
,
]);
}
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
throw
$e
;
}
}
public
function
failed
()
{
GoalAdGroup
::
whereIn
(
'id'
,
$this
->
goalAdExtensions
->
pluck
(
'id'
)
->
toArray
())
->
update
([
'reserve_create_at'
=>
null
,
]);
}
public
function
putParams
(
$params
)
{
$this
->
goalAdExtensions
=
$params
[
'goalAdExtensions'
];
}
private
function
requestPrepare
(
$params
)
{
$this
->
setService
(
'AdExtensions'
);
$this
->
setMethod
(
'add'
);
$this
->
putParams
(
$params
);
$variables
=
Variable
::
all
();
$lists
=
[];
$this
->
setParams
([
'AdExtensions'
=>
$this
->
goalAdExtensions
->
map
(
function
(
GoalAdExtension
$goalAdExtension
)
use
(
$variables
,
&
$lists
)
{
if
(
!
isset
(
$lists
[
$goalAdGroup
->
dictionary_campaign_id
]))
{
$list
=
Variable
::
getListVariablesByDictionaryCampaign
(
$goalAdExtension
->
dictionary_campaign_id
,
$variables
);
$lists
[
$goalAdExtension
->
dictionary_campaign_id
]
=
$list
;
}
else
{
$list
=
$lists
[
$goalAdExtension
->
dictionary_campaign_id
];
}
return
[
'Callout'
=>
[
'CalloutText'
=>
StrReplaceByVariables
::
getInstance
(
$goalAdExtension
->
adExtension
->
callout_text
,
$list
)
->
get
(),
],
];
})
->
toArray
(),
]);
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment