api 合同管理

main
panliang 2024-04-16 12:51:19 +08:00
parent 1517612709
commit 8a695cd2c9
8 changed files with 194 additions and 27 deletions

View File

@ -20,12 +20,19 @@ class AgreementService extends BaseService
if (isset($data['images']) && $data['images']) { if (isset($data['images']) && $data['images']) {
$images = []; $images = [];
foreach ($data['images'] as $value) { foreach ($data['images'] as $value) {
$image = data_get($value, 'value'); if (is_array($value)) {
$url = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image); $image = data_get($value, 'value');
array_push($images, [ $url = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
'value' => $image, array_push($images, [
'preview' => $url, 'value' => $image,
]); 'preview' => $url,
]);
} else {
array_push($images, [
'value' => $value,
'preview' => $value,
]);
}
} }
$data['images'] = $images; $data['images'] = $images;
} }
@ -47,14 +54,34 @@ class AgreementService extends BaseService
return true; return true;
} }
public function delete(string $ids): mixed public function update($primaryKey, $data): bool
{ {
$id = explode(',', $ids); $model = $this->query()->whereKey($primaryKey)->firstOrFail();
if (!$model->canUpdate()) {
return $this->setError('审核中, 无法修改');
}
$data = $this->resloveData($data, $model);
$validate = $this->validate($data, $model);
if ($validate !== true) {
$this->setError($validate);
foreach (Agreement::whereIn('id', $id)->get() as $item) { return false;
$item->delete();
} }
$model->update($data);
$this->currentModel = $model;
return true;
}
public function delete(string $ids): mixed
{
$list = $this->query()->with(['workflow'])->whereIn('id', explode(',', $ids))->get();
foreach ($list as $item) {
if (!$item->canUpdate()) {
return $this->setError('审核中, 无法删除');
}
$item->delete();
}
return true; return true;
} }
} }

View File

