duqinya лет назад: 6
Родитель
Сommit
d7e9a9641d

+ 2 - 0
app/Http/Controllers/CmsContentTemplateController.php

@@ -75,6 +75,8 @@ class CmsContentTemplateController extends BaseController
         $validator = Validator::make($request->all(), [
             'id' => 'required|exists:cms_content_template',
             'is_open' => ['required', Rule::in(0,1)],
+            'apply_type' => ['required', Rule::in(0,1)],
+            'city_id' => 'required',
         ]);
         if ($validator->fails()) {
             return $this->response->error($validator->errors()->first(), 500);

+ 17 - 6
app/Repositories/CmsContentTemplateRepository.php

@@ -35,8 +35,8 @@ class CmsContentTemplateRepository {
      */
     public function create($request)
     {
-        if($this->cmsContentTemplate->where('title', trim($request['title']))->exists()){
-            throw new HttpException(500, '该模块已经存在');
+        if($this->cmsContentTemplate->where(['city_id'=>$request['city_id'],'title'=>trim($request['title'])])->exists()){
+            throw new HttpException(500, '当前城市该模块已经存在');
         }
 
         $subject = [
@@ -57,12 +57,23 @@ class CmsContentTemplateRepository {
      */
     public function edit($request)
     {
+        if ($request['is_open']==0){
+            $where = [
+                'apply_type'=>$request['apply_type'],
+                'is_open'=>$request['is_open'],
+                'city_id'=>$request['city_id'],
+            ];
+            $template = $this->cmsContentTemplate->where($where)->get();
+            if (count($template)<1){
+                throw new HttpException(500, '同一城市同一模板必须有一个状态开启');
+            }
+        }
 
-        $template = $this->cmsContentTemplate->find($request['id']);
-        $template->is_open = $request['is_open'];
-        $template->updated_at = date('Y-m-d H:i:s');
+        $template_id = $this->cmsContentTemplate->find($request['id']);
+        $template_id->is_open = $request['is_open'];
+        $template_id->updated_at = date('Y-m-d H:i:s');
 
-        $res = $template->save();
+        $res = $template_id->save();
         if (!$res) {
             throw new HttpException(500, '修改状态失败');
         }