admin 员工状态

main
panliang 2024-03-22 17:02:49 +08:00
parent 7df41cc799
commit a29abff979
8 changed files with 101 additions and 76 deletions

View File

@ -1,65 +0,0 @@
<?php
namespace App\Admin\Controllers;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use App\Services\Admin\EmployeeService;
use Slowlyo\OwlAdmin\Renderers\Page;
use Slowlyo\OwlAdmin\Renderers\Form;
use App\Enums\EmployeeStatus;
class EmployeeController extends AdminController
{
protected string $serviceName = EmployeeService::class;
public function list(): Page
{
$crud = $this->baseCRUD()
->tableLayout('fixed')
->headerToolbar([
$this->createButton(true),
...$this->baseHeaderToolBar(),
])
->filter($this->baseFilter()->body([
amis()->GroupControl()->mode('horizontal')->body([
amisMake()->TextControl()->name('name')->label(__('employee.name'))->columnRatio(3)->clearable(),
amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->columnRatio(3)->clearable(),
amisMake()->SelectControl()->name('employee_status')->label(__('employee.employee_status'))->columnRatio(3)->clearable()->options(EmployeeStatus::options()),
]),
]))
->columns([
amisMake()->TableColumn()->name('id')->label(__('employee.id')),
amisMake()->TableColumn()->name('name')->label(__('employee.name')),
amisMake()->TableColumn()->name('phone')->label(__('employee.phone')),
amisMake()->TableColumn()->name('employee_status_text')->label(__('employee.employee_status')),
amisMake()->TableColumn()->name('created_at')->label(__('employee.created_at')),
$this->rowActions([
$this->rowShowButton(),
$this->rowEditButton(true),
$this->rowDeleteButton(),
]),
]);
return $this->baseList($crud);
}
public function form($edit): Form
{
return $this->baseForm()->title('')->body([
amisMake()->TextControl()->name('name')->label(__('employee.name'))->required(),
amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->required(),
amisMake()->TextControl()->name('username')->label(__('admin.username'))->value('${admin_user.username}')->required(!$edit),
amisMake()->TextControl()->name('password')->set('type', 'input-password')->label(__('admin.password'))->required(!$edit),
amisMake()->TextControl()->name('confirm_password')->set('type', 'input-password')->label(__('admin.confirm_password'))->required(!$edit),
]);
}
public function detail(): Form
{
return $this->baseDetail()->title('')->body(amisMake()->Property()->items([
['label' => __('employee.name'), 'content' => '${name}'],
['label' => __('employee.phone'), 'content' => '${phone}'],
]));
}
}

View File

@ -7,6 +7,7 @@ use App\Services\Admin\EmployeeService;
use Slowlyo\OwlAdmin\Renderers\Page;
use Slowlyo\OwlAdmin\Renderers\Form;
use App\Enums\EmployeeStatus;
use App\Models\Employee;
class EmployeeController extends AdminController
{
@ -31,7 +32,10 @@ class EmployeeController extends AdminController
amisMake()->TableColumn()->name('id')->label(__('employee.id')),
amisMake()->TableColumn()->name('name')->label(__('employee.name')),
amisMake()->TableColumn()->name('phone')->label(__('employee.phone')),
amisMake()->TableColumn()->name('employee_status_text')->label(__('employee.employee_status')),
// amisMake()->TableColumn()->name('employee_status_text')->label(__('employee.employee_status')),
amisMake()->TableColumn()->name('employee_status_text')->label(__('employee.employee_status'))->set('type', 'tag')->set('color', '${employee_status_color}'),
amisMake()->TableColumn()->name('created_at')->label(__('employee.created_at')),
$this->rowActions([
$this->rowShowButton(),
@ -49,6 +53,13 @@ class EmployeeController extends AdminController
amisMake()->TextControl()->name('name')->label(__('employee.name'))->required(),
amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->required(),
amisMake()->TagControl()->name('jobs')->label(__('employee.jobs'))
->source(admin_url('api/keywords/tree-list') . '?parent_key=' . Employee::JOB_KEY)
->labelField('name')
->valueField('key')
->joinValues(),
amisMake()->SelectControl()->name('employee_status')->label(__('employee.employee_status'))->options(EmployeeStatus::options()),
amisMake()->TextControl()->name('username')->label(__('admin.username'))->value('${admin_user.username}')->required(!$edit),
amisMake()->TextControl()->name('password')->set('type', 'input-password')->label(__('admin.password'))->required(!$edit),
amisMake()->TextControl()->name('confirm_password')->set('type', 'input-password')->label(__('admin.confirm_password'))->required(!$edit),
@ -60,6 +71,10 @@ class EmployeeController extends AdminController
return $this->baseDetail()->title('')->body(amisMake()->Property()->items([
['label' => __('employee.name'), 'content' => '${name}'],
['label' => __('employee.phone'), 'content' => '${phone}'],
['label' => __('employee.jobs'), 'content' => amisMake()->TagControl()->size('full')->name('jobs')->labelField('name')->valueField('key')->joinValues()->static()],
['label' => __('admin.username'), 'content' => '${admin_user.username}'],
['label' => __('employee.employee_status'), 'content' => amisMake()->Tag()->label('${employee_status_text}')->color('${employee_status_color}')],
]));
}
}

View File

@ -15,10 +15,18 @@ enum EmployeeStatus: int
];
}
public static function coplorMap()
{
return [
self::Online->value => 'success',
self::Offline->value => 'warning',
];
}
public static function options()
{
$list = [];
foreach (static::map() as $key => $value) {
foreach (self::map() as $key => $value) {
array_push($list, ['label' => $value, 'value' => $key]);
}
return $list;
@ -28,4 +36,9 @@ enum EmployeeStatus: int
{
return data_get(self::map(), $this->value);
}
public function color()
{
return data_get(self::coplorMap(), $this->value);
}
}

View File

@ -30,12 +30,13 @@ class Employee extends Model
'employee_status' => EmployeeStatus::Online,
];
protected $appends = ['employee_status_text'];
protected $appends = ['employee_status_text', 'employee_status_color'];
// 拥有的职位
public function jobs()
{
return $this->belongsToMany(Keyword::class, 'emplyee_jobs', 'employee_id', 'job');
// $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null
return $this->belongsToMany(Keyword::class, 'employee_jobs', 'employee_id', 'job', null, 'key');
}
// 关联登录账户
@ -50,4 +51,11 @@ class Employee extends Model
get: fn () => $this->employee_status ? $this->employee_status->text() : '',
);
}
protected function employeeStatusColor(): Attribute
{
return new Attribute(
get: fn () => $this->employee_status ? $this->employee_status->color() : '',
);
}
}

