Ticket5UploadDataController.php 7.4 KB
<?php
/**
 * @remark :
 * @name   :Ticket5UploadDataController.php
 * @author :lyh
 * @method :post
 * @time   :2025/11/8 09:35
 */

namespace App\Http\Controllers\Api\WorkOrder;

use App\Enums\Common\Code;
use App\Http\Controllers\Api\BaseController;
use App\Models\Ticket\Project5Cate;
use App\Models\Ticket\TicketUploadData;
use App\Models\WorkOrder\TicketProject;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class Ticket5UploadDataController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->model = new TicketUploadData();
    }
    /**
     * @remark :获取对应项目的分类
     * @name   :getProject5Cate
     * @author :lyh
     * @method :post
     * @time   :2025/11/8 09:27
     */
    public function getProject5Cate(){
        $this->request->validate([
            'post_id'=>'required',
            'type'=>'required',
        ],[
            'post_id.required' => 'post_id不能为空',
            'type.required' => '类型不能为空',
        ]);
        //查询是否有值
        $project5CateModel = new Project5Cate();
        $cateInfo = $project5CateModel->read(['project_id' => $this->param['post_id'],'type'=>$this->param['type'],'updated_at'=>['>', date('Y-m-d H:i:s', strtotime('-24 hours'))]]);
        if($cateInfo !== false){
            $resultData  = $cateInfo['text'];
            $this->response('success', Code::SUCCESS, $resultData);
        }
        //获取域名
        $ticketProjectMdoel = new TicketProject();
        $projectInfo = $ticketProjectMdoel->read(['post_id' => $this->param['post_id']]);
        if($projectInfo === false){
            $this->response('当前项目不存在或数据未同步',Code::SYSTEM_ERROR);
        }
        $action = $project5CateModel->getCateTypeAction($this->param['type']);
        if(empty($action)){
            $this->response('未知请求',Code::SYSTEM_ERROR);
        }
        $url = $projectInfo['website'].'wp-admin/admin-ajax.php?action='.$action;
        $data = http_get($url);
        if($data && $data['status'] == 200){
            $resultData  = $data['data'];
            $saveData = [
                'project_id' => $projectInfo['post_id'],
                'domain' => $projectInfo['website'],
                'text'=>json_encode($data['data']),
                'type' => $this->param['type'],
            ];
            $project5CateModel->addReturnId($saveData);
            $this->response('success', Code::SUCCESS,$resultData);
        }
        $this->response('success',Code::SUCCESS,$url);
    }

    /**
     * @remark :新增分类
     * @name   :addProject5Cate
     * @author :lyh
     * @method :post
     * @time   :2025/11/8 10:25
     */
    public function addProject5Cate()
    {
        $this->request->validate([
            'post_id'=>'required',
            'type'=>'required',
            'name'=>'required',
            'parent'=>'required',
        ],[
            'post_id.required' => 'post_id不能为空',
            'type.required' => '类型不能为空',
            'name.required' => '名称不能为空',
            'parent.required' => '上级ID不能为空',
        ]);
        //获取域名
        $ticketProjectMdoel = new TicketProject();
        $projectInfo = $ticketProjectMdoel->read(['post_id' => $this->param['post_id']]);
        if($projectInfo === false){
            $this->response('当前项目不存在或数据未同步',Code::SYSTEM_ERROR);
        }
        $project5CateModel = new Project5Cate();
        $action = $project5CateModel->saveCateTypeAction($this->param['type']);
        $url = $projectInfo['website'].'wp-admin/admin-ajax.php?action='.$action;
        if(empty($action)){
            $this->response('未知请求',Code::SYSTEM_ERROR);
        }
        $response = Http::withHeaders([])->asForm()->post($url, [
            'name' => $this->param['name'],
            'parent' => $this->param['parent'] ?? 0,
        ]);
        if ($response->successful()) {
            $result = $response->json(); // 获取 JSON 响应体
            if($result && $result['status'] == 200){
                //todo::更新分类数据
                $action1 = $project5CateModel->getCateTypeAction($this->param['type']);
                $url1 = $projectInfo['website'].'wp-admin/admin-ajax.php?action='.$action1;
                $data = http_get($url1);
                if($data && $data['status'] == 200){
                    $resultData  = $data['data'];
                    $saveData = [
                        'text'=>json_encode($data['data'],JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
                    ];
                    $project5CateModel->edit($saveData,['project_id' => $projectInfo['post_id'],'type'=>$this->param['type']]);
                    $this->response('success', Code::SUCCESS,$resultData);
                }
                $this->response('success');
            }
        }
        $this->response('请求失败',Code::SYSTEM_ERROR);
    }

    /**
     * @remark :提交数据
     * @name   :save
     * @author :lyh
     * @method :post
     * @time   :2025/11/10 11:01
     */
    public function save5Data()
    {
        $this->request->validate([
            'post_id' => 'required',
            'type' => 'required',
            'text' => 'required'
        ], [
            'post_id.required' => 'post_id不能为空',
            'type.required' => '类型不能为空',
            'text' => '数据详情不为空'
        ]);
        if(empty($this->param['text']['image'])){
            $this->response('参数错误',Code::SYSTEM_ERROR);
        }
        if(empty($this->param['text']['title'])){
            $this->response('参数错误,标题不能为空',Code::SYSTEM_ERROR);
        }
        //验证当前数据是否已提交
        $this->param['text'] = json_encode($this->param['text'], true);
//        $this->pushTicketByBots($this->param['post_id'],$this->param['project_type']);
        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['post_id'],'project_type'=>$this->model::TYPE_FIVE ,'type' => $this->param['type'], 'text' => $this->param['text'], 'status' => 0]);
            if ($info === false) {
                $this->param['project_type'] = $this->model::TYPE_FIVE ;
                $this->param['project_id'] = $this->param['post_id'];
                $id = $this->model->addReturnId($this->param);
            } else {
                $id = $info['id'];
            }
            $data = ['id' => $id];
            $this->response('success', Code::SUCCESS, $data);
        }
    }

    /**
     * @remark :钉钉通知
     * @name   :pushTicketByBots
     * @method :post
     * @time   :2025/11/10 11:10
     * @author :lyh
     */
    public function pushTicketByBots($post_id,$type = 1)
    {
        $project = TicketProject::where('post_id', $post_id)->where('project_cate',$type)->where('is_del', 0)->first();
        if ($project){
            return $project->pushWechatGroupMsg("您好,用户已上传数据,请审核!");
        }
        return true;
    }
}