cqp před 1 měsícem
rodič
revize
fa9fba726d

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

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

+ 25 - 0
app/Model/Priority.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Priority extends DataScopeBaseModel
+{
+    protected $guarded = [];
+    protected $table = "priority"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+    public static $field = ['title','id','code','sort','type'];
+
+    const TYPE_ONE = 1;
+    const TYPE_TWO = 2;
+    const TYPE_THREE = 3;
+    const TYPE_TITLE = [
+        self::TYPE_ONE => '项目',
+        self::TYPE_TWO => '节点',
+        self::TYPE_THREE => '任务',
+    ];
+}

+ 152 - 0
app/Service/PriorityService.php

@@ -0,0 +1,152 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Priority;
+use Illuminate\Support\Facades\DB;
+
+class PriorityService extends Service
+{
+    public function priorityEdit($data, $user){
+        list($status,$msg) = $this->priorityRule($data,$user,false);
+        if(!$status) return [$status, $msg];
+
+        try {
+            DB::beginTransaction();
+
+            $update = $msg['data'][0];
+            $model = new Priority();
+            $model->where('id',$data['id'])->update($update);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true,''];
+    }
+
+    public function priorityAdd($data,$user){
+        list($status,$msg) = $this->priorityRule($data,$user);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+
+            foreach ($msg['data'] as $value){
+                $model = new Priority();
+                $model->title = $value['title'];
+                $model->code = $value['code'];
+                $model->type = $value['type'];
+                $model->sort = $value['sort'];
+                $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 priorityDel($data, $user){
+        if(empty($data['id'])) return [false, 'ID不能为空'];
+
+        Priority::whereIn('id',$data['id'])->update([
+            'del_time'=>time()
+        ]);
+
+        return [true,''];
+    }
+
+    public function priorityCommon($data,$user, $field = []){
+        if(empty($field)) $field = Priority::$field;
+
+        $model = Priority::TopClear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select($field)
+            ->orderby('id', 'asc');
+
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
+
+        return $model;
+    }
+
+    public function priorityList($data, $user){
+        $model = $this->priorityCommon($data, $user);
+
+        $list = $model->get()->toArray();
+        $list = $this->fillPriorityList($list, $user);
+
+        return [true, $list];
+    }
+
+    public function fillPriorityList($list, $user, $is_export = false){
+        if(empty($list)) return $list;
+
+        if($is_export){
+            foreach ($list['data'] as $key => $value){
+                $list['data'][$key]['type_title'] = Priority::TYPE_TITLE[$value['type']] ?? "";
+            }
+        }
+
+        return $list;
+    }
+
+    public function priorityRule($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'];
+
+            $data['data'][$key]['top_depart_id'] = $top_depart_id;
+            $data['data'][$key]['upd_time'] = time();
+
+            if(! isset($value['sort'])) return [false, '排序字段sort不存在'];
+            if(! is_numeric($value['sort'])) return [false, '排序字段sort非合法数字'];
+
+            if(! isset(Priority::TYPE_TITLE[$value['type']])) return [false, 'type错误'];
+            if($is_check){
+                $data['data'][$key]['crt_time'] = time();
+                $bool = Priority::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 = Priority::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];
+    }
+}