<?php

namespace App\Traits;

use ArrayAccess;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Event;
use InvalidArgumentException;
use Spatie\EloquentSortable\EloquentModelSortedEvent;
use Spatie\EloquentSortable\Sortable;

/*
 * 列表排序is_pinned desc,order desc时用这个trait
 */
trait DistCommonTrait
{
    /*
     * 生成slug
     */
    public static function generateSlug($title) {
        $self = new self();
        $model = $self->model();
        $slug = generateSlug($title);
        for ($i=0;$i<5;$i++) {
            $count = $model->where('slug', $title)->where('dist_id', getDistributorId())->count();
            if ($count == 0) {
                return $slug;
            } else {
                $slug = $slug . '-' . generateRandomString();
            }
        }
        return $slug . '-' . generateRandomString(5);
    }
}
