作者 lyh

gx

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :DeleteProductCategory.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/5/16 14:59
  8 + */
  9 +
  10 +namespace App\Console\Commands\DeleteCategory;
  11 +
  12 +use App\Helper\Arr;
  13 +use App\Models\Blog\Blog;
  14 +use App\Models\Blog\BlogCategory;
  15 +use App\Models\Com\NoticeLog;
  16 +use App\Models\CustomModule\CustomModuleCategory;
  17 +use App\Models\CustomModule\CustomModuleContent;
  18 +use App\Models\News\News;
  19 +use App\Models\News\NewsCategory;
  20 +use App\Models\Product\Category;
  21 +use App\Models\Product\CategoryRelated;
  22 +use App\Models\Product\Product;
  23 +use App\Models\Project\Project;
  24 +use App\Services\ProjectServer;
  25 +use Illuminate\Console\Command;
  26 +use Illuminate\Support\Facades\DB;
  27 +
  28 +/**
  29 + * @remark :删除分类
  30 + * @name :DeleteProductCategory
  31 + * @author :lyh
  32 + * @method :post
  33 + * @time :2024/5/16 15:00
  34 + */
  35 +class DeleteCustomCategory extends Command
  36 +{
  37 + /**
  38 + * The name and signature of the console command.
  39 + *
  40 + * @var string
  41 + */
  42 + protected $signature = 'delete_custom_category';
  43 +
  44 + /**
  45 + * The console command description.
  46 + *
  47 + * @var string
  48 + */
  49 + protected $description = '删除扩展模块分类';
  50 +
  51 + /**
  52 + * Create a new command instance.
  53 + *
  54 + * @return void
  55 + */
  56 + public function __construct()
  57 + {
  58 + parent::__construct();
  59 + }
  60 +
  61 + /**
  62 + * @remark :批量处理
  63 + * @name :handle
  64 + * @author :lyh
  65 + * @method :post
  66 + * @time :2024/5/16 15:02
  67 + */
  68 + public function handle(){
  69 + while (true){
  70 + $noticeLogModel = new NoticeLog();
  71 + $list = $noticeLogModel->list(['status'=>NoticeLog::STATUS_PENDING,'type'=>NoticeLog::DELETE_CUSTOM_CATEGORY],'id',['*'],'asc',100);
  72 + if(empty($list)){
  73 + sleep(100);
  74 + }
  75 + foreach ($list as $item){
  76 + echo 'start:' . $item['id'] . PHP_EOL;
  77 + try {
  78 + $projectModel = new Project();
  79 + $projectInfo = $projectModel->read(['id'=>$item['data']['project_id']]);
  80 + if($projectInfo === false){
  81 + continue;
  82 + }
  83 + ProjectServer::useProject($projectInfo['id']);
  84 + $this->updateCategory();
  85 + DB::disconnect('custom_mysql');
  86 + $noticeLogModel->edit(['status'=>NoticeLog::STATUS_SUCCESS],['id'=>$item['id']]);
  87 + echo 'success:' . $item['id'] . PHP_EOL;
  88 + }catch (\Exception $e){
  89 + echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
  90 + errorLog('项目初始化失败', $item, $e);
  91 + }
  92 + }
  93 + return true;
  94 + }
  95 + }
  96 +
  97 + /**
  98 + * @remark :更新分类
  99 + * @name :updateProductCategory
  100 + * @author :lyh
  101 + * @method :post
  102 + * @time :2024/5/16 15:38
  103 + */
  104 + public function updateCategory(){
  105 + $page = 1;
  106 + $customModel = new CustomModuleContent();
  107 + while (true){
  108 + $customList = $customModel->lists(['status'=>0],$page,1000,'id',['id','category_id']);
  109 + if(empty($customList) || empty($customList['list'])){
  110 + return false;
  111 + }
  112 + foreach ($customList['list'] as $v){
  113 + $category_id_arr = Arr::setToArr(trim($v['category_id'],','));
  114 + if(empty($category_id_arr)){
  115 + continue;
  116 + }
  117 + $categoryModel = new CustomModuleCategory();
  118 + foreach ($category_id_arr as $k=>$cate_id){
  119 + $cateInfo = $categoryModel->read(['id'=>$cate_id],['id']);
  120 + if($cateInfo == false){
  121 + //删除关联表
  122 + unset($category_id_arr[$k]);
  123 + }
  124 + }
  125 + $str = !empty($category_id_arr) ? ','.Arr::arrToSet($category_id_arr).',' : '';
  126 + $customModel->edit(['category_id'=>$str],['id'=>$v['id']]);
  127 + }
  128 + $page++;
  129 + }
  130 + return true;
  131 + }
  132 +}
