作者 lyh

gx脚本导出产品

... ... @@ -9,8 +9,18 @@
namespace App\Console\Commands\Project;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\Detail;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplate;
use App\Models\Template\Setting;
use App\Models\User\User;
use App\Models\WebSetting\WebSettingSeo;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
... ... @@ -25,7 +35,7 @@ class DownloadProject extends Command
*
* @var string
*/
protected $signature = 'downloads_project';
protected $signature = 'downloads_products';
/**
* The console command description.
... ... @@ -34,47 +44,94 @@ class DownloadProject extends Command
*/
protected $description = '导出项目数据';
// public function handle(){
// $projectModel = new Project();
// $data = $projectModel->formatQuery(['channel'=>['like','%"channel_id": "57"%'],'delete_status'=>0])->with(['deploy_optimize'])->get()->toArray();
// if(!empty($data)){
// $result = $this->exportData($data);
// }
// echo date('Y-m-d H:i:s') . ' ' . json_encode($result) . PHP_EOL;
// return $result;
//
// }
public function handle(){
$data = [];
$projectModel = new Project();
$projectList = $projectModel->formatQuery(['delete_status'=>0,'type'=>['in',[2,3]]])->with(['deploy_optimize'])->select(['id','status','type','title','remain_day'])->get()->toArray();;
foreach ($projectList as $v){
ProjectServer::useProject($v['id']);
$seoModel = new WebSettingSeo();
$seoInfo = $seoModel->read(['project_id'=>$v['id']]);
if($seoInfo === false){
$data[] = $v;
ProjectServer::useProject(1225);
$data = $this->downloadProduct();
DB::disconnect('custom_mysql');
echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
return true;
// return $this->exportData($data);
}
public function downloadProduct()
{
$product = new Product();
$filed = ['id', 'project_id', 'title' ,'thumb' , 'route' ,'intro','content',
'category_id', 'status'];
$this->order = 'sort';
$lists = $product->list(['status'=>1],'id',$filed);
if(!empty($lists)){
$cate_data = $this->getCategoryList();//分类
$detailModel = new Detail();
foreach ($lists as $k => $v){
$detailInfo = $detailModel->read(['product_id'=>$v['id'],'column_id'=>1,'text_type'=>1],['content']);
if($detailInfo === false || ($detailInfo['content']['content'] == null)){
$v['content'] = '';
}else{
if(empty($seoInfo['single_page_suffix'])){
$data[] = $v;
$v['content'] = $detailInfo['content']['content'];
}
$v['url'] = 'https://www.autsikinta.com/' . getRouteMap(RouteMap::SOURCE_PRODUCT,$v['id']);
$v['category_id_text'] = $this->categoryName($v['id'],$cate_data);
//ToDo::处理图片及文件
if(!empty($v['thumb']) && !empty($v['thumb']['url'])){
$v['images'] = getImageUrl($v['thumb']['url'],$this->user['storage_type'] ?? 0,$this->user['project_location']);
}else{
$v['images'] = '';
}
DB::disconnect('custom_mysql');
echo date('Y-m-d H:i:s') . '数据详情$v:'. $v . PHP_EOL;
$lists[$k] = $v;
}
return $this->exportData($data);
}
return $lists;
}
public function categoryName($product_id,$data){
$cateRelatedModel = new CategoryRelated();
$category_id = $cateRelatedModel->where('product_id',$product_id)->pluck('cate_id')->toArray();
$category_name = '';
if(!empty($category_id) && !empty($data)){
foreach ($category_id as $v){
if(isset($data[$v])){
$category_name .= $data[$v].',';
}
}
$category_name = trim($category_name,',');
}
return $category_name;
}
/**
* @remark :获取所有分类
* @name :getCategoryList
* @author :lyh
* @method :post
* @time :2023/9/14 13:56
*/
public function getCategoryList(){
$data = Common::get_user_cache('product_category',$this->user['project_id']);
if(empty($data)){
$categoryModel = new Category();
$data = [];
$cateList = $categoryModel->list(['project_id'=>$this->user['project_id']],['id','title']);
if(!empty($cateList)){
foreach ($cateList as $value){
$data[$value['id']] = $value['title'];
}
}
Common::set_user_cache($data,'product_category',$this->user['project_id']);
}
return $data;
}
public function exportData($data){
// 创建一个新的 Excel 电子表格实例
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 添加表头
$sheet->setCellValue('A1', '项目ID');
$sheet->setCellValue('B1', '项目名称');
$sheet->setCellValue('C1', '域名');
$sheet->setCellValue('D1', '状态');
$sheet->setCellValue('E1', '剩余服务时间');
$sheet->setCellValue('A1', '产品名称');
$sheet->setCellValue('B1', '产品描述');
$sheet->setCellValue('C1', '产品内容');
$sheet->setCellValue('D1', '产品路由');
$sheet->setCellValue('E1', '产品分类');
$sheet->setCellValue('F1', '产品状态');
$sheet->setCellValue('G1', '产品主图');
$rowCount = 2;
// $allData = $this->countAll();
foreach ($data as $v) {
... ...