DeleteNewsCategory.php
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @remark :
* @name :DeleteProductCategory.php
* @author :lyh
* @method :post
* @time :2024/5/16 14:59
*/
namespace App\Console\Commands\DeleteCategory;
use App\Helper\Arr;
use App\Models\Com\NoticeLog;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
/**
* @remark :删除分类
* @name :DeleteProductCategory
* @author :lyh
* @method :post
* @time :2024/5/16 15:00
*/
class DeleteNewsCategory extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'delete_news_category';
/**
* The console command description.
*
* @var string
*/
protected $description = '删除新闻分类';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @remark :批量处理
* @name :handle
* @author :lyh
* @method :post
* @time :2024/5/16 15:02
*/
public function handle(){
while (true){
$noticeLogModel = new NoticeLog();
$list = $noticeLogModel->list(['status'=>NoticeLog::STATUS_PENDING,'type'=>NoticeLog::DELETE_NEWS_CATEGORY],'id',['*'],'asc',100);
if(empty($list)){
sleep(10);
continue;
}
foreach ($list as $item){
echo 'start:' . $item['id'] . PHP_EOL;
try {
$projectModel = new Project();
$projectInfo = $projectModel->read(['id'=>$item['data']['project_id']]);
if($projectInfo === false){
continue;
}
ProjectServer::useProject($projectInfo['id']);
$this->updateCategory();
DB::disconnect('custom_mysql');
$noticeLogModel->edit(['status'=>NoticeLog::STATUS_SUCCESS],['id'=>$item['id']]);
echo 'success:' . $item['id'] . '执行时间:'.date('Y-m-d H:i:s') . PHP_EOL;
}catch (\Exception $e){
echo 'error:' . $item['id'] . $e->getMessage() . '执行时间:'.date('Y-m-d H:i:s') . PHP_EOL;
errorLog('delete_news_category删除失败', $item, $e);
}
}
return true;
}
}
/**
* @remark :更新分类
* @name :updateProductCategory
* @author :lyh
* @method :post
* @time :2024/5/16 15:38
*/
public function updateCategory(){
$page = 1;
$newsModel = new News();
while (true){
$newsList = $newsModel->lists(['status'=>1],$page,1000,'id',['id','category_id']);
if(empty($newsList) || empty($newsList['list'])){
return false;
}
foreach ($newsList['list'] as $v){
$category_id_arr = Arr::setToArr(trim($v['category_id'],','));
if(empty($category_id_arr)){
continue;
}
$categoryModel = new NewsCategory();
foreach ($category_id_arr as $k=>$cate_id){
$cateInfo = $categoryModel->read(['id'=>$cate_id],['id']);
if($cateInfo === false){
//删除关联表
unset($category_id_arr[$k]);
}
}
$str = !empty($category_id_arr) ? ','.Arr::arrToSet($category_id_arr).',' : '';
$newsModel->edit(['category_id'=>$str],['id'=>$v['id']]);
}
$page++;
}
return true;
}
}