|
|
@@ -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];
|
|
|
+ }
|
|
|
+}
|