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']) {
$images = [];
foreach ($data['images'] as $value) {
$image = data_get($value, 'value');
$url = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
array_push($images, [
'value' => $image,
'preview' => $url,
]);
if (is_array($value)) {
$image = data_get($value, 'value');
$url = Str::startsWith($image, ['http://', 'https://']) ? $image : Storage::url($image);
array_push($images, [
'value' => $image,
'preview' => $url,
]);
} else {
array_push($images, [
'value' => $value,
'preview' => $value,
]);
}
}
$data['images'] = $images;
}
@ -47,14 +54,34 @@ class AgreementService extends BaseService
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) {
$item->delete();
return false;
}
$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;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Admin\Services;
use Slowlyo\OwlAdmin\Services\AdminService;
use Illuminate\Database\Eloquent\Model;
/**
* @method Region getModel()
@ -14,6 +15,13 @@ class BaseService extends AdminService
protected bool $modelSortAble = false;
protected Model $currentModel;
public function getCurrentModel()
{
return $this->currentModel;
}
public function sortColumn()
{
return 'id';
@ -78,7 +86,8 @@ class BaseService extends AdminService
return false;
}
$this->modelName::create($data);
$model = $this->modelName::create($data);
$this->currentModel = $model;
return true;
}
@ -93,8 +102,9 @@ class BaseService extends AdminService
return false;
}
return $model->update($data);
$model->update($data);
$this->currentModel = $model;
return true;
}
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 {
DB::beginTransaction();
$data = $service->resloveData($data);
$result = $service->validate($data);
if ($result !== true) {
if (!$service->store($data)) {
throw new RuntimeException($result);
}
$model = OvertimeApply::create($data);
$model = $service->getCurrentModel();
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());
@ -71,6 +69,7 @@ class OvertimeController extends Controller
if (!$service->update($id, $request->all())) {
throw new RuntimeException($service->getError());
}
$model = $service->getCurrentModel();
$workflow = WorkFlowService::make();
if (!$workflow->apply($model->workflow, $user)) {
throw new RuntimeException($workflow->getError());

View File

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

View File

@ -4,7 +4,7 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\{Request, Response};
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\Models\{WorkflowLog, WorkflowCheck};
use Illuminate\Support\Facades\DB;
@ -127,6 +127,7 @@ class WorkflowController extends Controller
'employee_sign_repairs' => EmployeeSignRepairResource::class,
'holiday_applies' => HolidayApplyResource::class,
'overtime_applies' => OvertimeApplyResource::class,
'agreements' => AgreementResource::class,
];
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

@ -70,6 +70,9 @@ Route::group([
// 报销管理
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']);