ServersIpLogic.php 2.9 KB
<?php
/**
 * @remark :
 * @name   :ServersIpLogic.php
 * @author :lyh
 * @method :post
 * @time   :2024/6/24 16:21
 */

namespace App\Http\Logic\Aside\Devops;

use App\Http\Logic\Aside\BaseLogic;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Project\Project;

class ServersIpLogic extends BaseLogic
{
    /**
     * 数据初始化
     */
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new ServersIp();
    }

    /**
     * @remark :获取数据详情
     * @name   :infoServersIp
     * @author :lyh
     * @method :post
     * @time   :2024/6/27 15:52
     */
    public function infoServersIp(){
        $info = $this->model->read(['id'=>$this->param['id']]);
        $projectModel = new Project();
        $info['project_title'] = $projectModel->formatQuery(['serve_id'=>$info['id']])->pluck('title')->toArray();
        return $this->success($info);
    }

    /**
     * @remark :保存数据
     * @name   :saveServersIp
     * @author :lyh
     * @method :post
     * @time   :2024/6/24 17:28
     */
    public function saveServersIp(){
        //验证域名是否唯一
        $info = $this->model->read(['domain'=>$this->param['domain']]);
        if($info !== false){
            $this->fail('当前初始域名已存在');
        }
        if(isset($this->param['id']) && !empty($this->param['id'])){
            $id = $this->param['id'];
            $this->model->edit($this->param,['id'=>$this->param['id']]);
        }else{
            $id = $this->model->addReturnId($this->param);
        }
        return $this->success(['id'=>$id]);
    }

    /**
     * @remark :批量添加
     * @name   :batchSaveServersIp
     * @author :lyh
     * @method :post
     * @time   :2024/6/24 17:25
     */
    public function batchSaveServersIp(){
        //獲取初始域名
        $serverModel = new Servers();
        $info = $serverModel->read(['id'=>$this->param['servers_id']]);
        if($info === false){
            $this->fail('當前服務器數據不存在');
        }
        foreach ($this->param['ip'] as $v){
            if(empty($v)){
                continue;
            }
            $param = [
                'ip'=>$v,
                'domain'=>ip_to_unique_string($v).'.'.$info['domain'],
                'servers_id'=>$this->param['servers_id']
            ];
            $this->model->addReturnId($param);
        }
        return $this->success();
    }

    /**
     * @remark :批量删除数据
     * @name   :batchDelServersIp
     * @author :lyh
     * @method :post
     * @time   :2024/6/27 13:55
     */
    public function batchDelServersIp(){
        foreach ($this->param['ids'] as $id){
            $info = $this->model->read(['id'=>$id],['total']);
            if($info['total'] != 0){
                continue;
            }
            $this->model->edit(['status'=>1],['id'=>$id]);
        }
        return $this->success();
    }
}