View File

@ -28,4 +28,9 @@ class KeywordFilter extends ModelFilter
Keyword::where('name','like', '%'.$parent_name.'%')->orWhere('key','like', '%'.$parent_name.'%')->value('id')
. '-%' ?? '');
}
public function parentKey($key)
{
return $this->where('parent_key', $key);
}
}

View File

@ -19,6 +19,42 @@ class EmployeeService extends BaseService
protected string $modelFilterName = EmployeeFilter::class;
public function store($data): bool
{
$data = $this->resloveData($data);
$validate = $this->validate($data);
if ($validate !== true) {
$this->setError($validate);
return false;
}
$model = $this->modelName::create($data);
if (isset($data['jobs']) && is_string($data['jobs'])) {
$this->resloveJob($model, explode(',', $data['jobs']));
}
return true;
}
public function update($primaryKey, $data): bool
{
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
$data = $this->resloveData($data, $model);
$validate = $this->validate($data, $model);
if ($validate !== true) {
$this->setError($validate);
return false;
}
if (isset($data['jobs']) && is_string($data['jobs'])) {
$this->resloveJob($model, explode(',', $data['jobs']));
}
return $model->update($data);
}
public function getEditData($id): Model|\Illuminate\Database\Eloquent\Collection|Builder|array|null
{
$model = $this->getModel();
@ -27,7 +63,7 @@ class EmployeeService extends BaseService
->filter(fn($item) => $item !== null)
->toArray();
return $this->query()->with(['adminUser'])->find($id)->makeHidden($hidden);
return $this->query()->with(['adminUser', 'jobs'])->find($id)->makeHidden($hidden);
}
public function resloveData($data, $model = null)
@ -54,15 +90,23 @@ class EmployeeService extends BaseService
return $data;
}
/**
* 处理职位
*
* @param Employee $model
* @param array $jobs(字典表 key 组成的数组)
*/
public function resloveJob($model, $jobs)
{
$model->jobs()->sync($jobs);
}
public function preDelete(array $ids)
{
// 删除管理员
$adminUserIds = Employee::whereIn('id', $ids)->pluck('admin_user_id')->implode(',');
$adminUserService = AdminUserService::make();
if (!$adminUserService->delete($adminUserIds)) {
$this->setError($adminUserService->getError());
return false;
}
$adminUserService->delete($adminUserIds);
return true;
}

View File

@ -25,7 +25,7 @@ return new class extends Migration
$table->comment('员工');
});
Schema::create('emplyee_jobs', function (Blueprint $table) {
Schema::create('employee_jobs', function (Blueprint $table) {
$table->foreignId('employee_id');
$table->string('job')->comment('职位, 字典表: keywords.job');
});
@ -37,6 +37,6 @@ return new class extends Migration
public function down(): void
{
Schema::dropIfExists('employees');
Schema::dropIfExists('emplyee_jobs');
Schema::dropIfExists('employee_jobs');
}
};

View File

@ -31,6 +31,11 @@ class KeywordSeeder extends Seeder
'name' => '广告位置',
'children' => [],
],
[
'key' => 'job',
'name' => '职位',
'children' => ['普通员工', '小组长', '主管']
]
];
$this->insertKeywors($keywords);