Browse Source

部门新增,修改,删除,列表

kely 1 year ago
parent
commit
9352d546f5

+ 68 - 0
app/Http/Controllers/API/DepartmentController.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace App\Http\Controllers\API;
+
+use App\Http\Controllers\Controller;
+use App\Http\Requests\API\Department\CreateOrUpdateRequest;
+use App\Http\Resources\API\DepartmentResource;
+use App\Models\Department;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+
+class DepartmentController extends Controller
+{
+
+    public function index(Request $request)
+    {
+        $department=Department::filter($request->all())->where("parent_id",0)->with(['children'])->simplePaginate();
+
+        return DepartmentResource::collection($department);
+    }
+
+
+    public function store(CreateOrUpdateRequest $request)
+    {
+        $department=new Department();
+
+        $department->mergeFillable([
+            'company_id'
+        ]);
+
+        $department->fill([
+            ...$request->all(),
+            'company_id' => Auth::user()->company_id,
+        ]);
+
+        $department->save();
+
+        return $this->created();
+    }
+
+
+    public function show(string $id)
+    {
+        $department=Department::query()->findOrFail($id);
+
+        return new DepartmentResource($department);
+    }
+
+
+
+    public function update(CreateOrUpdateRequest $request,string $id)
+    {
+        $department=Department::findOrFail($id);
+        $department->fill($request->all());
+        $department->save();
+        return $this->noContent();
+    }
+
+
+    public function destroy(string $id)
+    {
+        $department = Department::findOrFail($id);
+
+        $department->delete();
+
+        return $this->noContent();
+    }
+}

+ 55 - 0
app/Http/Requests/API/Department/CreateOrUpdateRequest.php

@@ -0,0 +1,55 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: kelyliang
+ * Date: 2024/2/29
+ * Time: 上午 11:37
+ */
+
+namespace App\Http\Requests\API\Department;
+
+
+use App\Http\Requests\RuleHelper;
+use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Validation\Rule;
+
+class CreateOrUpdateRequest extends FormRequest
+{
+    use RuleHelper;
+
+    /**
+     * Determine if the user is authorized to make this request.
+     */
+    public function authorize(): bool
+    {
+        return true;
+    }
+
+
+    public function rules(): array
+    {
+        return [
+            'name' => 'required|max:255',
+            'manager_id' => [
+                'required',
+                Rule::exists('users','id')->where($this->userCompanyWhere()),
+            ],
+            'parent_id' => [
+                $this->parentIdExistsRule(),
+            ]
+
+        ];
+    }
+
+    protected function parentIdExistsRule()
+    {
+        // 如果 parent_id 不为 0,返回 exists 规则
+        if ($this->input('parent_id') != 0) {
+            return Rule::exists('department', 'id')->where($this->userCompanyWhere());
+        }
+
+        // 如果 parent_id 为 0,返回空数组以跳过 exists 验证
+        return [];
+    }
+
+}

+ 25 - 0
app/Http/Resources/API/DepartmentResource.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: kelyliang
+ * Date: 2024/2/29
+ * Time: 上午 11:46
+ */
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class DepartmentResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return[
+          'id' => $this->id,
+          'name' => $this->name,
+          'parent_id'  => $this->parent_id,
+          'children' => $this->parent_id == 0 ? DepartmentResource::collection($this->children) : [],
+        ];
+    }
+}

+ 25 - 0
app/ModelFilters/DepartmentFilter.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: kelyliang
+ * Date: 2024/2/29
+ * Time: 上午 11:32
+ */
+
+namespace App\ModelFilters;
+
+
+use EloquentFilter\ModelFilter;
+
+class DepartmentFilter extends ModelFilter
+{
+    public $relations = [];
+
+    public function id($id): ModelFilter{
+        return $this->where('id',$id);
+    }
+    public function name($name): ModelFilter
+    {
+        return $this->where('name', 'like', "%$name%");
+    }
+}

+ 16 - 20
app/Models/Department.php

@@ -1,39 +1,35 @@
 <?php
-/**
- * Created by IntelliJ IDEA.
- * User: kelyliang
- * Date: 2024/2/18
- * Time: 下午 03:52
- */
 
 namespace App\Models;
+
+use App\Models\Scopes\CompanyScope;
+use EloquentFilter\Filterable;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
 class Department extends Model
 {
-    use HasFactory;
+    use HasFactory,Filterable;
 
     protected $table = 'department';
+    protected $fillable = [
+        'name',
+        'parent_id',
+        'company_id',
+        'manager_id',
+    ];
 
-    // 设置主键(可选,默认是'id')
-    protected $primaryKey = 'id';
-
-    // 定义允许批量赋值的字段(可选)
-    protected $fillable = ['name', 'parent_id','manager_id'];
 
 
-    // 例如,一个部门可能有多个子部门
-    public function children()
+    protected static function booted()
     {
-        return $this->hasMany(Department::class, 'parent_id');
+        parent::booted(); // TODO: Change the autogenerated stub
+
+        static::addGlobalScope(new CompanyScope);
     }
 
-    // 或者,一个部门可能属于另一个部门
-    public function parent()
+    public function children()
     {
-        return $this->belongsTo(Department::class, 'parent_id');
+        return $this->hasMany(Department::class, 'parent_id');
     }
-
 }
-

+ 1 - 1
config/app.php

@@ -70,7 +70,7 @@ return [
     |
     */
 
-    'timezone' => 'UTC',
+    'timezone' => 'PRC',
 
     /*
     |--------------------------------------------------------------------------

+ 1 - 0
routes/api.php

@@ -35,6 +35,7 @@ Route::middleware(['auth:sanctum'])->group(function () {
             'naming-rule' => API\NameRuleController::class,
             'task' => API\TaskController::class,
             'library' => API\LibraryController::class,
+            'department' => API\DepartmentController::class,
         ]);
 
         Route::patch("requirement/{requirement_id}/close",[API\RequirementController::class, 'close'])->name('requirement.close');