TicketUploadDataLogic.php 9.1 KB
<?php
/**
 * @remark :
 * @name   :TicketUploadDataLogic.php
 * @author :lyh
 * @method :post
 * @time   :2025/9/25 14:03
 */

namespace App\Http\Logic\Aside\Ticket;

use App\Helper\Arr;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Ticket\TicketUploadData;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;

class TicketUploadDataLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new TicketUploadData();
    }

    /**
     * @remark :获取审核列表
     * @name   :getDataList
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 14:17
     */
    public function getDataList($map = [],$page = 1,$row = 20,$order = 'id'){
        if(isset($this->map['text']) && !empty($this->map['text'])){
            $this->map['text'] = ['like','%'.$this->map['text'].'%'];
        }
        ProjectServer::useProject($map['project_id']);
        $data = $this->model->lists($map,$page,$row,$order);
        if(!empty($data) && !empty($data['list'])){
            foreach ($data['list'] as &$v){
                $v = $this->getHandleFileImage($v);
                $v['text']['cate_name'] = $this->cateText($v['type'],$v['text']['category_id'] ?? []);
            }
        }
        DB::disconnect('custom_mysql');
        return $this->success($data);
    }

    /**
     * @remark :处理数据
     * @name   :getHandleFileImage
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 16:53
     */
    public function getHandleFileImage($v){
        if($v['type'] == 1){
            if(!empty($v['text']['image'])){
                foreach ($v['text']['image'] as $gallery_k => $gallery_v){
                    $gallery_v['url'] = getImageUrl($gallery_v['url']);
                    $info['text']['image'][$gallery_k] = $gallery_v;
                }
            }
        }else{
            $v['text']['image'] = getImageUrl($v['text']['image']);
        }
        return $this->success($v);
    }

    /**
     * @remark :获取分类名称
     * @name   :cateText
     * @author :lyh
     * @method :post
     * @time   :2025/9/29 17:18
     */
    public function cateText($type,$category_id,$is_array = false)
    {
        if(empty($category_id)){
            return '';
        }else{
            $filed = 'name';
            if($type == 1){
                $cateModel = new Category();
                $filed = 'title';
            }elseif ($type == 2){
                $cateModel = new BlogCategory();
            }else{
                $cateModel  = new NewsCategory();
            }
            if($is_array){
                return $cateModel::pluck($filed, 'id')->toArray();
            }else{
                $cateArr = $cateModel->selectField(['id'=>['in',$category_id]],$filed);
                return implode(',',$cateArr);
            }
        }
    }

    /**
     * @remark :获取当前数据详情
     * @name   :getDetail
     * @author :lyh
     * @method :post
     * @time   :2025/9/26 09:31
     */
    public function getDetail(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        $info = $this->getHandleFileImage($info);
        $info['text']['cate'] = $this->cateText($info['type'],$info['text']['category_id'] ?? []);
        return $this->success($info);
    }

    /**
     * @remark :保存数据
     * @name   :saveData
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 17:01
     */
    public function saveData()
    {
        //获取当前数据详情
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        //审核成功执行
        if($this->param['status'] == 1){
            ProjectServer::useProject($info['project_id']);
            if($info['type'] == 1){
                $this->saveProductData($info);
            }elseif ($info['type'] == 2){
                $this->saveBlogData($info);
            }else{
                $this->saveNewsData($info);
            }
            DB::disconnect('custom_mysql');
        }
        $data = $this->model->edit(['status'=>$this->param['status'],'operator_id'=>$this->manager['id'],'remark'=>$this->param['remark'] ?? ''],['id'=>$this->param['id']]);
        return $this->success($data);
    }

    /**
     * @remark :保存数据详情
     * @name   :saveData
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 14:17
     */
    public function saveProductData($info){
        if(isset($info['text']['keyword_id']) && !empty($info['text']['keyword_id'])){
            $keyword_id = ','.Arr::arrToSet($info['text']['keyword_id']).',';
        }
        if(isset($info['text']['category_id']) && !empty($info['text']['category_id'])) {
            $category_id = ','.Arr::arrToSet($info['text']['category_id']).',';
        }
        if(isset($info['text']['image']) && !empty($info['text']['image'])){
            foreach ($info['text']['image'] as $k => $v){
                $v['url'] = str_replace_url($v['url']);
                $info['text']['image'][$k] = $v;
            }
            $info['text']['image'] = Arr::a2s($info['text']['image'] ?? []);
        }else{
            $info['text']['image'] = Arr::a2s([]);
        }
        try {
            $productModel = new Product();
            $data = [
                'project_id' => $info['project_id'],
                'title' => $info['text']['title'],
                'thumb'=>json_encode($info['text']['image'][0] ?? [],true),
                'gallery'=>json_encode($info['text']['image'] ?? [],true),
                'intro'=>$info['text']['remark'],
                'category_id'=>$category_id ?? '',
                'keyword_id'=>$keyword_id ?? '',
                'status'=>0,
            ];
            $id = $productModel->addReturnId($data);
            CategoryRelated::saveRelated($id, $info['text']['category_id'] ?? []);//分类关联
            KeywordRelated::saveRelated($id,$info['text']['keyword_id'] ?? []);//关键字关联
            $route = RouteMap::setRoute($data['title'],RouteMap::SOURCE_PRODUCT,$id,$info['project_id']);
            $this->model->edit(['route'=>$route],['id'=>$id]);
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :保存blog数据
     * @name   :saveBlogData
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 16:26
     */
    public function saveBlogData($info)
    {
        if(isset($info['text']['category_id']) && !empty($info['text']['category_id'])) {
            $category_id = ','.Arr::arrToSet($info['text']['category_id']).',';
        }
        if(isset($info['text']['image'])){
            $info['text']['image'] = str_replace_url($info['text']['image'] ?? '');
        }
        $data = [
            'project_id' => $info['project_id'],
            'name' => $info['text']['title'],
            'image'=>$info['text']['image'],
            'text'=>$info['text']['remark'],
            'category_id'=>$category_id ?? '',
            'status'=>0,
        ];
        try {
            $blogModel = new Blog();
            $id = $blogModel->addReturnId($data);
            $route = RouteMap::setRoute($data['name'],RouteMap::SOURCE_BLOG,$id,$info['project_id']);
            $this->model->edit(['url'=>$route],['id'=>$id]);
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :保存新闻数据
     * @name   :saveNewsData
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 16:34
     */
    public function saveNewsData($info)
    {
        if(isset($info['text']['category_id']) && !empty($info['text']['category_id'])) {
            $category_id = ','.Arr::arrToSet($info['text']['category_id']).',';
        }
        if(isset($info['text']['image'])){
            $info['text']['image'] = str_replace_url($info['text']['image'] ?? '');
        }
        $data = [
            'project_id' => $info['project_id'],
            'name' => $info['text']['title'],
            'image'=>$info['text']['image'],
            'text'=>$info['text']['remark'],
            'category_id'=>$category_id ?? '',
            'status'=>0,
        ];
        try {
            $newsModel = new News();
            $id = $newsModel->addReturnId($data);
            $route = RouteMap::setRoute($data['name'],RouteMap::SOURCE_NEWS,$id,$info['project_id']);
            $this->model->edit(['route'=>$route],['id'=>$id]);
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }
}