cqpCow před 2 roky
rodič
revize
048ebf5069

+ 61 - 0
app/Http/Controllers/Api/ScrappController.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\ScrappService;
+use Illuminate\Http\Request;
+
+class ScrappController extends BaseController
+{
+    public function scrappList(Request $request)
+    {
+        $service = new ScrappService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->scrappList($request->all(),$request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function scrappEdit(Request $request)
+    {
+        $service = new ScrappService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->scrappEdit($request->all(),$request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function scrappAdd(Request $request)
+    {
+        $service = new ScrappService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->scrappAdd($request->all(),$request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function scrappDel(Request $request)
+    {
+        $service = new ScrappService();
+        $user = $request->get('auth');
+        list($status,$data) = $service->scrappDel($request->all(),$request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 14 - 0
app/Model/Scrapp.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Scrapp extends Model
+{
+    protected $table = "scrapp"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}
+

+ 1 - 1
app/Service/EquipmentService.php

@@ -20,7 +20,7 @@ class EquipmentService extends Service
         return [true,'保存成功!'];
     }
 
-    public function equipmentAdd($data,$user){
+    public function equipmentAdd($data){
         list($status,$msg) = $this->equipmentRule($data);
         if(!$status) return [$status,$msg];
 

+ 85 - 0
app/Service/ScrappService.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Scrapp;
+
+/**
+ * 报废原因
+ * @package App\Models
+ */
+class ScrappService extends Service
+{
+    public function scrappEdit($data){
+        list($status,$msg) = $this->scrappRule($data,false);
+        if(!$status) return [$status,$msg];
+
+        $update = $msg['data'][0];
+        Scrapp::where('id',$data['id'])->update($update);
+
+        return [true,'保存成功!'];
+    }
+
+    public function scrappAdd($data){
+        list($status,$msg) = $this->scrappRule($data);
+        if(!$status) return [$status,$msg];
+
+        Scrapp::insert($msg['data']);
+
+        return [true,'保存成功!'];
+    }
+
+    public function scrappDel($data){
+        if($this->isEmpty($data,'id')) return [false,'ID必须!'];
+
+        Scrapp::whereIn('id',$data['id'])->update([
+            'del_time' => time()
+        ]);
+
+        return [true,'删除成功'];
+    }
+
+    public function scrappList($data){
+        $model = Scrapp::where('del_time',0)
+            ->select('*');
+
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+
+        $list = $this->limit($model,'',$data);
+
+        return [200,$list];
+    }
+
+    public function scrappRule($data,$is_add = true){
+        if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
+
+        $title = array_column($data['data'],'title');
+        $title = array_map(function($val) {
+            return $val !== null ? $val : 0;
+        }, $title);
+        $title_count = array_count_values($title);
+        foreach ($title as $value){
+            if(empty($value)) return [false,'名称不能为空!'];
+            if($title_count[$value] > 1) return [false,'名称不能重复'];
+        }
+
+        foreach ($data['data'] as $key => $value){
+            $data['data'][$key]['upd_time'] = time();
+            if($is_add){
+                $bool = Scrapp::whereRaw("title = '{$value['title']}'")
+                    ->where('del_time',0)
+                    ->exists();
+                $data['data'][$key]['crt_time'] = time();
+            }else{
+                if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
+                $bool = Scrapp::whereRaw("title = '{$value['title']}'")
+                    ->where('id','<>',$data['id'])
+                    ->where('del_time',0)
+                    ->exists();
+            }
+            if($bool) return [false,'名称不能重复'];
+        }
+
+        return [true,$data];
+    }
+}

+ 6 - 0
routes/api.php

@@ -91,6 +91,12 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('equipmentDel', 'Api\EquipmentController@equipmentDel');
     $route->any('equipmentList', 'Api\EquipmentController@equipmentList');
 
+    //报废原因
+    $route->any('scrappAdd', 'Api\ScrappController@scrappAdd');
+    $route->any('scrappEdit', 'Api\ScrappController@scrappEdit');
+    $route->any('scrappDel', 'Api\ScrappController@scrappDel');
+    $route->any('scrappList', 'Api\ScrappController@scrappList');
+
     $route->any('productList', 'Api\MaterialController@productList');
     $route->any('productEdit', 'Api\MaterialController@edit');
     $route->any('productAdd', 'Api\MaterialController@edd');