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 1c27787c
authored
Aug 20, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#20794 Сделать чтобы в целевых РК оставалась одна карточка
1 parent
1477dd45
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
323 additions
and
3 deletions
app/Console/Commands/AdvertisementsDelete.php
app/Models/Advertisement.php
app/Models/Pivots/DictionaryCampaign.php
app/Models/Pivots/GoalAdvertisement.php
app/Service/Requests/Direct/ArchiveAds.php
app/Service/Requests/Direct/DeleteAds.php
database/migrations/2021_08_20_090017_add_advertisements_delete_column.php
app/Console/Commands/AdvertisementsDelete.php
0 → 100644
View file @
1c27787
<?php
namespace
App\Console\Commands
;
use
App\Models\Advertisement
;
use
App\Models\Pivots\GoalAdvertisement
;
use
App\Models\Tokens
;
use
App\Service\Requests\Direct\DeleteAds
;
use
Carbon\Carbon
;
use
Illuminate\Console\Command
;
class
AdvertisementsDelete
extends
Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected
$signature
=
'ads:delete'
;
/**
* 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
()
{
$token
=
Tokens
::
firstWhere
(
'type'
,
'!='
,
Tokens
::
MAIN
);
if
(
$token
)
{
$this
->
sendRequest
(
$token
,
Advertisement
::
needDeleted
()
->
forNotReserveDelete
()
->
get
());
}
$tokens
=
Tokens
::
has
(
'dictionaryCampaignsEnabledForExternalSynchronized.goalAdvertisementsForNeedDeletedForNotReserveDelete'
)
->
where
(
'type'
,
'!='
,
Tokens
::
MAIN
)
->
get
();
foreach
(
$tokens
as
$token
)
{
$this
->
sendRequest
(
$token
,
$token
->
dictionaryCampaignsEnabledForExternalSynchronized
->
pluck
(
'goalAdvertisementsForNeedDeletedForNotReserveDelete'
)
->
collapse
());
}
return
0
;
}
private
function
sendRequest
(
$token
,
$ads
)
{
/* @var $ads Advertisement[]|GoalAdvertisement[] */
if
(
!
$ads
->
count
())
{
return
;
}
foreach
(
array_chunk
(
$ads
->
pluck
(
'external_id'
)
->
toArray
(),
1000
)
as
$items
)
{
if
(
$token
->
isMain
())
{
Advertisement
::
whereIn
(
'external_id'
,
$items
)
->
update
([
'reserve_delete_at'
=>
Carbon
::
now
(),
]);
}
else
{
GoalAdvertisement
::
whereIn
(
'external_id'
,
$items
)
->
update
([
'reserve_delete_at'
=>
Carbon
::
now
(),
]);
}
}
$request
=
new
DeleteAds
();
$request
->
setToken
(
$token
)
->
call
([
'ids'
=>
$ads
->
pluck
(
'external_id'
)
->
toArray
(),
]);
}
}
app/Models/Advertisement.php
View file @
1c27787
...
@@ -101,6 +101,8 @@ class Advertisement extends Model
...
@@ -101,6 +101,8 @@ class Advertisement extends Model
'updated_self'
,
'updated_self'
,
'archived_need'
,
'archived_need'
,
'reserve_archive_at'
,
'reserve_archive_at'
,
'deleted_need'
,
'reserve_delete_at'
,
];
];
protected
$casts
=
[
protected
$casts
=
[
...
@@ -117,6 +119,8 @@ class Advertisement extends Model
...
@@ -117,6 +119,8 @@ class Advertisement extends Model
'prefer_v_card_over_business'
=>
'boolean'
,
'prefer_v_card_over_business'
=>
'boolean'
,
'archived_need'
=>
'datetime'
,
'archived_need'
=>
'datetime'
,
'reserve_archive_at'
=>
'datetime'
,
'reserve_archive_at'
=>
'datetime'
,
'deleted_need'
=>
'datetime'
,
'reserve_delete_at'
=>
'datetime'
,
];
];
/**
/**
...
@@ -198,6 +202,24 @@ class Advertisement extends Model
...
@@ -198,6 +202,24 @@ class Advertisement extends Model
return
$query
->
whereNull
(
'reserve_archive_at'
);
return
$query
->
whereNull
(
'reserve_archive_at'
);
}
}
/**
* @param Builder $query
* @return Builder
*/
public
function
scopeNeedDeleted
(
$query
)
{
return
$query
->
whereNotNull
(
'deleted_need'
);
}
/**
* @param Builder $query
* @return Builder
*/
public
function
scopeForNotReserveDelete
(
$query
)
{
return
$query
->
whereNull
(
'reserve_delete_at'
);
}
public
function
groups
()
public
function
groups
()
{
{
return
$this
->
hasMany
(
AdGroup
::
class
,
'ad_group_id'
);
return
$this
->
hasMany
(
AdGroup
::
class
,
'ad_group_id'
);
...
...
app/Models/Pivots/DictionaryCampaign.php
View file @
1c27787
...
@@ -506,6 +506,11 @@ class DictionaryCampaign extends Pivot
...
@@ -506,6 +506,11 @@ class DictionaryCampaign extends Pivot
return
$this
->
goalAdvertisements
()
->
forExternal
()
->
needArchived
()
->
forNotReserveArchive
()
->
forNotArchived
();
return
$this
->
goalAdvertisements
()
->
forExternal
()
->
needArchived
()
->
forNotReserveArchive
()
->
forNotArchived
();
}
}
public
function
goalAdvertisementsForNeedDeletedForNotReserveDelete
()
{
return
$this
->
goalAdvertisements
()
->
forExternal
()
->
needDeleted
()
->
forNotReserveDelete
();
}
public
function
goalBidModifiers
()
public
function
goalBidModifiers
()
{
{
return
$this
->
hasMany
(
GoalBidModifier
::
class
,
'dictionary_campaign_id'
);
return
$this
->
hasMany
(
GoalBidModifier
::
class
,
'dictionary_campaign_id'
);
...
...
app/Models/Pivots/GoalAdvertisement.php
View file @
1c27787
...
@@ -77,6 +77,8 @@ class GoalAdvertisement extends Pivot
...
@@ -77,6 +77,8 @@ class GoalAdvertisement extends Pivot
'archive_at'
,
'archive_at'
,
'archived_need'
,
'archived_need'
,
'reserve_archive_at'
,
'reserve_archive_at'
,
'deleted_need'
,
'reserve_delete_at'
,
];
];
protected
$casts
=
[
protected
$casts
=
[
...
@@ -88,6 +90,8 @@ class GoalAdvertisement extends Pivot
...
@@ -88,6 +90,8 @@ class GoalAdvertisement extends Pivot
'archive_at'
=>
'datetime'
,
'archive_at'
=>
'datetime'
,
'archived_need'
=>
'datetime'
,
'archived_need'
=>
'datetime'
,
'reserve_archive_at'
=>
'datetime'
,
'reserve_archive_at'
=>
'datetime'
,
'deleted_need'
=>
'datetime'
,
'reserve_delete_at'
=>
'datetime'
,
];
];
public
$incrementing
=
true
;
public
$incrementing
=
true
;
...
@@ -111,6 +115,8 @@ class GoalAdvertisement extends Pivot
...
@@ -111,6 +115,8 @@ class GoalAdvertisement extends Pivot
'updated_need'
,
'updated_need'
,
'reserve_create_at'
,
'reserve_create_at'
,
'reserve_update_at'
,
'reserve_update_at'
,
'deleted_need'
,
'reserve_delete_at'
,
'archived_need'
,
'archived_need'
,
'reserve_archive_at'
,
'reserve_archive_at'
,
'archive_at'
,
'archive_at'
,
...
@@ -207,6 +213,24 @@ class GoalAdvertisement extends Pivot
...
@@ -207,6 +213,24 @@ class GoalAdvertisement extends Pivot
return
$query
->
whereNull
(
'reserve_archive_at'
);
return
$query
->
whereNull
(
'reserve_archive_at'
);
}
}
/**
* @param Builder $query
* @return Builder
*/
public
function
scopeNeedDeleted
(
$query
)
{
return
$query
->
whereNotNull
(
'deleted_need'
);
}
/**
* @param Builder $query
* @return Builder
*/
public
function
scopeForNotReserveDelete
(
$query
)
{
return
$query
->
whereNull
(
'reserve_delete_at'
);
}
public
function
advertisement
()
public
function
advertisement
()
{
{
return
$this
->
belongsTo
(
Advertisement
::
class
,
'advertisement_id'
);
return
$this
->
belongsTo
(
Advertisement
::
class
,
'advertisement_id'
);
...
...
app/Service/Requests/Direct/ArchiveAds.php
View file @
1c27787
...
@@ -42,10 +42,8 @@ class ArchiveAds extends DirectRequest
...
@@ -42,10 +42,8 @@ class ArchiveAds extends DirectRequest
foreach
(
$response
[
'result'
][
'ArchiveResults'
]
as
$key
=>
$archive_result
)
{
foreach
(
$response
[
'result'
][
'ArchiveResults'
]
as
$key
=>
$archive_result
)
{
if
(
!
isset
(
$archive_result
[
'Id'
]))
{
if
(
!
isset
(
$archive_result
[
'Id'
]))
{
Log
::
debug
(
"ArchiveAds, empty Id"
);
Log
::
debug
(
$archive_result
);
$external_id
=
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
][
$key
];
$external_id
=
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
][
$key
];
Log
::
debug
(
$external_id
);
/*
/*
* array (
* array (
...
@@ -61,16 +59,25 @@ class ArchiveAds extends DirectRequest
...
@@ -61,16 +59,25 @@ class ArchiveAds extends DirectRequest
if
(
$this
->
getToken
()
->
isMain
())
{
if
(
$this
->
getToken
()
->
isMain
())
{
Advertisement
::
whereExternalId
(
$external_id
)
Advertisement
::
whereExternalId
(
$external_id
)
->
update
([
->
update
([
'deleted_need'
=>
Carbon
::
now
(),
'archived_need'
=>
null
,
'archived_need'
=>
null
,
'reserve_archive_at'
=>
null
,
'reserve_archive_at'
=>
null
,
]);
]);
}
else
{
}
else
{
GoalAdvertisement
::
whereExternalId
(
$external_id
)
GoalAdvertisement
::
whereExternalId
(
$external_id
)
->
update
([
->
update
([
'deleted_need'
=>
Carbon
::
now
(),
'archived_need'
=>
null
,
'archived_need'
=>
null
,
'reserve_archive_at'
=>
null
,
'reserve_archive_at'
=>
null
,
]);
]);
}
}
continue
;
}
else
{
Log
::
debug
(
"ArchiveAds, empty Id"
);
Log
::
debug
(
$archive_result
);
Log
::
debug
(
$external_id
);
}
}
if
(
$this
->
getToken
()
->
isMain
())
{
if
(
$this
->
getToken
()
->
isMain
())
{
...
...
app/Service/Requests/Direct/DeleteAds.php
0 → 100644
View file @
1c27787
<?php
namespace
App\Service\Requests\Direct
;
use
App\Jobs\ProcessCallLimitedAPI
;
use
App\Models\Advertisement
;
use
App\Models\Pivots\GoalAdvertisement
;
use
App\Service\Contract\APIRequest
;
use
App\Service\Requests\DirectRequest
;
use
Illuminate\Support\Facades\Log
;
class
DeleteAds
extends
DirectRequest
{
protected
$max_count
=
10000
;
protected
$timestamp
;
public
function
call
(
$params
=
null
)
{
$this
->
requestPrepare
(
$params
);
$process
=
new
ProcessCallLimitedAPI
(
$this
);
dispatch
(
$process
)
->
onQueue
(
'limits'
);
}
public
function
getObjectsCount
()
{
return
count
(
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
]);
}
public
function
slice
(
$maxObjects
)
:
?
APIRequest
{
return
$this
->
sliceByKey
(
$maxObjects
,
[
'SelectionCriteria'
,
'Ids'
]);;
}
public
function
handle
(
$response
)
{
try
{
if
(
!
isset
(
$response
[
'result'
][
'DeleteResults'
]))
{
return
;
}
foreach
(
$response
[
'result'
][
'DeleteResults'
]
as
$key
=>
$delete_result
)
{
if
(
!
isset
(
$delete_result
[
'Id'
]))
{
if
(
isset
(
$delete_result
[
'Errors'
][
0
][
'Code'
])
&&
$delete_result
[
'Errors'
][
0
][
'Code'
]
==
8800
)
{
if
(
$external_id
=
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
][
$key
]){
if
(
$this
->
getToken
()
->
isMain
())
{
Advertisement
::
needDeleted
()
->
where
(
'external_id'
,
$external_id
)
->
delete
();
}
else
{
GoalAdvertisement
::
forExternal
()
->
needDeleted
()
->
where
(
'external_id'
,
$external_id
)
->
delete
();
}
continue
;
}
}
Log
::
debug
(
"DeleteAds, empty Id"
);
Log
::
debug
(
$delete_result
);
$external_id
=
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
][
$key
];
Log
::
debug
(
$external_id
);
if
(
$this
->
getToken
()
->
isMain
())
{
Advertisement
::
whereExternalId
(
$external_id
)
->
update
([
'reserve_delete_at'
=>
null
,
]);
}
else
{
GoalAdvertisement
::
whereExternalId
(
$external_id
)
->
update
([
'reserve_delete_at'
=>
null
,
]);
}
continue
;
}
$external_id
=
(
string
)
$delete_result
[
'Id'
];
if
(
$this
->
getToken
()
->
isMain
())
{
Advertisement
::
needDeleted
()
->
where
(
'external_id'
,
$external_id
)
->
delete
();
}
else
{
GoalAdvertisement
::
forExternal
()
->
needDeleted
()
->
where
(
'external_id'
,
$external_id
)
->
delete
();
}
}
}
catch
(
\Exception
$e
)
{
Log
::
debug
(
$e
);
throw
$e
;
}
}
public
function
failed
()
{
if
(
$this
->
getToken
()
->
isMain
())
{
Advertisement
::
whereIn
(
'external_id'
,
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
])
->
update
([
'reserve_delete_at'
=>
null
,
]);
}
else
{
GoalAdvertisement
::
whereIn
(
'external_id'
,
$this
->
getParams
()[
'SelectionCriteria'
][
'Ids'
])
->
update
([
'reserve_delete_at'
=>
null
,
]);
}
}
private
function
requestPrepare
(
$params
)
{
$this
->
setService
(
'Ads'
);
$this
->
setMethod
(
'delete'
);
$this
->
setParams
([
'SelectionCriteria'
=>
[
'Ids'
=>
$params
[
'ids'
],
],
]);
}
}
database/migrations/2021_08_20_090017_add_advertisements_delete_column.php
0 → 100644
View file @
1c27787
<?php
use
Illuminate\Database\Migrations\Migration
;
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Support\Facades\Schema
;
class
AddAdvertisementsDeleteColumn
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up
()
{
Schema
::
table
(
'advertisements'
,
function
(
Blueprint
$table
)
{
$table
->
timestamp
(
'deleted_need'
)
->
nullable
();
$table
->
timestamp
(
'reserve_delete_at'
)
->
nullable
();
});
Schema
::
table
(
'goal_advertisements'
,
function
(
Blueprint
$table
)
{
$table
->
timestamp
(
'deleted_need'
)
->
nullable
();
$table
->
timestamp
(
'reserve_delete_at'
)
->
nullable
();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down
()
{
//
}
}
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