duqinya 6 rokov pred
rodič
commit
7932e78ff6

+ 56 - 48
app/Repositories/CmsContentTemplateSetRepository.php

@@ -25,70 +25,78 @@ class CmsContentTemplateSetRepository {
      */
     public function set($request)
     {
-        $template = $this->cmsContentTemplate->where('id',$request['tpl_id'])->first();
+        $template = $this->cmsContentTemplate->select('id','city_name','city_id','title','apply_type','is_open')->where('id',$request['tpl_id'])->first();
         $templates = $template->toArray();
-
+        $where = [
+            'city_id'=>$templates['city_id'],
+            'title'=>$templates['title'],
+            'apply_type'=>$templates['apply_type'],
+        ];
+        $copys_template = $this->cmsContentTemplate->where($where)->get();
         $request = [];
-            if ($templates['is_open'] == 0){
-                $request['type'] = $templates['apply_type'];
-                $request['tpl_id'] = $templates['id'];
+        $i=0;
+        foreach ($copys_template as $k=>$v) {
+            if ($v['is_open'] == 0) {
+                $i = 1;
+                $request['type'] = $v['apply_type'];
+                $request['tpl_id'] = $v['id'];
                 return $this->preview($request);
             }
+        }
+            if ($i == 0) {
+                $new_template = [
+                    'title' => $templates['title'],
+                    'city_id' => $templates['city_id'],
+                    'city_name' => $templates['city_name'],
+                    'apply_type' => $templates['apply_type'],
+                    'is_open' => 0,
+                ];
+                DB::beginTransaction();
+                try {
+                    $res = $this->cmsContentTemplate->create($new_template);
+                    if (!$res) {
+                        throw new HttpException(500, '添加草稿模板失败');
+                    }
 
-            if ($templates['is_open'] == 1){
-                        $new_template = [
-                            'title' => $templates['title'],
-                            'city_id' => $templates['city_id'],
-                            'city_name' => $templates['city_name'],
-                            'apply_type' => $templates['apply_type'],
-                            'is_open' => 0,
-                        ];
-                        DB::beginTransaction();
-                        try {
-                            $res = $this->cmsContentTemplate->create($new_template);
-                            if (!$res) {
-                                throw new HttpException(500, '添加草稿模板失败');
-                            }
-
-                            $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
-                            if (count($template_set) > 0) {
-                                foreach ($template_set->toArray() as $key => $val) {
-                                    $copy_template = [
-                                        'rule' => $val['rule'],
-                                        'tpl_id' => $res->id,
-                                        'area_type' => $val['area_type'],
-                                        'status' => $val['status'],
-                                    ];
-                                    $result = $this->cmsContentTemplateSet->create($copy_template);
-                                    if (!$result) {
-                                        throw new HttpException(500, '生成模板内容失败');
-                                    }
-                                    $request['type'] = $res->apply_type;
-                                    $request['tpl_id'] = $result->tpl_id;
-                                }
+                    $template_set = $this->cmsContentTemplateSet->where('tpl_id', $templates['id'])->get();
+                    if (count($template_set) > 0) {
+                        foreach ($template_set->toArray() as $key => $val) {
+                            $copy_template = [
+                                'rule' => $val['rule'],
+                                'tpl_id' => $res->id,
+                                'area_type' => $val['area_type'],
+                                'status' => $val['status'],
+                            ];
+                            $result = $this->cmsContentTemplateSet->create($copy_template);
+                            if (!$result) {
+                                throw new HttpException(500, '生成模板内容失败');
                             }
-                            DB::commit();
-                           return $this->preview($request);
-    //                        return Response::create();
-
-                        } catch (QueryException $exception) {
-                            DB::rollBack();
-                            return Response::create([
-                                'message' => '生成模板内容失败,请重试',
-                                'error' => $exception->getMessage(),
-                                'status_code' => 500
-                            ]);
+                            $request['type'] = $res->apply_type;
+                            $request['tpl_id'] = $result->tpl_id;
                         }
                     }
+                    DB::commit();
+                    return $this->preview($request);
+                    //                        return Response::create();
 
+                } catch (QueryException $exception) {
+                    DB::rollBack();
+                    return Response::create([
+                        'message' => '生成模板内容失败,请重试',
+                        'error' => $exception->getMessage(),
+                        'status_code' => 500
+                    ]);
+                }
+            }
     }
 
+
     /**
      * banner配置
      */
     public function bannerSet($request)
     {
-        $banner_id = $request['id']??'';
+        $banner_id = $request['id'] ?? '';
             $rules = $request['rule'];
             if (is_array($rules)){
                 if (count($rules)>10) {