105 lines
2.7 KiB
PHP
Executable File
105 lines
2.7 KiB
PHP
Executable File
<?php
|
|
namespace App\Models\Auth;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Dcat\Admin\Models\Administrator;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use App\Models\EA\SystemModel;
|
|
use App\Models\SaleCreditModel;
|
|
use App\Models\EA\BusinessModel;
|
|
|
|
class AdminModel extends Administrator
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = ['username', 'password', 'name', 'avatar', 'employee_id'];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
static::deleting(function ($model) {
|
|
$model->aRoles()->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* A user has and belongs to many roles.
|
|
*
|
|
* @return BelongsToMany
|
|
*/
|
|
public function roles() :BelongsToMany
|
|
{
|
|
$pivotTable = config('admin.database.role_users_table');
|
|
|
|
$relatedModel = config('admin.database.roles_model');
|
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id')->withTimestamps()
|
|
->withoutTrashed()->whereRaw($pivotTable.'.deleted_at IS NULL');
|
|
}
|
|
|
|
public function system() {
|
|
return $this->belongsTo(SystemModel::class, 'system_id', 'system_id');
|
|
}
|
|
|
|
public function business() {
|
|
return $this->belongsTo(BusinessModel::class, 'business_id', 'business_id');
|
|
}
|
|
|
|
public function aRoles() {
|
|
return $this->hasMany(RoleUsersModel::class, 'user_id');
|
|
}
|
|
|
|
public function credits() {
|
|
return $this->hasMany(SaleCreditModel::class, 'employee_id', 'employee_id');
|
|
}
|
|
|
|
/**
|
|
* 获取销售人员
|
|
* @param unknown $q
|
|
*/
|
|
public function scopeSale($q) {
|
|
$q->whereHas('roles', function($q) {
|
|
$q->where('slug', 'saleinfo');//销售角色
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 数据options 用于页面出select
|
|
*
|
|
* @param unknown $select
|
|
* @param unknown $where
|
|
* @param unknown $order
|
|
*/
|
|
static public function dataOptions($select, $where = null, $order = [])
|
|
{
|
|
$query = self::select($select);
|
|
|
|
if ($where) {
|
|
$query = $query->where($where);
|
|
}
|
|
|
|
if ($order) {
|
|
if (count($order) == count($order, 1)) {
|
|
list ($attr, $desc) = $order;
|
|
$query = $query->orderBy($attr, $desc);
|
|
} else {
|
|
foreach ($order as $one) {
|
|
list ($attr, $desc) = $one;
|
|
$query = $query->orderBy($attr, $desc);
|
|
}
|
|
}
|
|
}
|
|
list ($key, $name) = is_array($select) ? $select : explode(',', $select);
|
|
|
|
$data = $query->groupBy($key)->get();
|
|
|
|
$options = [];
|
|
foreach ($data as $one) {
|
|
$options[$one->$key] = $one->$name;
|
|
}
|
|
|
|
return collect($options)->all();
|
|
}
|
|
}
|
|
|