作者 lyh

gx

<?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\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\Com\NoticeLog;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Product;
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 DeleteCustomCategory extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'delete_custom_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_CUSTOM_CATEGORY],'id',['*'],'asc',100);
if(empty($list)){
sleep(100);
}
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'] . PHP_EOL;
}catch (\Exception $e){
echo 'error:' . $item['id'] . $e->getMessage() . PHP_EOL;
errorLog('项目初始化失败', $item, $e);
}
}
return true;
}
}
/**
* @remark :更新分类
* @name :updateProductCategory
* @author :lyh
* @method :post
* @time :2024/5/16 15:38
*/
public function updateCategory(){
$page = 1;
$customModel = new CustomModuleContent();
while (true){
$customList = $customModel->lists(['status'=>0],$page,1000,'id',['id','category_id']);
if(empty($customList) || empty($customList['list'])){
return false;
}
foreach ($customList['list'] as $v){
$category_id_arr = Arr::setToArr(trim($v['category_id'],','));
if(empty($category_id_arr)){
continue;
}
$categoryModel = new CustomModuleCategory();
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).',' : '';
$customModel->edit(['category_id'=>$str],['id'=>$v['id']]);
}
$page++;
}
return true;
}
}
... ...
... ... @@ -11,6 +11,7 @@ namespace App\Http\Logic\Bside\CustomModule;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Com\NoticeLog;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\RouteMap\RouteMap;
... ... @@ -190,14 +191,16 @@ class CustomModuleCategoryLogic extends BaseLogic
public function categoryDel(){
$ids = $this->param['id'];
foreach ($ids as $id){
//删除路由
$this->delRoute($id);
$this->model->del(['id'=>$id]);
//同步删除产品字段category_id
$contentModel = new CustomModuleContent();
$contentModel->edit(['category_id'=>DB::raw("REPLACE(category_id, ',$id,' , ',')")],['category_id'=>['like','%,'.$id.',%']]);
$contentModel->edit(['category_id'=>null],['category_id'=>',']);
$str = [];
$this->getAllSub($id,$str);
$str[] = $id;
foreach ($str as $value){
//删除路由
$this->delRoute($value);
$this->model->del(['id'=>$value]);
}
}
NoticeLog::createLog(NoticeLog::DELETE_CUSTOM_CATEGORY, ['project_id' => $this->user['project_id']]);
return $this->success();
}
... ...
... ... @@ -17,6 +17,7 @@ class NoticeLog extends Base
const DELETE_PRODUCT_CATEGORY = 'delete_product_category';
const DELETE_BLOG_CATEGORY = 'delete_blog_category';
const DELETE_NEWS_CATEGORY = 'delete_news_category';
const DELETE_CUSTOM_CATEGORY = 'delete_custom_category';
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
const STATUS_FAIL = 2;
... ...