@ -3,6 +3,7 @@
namespace App\Admin\Services; namespace App\Admin\Services;
use Slowlyo\OwlAdmin\Services\AdminService; use Slowlyo\OwlAdmin\Services\AdminService;
use Illuminate\Database\Eloquent\Model;
/** /**
* @method Region getModel() * @method Region getModel()
@ -14,6 +15,13 @@ class BaseService extends AdminService
protected bool $modelSortAble = false; protected bool $modelSortAble = false;
protected Model $currentModel;
public function getCurrentModel()
{
return $this->currentModel;
}
public function sortColumn() public function sortColumn()
{ {
return 'id'; return 'id';
@ -78,7 +86,8 @@ class BaseService extends AdminService
return false; return false;
} }
$this->modelName::create($data); $model = $this->modelName::create($data);
$this->currentModel = $model;
return true; return true;
} }
@ -93,8 +102,9 @@ class BaseService extends AdminService
return false; return false;
} }
$model->update($data);
return $model->update($data); $this->currentModel = $model;
return true;
} }
public function delete(string $ids): mixed public function delete(string $ids): mixed

View File

@ -0,0 +1,102 @@
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Models\Agreement;
use App\Http\Resources\AgreementResource;
use App\Admin\Services\{AgreementService, WorkFlowService};
use App\Exceptions\RuntimeException;
use Illuminate\Support\Facades\DB;
/**
* 合同管理
*/
class AgreementController extends Controller
{
public function index(Request $request)
{
$user = $this->guard()->user();
$list = Agreement::with(['workflow'])
->where('employee_id', $user->id)
->filter($request->all())
->orderBy('id', 'desc')
->paginate($request->input('per_page'));
return AgreementResource::collection($list);
}
public function show($id)
{
$info = Agreement::with(['employee', 'workflow'])->findOrFail($id);
return AgreementResource::make($info);
}
public function store(Request $request, AgreementService $service)
{
$user = $this->guard()->user();
$data = $request->all();
$data['employee_id'] = $user->id;
try {
DB::beginTransaction();
if (!$service->store($data)) {
throw new RuntimeException($result);
}
$model = $service->getCurrentModel();
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function update($id, Request $request, AgreementService $service)
{
$user = $this->guard()->user();
$model = Agreement::where('employee_id', $user->id)->findOrFail($id);
try {
DB::beginTransaction();
if (!$service->update($id, $request->all())) {
throw new RuntimeException($service->getError());
}
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
public function destroy($id, AgreementService $service)
{
$user = $this->guard()->user();
$model = Agreement::where('employee_id', $user->id)->findOrFail($id);
try {
DB::beginTransaction();
if (!$service->delete($id)) {
throw new RuntimeException($service->getError());
}
DB::commit();
return response()->noContent();
} catch (\Exception $e) {
DB::rollBack();
throw new RuntimeException($e->getMessage());
}
}
}

View File

@ -42,12 +42,10 @@ class OvertimeController extends Controller
try { try {
DB::beginTransaction(); DB::beginTransaction();
$data = $service->resloveData($data); if (!$service->store($data)) {
$result = $service->validate($data);
if ($result !== true) {
throw new RuntimeException($result); throw new RuntimeException($result);
} }
$model = OvertimeApply::create($data); $model = $service->getCurrentModel();
$workflow = WorkFlowService::make(); $workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) { if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError()); throw new RuntimeException($workflow->getError());
@ -71,6 +69,7 @@ class OvertimeController extends Controller
if (!$service->update($id, $request->all())) { if (!$service->update($id, $request->all())) {
throw new RuntimeException($service->getError()); throw new RuntimeException($service->getError());
} }
$model = $service->getCurrentModel();
$workflow = WorkFlowService::make(); $workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) { if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError()); throw new RuntimeException($workflow->getError());

View File

@ -35,12 +35,10 @@ class SignRepairController extends Controller
try { try {
DB::beginTransaction(); DB::beginTransaction();
$data = $service->resloveData($data); if (!$service->store($data)) {
$result = $service->validate($data);
if ($result !== true) {
throw new RuntimeException($result); throw new RuntimeException($result);
} }
$model = EmployeeSignRepair::create($data); $model = $service->getCurrentModel();
$workflow = WorkFlowService::make(); $workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) { if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError()); throw new RuntimeException($workflow->getError());
@ -71,12 +69,9 @@ class SignRepairController extends Controller
try { try {
DB::beginTransaction(); DB::beginTransaction();
$data = $service->resloveData($request->all(), $model); if (!$service->update($id, $request->all())) {
$result = $service->validate($data, $model); throw new RuntimeException($service->getError());
if ($result !== true) {
throw new RuntimeException($result);
} }
$model->update($data);
$workflow = WorkFlowService::make(); $workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) { if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError()); throw new RuntimeException($workflow->getError());

View File

@ -4,7 +4,7 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\{Request, Response}; use Illuminate\Http\{Request, Response};
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use App\Http\Resources\{ReimbursementResource, WorkflowLogResource, EmployeeSignRepairResource, HolidayApplyResource, OvertimeApplyResource}; use App\Http\Resources\{ReimbursementResource, WorkflowLogResource, EmployeeSignRepairResource, HolidayApplyResource, OvertimeApplyResource, AgreementResource};
use App\Enums\CheckStatus; use App\Enums\CheckStatus;
use App\Models\{WorkflowLog, WorkflowCheck}; use App\Models\{WorkflowLog, WorkflowCheck};
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -127,6 +127,7 @@ class WorkflowController extends Controller
'employee_sign_repairs' => EmployeeSignRepairResource::class, 'employee_sign_repairs' => EmployeeSignRepairResource::class,
'holiday_applies' => HolidayApplyResource::class, 'holiday_applies' => HolidayApplyResource::class,
'overtime_applies' => OvertimeApplyResource::class, 'overtime_applies' => OvertimeApplyResource::class,
'agreements' => AgreementResource::class,
]; ];
return data_get($map, $key); return data_get($map, $key);
} }

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class AgreementResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'images' => $this->images,
'employee_id' => $this->employee_id,
'employee' => EmployeeResource::make($this->whenLoaded('employee')),
'workflow_check' => WorkflowCheckResource::make($this->whenLoaded('workflow')),
'created_at' => $this->created_at->timestamp,
];
}
}

View File

@ -71,6 +71,9 @@ Route::group([
// 报销管理 // 报销管理
Route::apiResource('reimbursements', \App\Http\Controllers\Api\ReimbursementController::class); Route::apiResource('reimbursements', \App\Http\Controllers\Api\ReimbursementController::class);
// 合同管理
Route::apiResource('agreements', \App\Http\Controllers\Api\AgreementController::class);
// 审核流程 // 审核流程
Route::get('workflow', [\App\Http\Controllers\Api\WorkflowController::class, 'index']); Route::get('workflow', [\App\Http\Controllers\Api\WorkflowController::class, 'index']);
Route::get('workflow/{id}', [\App\Http\Controllers\Api\WorkflowController::class, 'show']); Route::get('workflow/{id}', [\App\Http\Controllers\Api\WorkflowController::class, 'show']);