@@ -11,6 +11,7 @@ namespace App\Http\Logic\Bside\CustomModule; @@ -11,6 +11,7 @@ namespace App\Http\Logic\Bside\CustomModule;
11 11
12 use App\Helper\Arr; 12 use App\Helper\Arr;
13 use App\Http\Logic\Bside\BaseLogic; 13 use App\Http\Logic\Bside\BaseLogic;
  14 +use App\Models\Com\NoticeLog;
14 use App\Models\CustomModule\CustomModuleCategory; 15 use App\Models\CustomModule\CustomModuleCategory;
15 use App\Models\CustomModule\CustomModuleContent; 16 use App\Models\CustomModule\CustomModuleContent;
16 use App\Models\RouteMap\RouteMap; 17 use App\Models\RouteMap\RouteMap;
@@ -190,14 +191,16 @@ class CustomModuleCategoryLogic extends BaseLogic @@ -190,14 +191,16 @@ class CustomModuleCategoryLogic extends BaseLogic
190 public function categoryDel(){ 191 public function categoryDel(){
191 $ids = $this->param['id']; 192 $ids = $this->param['id'];
192 foreach ($ids as $id){ 193 foreach ($ids as $id){
193 - //删除路由  
194 - $this->delRoute($id);  
195 - $this->model->del(['id'=>$id]);  
196 - //同步删除产品字段category_id  
197 - $contentModel = new CustomModuleContent();  
198 - $contentModel->edit(['category_id'=>DB::raw("REPLACE(category_id, ',$id,' , ',')")],['category_id'=>['like','%,'.$id.',%']]);  
199 - $contentModel->edit(['category_id'=>null],['category_id'=>',']); 194 + $str = [];
  195 + $this->getAllSub($id,$str);
  196 + $str[] = $id;
  197 + foreach ($str as $value){
  198 + //删除路由
  199 + $this->delRoute($value);
  200 + $this->model->del(['id'=>$value]);
  201 + }
200 } 202 }
  203 + NoticeLog::createLog(NoticeLog::DELETE_CUSTOM_CATEGORY, ['project_id' => $this->user['project_id']]);
201 return $this->success(); 204 return $this->success();
202 } 205 }
203 206
@@ -17,6 +17,7 @@ class NoticeLog extends Base @@ -17,6 +17,7 @@ class NoticeLog extends Base
17 const DELETE_PRODUCT_CATEGORY = 'delete_product_category'; 17 const DELETE_PRODUCT_CATEGORY = 'delete_product_category';
18 const DELETE_BLOG_CATEGORY = 'delete_blog_category'; 18 const DELETE_BLOG_CATEGORY = 'delete_blog_category';
19 const DELETE_NEWS_CATEGORY = 'delete_news_category'; 19 const DELETE_NEWS_CATEGORY = 'delete_news_category';
  20 + const DELETE_CUSTOM_CATEGORY = 'delete_custom_category';
20 const STATUS_PENDING = 0; 21 const STATUS_PENDING = 0;
21 const STATUS_SUCCESS = 1; 22 const STATUS_SUCCESS = 1;
22 const STATUS_FAIL = 2; 23 const STATUS_FAIL = 2;