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 c1a470e0
authored
Jun 22, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#19498 Снхронизация объявлений с целевыми
1 parent
780d9d2f
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
297 additions
and
18 deletions
app/Console/Commands/AdvertisementsUpdate.php
app/Models/Advertisement.php
app/Service/Requests/Direct/AddAds.php
app/Service/Requests/Direct/GetAds.php
app/Service/Requests/Direct/UpdateAdGroups.php
app/Service/Requests/Direct/UpdateAds.php
app/Console/Commands/AdvertisementsUpdate.php
0 → 100644
View file @
c1a470e
<?php
namespace
App\Console\Commands
;
use
App\Models\Tokens
;
use
App\Service\API\API
;
use
App\Service\Requests\APIRequest
;
use
Illuminate\Console\Command
;
use
Illuminate\Database\Eloquent\Relations\HasMany
;
class
AdvertisementsUpdate
extends
Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected
$signature
=
'ads:update'
;
/**
* The console command description.
*
* @var string
*/
protected
$description
=
'Изменение созданных объявлений с целевого аккаунта'
;
/**
* Create a new command instance.
*
* @return void
*/
public
function
__construct
()
{
parent
::
__construct
();
}
/**
* Execute the console command.
*
* @return int
*/
public
function
handle
()
{
$tokens
=
Tokens
::
whereHas
(
'dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedUpdated.advertisement'
)
->
where
(
'type'
,
'!='
,
Tokens
::
MAIN
)
->
get
();
foreach
(
$tokens
as
$token
)
{
$token
->
load
([
'dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedUpdated'
=>
function
(
HasMany
$query
)
{
return
$query
->
has
(
'advertisement'
);
},
'dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedUpdated.advertisement'
,
]);
$factory
=
APIRequest
::
getInstance
(
API
::
YANDEX
);
$factory
->
setToken
(
$token
);
$factory
->
getRequest
(
'Ads'
,
'add'
)
->
call
([
'goalAds'
=>
$token
->
dictionaryCampaignsEnabledForExternalSynchronized
->
pluck
(
'goalAdvertisementsForNeedUpdated'
)
->
collapse
(),
]);
}
return
0
;
}
}
app/Models/Advertisement.php
View file @
c1a470e
...
@@ -2,8 +2,7 @@
...
@@ -2,8 +2,7 @@
namespace
App\Models
;
namespace
App\Models
;
use
App\Models\Pivots\GoalAdGroup
;
use
App\Models\Pivots\GoalAdvertisement
;
use
Carbon\Carbon
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
use
Illuminate\Database\Eloquent\SoftDeletes
;
...
@@ -121,23 +120,25 @@ class Advertisement extends Model
...
@@ -121,23 +120,25 @@ class Advertisement extends Model
static
public
function
getPropertiesWatch
()
static
public
function
getPropertiesWatch
()
{
{
return
collect
([
return
collect
([
'age_label'
,
'title'
,
'title2'
,
'text'
,
'href'
,
'display_url_path'
,
'v_card_id'
,
'ad_image_hash'
,
'site_link_set_id'
,
'ad_extensions'
,
'video_extension'
,
'price_extension'
,
'turbo_page_id'
,
'business_id'
,
'prefer_v_card_over_business'
,
]);
]);
}
}
public
static
function
boot
()
{
parent
::
boot
();
static
::
created
(
function
(
Advertisement
$advertisement
)
{
//
});
static
::
updated
(
function
(
Advertisement
$advertisement
)
{
//
});
}
/**
/**
* @param Builder $query
* @param Builder $query
* @return Builder
* @return Builder
...
@@ -162,4 +163,9 @@ class Advertisement extends Model
...
@@ -162,4 +163,9 @@ class Advertisement extends Model
return
$this
->
campaign
()
->
forEnabled
()
->
forManaged
();
return
$this
->
campaign
()
->
forEnabled
()
->
forManaged
();
}
}
public
function
goalAdvertisements
()
{
return
$this
->
hasMany
(
GoalAdvertisement
::
class
,
'advertisement_id'
);
}
}
}
app/Service/Requests/Direct/AddAds.php
View file @
c1a470e
...
@@ -3,7 +3,6 @@
...
@@ -3,7 +3,6 @@
namespace
App\Service\Requests\Direct
;
namespace
App\Service\Requests\Direct
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Pivots\GoalAdGroup
;
use
App\Models\Pivots\GoalAdvertisement
;
use
App\Models\Pivots\GoalAdvertisement
;
use
App\Models\Variable
;
use
App\Models\Variable
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Contract\APIRequest
;
...
...
app/Service/Requests/Direct/GetAds.php
View file @
c1a470e
...
@@ -7,6 +7,7 @@ use App\Models\AdGroup;
...
@@ -7,6 +7,7 @@ use App\Models\AdGroup;
use
App\Models\Advertisement
;
use
App\Models\Advertisement
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
App\Service\Requests\DirectRequest
;
use
Carbon\Carbon
;
use
Illuminate\Support\Facades\Log
;
use
Illuminate\Support\Facades\Log
;
class
GetAds
extends
DirectRequest
class
GetAds
extends
DirectRequest
...
@@ -135,10 +136,20 @@ class GetAds extends DirectRequest
...
@@ -135,10 +136,20 @@ class GetAds extends DirectRequest
]);
]);
}
}
Advertisement
::
updateOrCreate
([
$advertisement
=
Advertisement
::
updateOrCreate
([
'external_id'
=>
$external_id
'external_id'
=>
$external_id
],
$data
);
],
$data
);
if
(
$advertisement
->
wasRecentlyCreated
)
{
$campaign_ids_synced_need
[
$advertisement
->
campaign_id
]
=
true
;
}
elseif
(
$advertisement
->
wasChanged
([
'campaign_id'
]))
{
$campaign_ids_synced_need
[
$advertisement
->
campaign_id
]
=
true
;
}
elseif
(
$advertisement
->
wasChanged
(
$advertisement
::
getPropertiesWatch
()
->
toArray
()))
{
$advertisement
->
goalAdvertisements
()
->
has
(
'dictionaryCampaign'
)
->
forExternal
()
->
update
([
'updated_need'
=>
Carbon
::
now
(),
]);
}
}
else
{
}
else
{
//
//
}
}
...
...
app/Service/Requests/Direct/UpdateAdGroups.php
View file @
c1a470e
...
@@ -59,7 +59,7 @@ class UpdateAdGroups extends DirectRequest
...
@@ -59,7 +59,7 @@ class UpdateAdGroups extends DirectRequest
continue
;
continue
;
}
}
$external_id
=
(
string
)
$update_result
[
'Id'
];
$external_id
=
(
string
)
$update_result
[
'Id'
];
GoalAdGroup
::
forExternal
()
->
needUpdated
()
GoalAdGroup
::
forExternal
()
->
needUpdated
()
->
where
(
'external_id'
,
$external_id
)
->
where
(
'external_id'
,
$external_id
)
...
...
app/Service/Requests/Direct/UpdateAds.php
0 → 100644
View file @
c1a470e
<?php
namespace
App\Service\Requests\Direct
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Pivots\GoalAdvertisement
;
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
UpdateAds
extends
DirectRequest
{
protected
$max_count
=
1000
;
protected
$timestamp
;
/* @var Collection|GoalAdvertisement[] */
protected
$goalAds
;
public
function
call
(
$params
=
null
)
{
$this
->
requestPrepare
(
$params
);
$process
=
new
ProcessCallLimitedAPI
(
$this
);
dispatch
(
$process
)
->
onQueue
(
'limits'
);
}
public
function
getObjectsCount
()
{
return
count
(
$this
->
getParams
()[
'Ads'
]);
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
{
$splinter
=
$this
->
sliceByKey
(
$maxObjects
,
'Ads'
);
$splinter
->
putParams
([
'goalAds'
=>
$this
->
goalAds
->
slice
(
$maxObjects
)
->
values
(),
]);
$this
->
putParams
([
'goalAds'
=>
$this
->
goalAds
->
slice
(
0
,
$maxObjects
),
]);
return
$splinter
;
}
public
function
handle
(
$response
)
{
try
{
if
(
isset
(
$response
[
'result'
][
'Errors'
]))
{
Log
::
debug
(
$response
[
'Errors'
]);
Log
::
debug
(
$this
->
getParams
());
}
if
(
!
isset
(
$response
[
'result'
][
'UpdateResults'
]))
{
return
;
}
foreach
(
$response
[
'result'
][
'UpdateResults'
]
as
$key
=>
$update_result
)
{
if
(
!
isset
(
$add_result
[
'Id'
]))
{
Log
::
debug
(
"UpdateAds, empty Id"
);
Log
::
debug
(
$update_result
);
Log
::
debug
(
$this
->
getParams
());
continue
;
}
$external_id
=
(
string
)
$update_result
[
'Id'
];
GoalAdvertisement
::
forExternal
()
->
needUpdated
()
->
where
(
'external_id'
,
$external_id
)
->
update
([
'updated_need'
=>
null
,
]);
}
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
throw
$e
;
}
}
public
function
putParams
(
$params
)
{
$this
->
goalAds
=
$params
[
'goalAds'
];
}
private
function
requestPrepare
(
$params
)
{
$this
->
setService
(
'Ads'
);
$this
->
setMethod
(
'update'
);
$this
->
putParams
(
$params
);
$variables
=
Variable
::
all
();
$lists
=
[];
$this
->
setParams
([
'Ads'
=>
$this
->
goalAds
->
map
(
function
(
GoalAdvertisement
$goalAdvertisement
)
use
(
$variables
,
&
$lists
)
{
if
(
!
isset
(
$lists
[
$goalAdvertisement
->
dictionary_campaign_id
]))
{
$list
=
Variable
::
getListVariablesByDictionaryCampaign
(
$goalAdvertisement
->
dictionary_campaign_id
,
$variables
);
$lists
[
$goalAdvertisement
->
dictionary_campaign_id
]
=
$list
;
}
else
{
$list
=
$lists
[
$goalAdvertisement
->
dictionary_campaign_id
];
}
$advertisement
=
$goalAdvertisement
->
advertisement
;
$data
=
[
'Id'
=>
$goalAdvertisement
->
external_id
,
'TextAd'
=>
[
'Title'
=>
StrReplaceByVariables
::
getInstance
(
$advertisement
->
title
,
$list
)
->
get
(),
'Text'
=>
StrReplaceByVariables
::
getInstance
(
$advertisement
->
text
,
$list
)
->
get
(),
'Mobile'
=>
$advertisement
->
mobile
?
'YES'
:
'NO'
,
'AgeLabel'
=>
$advertisement
->
age_label
,
],
];
if
(
$advertisement
->
title2
)
{
$data
[
'TextAd'
][
'Title2'
]
=
StrReplaceByVariables
::
getInstance
(
$advertisement
->
title2
,
$list
)
->
get
();
}
if
(
$advertisement
->
href
)
{
$data
[
'TextAd'
][
'Href'
]
=
$advertisement
->
href
;
}
if
(
$advertisement
->
display_url_path
)
{
$data
[
'TextAd'
][
'DisplayUrlPath'
]
=
$advertisement
->
display_url_path
;
}
if
(
$advertisement
->
v_card_id
)
{
$data
[
'TextAd'
][
'VCardId'
]
=
$advertisement
->
v_card_id
;
}
if
(
$advertisement
->
ad_image_hash
)
{
$data
[
'TextAd'
][
'AdImageHash'
]
=
$advertisement
->
ad_image_hash
;
}
if
(
$advertisement
->
site_link_set_id
)
{
$data
[
'TextAd'
][
'SitelinkSetId'
]
=
$advertisement
->
site_link_set_id
;
}
if
(
$advertisement
->
ad_extensions
)
{
$data
[
'TextAd'
][
'CalloutSetting'
]
=
[
'AdExtensions'
=>
array_map
(
function
(
$ad_extension
)
{
return
[
'AdExtensionId'
=>
$ad_extension
[
'AdExtensionId'
],
'Operation'
=>
'SET'
,
];
},
$advertisement
->
ad_extensions
)
];
}
if
(
$advertisement
->
video_extension
)
{
$data
[
'TextAd'
][
'VideoExtension'
]
=
[
'CreativeId'
=>
$advertisement
->
video_extension
[
'CreativeId'
],
];
}
if
(
$advertisement
->
price_extension
)
{
$data
[
'TextAd'
][
'PriceExtension'
]
=
[
'Price'
=>
$advertisement
->
price_extension
[
'Price'
],
'PriceQualifier'
=>
$advertisement
->
price_extension
[
'PriceQualifier'
],
'PriceCurrency'
=>
$advertisement
->
price_extension
[
'PriceCurrency'
],
];
if
(
isset
(
$advertisement
->
price_extension
[
'OldPrice'
])
&&
!
is_null
(
$advertisement
->
price_extension
[
'OldPrice'
]))
{
$data
[
'TextAd'
][
'PriceExtension'
][
'OldPrice'
]
=
$advertisement
->
price_extension
[
'OldPrice'
];
}
}
if
(
$advertisement
->
turbo_page_id
)
{
$data
[
'TextAd'
][
'TurboPageId'
]
=
$advertisement
->
turbo_page_id
;
}
if
(
$advertisement
->
business_id
)
{
$data
[
'TextAd'
][
'BusinessId'
]
=
$advertisement
->
business_id
;
}
if
(
$advertisement
->
prefer_v_card_over_business
)
{
$data
[
'TextAd'
][
'PreferVCardOverBusiness'
]
=
$advertisement
->
prefer_v_card_over_business
?
'YES'
:
'NO'
;
}
return
$data
;
})
->
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