admin 合同管理

main
panliang 2024-04-07 16:03:55 +08:00
parent 7d45208cef
commit d62f0c8055
8 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,98 @@
<?php
namespace App\Admin\Controllers;
use App\Admin\Services\AgreementService;
use Slowlyo\OwlAdmin\Renderers\Form;
use Slowlyo\OwlAdmin\Renderers\Page;
use Slowlyo\OwlAdmin\Admin;
use App\Enums\EmployeeStatus;
use App\Models\Agreement;
use Illuminate\Support\{Arr};
use Illuminate\Support\Facades\{Storage};
use Illuminate\Http\Request;
/**
* 合同管理
*/
class AgreementController extends AdminController
{
protected string $serviceName = AgreementService::class;
public function list(): Page
{
$user = Admin::user();
$crud = $this->baseCRUD()
->tableLayout('fixed')
->headerToolbar([
$this->createTypeButton('drawer', 'xl')->visible($user->can('admin.agreement.create')),
...$this->baseHeaderToolBar(),
])
->bulkActions([])
->columns([
amisMake()->TableColumn()->name('id')->label(__('agreement.id')),
amisMake()->TableColumn()->name('name')->label(__('agreement.name')),
amisMake()->TableColumn()->name('employee.name')->label(__('agreement.employee_id')),
amisMake()->TableColumn()->name('created_at')->label(__('agreement.created_at')),
amisMake()->Operation()->label(__('admin.actions'))->buttons([
$this->rowShowTypeButton('drawer', 'xl')->visible($user->can('admin.agreement.view')),
$this->rowEditTypeButton('drawer', 'xl')->visible($user->can('admin.agreement.update')),
$this->rowDeleteButton()->visible($user->can('admin.agreement.delete')),
amisMake()->AjaxAction()
->level('link')
->label('打包下载')
->api('post:'.admin_url('agreement/download?id=${id}'))
->visible($user->can('admin.agreement.download')),
]),
]);
return $this->baseList($crud);
}
public function form(): Form
{
return $this->baseForm()->body([
amisMake()->TextControl()->name('name')->label(__('agreement.name'))->required(),
amisMake()->SelectControl()->name('employee_id')->label(__('agreement.employee_id'))
->source(admin_url('api/employees?_all=1&store_id_gt=0&employee_status='.EmployeeStatus::Online->value))
->labelField('name')
->valueField('id')
->searchable()
->required(),
amisMake()->ImageControl()->name('images')->label(__('agreement.images'))
->multiple()
->draggable()
->joinValues(false),
amisMake()->TextControl()->name('remarks')->label(__('agreement.remarks')),
]);
}
public function detail(): Form
{
return $this->baseDetail()->title('')->body(amisMake()->Property()->items([
['label' => __('agreement.name'), 'content' => '${name}'],
['label' => __('agreement.employee_id'), 'content' => '${employee.name}'],
['label' => __('agreement.created_at'), 'content' => '${created_at}'],
['label' => __('agreement.remarks'), 'content' => '${remarks}', 'span' => 3],
['label' => __('agreement.images'), 'content' => amisMake()->Images()->name('images')->enlargeAble(), 'span' => 3],
]));
}
public function download(Request $request)
{
$id = $request->input('id');
$images = Agreement::whereIn('id', is_array($id) ? $id : explode(',', $id))->pluck('images');
$list = Arr::flatten($images);
$baseUrl = Storage::url('');
$zip = new \ZipArchive();
$filename = time().'.zip';
$zip->open($filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
foreach($list as $item) {
$path = str_replace($baseUrl, '', $item);
$info = pathinfo($path);
$zip->addFile(Storage::path($path), data_get($info, 'basename'));
}
$zip->close();
return response()->download($filename)->deleteFileAfterSend();
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Admin\Filters;
use Carbon\Carbon;
use EloquentFilter\ModelFilter;
class AgreementFilter extends ModelFilter
{
protected $drop_id = false;
public $relations = [
'employee' => [
'employee_name' => 'name',
'employee_search' => 'search',
],
'invitor' => [
'invitor_name' => 'name',
'invitor_search' => 'search',
],
];
public function employeeId($key)
{
$this->where('employee_id', $key);
}
public function dateRange($dates)
{
$dates = explode(',', $dates);
$start = Carbon::createFromTimestamp(data_get($dates, 0, time()))->startOfDay();
$end = Carbon::createFromTimestamp(data_get($dates, 1, time()))->endOfDay();
$this->whereBetween('created_at', [$start, $end]);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace App\Admin\Services;
use App\Admin\Filters\AgreementFilter;
use App\Models\Agreement;
use Illuminate\Support\Facades\{Validator, Storage};
use Illuminate\Support\Str;
class AgreementService extends BaseService
{
protected array $withRelationships = ['employee'];
protected string $modelName = Agreement::class;
protected string $modelFilterName = AgreementFilter::class;
public function resloveData($data, $model = null)
{
if (isset($data['images'])) {
$images = [];
foreach ($data['images'] as $value) {
$image = is_array($value) ? data_get($value, 'value') : $value;
$url = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
array_push($images, $url);
}
$data['images'] = $images;
}
return $data;
}
public function validate($data, $model = null)
{
$createRules = [
'employee_id' => ['required'],
'name' => ['required'],
];
$updateRules = [];
$validator = Validator::make($data, $model ? $updateRules : $createRules);
if ($validator->fails()) {
return $validator->errors()->first();
}
return true;
}
}

View File

@ -27,6 +27,7 @@ use App\Admin\Controllers\System\AdminRoleController;
use App\Admin\Controllers\System\AdminUserController;
use App\Admin\Controllers\System\KeywordController;
use App\Admin\Controllers\System\WorkflowController;
use App\Admin\Controllers\AgreementController;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
@ -180,6 +181,9 @@ Route::group([
$router->resource('ads', \App\Admin\Controllers\AdController::class);
$router->post('agreement/download', [AgreementController::class, 'download'])->name('agreement.download');
$router->resource('agreement', AgreementController::class);
//修改上传
$router->post('upload_file', [\App\Admin\Controllers\IndexController::class, 'uploadFile']);
$router->post('upload_image', [\App\Admin\Controllers\IndexController::class, 'uploadImage']);

View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
/**
* 合同
*/
class Agreement extends Model
{
use HasDateTimeFormatter, Filterable;
protected $fillable = ['name', 'employee_id', 'images', 'remarks'];
protected $casts = [
'images' => 'json',
];
public function modelFilter()
{
return \App\Admin\Filters\AgreementFilter::class;
}
public function employee()
{
return $this->belongsTo(Employee::class, 'employee_id');
}
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('agreements', function (Blueprint $table) {
$table->id();
$table->string('name')->comment('名称');
$table->foreignId('employee_id')->comment('上传人');
$table->json('images')->nullable()->comment('图片列表');
$table->string('remarks')->nullable()->comment('备注');
$table->timestamps();
$table->comment('合同');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('agreements');
}
};

View File

@ -250,6 +250,16 @@ class AdminPermissionSeeder extends Seeder
],
],
'agreement' => [
'name' => '合同管理',
'icon' => '',
'uri' => '/agreement',
'resource' => true,
'children' => [
'download' => '打包下载',
]
],
/*
|--------------------------------------------------------------------------
| 系统管理

View File

@ -0,0 +1,12 @@
<?php
return [
'id' => 'ID',
'created_at' => '创建时间',
'updated_at' => '更新时间',
'name' => '名称',
'employee_id' => '上传人',
'images' => '合同图片',
'remarks' => '备注',
];