BCustom.php 2.8 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * b端控制, c端显示的自定义页面
 * @author:dc
 * @time 2023/5/8 16:14
 * Class BCustom
 * @package App\Models
 */
class BCustom extends Base
{

    protected $table = 'gl_web_custom';

    use SoftDeletes;

    public $hidden = ['deleted_at','project_id'];


    /**
     * 显示
     */
    const STATUS_ACTIVE = 1;

    /**
     * 隐藏
     */
    const STATUS_DISABLED = 0;


    /**
     * 创建或者新增导航栏
     * @param int $project_id
     * @param array $data
     * @param int $id
     * @return int
     * @author:dc
     * @time 2023/5/8 16:24
     */
    public static function _save(int $project_id, array $data, int $id = 0):int {
        if($id){
            $model = static::where('id',$id)->where('project_id', $project_id)->first();
            if(!$model){
                return -1;
            }
        }else{
            $model = new static();
            $model->project_id = $project_id;

        }

        $model->name = $data['name'];
        $model->title = $data['title'];
        $model->keywords = $data['keywords'];
        $model->description = $data['description'];
        $model->url = $data['url'];
        $model->status = $data['status'];
        $model->html = $data['html'];

        $model->save();

        // 创建路由标识
        try {
            RouteMap::setRoute($model->url,RouteMap::SOURCE_CUSTOM,$model->id,$project_id);
        }catch (\Throwable $e){

        }


        return $model->id;
    }


    /**
     * 删除
     * @param int $project_id
     * @param int $id
     * @return mixed
     * @author:dc
     * @time 2023/5/8 16:27
     */
    public static function _del(int $project_id, int $id){
        return static::where(['project_id'=>$project_id,'id'=>$id])->delete();
    }


    /**
     * 查询当前项目下的所有信息
     * @param int $project_id
     * @return mixed
     * @author:dc
     * @time 2023/5/8 16:29
     */
    public static function _all(int $project_id, int $limit = 20)
    {
        return static::where(function ($query) use ($project_id){
            // 那个公司
            $query->where('project_id',$project_id);
        })
            ->select(['id','name','title','status','url','keywords','description','created_at','updated_at'])
            ->paginate($limit);
    }

    /**
     * 查询一条数据
     * @param int $project_id
     * @param int $id
     * @return mixed
     * @author:dc
     * @time 2023/5/8 17:04
     */
    public static function _find(int $project_id, int $id, $array = false)
    {
        $data =  static::where(['id'=>$id,'project_id'=>$project_id])->first();
        if($data){
            return $array ? $data->toArray() : $data;
        }
        return [];
    }


}