TicketUploadDataController.php 8.7 KB
<?php
/**
 * @remark :
 * @name   :TicketUploadDataController.php
 * @author :lyh
 * @method :post
 * @time   :2025/9/25 09:40
 */

namespace App\Http\Controllers\Api\WorkOrder;

use App\Enums\Common\Code;
use App\Http\Controllers\Api\BaseController;
use App\Models\Blog\BlogCategory;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\Ticket\TicketUploadData;
use App\Services\ProjectServer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * @remark :上传产品/博客/新闻模块
 * @name   :TicketUploadDataController
 * @author :lyh
 * @method :post
 * @time   :2025/9/25 09:40
 */
class TicketUploadDataController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new TicketUploadData();
    }

    /**
     * @remark :已提交列表
     * @name   :lists
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 10:28
     */
    public function lists()
    {
        $this->request->validate([
            'project_id' => 'required',
        ], [
            'project_id.required' => 'project_id不能为空',
        ]);
        $data = $this->model->lists($this->map, $this->page, $this->row, $this->order);
        ProjectServer::useProject($this->map['project_id']);
        if(!empty($data) && !empty($data['list'])){
            foreach ($data['list'] as &$item) {
                $item = $this->getHandleFileImage($item);
                $item['text']['cate_name'] = $this->cateText($item['type'],$item['text']['category_id'] ?? []);
            }
        }
        DB::disconnect('custom_mysql');
        $this->response('success', Code::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']);
                    $v['text']['image'][$gallery_k] = $gallery_v;
                }
            }
        }else{
            $v['text']['image'] = getImageUrl($v['text']['image']);
        }
        return $v;
    }
    /**
     * @remark :获取分类名称
     * @name   :cateText
     * @author :lyh
     * @method :post
     * @time   :2025/9/29 17:18
     */
    public function cateText($type,$category_id,$keyword_id = [],$is_array = false)
    {
        if(empty($category_id)){
            return '';
        }else{
            $filed = 'name';
            if($type == 1){
                $cateModel = new Category();
                $keywodModel = new KeyWord();
                $filed = 'title';
            }elseif ($type == 2){
                $cateModel = new BlogCategory();
            }else{
                $cateModel  = new NewsCategory();
            }
            if($is_array){
                $cate_arr = $cateModel->whereIn('id', (array)$category_id)
                    ->pluck($filed, 'id')
                    ->toArray();
                if($type == 1){
                    $keywod_arr = $keywodModel->whereIn('id', (array)$keyword_id)
                        ->pluck($filed, 'id')
                        ->toArray();
                    return ['keywod_arr' => $keywod_arr, 'cate_arr' => $cate_arr];
                }
                return ['cate_arr' => $cate_arr];
            }else{
                $cateArr = $cateModel->selectField(['id'=>['in',$category_id]],$filed);
                return implode(',',$cateArr);
            }
        }
    }


    /**
     * @remark :获取数据详情
     * @name   :info
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 10:35
     */
    public function info()
    {
        $this->request->validate([
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $info = $this->model->read($this->param);
        if($info === false){
            $this->response('当前数据不存在或已被删除',Code::SYSTEM_ERROR);
        }
        ProjectServer::useProject($info['project_id']);
        $info['text']['cate_name'] = $this->cateText($info['type'],$info['text']['category_id'] ?? [],$info['text']['keyword_id'] ?? [],true);
        $info = $this->getHandleFileImage($info);
        DB::disconnect('custom_mysql');
        $this->response('success', Code::SUCCESS, $info);
    }

    /**
     * @remark :提交数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 09:48
     */
    public function save()
    {
        $this->request->validate([
            'project_id' => 'required',
            'type' => 'required',
            'text' => 'required'
        ], [
            'project_id.required' => 'project_id不能为空',
            'type.required' => '上传类型不能为空',
            'text' => '数据详情不为空'
        ]);
        //验证当前数据是否已提交
        $this->param['text'] = json_encode($this->param['text'], true);
        if(isset($this->param['id']) && !empty($this->param['id'])){
            //执行编辑
            $info = $this->model->read(['id'=>$this->param['id']]);
            if($info['status'] == 0){
                $this->model->edit($this->param,['id'=>$this->param['id']]);
            }else{
                $this->response('当前状态不允许编辑', Code::SYSTEM_ERROR);
            }
            $this->response('success');
        }else{
            $info = $this->model->read(['project_id' => $this->param['project_id'], 'type' => $this->param['type'], 'text' => $this->param['text'], 'status' => 0]);
            if ($info === false) {
                $id = $this->model->addReturnId($this->param);
            } else {
                $id = $info['id'];
            }
            $data = ['id' => $id];
            $this->response('success', Code::SUCCESS, $data);
        }
    }

    /**
     * @remark :根据项目获取分类
     * @name   :getProductCate
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 15:41
     */
    public function getProductCate()
    {
        $this->request->validate([
            'project_id' => 'required',
            'type' => 'required',
            'search' => 'required'
        ], [
            'project_id.required' => 'project_id不能为空',
            'type.required' => 'type不能为空',
            'search.required' => '搜索参数不能为空',
        ]);
        ProjectServer::useProject($this->param['project_id']);
        if ($this->param['type'] == 1) {
            //todo::搜索获取分类
            $productCateModel = new Category();
            $data = $productCateModel->lists(['title' => ['like','%' . $this->param['search'] . '%']], 1, 20,'id',['id','title as name']);
        } else {
            $keywordModel = new Keyword();
            $data = $keywordModel->lists(['title' => ['like','%' . $this->param['search'] . '%']], 1, 20,'id',['id','title as name']);
        }
        DB::disconnect('custom_mysql');
        $this->response('success', Code::SUCCESS, $data);
    }

    /**
     * @remark :获取博客分类
     * @name   :getBlogCate
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 15:42
     */
    public function getBlogCate()
    {
        $this->request->validate([
            'project_id' => 'required',
            'search' => 'required'
        ], [
            'project_id.required' => 'project_id不能为空',
            'search.required' => '搜索参数不能为空',
        ]);
        ProjectServer::useProject($this->param['project_id']);
        $blogCateModel = new BlogCategory();
        $data = $blogCateModel->lists(['name' => ['like' ,'%' . $this->param['search'] . '%']], 1, 20,'id',['id','name']);
        DB::disconnect('custom_mysql');
        $this->response('success', Code::SUCCESS, $data);
    }

    /**
     * @remark :获取新闻分类
     * @name   :getNewsCate
     * @author :lyh
     * @method :post
     * @time   :2025/9/25 15:43
     */
    public function getNewsCate()
    {
        $this->request->validate([
            'project_id' => 'required',
            'search' => 'required'
        ], [
            'project_id.required' => 'project_id不能为空',
            'search.required' => '搜索参数不能为空',
        ]);
        ProjectServer::useProject($this->param['project_id']);
        $newsCateModel = new NewsCategory();
        $data = $newsCateModel->lists(['name' => ['like' , '%' . $this->param['search'] . '%']], 1, 20,'id',['id','name']);
        DB::disconnect('custom_mysql');
        $this->response('success', Code::SUCCESS, $data);
    }
}