cqp 3 mesi fa
parent
commit
19b477e302

+ 62 - 0
app/Http/Controllers/Api/FeeController.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\FeeService;
+use Illuminate\Http\Request;
+
+class FeeController extends BaseController
+{
+    public function feeEdit(Request $request)
+    {
+        $service = new FeeService();
+        $user = $request->userData;
+        list($status,$data) = $service->feeEdit($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function feeAdd(Request $request)
+    {
+        $service = new FeeService();
+        $user = $request->userData;
+        list($status,$data) = $service->feeAdd($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+    public function feeDel(Request $request)
+    {
+        $service = new FeeService();
+        $user = $request->userData;
+        list($status,$data) = $service->feeDel($request->all(), $user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function feeList(Request $request)
+    {
+        $service = new FeeService();
+        $user = $request->userData;
+        list($status,$data) = $service->feeList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 17 - 0
app/Model/Fee.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Fee extends DataScopeBaseModel
+{
+    protected $guarded = [];
+    protected $table = "fee"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+    const IS_UES = 1;//启用
+
+    public static $field = ['title','id','code','is_use','parent_id'];
+}

+ 8 - 4
app/Service/EmployeeService.php

@@ -616,7 +616,11 @@ class EmployeeService extends Service
 
         foreach ($data['data'] as $key => $value){
             $top_depart_id = $user['top_depart_id'];
-            if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = $user['top_depart_id'];
+            $parent_id = $value['parent_id'];
+            if(empty($value['parent_id'])) {
+                $parent_id = $top_depart_id;
+                $data['data'][$key]['parent_id'] = $parent_id;
+            }
 
             $data['data'][$key]['top_depart_id'] = $top_depart_id;
             $data['data'][$key]['upd_time'] = time();
@@ -624,18 +628,18 @@ class EmployeeService extends Service
             if($is_check){
                 $data['data'][$key]['crt_time'] = time();
                 $bool = Depart::whereRaw("binary code = '{$value['code']}'")
-                    ->where('parent_id', $top_depart_id)
+                    ->where('top_depart_id', $top_depart_id)
                     ->where('del_time',0)
                     ->exists();
             }else{
                 if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
                 $bool = Depart::whereRaw("binary code = '{$value['code']}'")
-                    ->where('parent_id', $top_depart_id)
+                    ->where('top_depart_id', $top_depart_id)
                     ->where('id','<>',$data['id'])
                     ->where('del_time',0)
                     ->exists();
             }
-            if($bool) return [false,'部门编码不能重复'];
+            if($bool) return [false,'部门编码已存在'];
         }
 
         return [true, $data];

+ 154 - 0
app/Service/FeeService.php

@@ -0,0 +1,154 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Fee;
+use Illuminate\Support\Facades\DB;
+
+class FeeService extends Service
+{
+    public function feeEdit($data, $user){
+        list($status,$msg) = $this->feeRule($data,$user,false);
+        if(!$status) return [$status, $msg];
+
+        $update = $msg['data'][0];
+        $model = new Fee();
+        $model->where('id',$data['id'])->update($update);
+
+        return [true, ''];
+    }
+
+    public function feeAdd($data,$user){
+        list($status,$msg) = $this->feeRule($data,$user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($msg['data'] as $value){
+                $model = new Fee();
+                $model->parent_id = $value['parent_id'];
+                $model->title = $value['title'];
+                $model->code = $value['code'];
+                $model->top_depart_id = $value['top_depart_id'];
+                $model->save();
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function feeDel($data, $user){
+        list($status,$msg) = $this->checkFeeDel($data);
+        if(! $status) return [false, $msg];
+
+        Fee::whereIn('id',$data['id'])->update([
+            'del_time'=>time()
+        ]);
+
+        return [true,''];
+    }
+
+    public function checkFeeDel($data){
+        if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
+
+        $bool = Fee::whereIn('parent_id', $data['id'])->where('del_time',0)->exists();
+        if($bool) return [false,'该费用存在子类型'];
+
+        return [true, ''];
+    }
+
+    public function feeCommon($data,$user, $field = []){
+        if(empty($field)) $field = Fee::$field;
+
+        $model = Fee::TopClear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select($field)
+            ->orderby('id', 'asc');
+
+        if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_id']);
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
+
+        return $model;
+    }
+
+    public function feeList($data, $user){
+        $model = $this->feeCommon($data, $user);
+
+        $list = $model->get()->toArray();
+        $list = $this->fillFeeList($list, $user);
+        $list_tree = $list;
+        if(! empty($list_tree)) {
+            $minParentId = min(array_column($list_tree, 'parent_id'));
+            $list_tree = $this->makeTree($minParentId,$list_tree);
+            $list_tree = $this->set_sort_circle($list_tree);
+        }
+
+        return [true,['data' => $list,'tree' => $list_tree]];
+    }
+
+    public function fillFeeList($list,$user){
+        if(empty($list)) return $list;
+
+//        foreach ($list as $key => $value){
+//
+//        }
+
+        return $list;
+    }
+
+    public function feeRule($data,$user, $is_check = true){
+        if(empty($data['data'])) return [false,'数据不能为空!'];
+
+        $code = array_column($data['data'],'code');
+        $title = array_column($data['data'],'title');
+        $code = array_map(function($val) {
+            return $val !== null ? $val : 0;
+        }, $code);
+        $title = array_map(function($val) {
+            return $val !== null ? $val : 0;
+        }, $title);
+        $code_count = array_count_values($code);
+        $title_count = array_count_values($title);
+        foreach ($code as $value){
+            if(empty($value)) return [false,'编码不能为空!'];
+            if($code_count[$value] > 1) return [false,'编码不能重复'];
+        }
+        foreach ($title as $value){
+            if(empty($value)) return [false,'名称不能为空!'];
+            if($title_count[$value] > 1) return [false,'名称不能重复'];
+        }
+
+        foreach ($data['data'] as $key => $value){
+            $top_depart_id = $user['top_depart_id'];
+            if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
+
+            $data['data'][$key]['top_depart_id'] = $top_depart_id;
+            $data['data'][$key]['upd_time'] = time();
+
+            if($is_check){
+                $data['data'][$key]['crt_time'] = time();
+                $bool = Fee::whereRaw("binary code = '{$value['code']}'")
+                    ->where('top_depart_id', $top_depart_id)
+                    ->where('del_time',0)
+                    ->exists();
+            }else{
+                if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
+                $bool = Fee::whereRaw("binary code = '{$value['code']}'")
+                    ->where('top_depart_id', $top_depart_id)
+                    ->where('id','<>',$data['id'])
+                    ->where('del_time',0)
+                    ->exists();
+            }
+            if($bool) return [false,'编码已存在'];
+        }
+
+        return [true, $data];
+    }
+}

+ 6 - 0
routes/api.php

@@ -82,5 +82,11 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('itemAdd', 'Api\ItemController@itemAdd');
     $route->any('itemDel', 'Api\ItemController@itemDel');
     $route->any('itemDetail', 'Api\ItemController@itemDetail');
+
+    //费用类型
+    $route->any('feeAdd', 'Api\FeeController@feeAdd')->name('fee.add');
+    $route->any('feeEdit', 'Api\FeeController@feeEdit')->name('fee.edit');
+    $route->any('feeDel', 'Api\FeeController@feeDel')->name('fee.del');
+    $route->any('feeList', 'Api\FeeController@feeList');
 });